Skip to content

Commit

Permalink
Release v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodirlam committed May 19, 2024
1 parent 88c855c commit ab0061d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.0] - 2022-06-08
## [0.1.1] - 2024-05-19

### Fixed

- Setting struct values
- Defining traits

## [0.1.0] - 2024-05-13

### Added

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ by adding `refactory` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:refactory, "~> 0.1.0", only: :test}
{:refactory, "~> 0.1.1", only: :test}
]
end
```
Expand All @@ -24,7 +24,7 @@ Refactory allows generating Ecto records with nested overrides for your tests.

To start using Refactory, first define a factory module:

```
```elixir
defmodule MyApp.Factory do
use Refactory, repo: MyApp.Repo
end
Expand All @@ -49,7 +49,7 @@ A trait can be

## Basic example

```
```elixir
defmodule MyApp.Factory do
use Refactory, repo: MyApp.Repo
end
Expand All @@ -72,7 +72,7 @@ MyApp.Factory.build(MyApp.List, %{
Default traits can be defined in the factory module.
They are always applied first.

```
```elixir
defmodule MyApp.Factory do
use Refactory, repo: MyApp.Repo

Expand All @@ -93,7 +93,7 @@ MyApp.Factory.build(MyApp.List)

Custom traits can be defined in the factory module and then used by their name.

```
```elixir
defmodule MyApp.Factory do
use Refactory, repo: MyApp.Repo

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.1.1
14 changes: 5 additions & 9 deletions lib/refactory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ defmodule Refactory do
"""
def build(module, type, traits \\ %{}) do
case resolve_refinement(module, type, {:default, traits}) do
record = %{__struct__: ^module} ->
record = %{__struct__: ^type} ->
record

record = %{__struct__: _module} ->
raise ArgumentError, "Expected a struct of type #{module}. Got #{inspect(record)}"
record = %{__struct__: _other_type} ->
raise ArgumentError, "Expected a struct of type #{type}. Got #{inspect(record)}"

attrs ->
do_build(module, type, attrs)
Expand Down Expand Up @@ -130,12 +130,8 @@ defmodule Refactory do
struct!(type, deep_merge(Map.from_struct(left), Map.from_struct(right)))
end

defp deep_resolve(_key, %{__struct__: type}, _right, _concat_lists?, _struct_overrides?) do
raise ArgumentError, "#{type} cannot be merged with non-#{type}."
end

defp deep_resolve(_key, _left, %{__struct__: type}, _concat_lists?, _struct_overrides?) do
raise ArgumentError, "Non-#{type} cannot be merged with #{type}."
defp deep_resolve(_key, %{__struct__: type}, %{__struct__: other_type}, _concat_lists?, _struct_overrides?) do
raise ArgumentError, "#{type} cannot be merged with #{other_type}."
end

defp deep_resolve(_key, %{} = left, %{} = right, _concat_lists?, _struct_overrides?) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Refactory.MixProject do
defp package do
[
description: "Generate test data recursively with traits",
files: ["lib", "mix.exs", "README*"],
files: ["lib", "mix.exs", "README*", "VERSION"],
maintainers: ["Arno Dirlam"],
licenses: ["MIT"],
links: %{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Test.Support.RefinementsTest do
defmodule Test.Support.TraitsTest do
use Refactory.Test.DataCase, async: true

defmodule Traits do
defmodule Factories do
use Refactory, repo: Refactory.Test.Repo

def trait(List, :default) do
Expand Down Expand Up @@ -32,7 +32,7 @@ defmodule Test.Support.RefinementsTest do

test "works with normal overrides" do
now = DateTime.utc_now()
list = Traits.build(List, %{archived_at: now})
list = Factories.build(List, %{archived_at: now})

assert %List{
archived_at: ^now,
Expand All @@ -41,7 +41,7 @@ defmodule Test.Support.RefinementsTest do
end

test "works with nested overrides" do
list_tag = Traits.build(ListTag, %{list: %{created_by: %{last_name: "Vega"}}})
list_tag = Factories.build(ListTag, %{list: %{created_by: %{last_name: "Vega"}}})

assert %ListTag{
list: %List{
Expand All @@ -53,9 +53,9 @@ defmodule Test.Support.RefinementsTest do
end

test "works with struct override" do
user = Traits.build(User)
user = Factories.build(User)

record = Traits.build(ListTag, %{list: %{created_by: user}})
record = Factories.build(ListTag, %{list: %{created_by: user}})

assert %ListTag{
list: %List{
Expand All @@ -65,15 +65,15 @@ defmodule Test.Support.RefinementsTest do
end

test "raises on invalid struct override" do
user = Traits.build(User)
user = Factories.build(User)

assert_raise ArgumentError, fn ->
Traits.build(ListTag, %{list: user})
Factories.build(ListTag, %{list: user})
end
end

test "works with simple trait" do
list_tag = Traits.build(ListTag, %{list: %{created_by: :refined}})
list_tag = Factories.build(ListTag, %{list: %{created_by: :refined}})

assert %ListTag{
list: %List{
Expand All @@ -89,7 +89,7 @@ defmodule Test.Support.RefinementsTest do
# department = get_dept_by(name: "Construction")

# record =
# Traits.build(
# Factories.build(
# Timecards.TimecardData,
# %{timecard: %{offer: {:department_name, "Construction"}}}
# )
Expand All @@ -107,7 +107,7 @@ defmodule Test.Support.RefinementsTest do
# department = get_dept_by(name: "Construction")

# record =
# Traits.build(
# Factories.build(
# Timecards.TimecardData,
# %{timecard: %{offer: %{department: {:name, "Construction"}}}}
# )
Expand All @@ -123,13 +123,13 @@ defmodule Test.Support.RefinementsTest do

test "raises on unknown trait" do
assert_raise ArgumentError, fn ->
Traits.build(List, %{created_by: :unknown})
Factories.build(List, %{created_by: :unknown})
end
end

test "works with trait + override" do
record =
Traits.build(
Factories.build(
ListTag,
%{
list: %{
Expand Down

0 comments on commit ab0061d

Please sign in to comment.