Skip to content

Commit 79052cf

Browse files
committed
WIP 20250515-1655
1 parent 64fd265 commit 79052cf

File tree

2 files changed

+102
-72
lines changed

2 files changed

+102
-72
lines changed

lib/transformer.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,23 @@ defmodule AshRandomParams.Transformer do
6666
init_keys = init_params |> Map.keys()
6767
union_keys = tagged_unions |> Map.values()
6868

69+
belongs_to_names =
70+
resource
71+
|> Ash.Resource.Info.relationships()
72+
|> Enum.flat_map(fn
73+
%BelongsTo{name: name, source_attribute: src_attr} -> [name, src_attr]
74+
_ -> []
75+
end)
76+
6977
new_attrs =
7078
Ash.Resource.Info.attributes(resource)
7179
|> Enum.filter(&(&1.name in action.accept))
80+
|> Enum.reject(&(&1.name in belongs_to_names))
7281
|> Enum.reject(&(&1.name in init_keys))
7382
|> Enum.reject(&(&1.name in union_keys))
7483
|> Enum.reject(&(&1.name in exclude))
7584
|> Enum.reject(&(&1.allow_nil? and &1.name not in include))
7685

77-
belongs_to_names =
78-
resource
79-
|> Ash.Resource.Info.relationships()
80-
|> Enum.flat_map(fn
81-
%BelongsTo{name: name, source_attribute: src_attr} -> [name, src_attr]
82-
_ -> []
83-
end)
8486

8587
new_args =
8688
action.arguments

test/ash_random_params_test.exs

Lines changed: 93 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule AshRandomParamsTest do
22
use ExUnit.Case, async: true
33

4-
alias __MODULE__.{Random, Post, Domain}
4+
alias __MODULE__.{Random, Post, Author, Domain}
55

66
defmodule Post do
77
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets, extensions: [AshRandomParams]
@@ -24,11 +24,35 @@ defmodule AshRandomParamsTest do
2424
defaults [:read, :destroy, create: :*, update: :*]
2525
end
2626

27+
relationships do
28+
belongs_to :author, Author, allow_nil?: false, public?: true
29+
end
30+
2731
random_params do
2832
random Random
2933
end
3034
end
3135

36+
defmodule Author do
37+
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
38+
39+
ets do
40+
private? true
41+
end
42+
43+
attributes do
44+
uuid_primary_key :id
45+
end
46+
47+
relationships do
48+
has_many :posts, Post, public?: true
49+
end
50+
51+
actions do
52+
defaults [:read, :destroy, create: :*, update: :*]
53+
end
54+
end
55+
3256
defmodule Random do
3357
use AshRandomParams.Random
3458

@@ -48,80 +72,84 @@ defmodule AshRandomParamsTest do
4872

4973
resources do
5074
resource Post
75+
resource Author
5176
end
5277
end
5378

54-
test "with no options" do
55-
assert {:ok, ret} =
56-
Post
57-
|> Ash.ActionInput.for_action(
58-
:random_params,
59-
%{
60-
action: :create
61-
}
62-
)
63-
|> Ash.run_action()
64-
65-
assert ret.req_str |> String.starts_with?("req_str-")
66-
assert ret.opt_str == nil
67-
assert ret.req_int == 777
68-
assert ret.opt_int == nil
69-
end
79+
describe "options" do
80+
test "with no options" do
81+
assert {:ok, ret} =
82+
Post
83+
|> Ash.ActionInput.for_action(
84+
:random_params,
85+
%{
86+
action: :create
87+
}
88+
)
89+
|> Ash.run_action()
90+
91+
assert ret.req_str |> String.starts_with?("req_str-")
92+
assert ret.opt_str == nil
93+
assert ret.req_int == 777
94+
assert ret.opt_int == nil
95+
assert ret |> Map.has_key?(:author_id)
96+
end
7097

71-
test "with init_params" do
72-
assert {:ok, ret} =
73-
Post
74-
|> Ash.ActionInput.for_action(
75-
:random_params,
76-
%{
77-
action: :create,
78-
init_params: %{
79-
req_int: 123
98+
test "with init_params" do
99+
assert {:ok, ret} =
100+
Post
101+
|> Ash.ActionInput.for_action(
102+
:random_params,
103+
%{
104+
action: :create,
105+
init_params: %{
106+
req_int: 123
107+
}
80108
}
81-
}
82-
)
83-
|> Ash.run_action()
109+
)
110+
|> Ash.run_action()
84111

85-
assert ret.req_str |> String.starts_with?("req_str-")
86-
assert ret.opt_str == nil
87-
assert ret.req_int == 123
88-
assert ret.opt_int == nil
89-
end
112+
assert ret.req_str |> String.starts_with?("req_str-")
113+
assert ret.opt_str == nil
114+
assert ret.req_int == 123
115+
assert ret.opt_int == nil
116+
end
90117

91-
test "with include" do
92-
assert {:ok, ret} =
93-
Post
94-
|> Ash.ActionInput.for_action(
95-
:random_params,
96-
%{
97-
action: :create,
98-
include: [:opt_str]
99-
}
100-
)
101-
|> Ash.run_action()
118+
test "with include" do
119+
assert {:ok, ret} =
120+
Post
121+
|> Ash.ActionInput.for_action(
122+
:random_params,
123+
%{
124+
action: :create,
125+
include: [:opt_str]
126+
}
127+
)
128+
|> Ash.run_action()
102129

103-
assert ret.req_str |> String.starts_with?("req_str-")
104-
assert ret.opt_str |> String.starts_with?("opt_str-")
105-
assert ret.req_int == 777
106-
assert ret.opt_int == nil
107-
end
130+
assert ret.req_str |> String.starts_with?("req_str-")
131+
assert ret.opt_str |> String.starts_with?("opt_str-")
132+
assert ret.req_int == 777
133+
assert ret.opt_int == nil
134+
end
108135

109-
test "with exclude" do
110-
assert {:ok, ret} =
111-
Post
112-
|> Ash.ActionInput.for_action(
113-
:random_params,
114-
%{
115-
action: :create,
116-
exclude: [:req_str]
117-
}
118-
)
119-
|> Ash.run_action()
136+
test "with exclude" do
137+
assert {:ok, ret} =
138+
Post
139+
|> Ash.ActionInput.for_action(
140+
:random_params,
141+
%{
142+
action: :create,
143+
exclude: [:req_str]
144+
}
145+
)
146+
|> Ash.run_action()
120147

121-
assert ret.req_str == nil
122-
assert ret.opt_str == nil
123-
assert ret.req_int == 777
124-
assert ret.opt_int == nil
148+
assert ret.req_str == nil
149+
assert ret.opt_str == nil
150+
assert ret.req_int == 777
151+
assert ret.opt_int == nil
152+
end
125153
end
126154

127155
test "code interface" do

0 commit comments

Comments
 (0)