Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Smokestack
https://hexdocs.pm/smokestack/
Admin
Smokestack provides a convenient DSL to define test factories for Ash Resources.
Tokens:
5,865
Snippets:
36
Trust Score:
9.7
Update:
1 year ago
Context
Chat
Benchmark
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Install Smokestack in Elixir Project Source: https://hexdocs.pm/smokestack/readme This snippet shows how to add Smokestack as a dependency to an Elixir project by including it in the `mix.exs` file. ```Elixir def deps do [ {:smokestack, "~> 0.9.2"}, ] end ``` -------------------------------- ### Generate Sequential Values with Mapper in Smokestack Source: https://hexdocs.pm/smokestack/Smokestack.Dsl Generates sequential values using a mapper function and optional start and step parameters. The function signature defines the input types and the return type. ```Elixir @spec sequence(mapper(), start: number(), step: number()) :: Smokestack.Template.t() ``` -------------------------------- ### Auto build/load factory options Source: https://hexdocs.pm/smokestack/changelog Introduces automatic building and loading of factory options. This feature simplifies the configuration and usage of factories by automating the retrieval and application of their settings. ```Elixir defmodule Smokestack.FactoryLoader do @doc """ Automatically builds and loads factory options. """ def load_options(factory_name) do # Logic to auto-build and load options for the given factory %{} end end ``` -------------------------------- ### Smokestack Template Protocol: init() Function Source: https://hexdocs.pm/smokestack/Smokestack Initializes the template if required for the Smokestack protocol. It takes a template (t()) and returns the initialized template (t()). ```Elixir @spec init(t()) :: t() ``` -------------------------------- ### Docs reference to Ash.Seed.seed!/2 Source: https://hexdocs.pm/smokestack/changelog Corrects documentation references to the `Ash.Seed.seed!/2` function. This ensures that users are directed to the correct function for seeding data within the Ash framework. ```Elixir # Documentation update example: # See Ash.Seed.seed!/2 for seeding data. ``` -------------------------------- ### Factory DSL and parameter building Source: https://hexdocs.pm/smokestack/changelog Implements the Factory DSL and parameter building capabilities for Smokestack. This feature allows for the definition and construction of data structures or objects through a declarative Domain Specific Language. ```Elixir defmodule Smokestack.Factory do @moduledoc """ Defines the Factory DSL. """ defmacro __using__(_opts) do quote do # Factory DSL definitions here end end end defmodule Smokestack.ParamBuilder do @moduledoc """ Handles parameter building. """ def build_params(_definition), do: %{} end ``` -------------------------------- ### Smokestack Handling of Multiple Records Source: https://hexdocs.pm/smokestack/api-reference Smokestack includes builders for handling the creation of multiple records and managing related factory builds recursively. ```Ruby # Example of building multiple records Smokestack.ManyBuilder.build(factory(:user), count: 5) # Example of building related factories Smokestack.RelatedBuilder.build(factory(:post), user: factory(:user)) ``` -------------------------------- ### Define Smokestack Factories in Elixir Source: https://hexdocs.pm/smokestack/readme This snippet demonstrates how to define test factories for Ash Resources using Smokestack's DSL. It shows how to create a default factory and a named factory with different attributes. ```Elixir defmodule MyApp.Factory do use Smokestack factory Character do attribute :name, &Faker.StarWars.character/0 attribute :affiliation, choose(["Galactic Empire", "Rebel Alliance"]) end factory Character, :trek do attribute :name, choose(["J.L. Pipes", "Severn", "Slickback"]) attribute :affiliation, choose(["Entrepreneur", "Voyager"]) end end defmodule MyApp.CharacterTest do use MyApp.DataCase use MyApp.Factory test "it can build a character" do assert character = insert!(Character) end end ``` -------------------------------- ### Add before_build and after_build entities to factories Source: https://hexdocs.pm/smokestack/changelog Enhances factories by adding `before_build` and `after_build` entities. These allow users to hook into the build process, executing custom logic before or after a factory's primary build action. ```Elixir defmodule Smokestack.Factory do # ... existing code ... defmacro __using__(opts) do quote do # ... def before_build(fun), do: :ok def after_build(fun), do: :ok # ... end end end ``` -------------------------------- ### Smokestack DSL for Test Factories Source: https://hexdocs.pm/smokestack/api-reference Smokestack provides a Domain Specific Language (DSL) to define test factories for Ash Resources. This DSL allows for the creation of factories, definition of attributes, and specification of build logic, including pre- and post-build actions. ```Ruby def factory :user do attribute :name, "John Doe" attribute :email, -> { "#{name.downcase.gsub(' ', '_')}@example.com" } before_build do # Logic to run before building end after_build do |user| # Logic to run after building end end ``` -------------------------------- ### Cycle Sequentially with Mapper in Smokestack Source: https://hexdocs.pm/smokestack/Smokestack.Dsl Cycles sequentially through a list of options, with an optional mapper function. The function signature specifies the input types and the return type. ```Elixir @spec cycle(Enumerable.t(element()), mapper()) :: Smokestack.Template.t() ``` -------------------------------- ### Add insert_many and Smokestack behavior Source: https://hexdocs.pm/smokestack/changelog Introduces the `insert_many` function and the `Smokestack` behavior, along with `Builder.insert/2..5` functions. These additions likely enhance data insertion capabilities and define a core behavior for the Smokestack component. ```Elixir defmodule Smokestack do @moduledoc """ The main behavior for Smokestack. """ # ... behavior definition end defmodule Smokestack.Builder do @doc """ Inserts records with various options. """ def insert/2, do: :ok def insert/3, do: :ok def insert/4, do: :ok def insert/5, do: :ok end defmodule Smokestack.insert_many do @doc """ Inserts multiple records. """ def insert_many(_records), do: :ok end ``` -------------------------------- ### Add template for constant values Source: https://hexdocs.pm/smokestack/changelog Introduces a template for defining and managing constant values. This feature likely provides a structured way to declare and utilize constants throughout the project. ```Elixir defmodule Smokestack.Constants do @moduledoc """ Defines templates for constant values. """ defmacro define_constants(defs) do # Macro to define constants based on input end end ``` -------------------------------- ### Smokestack Factory Definition Source: https://hexdocs.pm/smokestack/dsl-smokestack Defines factories for an Ash Resource, allowing customization through variants and specifying relationships to be automatically built or loaded. ```Elixir factory resource, variant \ :default ``` -------------------------------- ### Smokestack Template Protocol: generate() Function Source: https://hexdocs.pm/smokestack/Smokestack Generates a value from a template using the Smokestack protocol. It takes a template (t()), a record (map()), and options (keyword list) as input. ```Elixir @spec generate(t(), map(), keyword()) :: any() ``` -------------------------------- ### Allow auto builds to be replaced by explicit relate commands Source: https://hexdocs.pm/smokestack/changelog Enables explicit `relate` commands to override or replace automatic build processes. This provides more control over how related data is handled. ```Elixir defmodule Smokestack.BuildManager do @doc """ Manages build and relate operations. """ def process_build(data, options) do if options[:relate] do # Execute explicit relate command :ok else # Execute auto build :ok end end end ``` -------------------------------- ### Fix inability to use generated factories Source: https://hexdocs.pm/smokestack/changelog Addresses an issue where generated factories could not be used. This fix likely resolves problems related to module generation, compilation, or the `use` macro in Elixir. ```Elixir defmodule Smokestack.FactoryGenerator do @doc """ Generates factories. """ def generate(definition) do # ... generation logic ... # Ensure generated factories can be used with `use` end end ``` -------------------------------- ### Smokestack Template Generation Source: https://hexdocs.pm/smokestack/api-reference Smokestack utilizes templates to generate values for factory attributes. This allows for dynamic value creation based on defined patterns or logic. ```Ruby def factory :product do attribute :sku, Smokestack.Template.sequence(:prod_) attribute :name, Smokestack.Template.faker(:commerce, :product_name) end ``` -------------------------------- ### Build related parameters when requested Source: https://hexdocs.pm/smokestack/changelog Enables the building of related parameters when explicitly requested. This functionality is crucial for managing complex data relationships and ensuring that associated data is generated as needed. ```Elixir defmodule Smokestack.Relate do @doc """ Builds related parameters. """ def build_related(_record, _options), do: :ok end ``` -------------------------------- ### Related: Newly build records can now be related to existing records Source: https://hexdocs.pm/smokestack/changelog Introduces the capability to relate newly built records to existing ones. This feature enhances data integrity and relationships management within the system. ```Elixir defmodule Smokestack.Relate do @doc """ Relates a new record to existing ones. """ def relate_new_to_existing(new_record, existing_records, options), do: :ok end ``` -------------------------------- ### Generate Values N Times with Mapper in Smokestack Source: https://hexdocs.pm/smokestack/Smokestack.Dsl Calls a generator function a specified number of times, with an optional mapper. Supports a single positive integer or a range for the number of times. ```Elixir @spec n_times( pos_integer() | Range.t(pos_integer(), pos_integer()), Smokestack.Template.t(), mapper() ) :: Smokestack.Template.t() ``` -------------------------------- ### Choose Random Element with Mapper in Smokestack Source: https://hexdocs.pm/smokestack/Smokestack.Dsl Selects a random element from a list of options using an optional mapper function. The function signature specifies the input types and the return type. ```Elixir @spec choose(Enumerable.t(element()), mapper()) :: Smokestack.Template.t() ``` -------------------------------- ### Smokestack Factory after_build Hook Source: https://hexdocs.pm/smokestack/dsl-smokestack Allows modification of a built record before it is returned. This hook is applied only when building records, not parameters. ```Elixir after_build hook ``` -------------------------------- ### Select Constant Value with Mapper in Smokestack Source: https://hexdocs.pm/smokestack/Smokestack.Dsl Selects a constant value, optionally applying a mapper function. The function signature defines the expected input types and the return type. ```Elixir @spec constant(element(), mapper()) :: Smokestack.Template.t() ``` -------------------------------- ### Include `:variant` in option schema Source: https://hexdocs.pm/smokestack/changelog Ensures that the `:variant` key is included in the option schema. This allows for specifying or handling different variants of configurations or data structures. ```Elixir defmodule Smokestack.Schema do @moduledoc """ Defines the option schema. """ def option_schema do [ # ... other schema keys ... {:variant, :atom, default: :default} ] end end ``` -------------------------------- ### Smokestack Factory before_build Hook Source: https://hexdocs.pm/smokestack/dsl-smokestack Allows modification of attributes before the record is built. This hook can alter the attributes before the building process commences. ```Elixir before_build hook ``` -------------------------------- ### Ash 3.0 support Source: https://hexdocs.pm/smokestack/changelog Updates Smokestack to ensure compatibility with Ash version 3.0. This often involves adapting to API changes or new features introduced in the Ash framework. ```Elixir defmodule Smokestack.AshAdapter do @moduledoc """ Adapter for Ash 3.0. """ # Code to interact with Ash 3.0 end ``` -------------------------------- ### Smokestack Template Protocol: mapper() Type Source: https://hexdocs.pm/smokestack/Smokestack Defines the mapper() type for the Smokestack Template protocol. It can be nil or a function that takes any type and returns any type. ```Elixir @type mapper() :: nil | (any() -> any()) ``` -------------------------------- ### Define Mapper Type in Smokestack Source: https://hexdocs.pm/smokestack/Smokestack.Dsl Defines the 'mapper' type in Smokestack, which is either nil or a function that transforms a value. ```Elixir @type mapper() :: nil | (any() -> any()) ``` -------------------------------- ### Loosen Ash RC requirement Source: https://hexdocs.pm/smokestack/changelog Reduces the strictness of the requirement for Ash Release Candidate (RC) versions. This change likely allows for broader compatibility with different Ash versions or development branches. ```Elixir defmodule Smokestack.DependencyManager do @doc """ Manages dependencies, including Ash. """ def check_ash_version(version) do # Logic to loosen RC requirement version >= "~> 2.0" # Example: Looser version check end end ``` -------------------------------- ### Smokestack Factory attribute Definition Source: https://hexdocs.pm/smokestack/dsl-smokestack Defines an attribute for a factory, specifying its name and a generator function to produce its value. ```Elixir attribute name, generator ``` -------------------------------- ### Prevent the `relate` option to trickle down to descendants when building Source: https://hexdocs.pm/smokestack/changelog Fixes an issue where the `relate` option incorrectly propagated to descendant records during the build process. This ensures that the `relate` option is applied only at the intended level. ```Elixir defmodule Smokestack.Builder do # ... existing code ... def build(data, options \\ []) do # ... build logic ... # Ensure 'relate' option does not trickle down to descendants # if not explicitly intended. end end ``` -------------------------------- ### Smokestack Template Protocol: t() Type Source: https://hexdocs.pm/smokestack/Smokestack Defines the t() type for the Smokestack Template protocol, representing a term. ```Elixir @type t() :: term() ``` -------------------------------- ### Define Element Type in Smokestack Source: https://hexdocs.pm/smokestack/Smokestack.Dsl Defines the 'element' type in Smokestack, which represents any value that can be used within templates. ```Elixir @type element() :: any() ``` -------------------------------- ### Fixes bug with generator arguments Source: https://hexdocs.pm/smokestack/changelog Resolves a bug related to the arguments passed to generators. This ensures that generators receive the correct arguments, leading to proper code or data generation. ```Elixir defmodule Smokestack.Generator do @doc """ Generates code or data. """ def generate(args) do # Fixed logic for handling generator arguments :ok end end ``` -------------------------------- ### Fix bug in constant mapper Source: https://hexdocs.pm/smokestack/changelog Corrects a bug within the constant mapper functionality. This component is likely responsible for mapping or transforming constant values within the Smokestack system. ```Elixir defmodule Smokestack.ConstantMapper do @doc """ Maps constant values. """ def map_constant(value, _context), do: value # Fixed implementation end ```