mirror of
https://github.com/ash-project/ash.git
synced 2024-09-19 21:13:10 +12:00
test: add example factory pattern for tests
This commit is contained in:
parent
225fcf9199
commit
459426135e
1 changed files with 30 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue