Ash FrameworkAF
Ash Framework8mo ago
9 replies
swrenn

Using seed and changeset generators with child resources

I have a Child resource that belongs_to a Parent. I'd like to use the seed and changeset generators with these resources in my tests. I'd like to

- generate the Child along with a unique Parent in one generator call
- child = generate(child_seed())
- child = generate(child_changeset())
- optionally accept a parent id to bypass parent generation
- child = generate(child_seed(parent_id: 123))
- child = generate(child_changeset(parent_id: 123))
- have this work in property tests

My first try with the changeset generator looks like this. It works, but I don't like it.

  def child_changeset(opts \\ []) do
    changeset_generator(
      Child,
      :create,
      uses: %{},
      defaults: fn %{} ->
        if Keyword.get(opts, :parent_id) do
          []
        else
          parent = generate(ParentGenerator.parent_changeset())
          [parent_id: parent.id]
        end
      end,
      overrides: opts
    )
  end


I poked away at an implementation using seed_generator but failed to get something that worked.

I have a few questions:

- What is the best way to do this?
- Does anyone else use the generators in this way?
- Is the purpose of uses to wrap constants with StreamData.constant?
- Why the difference in interface between seed_generator and changeset_generator beyond changeset_generator taking an action?

Thanks
Was this page helpful?