test: add example factory pattern for tests

This commit is contained in:
Zach Daniel 2024-06-06 10:30:17 -04:00
parent 225fcf9199
commit 459426135e

View file

@ -230,6 +230,7 @@ defmodule Ash.Test.GeneratorTest do
meta: %{},
metadata: %{}
}
describe "seed_input" do
test "it returns attributes generated" do
Author
@ -240,6 +241,35 @@ defmodule Ash.Test.GeneratorTest do
end)
end
defmodule Factory do
def post(params \\ %{}) do
defaults = %{
name: StreamData.repeatedly(&Ash.UUID.generate/0)
}
Ash.Generator.seed_input(Post, Map.merge(defaults, params))
end
def author(params \\ %{}) do
defaults = %{
name: StreamData.repeatedly(&Ash.UUID.generate/0),
posts: StreamData.list_of(post(), min_length: 1, max_length: 5),
meta: %{},
metadata: %{}
}
Ash.Generator.seed_input(Author, Map.merge(defaults, params))
end
end
test "individual constructors can be supplied" do
check all(input <- Factory.author()) do
post_count = Enum.count(seed!(Author, input).posts)
assert post_count >= 1
assert post_count <= 5
end
end
test "it can be used in property testing" do
check all(input <- Ash.Generator.seed_input(Author, @meta_generator)) do
seed!(Author, input)