Skip to content

Commit 453fd76

Browse files
committed
WIP 20250517-1158
1 parent 309466c commit 453fd76

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule MyApp.Post do
1515
attribute :author, :string, allow_nil?: false
1616
attribute :title, :string, allow_nil?: false
1717
attribute :content, :string, allow_nil?: true
18-
attriubte :tag, :string, allow_nil?: true, default: "JS"
18+
attriubte :tag, :string, allow_nil?: false, default: "JS"
1919
end
2020

2121
random_params do
@@ -41,9 +41,21 @@ Post.random_params!(:create, %{author: "James"}, %{
4141
})
4242
```
4343

44-
### Options
44+
### Default Behavior
45+
46+
By default, it generates random values for attributes and arguments that have `allow_nil?: false` and no default value (`default == nil`). In the example above, `author` and `title` fall into this category.
47+
48+
### Belongs To Relationships
4549

46-
By default, it generates random values for attributes and arguments that have `allow_nil?: false` and no default value (`default == nil`).
50+
For attributes and arguments that match the `name` or `source_attribute` of a `belongs_to` relationship:
51+
- In `create` actions, they are generated with `nil` values
52+
- In other actions, they are not generated at all
53+
54+
This behavior exists because:
55+
- In `create` actions, omitting a value is equivalent to setting it to `nil`
56+
- In `update` actions, omitting a value preserves the existing relationship, while explicitly setting it to `nil` removes the relationship
57+
58+
### Options
4759

4860
- `populate`: Forces generation of random values for specified attributes/arguments, overriding the default behavior
4961
- `omit`: Prevents generation of random values for specified attributes/arguments, overriding the default behavior

test/ash_random_params_test.exs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ defmodule AshRandomParamsTest do
2121
end
2222

2323
actions do
24-
defaults [:read, :destroy, create: :*, update: :*]
24+
defaults [:read, :destroy]
25+
26+
create :create do
27+
accept :*
28+
argument :author, :struct, allow_nil?: false, constraints: [instance_of: Author]
29+
end
30+
31+
update :update do
32+
accept :*
33+
argument :author, :struct, allow_nil?: false, constraints: [instance_of: Author]
34+
end
2535
end
2636

2737
relationships do
@@ -76,15 +86,15 @@ defmodule AshRandomParamsTest do
7686
end
7787
end
7888

79-
test "with no options" do
89+
test "create action" do
8090
assert %{
91+
author: nil,
8192
author_id: nil,
8293
req_str: "req_str-" <> _,
8394
opt_str: nil,
8495
req_int: 777,
8596
opt_int: 123
8697
} =
87-
params =
8898
Post
8999
|> Ash.ActionInput.for_action(
90100
:random_params,
@@ -93,8 +103,27 @@ defmodule AshRandomParamsTest do
93103
}
94104
)
95105
|> Ash.run_action!()
106+
end
107+
108+
test "update action" do
109+
assert %{
110+
req_str: "req_str-" <> _,
111+
opt_str: nil,
112+
req_int: 777,
113+
opt_int: 123
114+
} =
115+
params =
116+
Post
117+
|> Ash.ActionInput.for_action(
118+
:random_params,
119+
%{
120+
action: :update
121+
}
122+
)
123+
|> Ash.run_action!()
96124

97-
refute params |> Map.has_key?(:author)
125+
refute Map.has_key?(params, :author)
126+
refute Map.has_key?(params, :author_id)
98127
end
99128

100129
describe "options" do

0 commit comments

Comments
 (0)