AshScenario (ash_scenario v0.6.0)
View SourceTest data generation for your Ash application.
AshScenario provides a DSL for defining named prototypes in your Ash resources, allowing you to create test data with automatic dependency resolution.
Usage
Add the DSL to your Ash resources:
defmodule MyApp.Post do
use Ash.Resource, extensions: [AshScenario.Dsl]
prototypes do
prototype :example_post,
title: "My Example Post",
content: "This is example content",
blog_id: :example_blog # Reference to prototype in Blog resource
end
end
defmodule MyApp.Blog do
use Ash.Resource, extensions: [AshScenario.Dsl]
prototypes do
prototype :example_blog,
name: "My Example Blog"
end
endThen run scenarios:
# Run prototypes with database persistence (default)
AshScenario.run([
{MyApp.Blog, :example_blog},
{MyApp.Post, :example_post}
])
# Run prototypes as in-memory structs
AshScenario.run([
{MyApp.Post, :example_post}
], strategy: :struct)
# Run all prototypes for a resource
AshScenario.run_all(MyApp.Post)
Summary
Functions
Use this in your Ash resources to enable prototype support.
Clear all registered prototypes (useful for testing).
Returns any DSL patches provided by AshScenario. Currently returns an empty list as no patches are defined.
Get prototype information from a resource module.
Register prototypes from a resource module. This is typically called automatically when the resource is compiled.
Execute prototypes with the specified strategy.
Execute all prototypes defined for a resource module.
Run a named scenario from a test module.
Returns the DSL sections provided by AshScenario. Delegates to AshScenario.Dsl for the actual sections.
Start the scenario registry (should be called in your application supervision tree).
Functions
Use this in your Ash resources to enable prototype support.
Example
defmodule MyApp.Post do
use Ash.Resource, extensions: [AshScenario.Dsl]
# ... your resource definition
end
Clear all registered prototypes (useful for testing).
Returns any DSL patches provided by AshScenario. Currently returns an empty list as no patches are defined.
Get prototype information from a resource module.
Register prototypes from a resource module. This is typically called automatically when the resource is compiled.
Execute prototypes with the specified strategy.
Parameters
prototype_refs- List of prototype references as{Module, :prototype_name}tuplesopts- Options for execution
Options
:strategy- Execution strategy (:databaseor:struct, defaults to:database):domain- The Ash domain to use (will be inferred if not provided):overrides- Map of attribute overrides keyed by prototype reference
Examples
# Execute with database persistence (default)
{:ok, resources} = AshScenario.run([
{User, :admin},
{Post, :published_post}
])
# Execute as in-memory structs
{:ok, structs} = AshScenario.run([
{User, :admin}
], strategy: :struct)
# With overrides
{:ok, resources} = AshScenario.run([
{Post, :draft}
], overrides: %{{Post, :draft} => %{title: "Custom Title"}})
Execute all prototypes defined for a resource module.
Parameters
resource_module- The Ash resource module containing prototype definitionsopts- Options for execution
Options
:strategy- Execution strategy (:databaseor:struct, defaults to:database):domain- The Ash domain to use (will be inferred if not provided)
Examples
{:ok, resources} = AshScenario.run_all(Post)
{:ok, structs} = AshScenario.run_all(Post, strategy: :struct)
Run a named scenario from a test module.
This function works with the scenario DSL for defining named test setups.
Options
:domain- The Ash domain to use (will be inferred if not provided):strategy- Execution strategy (:databaseor:struct, defaults to:database)
Examples
{:ok, instances} = AshScenario.run_scenario(MyTest, :basic_setup)
{:ok, instances} = AshScenario.run_scenario(MyTest, :basic_setup, domain: MyApp.Domain)
{:ok, structs} = AshScenario.run_scenario(MyTest, :basic_setup, strategy: :struct)
Returns the DSL sections provided by AshScenario. Delegates to AshScenario.Dsl for the actual sections.
Start the scenario registry (should be called in your application supervision tree).