Skip to content

Commit 2abe8c5

Browse files
committed
fill-unill
1 parent 0ceddef commit 2abe8c5

File tree

4 files changed

+90
-63
lines changed

4 files changed

+90
-63
lines changed

README.md

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,86 @@
11
# AshRandomParams
22

3-
Shortcut DSL for attributes and relationships in Ash resources.
3+
A library that generates random parameters for Ash resource actions. It provides a convenient way to create random test data for your Ash resources by automatically generating random values for accepts and arguments.
44

5-
### For attribute
5+
## Installation
66

7-
| DSL | allow_nil? | public? |
8-
|:---:|:----------:|:-------:|
9-
| `attribute` (Ash) | true | false |
10-
| `req` | false | true |
11-
| `req_prv` | false | false |
12-
| `opt` | true | true |
13-
| `opt_prv` | true | false |
7+
Add `ash_random_params` to your list of dependencies in `mix.exs`:
148

15-
### For belongs_to
9+
```elixir
10+
def deps do
11+
[
12+
{:ash_random_params, "~> 0.2.0"}
13+
]
14+
end
15+
```
1616

17-
| DSL | allow_nil? | public? |
18-
|:---:|:----------:|:-------:|
19-
| `belongs_to` (Ash) | true | false |
20-
| `req_belongs_to` | false | true |
21-
| `req_prv_belongs_to` | false | false |
22-
| `opt_belongs_to` | true | true |
23-
| `opt_prv_belongs_to` | true | false |
17+
## Usage
2418

25-
## Usage Example
19+
Add the `random_params` DSL to your Ash resource:
2620

2721
```elixir
28-
defmodule MyApp.User do
22+
defmodule MyApp.Post do
2923
use Ash.Resource, extensions: [AshRandomParams]
3024

3125
attributes do
3226
uuid_primary_key :id
27+
attribute :title, :string, allow_nil?: false
28+
attribute :content, :string, allow_nil?: true
29+
end
3330

34-
# Required attributes
35-
req :email, :string # allow_nil?: false, public?: true
36-
req_prv :password_hash, :string # allow_nil?: false, public?: false
31+
random_params do
32+
random MyRandom # Optional: specify your custom random generator
33+
end
34+
end
35+
```
3736

38-
# Optional attributes
39-
opt :name, :string # allow_nil?: true, public?: true
40-
opt_prv :last_login_at, :utc_datetime # allow_nil?: true, public?: false
37+
### Custom Random Generator
4138

42-
# Original attribute macro still works
43-
attribute :nickname, :string, allow_nil?: true
44-
end
39+
You can implement a custom random generator by using the `AshRandomParams.Random` behaviour:
4540

46-
relationships do
47-
# Required relationships
48-
req_belongs_to :company, Company # allow_nil?: false, public?: true
49-
req_prv_belongs_to :created_by, User # allow_nil?: false, public?: false
41+
```elixir
42+
defmodule MyRandom do
43+
use AshRandomParams.Random
5044

51-
# Optional relationships
52-
opt_belongs_to :manager, User # allow_nil?: true, public?: true
53-
opt_prv_belongs_to :updated_by, User # allow_nil?: true, public?: false
45+
@impl AshRandomParams.Random
46+
def random(%{type: Ash.Type.Integer}, _opts, _context) do
47+
777 # Custom random integer
48+
end
5449

55-
# Original belongs_to macro still works
56-
belongs_to :department, Department, allow_nil?: true
50+
@impl AshRandomParams.Random
51+
def random(attr_or_arg, opts, context) do
52+
# Fallback to DefaultRandom for all other types
53+
AshRandomParams.DefaultRandom.random(attr_or_arg, opts, context)
5754
end
5855
end
5956
```
6057

61-
## Installation
62-
63-
Add `ash_random_params` to your list of dependencies in `mix.exs`:
58+
### Using Random Params
6459

6560
```elixir
66-
def deps do
67-
[
68-
{:ash_random_params, "~> 0.2.0"}
69-
]
70-
end
61+
# Basic usage
62+
Post.random_params!(:create)
63+
64+
# With initial params
65+
Post.random_params!(:create, %{key: "value"})
66+
67+
# With options
68+
Post.random_params!(:create, %{key: "value"}, %{
69+
fill: [:optional_field], # Fill optional fields
70+
unfill: [:required_field] # Leave required fields as nil
71+
})
7172
```
7273

74+
## Features
75+
76+
- Automatically generates random values for action accepts and arguments
77+
- Supports custom random value generators
78+
- Useful for testing and development
79+
7380
## License
7481

7582
MIT
7683

