You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
decentralised_book_index/test/support/generators.ex

67 lines
1.5 KiB

defmodule DecentralisedBookIndex.Generators do
@moduledoc "Generators for tests"
use Ash.Generator
alias DecentralisedBookIndex.Metadata
@doc """
Generates user changesets with the `:register_with_password` action.
"""
def user(opts \\ []) do
changeset_generator(
DecentralisedBookIndex.Accounts.User,
:register_with_password,
defaults: [
# Generates unique values using an auto-incrementing sequence
# eg. `user1@example.com`, `user2@example.com`, etc.
email: sequence(:user_email, &"user#{&1}@example.com"),
password: "password",
password_confirmation: "password"
],
overrides: opts,
authorize?: false
)
end
def author(opts \\ []) do
actor =
opts[:actor] ||
once(:default_actor, fn ->
generate(user())
end)
changeset_generator(
Metadata.Author,
:create,
defaults: [
name: sequence(:name, &"Author #{&1}"),
description: sequence(:name, &"Description #{&1}"),
author_alias_registry_id: nil
],
overrides: opts,
actor: actor
)
end
@doc """
Generates a list of author roles.
Example:
> author_roles = author_roles()
"""
def author_roles(opts \\ []) do
actor =
opts[:actor] ||
once(:default_actor, fn ->
generate(user())
end)
count =
opts[:count] || 2
for order <- 1..count do
%{order: order, author_id: generate(author(actor: actor)).id, role: ""}
end
end
end