zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

commit 10977de67412392d885a8b2a25f99f4cb444121f
parent 530f0373faf162be9ce16acd8d06a548b3dd1f0e
Author: srfsh <dev@srf.sh>
Date:   Mon, 22 Aug 2022 22:01:49 +0300

Zenflows{Test,}.VF.EconomicEvent: add paging support and small improvements

Diffstat:
Msrc/zenflows/vf/economic_event/domain.ex | 44++++++++++++++++++++++----------------------
Msrc/zenflows/vf/economic_event/resolv.ex | 45+++++++++++++++++++++++----------------------
Msrc/zenflows/vf/economic_event/type.ex | 67+++++++++++++++++++++++--------------------------------------------
Mtest/vf/economic_event/domain.test.exs | 180+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
4 files changed, 180 insertions(+), 156 deletions(-)

diff --git a/src/zenflows/vf/economic_event/domain.ex b/src/zenflows/vf/economic_event/domain.ex @@ -22,6 +22,7 @@ import Ecto.Query alias Ecto.{Changeset, Multi} alias Zenflows.DB.Repo +alias Zenflows.GQL.Paging alias Zenflows.VF.{ Action, EconomicEvent, @@ -30,19 +31,29 @@ alias Zenflows.VF.{ } @typep repo() :: Ecto.Repo.t() -@typep error() :: Ecto.Changeset.t() | String.t() -@typep changes() :: Ecto.Multi.changes() +@typep chgset() :: Ecto.Changeset.t() @typep id() :: Zenflows.DB.Schema.id() @typep params() :: Zenflows.DB.Schema.params() -@spec by_id(repo(), id()) :: EconomicEvent.t() | nil -def by_id(repo \\ Repo, id) do - repo.get(EconomicEvent, id) +@spec one(repo(), id() | map() | Keyword.t()) + :: {:ok, EconomicEvent.t()} | {:error, String.t()} +def one(repo \\ Repo, _) +def one(repo, id) when is_binary(id), do: one(repo, id: id) +def one(repo, clauses) do + case repo.get_by(EconomicEvent, clauses) do + nil -> {:error, "not found"} + found -> {:ok, found} + end +end + +@spec all(Paging.params()) :: Paging.result(EconomicEvent.t()) +def all(params) do + Paging.page(EconomicEvent, params) end @spec create(params(), params()) :: {:ok, EconomicEvent.t(), EconomicResource.t(), nil} | {:ok, EconomicEvent.t(), nil, EconomicResource.t()} - | {:ok, EconomicEvent.t()} | {:error, error()} + | {:ok, EconomicEvent.t()} | {:error, String.t() | chgset()} def create(evt_params, res_params) do Multi.new() |> Multi.insert(:created_evt, EconomicEvent.chgset(evt_params)) @@ -976,11 +987,13 @@ defp handle_multi("move", evt, res_params) do end) end -@spec update(id(), params()) :: {:ok, EconomicEvent.t()} | {:error, error()} +@spec update(id(), params()) + :: {:ok, EconomicEvent.t()} | {:error, String.t() | chgset()} def update(id, params) do Multi.new() - |> Multi.run(:get, multi_get(id)) - |> Multi.update(:update, &EconomicEvent.chgset(&1.get, params)) + |> Multi.put(:id, id) + |> Multi.run(:one, &one/2) + |> Multi.update(:update, &EconomicEvent.chgset(&1.one, params)) |> Repo.transaction() |> case do {:ok, %{update: ee}} -> {:ok, ee} @@ -1049,17 +1062,4 @@ end def preload(eco_evt, :triggered_by) do Repo.preload(eco_evt, :triggered_by) end - -# Returns a EconomicEvent in ok-err tuple from given ID. Used inside -# Ecto.Multi.run/5 to get a record in transaction. -@spec multi_get(id()) - :: (repo(), changes() -> {:ok, EconomicEvent.t()} | {:error, String.t()}) -defp multi_get(id) do - fn repo, _ -> - case by_id(repo, id) do - nil -> {:error, "not found"} - ee -> {:ok, ee} - end - end -end end diff --git a/src/zenflows/vf/economic_event/resolv.ex b/src/zenflows/vf/economic_event/resolv.ex @@ -20,16 +20,17 @@ defmodule Zenflows.VF.EconomicEvent.Resolv do use Absinthe.Schema.Notation -alias Zenflows.VF.{ - EconomicEvent, - EconomicEvent.Domain, -} +alias Zenflows.VF.EconomicEvent.Domain -def economic_event(%{id: id}, _info) do - {:ok, Domain.by_id(id)} +def economic_event(params, _) do + Domain.one(params) end -def create_economic_event(%{event: evt_params} = params, _info) do +def economic_events(params, _) do + Domain.all(params) +end + +def create_economic_event(%{event: evt_params} = params, _) do res_params = params[:new_inventoried_resource] case Domain.create(evt_params, res_params) do {:ok, evt, res, nil} -> @@ -48,78 +49,78 @@ def create_economic_event(%{event: evt_params} = params, _info) do end end -def update_economic_event(%{economic_event: %{id: id} = params}, _info) do +def update_economic_event(%{economic_event: %{id: id} = params}, _) do with {:ok, eco_evt} <- Domain.update(id, params) do {:ok, %{economic_event: eco_evt}} end end -def action(%EconomicEvent{} = eco_evt, _args, _info) do +def action(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :action) {:ok, eco_evt.action} end -def input_of(%EconomicEvent{} = eco_evt, _args, _info) do +def input_of(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :input_of) {:ok, eco_evt.input_of} end -def output_of(%EconomicEvent{} = eco_evt, _args, _info) do +def output_of(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :output_of) {:ok, eco_evt.output_of} end -def provider(%EconomicEvent{} = eco_evt, _args, _info) do +def provider(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :provider) {:ok, eco_evt.provider} end -def receiver(%EconomicEvent{} = eco_evt, _args, _info) do +def receiver(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :receiver) {:ok, eco_evt.receiver} end -def resource_inventoried_as(%EconomicEvent{} = eco_evt, _args, _info) do +def resource_inventoried_as(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :resource_inventoried_as) {:ok, eco_evt.resource_inventoried_as} end -def to_resource_inventoried_as(%EconomicEvent{} = eco_evt, _args, _info) do +def to_resource_inventoried_as(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :to_resource_inventoried_as) {:ok, eco_evt.to_resource_inventoried_as} end -def resource_quantity(%EconomicEvent{} = eco_evt, _args, _info) do +def resource_quantity(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :resource_quantity) {:ok, eco_evt.resource_quantity} end -def effort_quantity(%EconomicEvent{} = eco_evt, _args, _info) do +def effort_quantity(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :effort_quantity) {:ok, eco_evt.effort_quantity} end -def resource_conforms_to(%EconomicEvent{} = eco_evt, _args, _info) do +def resource_conforms_to(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :resource_conforms_to) {:ok, eco_evt.resource_conforms_to} end -def to_location(%EconomicEvent{} = eco_evt, _args, _info) do +def to_location(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :to_location) {:ok, eco_evt.to_location} end -def at_location(%EconomicEvent{} = eco_evt, _args, _info) do +def at_location(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :at_location) {:ok, eco_evt.at_location} end -def realization_of(%EconomicEvent{} = eco_evt, _args, _info) do +def realization_of(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :realization_of) {:ok, eco_evt.realization_of} end -def triggered_by(%EconomicEvent{} = eco_evt, _args, _info) do +def triggered_by(eco_evt, _, _) do eco_evt = Domain.preload(eco_evt, :triggered_by) {:ok, eco_evt.triggered_by} end diff --git a/src/zenflows/vf/economic_event/type.ex b/src/zenflows/vf/economic_event/type.ex @@ -173,30 +173,13 @@ object :economic_event do resolve: &Resolv.triggered_by/3 end -object :economic_event_response do - @desc "Details of the newly created event." - field :economic_event, non_null(:economic_event) - - #@desc """ - #Details of any newly created `EconomicResource`, for events that - #create new resources. - #""" - #field :economic_resource, :economic_resource -end - input_object :economic_event_create_params do @desc @action field :action_id, non_null(:string), name: "action" - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @input_of_id field :input_of_id, :id, name: "input_of" - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @output_of_id field :output_of_id, :id, name: "output_of" @@ -206,24 +189,15 @@ input_object :economic_event_create_params do @desc @receiver_id field :receiver_id, :id, name: "receiver" - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @resource_inventoried_as_id field :resource_inventoried_as_id, :id, name: "resource_inventoried_as" - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @to_resource_inventoried_as_id field :to_resource_inventoried_as_id, :id, name: "to_resource_inventoried_as" @desc @resource_classified_as field :resource_classified_as, list_of(non_null(:uri)) - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @resource_conforms_to_id field :resource_conforms_to_id, :id, name: "resource_conforms_to" @@ -245,30 +219,18 @@ input_object :economic_event_create_params do @desc @note field :note, :string - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @to_location_id field :to_location_id, :id, name: "to_location" - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @at_location_id field :at_location_id, :id, name: "at_location" - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @realization_of_id field :realization_of_id, :id, name: "realization_of" @desc @agreed_in field :agreed_in, :string - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @triggered_by_id field :triggered_by_id, :id, name: "triggered_by" end @@ -279,28 +241,45 @@ input_object :economic_event_update_params do @desc @note field :note, :string - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @realization_of_id field :realization_of_id, :id, name: "realization_of" @desc @agreed_in field :agreed_in, :string - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. @desc @triggered_by_id field :triggered_by_id, :id, name: "triggered_by" end +object :economic_event_response do + @desc "Details of the newly created event." + field :economic_event, non_null(:economic_event) +end + +object :economic_event_edge do + field :cursor, non_null(:id) + field :node, non_null(:economic_event) +end + +object :economic_event_connection do + field :page_info, non_null(:page_info) + field :edges, non_null(list_of(non_null(:economic_event_edge))) +end + object :query_economic_event do field :economic_event, :economic_event do arg :id, non_null(:id) resolve &Resolv.economic_event/2 end + field :economic_events, :economic_event_connection do + arg :first, :integer + arg :after, :id + arg :last, :integer + arg :before, :id + resolve &Resolv.economic_events/2 + end + #recipeFlow(start: ID, limit: Int): [EconomicEvent!] end diff --git a/test/vf/economic_event/domain.test.exs b/test/vf/economic_event/domain.test.exs @@ -77,8 +77,8 @@ setup ctx do end #@tag :skip -#test "by_id/1 returns a EconomicEvent" do -# assert %EconomicEvent{} = Domain.by_id(eco_evt.id) +#test "one/1 returns a EconomicEvent", %{inserted: new} do +# assert {:ok, %EconomicEvent{}} = Domain.one(new.id) #end describe "`create/2` with raise:" do @@ -142,9 +142,9 @@ describe "`create/2` with raise:" do end test "pass with `:resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value + params.resource_quantity.has_numerical_value @@ -242,9 +242,9 @@ describe "`create/2` with produce:" do end test "pass with `:resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value + params.resource_quantity.has_numerical_value @@ -295,9 +295,9 @@ describe "`create/2` with lower:" do end test "pass when all good", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value - params.resource_quantity.has_numerical_value assert res_after.onhand_quantity_has_numerical_value == @@ -349,9 +349,9 @@ describe "`create/2` with consume:" do end test "pass when all good", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value - params.resource_quantity.has_numerical_value @@ -515,7 +515,7 @@ describe "`create/2` with dropoff:" do test "pass when all good", %{params: params} do assert {:ok, %EconomicEvent{} = evt} = Domain.create(params, nil) - res = EconomicResource.Domain.by_id(evt.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(evt.resource_inventoried_as_id) assert res.current_location_id == params.to_location_id end @@ -566,9 +566,9 @@ describe "`create/2` with accept:" do end test "pass when all good", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value @@ -662,12 +662,12 @@ describe "`create/2` with modify:" do end test "pass when all good", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_before.stage_id == nil assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - proc = Process.Domain.by_id(params.output_of_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, proc} = Process.Domain.one(params.output_of_id) assert res_after.stage_id == proc.based_on_id assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value @@ -758,7 +758,7 @@ describe "`create/2` with transferCustody:" do end test "pass without `:to_resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) contained_ids = Enum.map(0..9, fn _ -> agent = Factory.insert!(:agent) @@ -788,7 +788,7 @@ describe "`create/2` with transferCustody:" do lot_id: Factory.insert!(:product_batch).id, } assert {:ok, %EconomicEvent{} = evt, _, %EconomicResource{} = to_res} = Domain.create(params, res_params) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value @@ -821,12 +821,12 @@ describe "`create/2` with transferCustody:" do @tag :want_to_resource test "pass with `:to_resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - to_res_before = EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, to_res_before} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - to_res_after = EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, to_res_after} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value @@ -864,7 +864,7 @@ describe "`create/2` with transferCustody:" do @tag :want_container test "fail when the resource is a container and onhand-quantity is non-positive", %{params: params} do err = "the transfer-custody events need container resources to have positive onhand-quantity" - res = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) Changeset.change(res, onhand_quantity_has_numerical_value: 0.0) |> Repo.update!() assert {:error, ^err} = Domain.create(params, nil) @@ -903,7 +903,9 @@ describe "`create/2` with transferCustody:" do } assert {:ok, _, tmp_res, _} = Domain.create(raise_params, %{name: Factory.str("name")}) # TODO: use combine-separate when implemented instead - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(contained_in_id: tmp_res.id) |> Repo.update!() @@ -936,7 +938,9 @@ describe "`create/2` with transferCustody:" do @tag :want_to_resource test "fail when event's unit and to-resource's unit differ", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(onhand_quantity_has_unit_id: Factory.insert!(:unit).id) |> Repo.update!() @@ -946,7 +950,9 @@ describe "`create/2` with transferCustody:" do @tag :want_to_resource test "fail when resoure and to-resource don't conform to the same spec", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(conforms_to_id: Factory.insert!(:resource_specification).id) |> Repo.update!() @@ -1000,7 +1006,7 @@ describe "`create/2` with transferAllRights:" do end test "pass without `:to_resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) contained_ids = Enum.map(0..9, fn _ -> agent = Factory.insert!(:agent) @@ -1030,7 +1036,7 @@ describe "`create/2` with transferAllRights:" do lot_id: Factory.insert!(:product_batch).id, } assert {:ok, %EconomicEvent{} = evt, _, %EconomicResource{} = to_res} = Domain.create(params, res_params) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value - params.resource_quantity.has_numerical_value @@ -1062,12 +1068,12 @@ describe "`create/2` with transferAllRights:" do @tag :want_to_resource test "pass with `:to_resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - to_res_before = EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, to_res_before} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - to_res_after = EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, to_res_after} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value - params.resource_quantity.has_numerical_value @@ -1105,7 +1111,7 @@ describe "`create/2` with transferAllRights:" do @tag :want_container test "fail when the resource is a container and accounting-quantity is non-positive", %{params: params} do err = "the transfer-all-rights events need container resources to have positive accounting-quantity" - res = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) Changeset.change(res, accounting_quantity_has_numerical_value: 0.0) |> Repo.update!() assert {:error, ^err} = Domain.create(params, nil) @@ -1144,7 +1150,9 @@ describe "`create/2` with transferAllRights:" do } assert {:ok, _, tmp_res, _} = Domain.create(raise_params, %{name: Factory.str("name")}) # TODO: use combine-separate when implemented instead - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(contained_in_id: tmp_res.id) |> Repo.update!() @@ -1177,7 +1185,9 @@ describe "`create/2` with transferAllRights:" do @tag :want_to_resource test "fail when event's unit and to-resource's unit differ", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(accounting_quantity_has_unit_id: Factory.insert!(:unit).id) |> Repo.update!() @@ -1187,7 +1197,9 @@ describe "`create/2` with transferAllRights:" do @tag :want_to_resource test "fail when resoure and to-resource don't conform to the same spec", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(conforms_to_id: Factory.insert!(:resource_specification).id) |> Repo.update!() @@ -1241,7 +1253,7 @@ describe "`create/2` with transfer:" do end test "pass without `:to_resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) contained_ids = Enum.map(0..9, fn _ -> agent = Factory.insert!(:agent) @@ -1271,7 +1283,7 @@ describe "`create/2` with transfer:" do lot_id: Factory.insert!(:product_batch).id, } assert {:ok, %EconomicEvent{} = evt, _, %EconomicResource{} = to_res} = Domain.create(params, res_params) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value - params.resource_quantity.has_numerical_value @@ -1304,12 +1316,12 @@ describe "`create/2` with transfer:" do @tag :want_to_resource test "pass with `:to_resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - to_res_before = EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, to_res_before} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - to_res_after = EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, to_res_after} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value - params.resource_quantity.has_numerical_value @@ -1323,7 +1335,9 @@ describe "`create/2` with transfer:" do end test "fail when provider doesn't have accountability over the resource", %{params: params} do - EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + + res |> Changeset.change(primary_accountable_id: Factory.insert!(:agent).id) |> Repo.update!() assert {:error, "you don't have accountability over this resource"} = @@ -1331,7 +1345,9 @@ describe "`create/2` with transfer:" do end test "fail when provider doesn't have custody over the resource", %{params: params} do - EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + + res |> Changeset.change(custodian_id: Factory.insert!(:agent).id) |> Repo.update!() assert {:error, "you don't have custody over this resource"} = @@ -1352,7 +1368,9 @@ describe "`create/2` with transfer:" do @tag :want_to_resource test "fail when event's unit and to-resource's unit differ", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(accounting_quantity_has_unit_id: Factory.insert!(:unit).id) |> Repo.update!() @@ -1363,7 +1381,7 @@ describe "`create/2` with transfer:" do @tag :want_container test "fail when the resource is a container and accounting-quantity is non-positive", %{params: params} do err = "the transfer events need container resources to have positive accounting-quantity" - res = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) Changeset.change(res, accounting_quantity_has_numerical_value: 0.0) |> Repo.update!() assert {:error, ^err} = Domain.create(params, nil) @@ -1375,7 +1393,7 @@ describe "`create/2` with transfer:" do @tag :want_container test "fail when the resource is a container and onhand-quantity is non-positive", %{params: params} do err = "the transfer events need container resources to have positive onhand-quantity" - res = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) Changeset.change(res, onhand_quantity_has_numerical_value: 0.0) |> Repo.update!() assert {:error, ^err} = Domain.create(params, nil) @@ -1386,7 +1404,9 @@ describe "`create/2` with transfer:" do @tag :want_container test "fail when event's quantity value and resource's accounting-quantity value differ", %{params: params} do - EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + + res |> Changeset.change(accounting_quantity_has_numerical_value: params.resource_quantity.has_numerical_value + 1) |> Repo.update!() assert {:error, "the transfer events need to fully transfer the resource"} = @@ -1395,7 +1415,9 @@ describe "`create/2` with transfer:" do @tag :want_container test "fail when event's quantity value and resource's onhnad-quantity value differ", %{params: params} do - EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + + res |> Changeset.change(onhand_quantity_has_numerical_value: params.resource_quantity.has_numerical_value + 1) |> Repo.update!() assert {:error, "the transfer events need to fully transfer the resource"} = @@ -1425,7 +1447,9 @@ describe "`create/2` with transfer:" do } assert {:ok, _, tmp_res, _} = Domain.create(raise_params, %{name: Factory.str("name")}) # TODO: use combine-separate when implemented instead - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(contained_in_id: tmp_res.id) |> Repo.update!() @@ -1458,7 +1482,9 @@ describe "`create/2` with transfer:" do @tag :want_to_resource test "fail when resoure and to-resource don't conform to the same spec", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(conforms_to_id: Factory.insert!(:resource_specification).id) |> Repo.update!() @@ -1511,7 +1537,7 @@ describe "`create/2` with move:" do end test "pass without `:to_resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) contained_ids = Enum.map(0..9, fn _ -> agent = Factory.insert!(:agent) @@ -1541,7 +1567,7 @@ describe "`create/2` with move:" do lot_id: Factory.insert!(:product_batch).id, } assert {:ok, %EconomicEvent{} = evt, _, %EconomicResource{} = to_res} = Domain.create(params, res_params) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value - params.resource_quantity.has_numerical_value @@ -1574,12 +1600,12 @@ describe "`create/2` with move:" do @tag :want_to_resource test "pass with `:to_resource_inventoried_as`", %{params: params} do - res_before = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - to_res_before = EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res_before} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, to_res_before} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) assert {:ok, %EconomicEvent{}} = Domain.create(params, nil) - res_after = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) - to_res_after = EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res_after} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + {:ok, to_res_after} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) assert res_after.accounting_quantity_has_numerical_value == res_before.accounting_quantity_has_numerical_value - params.resource_quantity.has_numerical_value @@ -1593,7 +1619,9 @@ describe "`create/2` with move:" do end test "fail when provider doesn't have accountability over the resource", %{params: params} do - EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + + res |> Changeset.change(primary_accountable_id: Factory.insert!(:agent).id) |> Repo.update!() assert {:error, "you don't have accountability over resource-inventoried-as"} = @@ -1601,7 +1629,9 @@ describe "`create/2` with move:" do end test "fail when provider doesn't have custody over the resource", %{params: params} do - EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + + res |> Changeset.change(custodian_id: Factory.insert!(:agent).id) |> Repo.update!() assert {:error, "you don't have custody over resource-inventoried-as"} = @@ -1610,7 +1640,9 @@ describe "`create/2` with move:" do @tag :want_to_resource test "fail when provider doesn't have accountability over the to-resource", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(primary_accountable_id: Factory.insert!(:agent).id) |> Repo.update!() assert {:error, "you don't have accountability over to-resource-inventoried-as"} = @@ -1619,7 +1651,9 @@ describe "`create/2` with move:" do @tag :want_to_resource test "fail when provider doesn't have custody over the to-resource", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(custodian_id: Factory.insert!(:agent).id) |> Repo.update!() assert {:error, "you don't have custody over to-resource-inventoried-as"} = @@ -1640,7 +1674,9 @@ describe "`create/2` with move:" do @tag :want_to_resource test "fail when event's unit and to-resource's unit differ", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(accounting_quantity_has_unit_id: Factory.insert!(:unit).id) |> Repo.update!() @@ -1651,7 +1687,7 @@ describe "`create/2` with move:" do @tag :want_container test "fail when the resource is a container and accounting-quantity is non-positive", %{params: params} do err = "the move events need container resources to have positive accounting-quantity" - res = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) Changeset.change(res, accounting_quantity_has_numerical_value: 0.0) |> Repo.update!() assert {:error, ^err} = Domain.create(params, nil) @@ -1663,7 +1699,7 @@ describe "`create/2` with move:" do @tag :want_container test "fail when the resource is a container and onhand-quantity is non-positive", %{params: params} do err = "the move events need container resources to have positive onhand-quantity" - res = EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) Changeset.change(res, onhand_quantity_has_numerical_value: 0.0) |> Repo.update!() assert {:error, ^err} = Domain.create(params, nil) @@ -1674,7 +1710,9 @@ describe "`create/2` with move:" do @tag :want_container test "fail when event's quantity value and resource's accounting-quantity value differ", %{params: params} do - EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + + res |> Changeset.change(accounting_quantity_has_numerical_value: params.resource_quantity.has_numerical_value + 1) |> Repo.update!() assert {:error, "the move events need to fully move the resource"} = @@ -1683,7 +1721,9 @@ describe "`create/2` with move:" do @tag :want_container test "fail when event's quantity value and resource's onhnad-quantity value differ", %{params: params} do - EconomicResource.Domain.by_id(params.resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.resource_inventoried_as_id) + + res |> Changeset.change(onhand_quantity_has_numerical_value: params.resource_quantity.has_numerical_value + 1) |> Repo.update!() assert {:error, "the move events need to fully move the resource"} = @@ -1713,7 +1753,9 @@ describe "`create/2` with move:" do } assert {:ok, _, tmp_res, _} = Domain.create(raise_params, %{name: Factory.str("name")}) # TODO: use combine-separate when implemented instead - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(contained_in_id: tmp_res.id) |> Repo.update!() @@ -1746,7 +1788,9 @@ describe "`create/2` with move:" do @tag :want_to_resource test "fail when resoure and to-resource don't conform to the same spec", %{params: params} do - EconomicResource.Domain.by_id(params.to_resource_inventoried_as_id) + {:ok, res} = EconomicResource.Domain.one(params.to_resource_inventoried_as_id) + + res |> Changeset.change(conforms_to_id: Factory.insert!(:resource_specification).id) |> Repo.update!()