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.
94 lines
2.0 KiB
94 lines
2.0 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
|
|
|
|
@doc """
|
|
Generates a list of bids.
|
|
|
|
Example:
|
|
> bids = bids()
|
|
"""
|
|
def bids(opts \\ []) do
|
|
actor =
|
|
opts[:actor] ||
|
|
once(:default_actor, fn ->
|
|
generate(user())
|
|
end)
|
|
|
|
count =
|
|
opts[:count] || 2
|
|
|
|
types = ["isbn10", "isbn13", "asin"]
|
|
|
|
if count > length(types), do: count = length(types)
|
|
|
|
random_types = Enum.take_random(types, count)
|
|
|
|
for {order, type} <- Enum.zip(1..count, random_types) do
|
|
%{order: order, type: type, bid: "1234567890"}
|
|
end
|
|
end
|
|
end
|