From ab0061d13aeef6c503f56dc306c1d5a940cd9a7c Mon Sep 17 00:00:00 2001 From: Arno Dirlam Date: Sun, 19 May 2024 12:56:13 +0200 Subject: [PATCH] Release v0.1.1 --- CHANGELOG.md | 9 ++++++- README.md | 10 +++---- VERSION | 2 +- lib/refactory.ex | 14 ++++------ mix.exs | 2 +- .../{refinements_test.exs => traits_test.exs} | 26 +++++++++---------- 6 files changed, 33 insertions(+), 30 deletions(-) rename test/refactory/{refinements_test.exs => traits_test.exs} (83%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93faba4..021cceb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index c4272a7..1b526d5 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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 @@ -49,7 +49,7 @@ A trait can be ## Basic example -``` +```elixir defmodule MyApp.Factory do use Refactory, repo: MyApp.Repo end @@ -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 @@ -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 diff --git a/VERSION b/VERSION index 6c6aa7c..6da28dd 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0 \ No newline at end of file +0.1.1 \ No newline at end of file diff --git a/lib/refactory.ex b/lib/refactory.ex index 6b250ea..0f02598 100644 --- a/lib/refactory.ex +++ b/lib/refactory.ex @@ -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) @@ -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 diff --git a/mix.exs b/mix.exs index 9453316..60f40a3 100644 --- a/mix.exs +++ b/mix.exs @@ -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: %{ diff --git a/test/refactory/refinements_test.exs b/test/refactory/traits_test.exs similarity index 83% rename from test/refactory/refinements_test.exs rename to test/refactory/traits_test.exs index e962e45..1fb0bf5 100644 --- a/test/refactory/refinements_test.exs +++ b/test/refactory/traits_test.exs @@ -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 @@ -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, @@ -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{ @@ -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{ @@ -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{ @@ -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"}}} # ) @@ -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"}}}} # ) @@ -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: %{