|
| 1 | +# AshRandomParams |
| 2 | + |
| 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. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +Add the `random_params` DSL to your Ash resource: |
| 8 | + |
| 9 | +```elixir |
| 10 | +defmodule Post do |
| 11 | + use Ash.Resource, extensions: [AshRandomParams] |
| 12 | + |
| 13 | + attributes do |
| 14 | + uuid_primary_key :id |
| 15 | + attribute :author, :string, allow_nil?: false |
| 16 | + attribute :title, :string, allow_nil?: false |
| 17 | + attribute :content, :string, allow_nil?: true |
| 18 | + attribute :tag, :string, allow_nil?: false, default: "JS" |
| 19 | + end |
| 20 | + |
| 21 | + random_params do |
| 22 | + random MyRandom # Optional: specify your custom random generator |
| 23 | + end |
| 24 | +end |
| 25 | +``` |
| 26 | + |
| 27 | +### Using Random Params |
| 28 | + |
| 29 | +```elixir |
| 30 | +# Basic usage |
| 31 | +Post.random_params!(:create) |
| 32 | +=> %{author: "author-81491", title: "title-388112", content: nil, tag: "JS"} |
| 33 | + |
| 34 | +# With initial params |
| 35 | +Post.random_params!(:create, %{author: "James"}) |
| 36 | +=> %{author: "James", title: "title-388112", content: nil, tag: "JS"} |
| 37 | + |
| 38 | +# With options |
| 39 | +Post.random_params!(:create, %{author: "James"}, %{ |
| 40 | + populate: [:content], |
| 41 | + omit: [:title], |
| 42 | + include_defaults?: false |
| 43 | +}) |
| 44 | +=> %{author: "James", content: "content-38128"} |
| 45 | +``` |
| 46 | + |
| 47 | +### Default Behavior |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +### Belongs To Relationships |
| 52 | + |
| 53 | +For attributes and arguments that match the `name` or `source_attribute` of a `belongs_to` relationship: |
| 54 | +- In `create` actions, they are generated with `nil` values |
| 55 | +- In other actions, they are not generated at all |
| 56 | + |
| 57 | +This behavior exists because: |
| 58 | +- In `create` actions, omitting a value is equivalent to setting it to `nil` |
| 59 | +- In `update` actions, omitting a value preserves the existing relationship, while explicitly setting it to `nil` removes the relationship |
| 60 | + |
| 61 | +### Options |
| 62 | + |
| 63 | +- `populate`: Forces generation of random values for specified attributes/arguments, overriding the default behavior |
| 64 | +- `omit`: Prevents generation of random values for specified attributes/arguments, overriding the default behavior |
| 65 | +- `include_defaults?`: When set to `true`, includes default values for attributes/arguments that have either `allow_nil?: true` or a non-nil default value. Defaults to `true`. In the example above, this would add `%{content: nil, tag: "JS"}` to the generated params. |
| 66 | + |
| 67 | +### Custom Random Generator |
| 68 | + |
| 69 | +You can implement a custom random generator by using the `AshRandomParams.Random` behaviour: |
| 70 | + |
| 71 | +```elixir |
| 72 | +defmodule MyRandom do |
| 73 | + use AshRandomParams.Random |
| 74 | + |
| 75 | + @impl AshRandomParams.Random |
| 76 | + def random(%{type: Ash.Type.Integer}, _opts, _context) do |
| 77 | + 777 |
| 78 | + end |
| 79 | + |
| 80 | + @impl AshRandomParams.Random |
| 81 | + def random(attr_or_arg, opts, context) do |
| 82 | + # Fallback to DefaultRandom for all other types |
| 83 | + AshRandomParams.DefaultRandom.random(attr_or_arg, opts, context) |
| 84 | + end |
| 85 | +end |
| 86 | +``` |
| 87 | + |
| 88 | +## Features |
| 89 | + |
| 90 | +- Automatically generates random values for action accepts and arguments |
| 91 | +- Supports custom random value generators |
| 92 | +- Useful for testing and development |
| 93 | + |
| 94 | +## Installation |
| 95 | + |
| 96 | +Add `ash_random_params` to your list of dependencies in `mix.exs`: |
| 97 | + |
| 98 | +```elixir |
| 99 | +def deps do |
| 100 | + [ |
| 101 | + {:ash_random_params, "~> 0.1.0"} |
| 102 | + ] |
| 103 | +end |
| 104 | +``` |
| 105 | + |
| 106 | +## License |
| 107 | + |
| 108 | +MIT |
| 109 | + |
| 110 | +## Links |
| 111 | + |
| 112 | +- [GitHub Repository](https://github.com/devall-org/ash_random_params) |
0 commit comments