Skip to content

Repository files navigation

AshRandomParams

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.

Usage

Add the random_params DSL to your Ash resource:

defmodulePostdouseAsh.Resource,extensions: [AshRandomParams]attributesdouuid_primary_key:idattribute:author,:string,allow_nil?: falseattribute:title,:string,allow_nil?: falseattribute:content,:string,allow_nil?: trueattribute:tag,:string,allow_nil?: false,default: "JS"endrandom_paramsdorandomMyRandom# Optional: specify your custom random generatorendend

Using Random Params

# Basic usagePost.random_params!(:create)=>%{author: "author-81491",title: "title-388112",content: nil,tag: "JS"}# With initial paramsPost.random_params!(:create,%{author: "James"})=>%{author: "James",title: "title-388112",content: nil,tag: "JS"}# With optionsPost.random_params!(:create,%{author: "James"},%{enforce_random: [:content],exclude: [:title],include_defaults?: false})=>%{author: "James",content: "content-38128"}

Default Behavior

By default, it generates random values for accepts and arguments that have allow_nil?: false and no default value (default == nil). In the example above, author and title fall into this category.

Belongs To Relationships

For accepts and arguments that match the name or source_attribute of a belongs_to relationship:

  • In create actions, they are generated with nil values
  • In other actions, they are not generated at all

This behavior exists because:

  • In create actions, excluding a value is equivalent to setting it to nil
  • In update actions, excluding a value preserves the existing relationship, while explicitly setting it to nil removes the relationship

Options

  • enforce_random: Forces generation of random values for specified accepts/arguments, overriding the default behavior
  • exclude: Prevents generation of random values for specified accepts/arguments, overriding the default behavior
  • include_defaults?: When set to true, includes default values for accepts/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.

Custom Random Generator

You can implement a custom random generator by using the AshRandomParams.Random behaviour:

defmoduleMyRandomdouseAshRandomParams.Random@implAshRandomParams.Randomdefrandom(%{type: Ash.Type.Integer},_opts,_context)do777end@implAshRandomParams.Randomdefrandom(attr_or_arg,opts,context)do# Fallback to DefaultRandom for all other typesAshRandomParams.DefaultRandom.random(attr_or_arg,opts,context)endend

Features

  • Automatically generates random values for action accepts and arguments
  • Supports custom random value generators
  • Useful for testing and development

Installation

Add ash_random_params to your list of dependencies in mix.exs:

defdepsdo[{:ash_random_params,"~> 0.2.1"}]end

License

MIT

Links

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages