Skip to content

Commit

Permalink
Make :from option really optional for produce/update instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
fuelen committed Jul 28, 2023
1 parent 2504807 commit 720572e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 30 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Expand Up @@ -2,6 +2,7 @@
locals_without_parens = [
produce: 2,
update: 2,
update: 1,
delete: 1,
produce: 1,
param: 2,
Expand Down
2 changes: 2 additions & 0 deletions README.md
@@ -1,4 +1,5 @@
# SeedFactory

A utility for producing entities using business logic defined by your application.

The main idea of `SeedFactory` is to produce entities in tests according to your application business logic (read as context functions if you use https://hexdocs.pm/phoenix/contexts.html) whenever it is possible and avoid direct inserts to the database (opposed to `ex_machina`).
Expand All @@ -9,6 +10,7 @@ See docs for details <https://hexdocs.pm/seed_factory>.
## Installation

The package can be installed by adding `seed_factory` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
Expand Down
8 changes: 4 additions & 4 deletions lib/seed_factory.ex
Expand Up @@ -42,7 +42,7 @@ defmodule SeedFactory do
end
end)
produce :company, from: :company
produce :company
end
command :create_user do
Expand All @@ -52,8 +52,8 @@ defmodule SeedFactory do
resolve(fn args -> MyApp.Users.create_user(args.company, args.name, args.role) end)
produce :user, from: :user
produce :profile, from: :profile
produce :user
produce :profile
end
command :activate_user do
Expand All @@ -65,7 +65,7 @@ defmodule SeedFactory do
{:ok, %{user: user}}
end)
update :user, from: :user
update :user
end
trait :pending, :user do
Expand Down
2 changes: 2 additions & 0 deletions lib/seed_factory/dsl.ex
Expand Up @@ -8,12 +8,14 @@ defmodule SeedFactory.DSL do
@producing_instruction %Spark.Dsl.Entity{
name: :produce,
args: [:entity],
transform: {SeedFactory.ProducingInstruction, :transform, []},
target: SeedFactory.ProducingInstruction,
schema: SeedFactory.ProducingInstruction.schema()
}
@updating_instruction %Spark.Dsl.Entity{
name: :update,
args: [:entity],
transform: {SeedFactory.UpdatingInstruction, :transform, []},
target: SeedFactory.UpdatingInstruction,
schema: SeedFactory.UpdatingInstruction.schema()
}
Expand Down
8 changes: 7 additions & 1 deletion lib/seed_factory/producing_instruction.ex
Expand Up @@ -10,10 +10,16 @@ defmodule SeedFactory.ProducingInstruction do
],
from: [
type: :atom,
required: true,
doc: "A name of the field returned by resolver"
]
]

def schema, do: @schema

def transform(instruction) do
case instruction.from do
nil -> {:ok, %{instruction | from: instruction.entity}}
_from -> {:ok, instruction}
end
end
end
24 changes: 22 additions & 2 deletions lib/seed_factory/schema.ex
Expand Up @@ -69,24 +69,44 @@ defmodule SeedFactory.Schema do
### Options
* `:from` - an atom that specifies the key of the map returned by the resolver.
* `:from` - an atom that specifies the key of the map returned by the resolver. Defaults to specified entity name (first argument)
```elixir
produce :entity_name, from: :source_key
```
```elixir
resolve(fn args ->
{user, profile} = MyApp.register_user!(args)
{:ok, %{user: user, profile: profile}}
end)
produce :user
produce :user_profile, from: :profile
```
## Updating Entities
The `update` directive modifies an existing entity within the context. It takes two arguments: the name of the entity being updated and options.
### Options
* `:from` - an atom that specifies the key of the map returned by the resolver.
* `:from` - an atom that specifies the key of the map returned by the resolver. Defaults to specified entity name (first argument)
```elixir
update :entity_name, from: :source_key
```
```elixir
resolve(fn args ->
{user, profile} = MyApp.update_user!(args)
{:ok, %{user: user, profile: profile}}
end)
update :user
update :user_profile, from: :profile
```
## Deleting Entities
The `delete` directive removes an entity from the context.
Expand Down
8 changes: 7 additions & 1 deletion lib/seed_factory/updating_instruction.ex
Expand Up @@ -10,10 +10,16 @@ defmodule SeedFactory.UpdatingInstruction do
],
from: [
type: :atom,
required: true,
doc: "A name of the field returned by resolver"
]
]

def schema, do: @schema

def transform(instruction) do
case instruction.from do
nil -> {:ok, %{instruction | from: instruction.entity}}
_from -> {:ok, instruction}
end
end
end
20 changes: 10 additions & 10 deletions test/support/schema_example.exs
Expand Up @@ -28,15 +28,15 @@ defmodule SchemaExample do
raise "BOOM"
end)

update :user, from: :user
update :user
end

command :resolve_with_error do
resolve(fn _args ->
{:error, %{message: "OOPS", other_key: :data}}
end)

update :user, from: :user
update :user
end

command :create_org do
Expand All @@ -52,7 +52,7 @@ defmodule SchemaExample do
{:ok, %{org: org}}
end)

produce :org, from: :org
produce :org
end

command :create_office do
Expand All @@ -64,7 +64,7 @@ defmodule SchemaExample do
{:ok, %{office: office}}
end)

produce :office, from: :office
produce :office
end

command :create_draft_project do
Expand All @@ -87,7 +87,7 @@ defmodule SchemaExample do
{:ok, %{project: %{args.project | draft?: false, published_by_id: args.published_by.id}}}
end)

produce :project, from: :project
produce :project
delete :draft_project
end

Expand Down Expand Up @@ -116,8 +116,8 @@ defmodule SchemaExample do
{:ok, %{user: user, profile: profile}}
end)

produce :user, from: :user
produce :profile, from: :profile
produce :user
produce :profile
end

command :activate_user do
Expand All @@ -131,15 +131,15 @@ defmodule SchemaExample do
{:ok, %{user: %{args.user | status: :active, plan: args.finances.plan}}}
end)

update :user, from: :user
update :user
end

command :suspend_user do
param :user, entity: :user, with_traits: [:active]

resolve(fn args -> {:ok, %{user: %{args.user | status: :suspended}}} end)

update :user, from: :user
update :user
end

command :delete_user do
Expand Down Expand Up @@ -171,7 +171,7 @@ defmodule SchemaExample do
end)

produce :virtual_file, from: :file
update :project, from: :project
update :project
end

trait :pending, :user do
Expand Down
13 changes: 1 addition & 12 deletions test/support/schema_example_extended.exs
Expand Up @@ -8,17 +8,6 @@ defmodule SchemaExampleExtended do
{:ok, %{conn: %{}}}
end)

produce :conn, from: :conn
end

command :create_session do
param :user, entity: :user, with_traits: [:active]
param :conn, entity: :conn

resolve(fn %{user: user, conn: conn} ->
{:ok, Map.put(conn, :logged_in_as, user.id)}
end)

update :conn, from: :conn
produce :conn
end
end

0 comments on commit 720572e

Please sign in to comment.