84+
## Links
85+
86+
- [GitHub Repository](https://github.com/devall-org/ash_random_params)

lib/ash_random_params.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule AshRandomParams do
22
@random_params %Spark.Dsl.Section{
33
name: :random_params,
44
describe: """
5-
random_params 설정
5+
random_params configuration
66
""",
77
schema: [
88
random: [
@@ -11,7 +11,7 @@ defmodule AshRandomParams do
1111
required: false,
1212
default: {AshRandomParams.DefaultRandom, []},
1313
doc: """
14-
random attribute, argument 생성 함수
14+
random attribute, argument generation function
1515
"""
1616
]
1717
],

lib/transformer.ex

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ defmodule AshRandomParams.Transformer do
44
alias Ash.Resource.Relationships.BelongsTo
55
alias Ash.Resource.Builder
66

7-
# def before?(AshCloak.Transformers.SetupEncryption), do: true
87
def before?(Ash.Resource.Transformers.BelongsToAttribute), do: false
98
def before?(_), do: true
109

@@ -20,17 +19,17 @@ defmodule AshRandomParams.Transformer do
2019
def add_random_params(dsl_state) do
2120
action = Builder.build_action_argument(:action, :atom, default: :create)
2221
init_params = Builder.build_action_argument(:init_params, :map, default: %{})
23-
exclude = Builder.build_action_argument(:exclude, {:array, :atom}, default: [])
24-
include = Builder.build_action_argument(:include, {:array, :atom}, default: [])
22+
fill = Builder.build_action_argument(:fill, {:array, :atom}, default: [])
23+
unfill = Builder.build_action_argument(:unfill, {:array, :atom}, default: [])
2524
tagged_unions = Builder.build_action_argument(:tagged_unions, :map, default: %{})
2625

2726
dsl_state
2827
|> Builder.add_action(:action, :random_params,
2928
returns: :map,
30-
arguments: [action, init_params, exclude, include, tagged_unions],
29+
arguments: [action, init_params, unfill, fill, tagged_unions],
3130
run: &__MODULE__.do_random_params/2
3231
)
33-
|> Builder.add_interface(:random_params, args: [:action, :init_params])
32+
|> Builder.add_interface(:random_params, args: [:action, {:optional, :init_params}])
3433
end
3534

3635
def add_random_attr(dsl_state) do
@@ -51,8 +50,8 @@ defmodule AshRandomParams.Transformer do
5150
arguments: %{
5251
action: action,
5352
init_params: init_params,
54-
exclude: exclude,
55-
include: include,
53+
unfill: unfill,
54+
fill: fill,
5655
tagged_unions: tagged_unions
5756
}
5857
} =
@@ -105,10 +104,10 @@ defmodule AshRandomParams.Transformer do
105104
|> Enum.reject(&(&1.name in rel_names))
106105
|> Enum.reject(&(&1.name in belongs_to_attrs))
107106
|> Enum.reject(&(&1.name in union_keys))
108-
|> Enum.reject(&(&1.name in exclude))
107+
|> Enum.reject(&(&1.name in unfill))
109108
|> Enum.reject(& &1.allow_nil?)
110109

111-
fields_by_fill = candidates |> Enum.filter(&(&1.name in include))
110+
fields_by_fill = candidates |> Enum.filter(&(&1.name in fill))
112111

113112
(fields_by_unfill ++ fields_by_fill)
114113
|> Enum.reject(&(&1.name in init_keys))

test/ash_random_params_test.exs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ defmodule AshRandomParamsTest do
119119
|> Ash.run_action!()
120120
end
121121

122-
test "with include" do
122+
test "with fill" do
123123
assert %{
124124
author_id: nil,
125125
req_str: "req_str-" <> _,
@@ -132,13 +132,13 @@ defmodule AshRandomParamsTest do
132132
:random_params,
133133
%{
134134
action: :create,
135-
include: [:opt_str]
135+
fill: [:opt_str]
136136
}
137137
)
138138
|> Ash.run_action!()
139139
end
140140

141-
test "with exclude" do
141+
test "with unfill" do
142142
assert %{
143143
author_id: nil,
144144
req_str: nil,
@@ -151,7 +151,7 @@ defmodule AshRandomParamsTest do
151151
:random_params,
152152
%{
153153
action: :create,
154-
exclude: [:req_str]
154+
unfill: [:req_str]
155155
}
156156
)
157157
|> Ash.run_action!()
@@ -162,14 +162,32 @@ defmodule AshRandomParamsTest do
162162
assert %{
163163
author_id: nil,
164164
req_str: "req_str-" <> _,
165-
opt_str: "opt_str-" <> _,
165+
opt_str: nil,
166166
req_int: 777,
167167
opt_int: nil
168+
} =
169+
Post.random_params!(:create)
170+
171+
assert %{
172+
author_id: nil,
173+
req_str: "req_str-" <> _,
174+
opt_str: nil,
175+
req_int: 123,
176+
opt_int: 456
177+
} =
178+
Post.random_params!(:create, %{req_int: 123, opt_int: 456})
179+
180+
assert %{
181+
author_id: nil,
182+
req_str: "req_str-" <> _,
183+
opt_str: nil,
184+
req_int: 777,
185+
opt_int: 456
168186
} =
169187
Post.random_params!(
170188
:create,
171-
%{},
172-
%{include: [:opt_str]}
189+
%{opt_int: 456},
190+
%{fill: [:opt_int]}
173191
)
174192
end
175193
end

0 commit comments

Comments
 (0)