parent
02d35ee056
commit
e3da0dc129
@ -0,0 +1,66 @@
|
||||
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
|
Loading…
Reference in new issue