zf

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

commit bc23a1a3150dfa3958448790fd54a0d53a2d42f4
parent d1417e74e07fe90116885c594ba96db78f8e360f
Author: srfsh <dev@srf.sh>
Date:   Mon, 22 Aug 2022 20:27:48 +0300

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

Diffstat:
Msrc/zenflows/vf/agent_relationship_role/domain.ex | 51+++++++++++++++++++++++++++------------------------
Msrc/zenflows/vf/agent_relationship_role/resolv.ex | 21+++++++++++++--------
Msrc/zenflows/vf/agent_relationship_role/type.ex | 42++++++++++++++++++++++++++++--------------
Mtest/vf/agent_relationship_role/domain.test.exs | 44+++++++++++++++++++++++++++-----------------
Mtest/vf/agent_relationship_role/type.test.exs | 119+++++++++++++++++++++++++++++++++++++------------------------------------------
5 files changed, 150 insertions(+), 127 deletions(-)

diff --git a/src/zenflows/vf/agent_relationship_role/domain.ex b/src/zenflows/vf/agent_relationship_role/domain.ex @@ -20,35 +20,48 @@ defmodule Zenflows.VF.AgentRelationshipRole.Domain do alias Ecto.Multi alias Zenflows.DB.Repo +alias Zenflows.GQL.Paging alias Zenflows.VF.AgentRelationshipRole @typep repo() :: Ecto.Repo.t() @typep chgset() :: Ecto.Changeset.t() -@typep changes() :: Ecto.Multi.changes() @typep id() :: Zenflows.DB.Schema.id() @typep params() :: Zenflows.DB.Schema.params() -@spec by_id(repo(), id()) :: AgentRelationshipRole.t() | nil -def by_id(repo \\ Repo, id) do - repo.get(AgentRelationshipRole, id) +@spec one(repo(), id() | map() | Keyword.t()) + :: {:ok, AgentRelationshipRole.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(AgentRelationshipRole, clauses) do + nil -> {:error, "not found"} + found -> {:ok, found} + end +end + +@spec all(Paging.params()) :: Paging.result(AgentRelationshipRole.t()) +def all(params) do + Paging.page(AgentRelationshipRole, params) end @spec create(params()) :: {:ok, AgentRelationshipRole.t()} | {:error, chgset()} def create(params) do Multi.new() - |> Multi.insert(:rel_role, AgentRelationshipRole.chgset(params)) + |> Multi.insert(:insert, AgentRelationshipRole.chgset(params)) |> Repo.transaction() |> case do - {:ok, %{rel_role: rr}} -> {:ok, rr} + {:ok, %{insert: rr}} -> {:ok, rr} {:error, _, cset, _} -> {:error, cset} end end -@spec update(id(), params()) :: {:ok, AgentRelationshipRole.t()} | {:error, chgset()} +@spec update(id(), params()) + :: {:ok, AgentRelationshipRole.t()} | {:error, String.t() | chgset()} def update(id, params) do Multi.new() - |> Multi.run(:get, multi_get(id)) - |> Multi.update(:update, &AgentRelationshipRole.chgset(&1.get, params)) + |> Multi.put(:id, id) + |> Multi.run(:one, &one/2) + |> Multi.update(:update, &AgentRelationshipRole.chgset(&1.one, params)) |> Repo.transaction() |> case do {:ok, %{update: rr}} -> {:ok, rr} @@ -56,11 +69,13 @@ def update(id, params) do end end -@spec delete(id()) :: {:ok, AgentRelationshipRole.t()} | {:error, chgset()} +@spec delete(id()) + :: {:ok, AgentRelationshipRole.t()} | {:error, String.t() | chgset()} def delete(id) do Multi.new() - |> Multi.run(:get, multi_get(id)) - |> Multi.delete(:delete, &(&1.get)) + |> Multi.put(:id, id) + |> Multi.run(:one, &one/2) + |> Multi.delete(:delete, &(&1.one)) |> Repo.transaction() |> case do {:ok, %{delete: rr}} -> {:ok, rr} @@ -72,16 +87,4 @@ end def preload(rel_role, :role_behavior) do Repo.preload(rel_role, :role_behavior) end - -# Returns an AgentRelationshipRole 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, AgentRelationshipRole.t()} | {:error, String.t()}) -defp multi_get(id) do - fn repo, _ -> - case by_id(repo, id) do - nil -> {:error, "not found"} - rr -> {:ok, rr} - end - end -end end diff --git a/src/zenflows/vf/agent_relationship_role/resolv.ex b/src/zenflows/vf/agent_relationship_role/resolv.ex @@ -20,30 +20,35 @@ defmodule Zenflows.VF.AgentRelationshipRole.Resolv do alias Zenflows.VF.{AgentRelationshipRole, AgentRelationshipRole.Domain} -def role_behavior(%AgentRelationshipRole{} = rel_role, _args, _info) do - rel_role = Domain.preload(rel_role, :role_behavior) - {:ok, rel_role.role_behavior} +def agent_relationship_role(params, _) do + Domain.one(params) end -def agent_relationship_role(%{id: id}, _info) do - {:ok, Domain.by_id(id)} +def agent_relationship_roles(params, _) do + Domain.all(params) end -def create_agent_relationship_role(%{agent_relationship_role: params}, _info) do +def create_agent_relationship_role(%{agent_relationship_role: params}, _) do with {:ok, rel_role} <- Domain.create(params) do {:ok, %{agent_relationship_role: rel_role}} end end -def update_agent_relationship_role(%{agent_relationship_role: %{id: id} = params}, _info) do +def update_agent_relationship_role(%{agent_relationship_role: %{id: id} = params}, _) do with {:ok, rel_role} <- Domain.update(id, params) do {:ok, %{agent_relationship_role: rel_role}} end end -def delete_agent_relationship_role(%{id: id}, _info) do +def delete_agent_relationship_role(%{id: id}, _) do with {:ok, _rel_role} <- Domain.delete(id) do {:ok, true} end end + +def role_behavior(%AgentRelationshipRole{} = rel_role, _, _) do + rel_role = Domain.preload(rel_role, :role_behavior) + {:ok, rel_role.role_behavior} +end + end diff --git a/src/zenflows/vf/agent_relationship_role/type.ex b/src/zenflows/vf/agent_relationship_role/type.ex @@ -25,6 +25,7 @@ alias Zenflows.VF.AgentRelationshipRole.Resolv @role_behavior """ The general shape or behavior grouping of an agent relationship role. """ +@role_behavior_id "(`RoleBehavior`) #{@role_behavior}" @role_label """ The human readable name of the role, from the subject to the object. """ @@ -50,15 +51,8 @@ object :agent_relationship_role do field :note, :string end -object :agent_relationship_role_response do - field :agent_relationship_role, non_null(:agent_relationship_role) -end - input_object :agent_relationship_role_create_params do - # TODO: When - # https://github.com/absinthe-graphql/absinthe/issues/1126 results, - # apply the correct changes if any. - @desc "(`RoleBhavior`) " <> @role_behavior + @desc @role_behavior_id field :role_behavior_id, :id, name: "role_behavior" @desc @role_label @@ -74,7 +68,7 @@ end input_object :agent_relationship_role_update_params do field :id, non_null(:id) - @desc "(`RoleBhavior`) " <> @role_behavior + @desc @role_behavior_id field :role_behavior_id, :id, name: "role_behavior" @desc @role_label @@ -87,6 +81,20 @@ input_object :agent_relationship_role_update_params do field :note, :string end +object :agent_relationship_role_response do + field :agent_relationship_role, non_null(:agent_relationship_role) +end + +object :agent_relationship_role_edge do + field :cursor, non_null(:id) + field :node, non_null(:agent_relationship_role) +end + +object :agent_relationship_role_connection do + field :page_info, non_null(:page_info) + field :edges, non_null(list_of(non_null(:agent_relationship_role_edge))) +end + object :query_agent_relationship_role do @desc "Retrieve details of an agent relationship role by its ID." field :agent_relationship_role, :agent_relationship_role do @@ -94,11 +102,17 @@ object :query_agent_relationship_role do resolve &Resolv.agent_relationship_role/2 end - # @desc """ - # Retrieve possible kinds of associations that agents may have - # with one another in this collaboration space. - # """ - # agentRelationshipRoles(start: ID, limit: Int): [AgentRelationshipRole!] + @desc """ + Retrieve possible kinds of associations that agents may have + with one another in this collaboration space. + """ + field :agent_relationship_roles, :agent_relationship_role_connection do + arg :first, :integer + arg :after, :id + arg :last, :integer + arg :before, :id + resolve &Resolv.agent_relationship_roles/2 + end end object :mutation_agent_relationship_role do diff --git a/test/vf/agent_relationship_role/domain.test.exs b/test/vf/agent_relationship_role/domain.test.exs @@ -29,42 +29,46 @@ setup do inverse_role_label: Factory.uniq("inverse role label"), note: Factory.uniq("note"), }, - agent_relationship_role: Factory.insert!(:agent_relationship_role), + inserted: Factory.insert!(:agent_relationship_role), } end -test "by_id/1 returns an AgentRelationshipRole", %{agent_relationship_role: rel_role} do - assert %AgentRelationshipRole{} = Domain.by_id(rel_role.id) +describe "one/1" do + test "with good id: finds the AgentRelationshipRole", %{inserted: %{id: id}} do + assert {:ok, %AgentRelationshipRole{}} = Domain.one(id) + end + + test "with bad id: doesn't find the AgentRelationshiprole"do + assert {:error, "not found"} = Domain.one(Factory.id()) + end end describe "create/1" do - test "creates an AgentRelationshipRole with valid params", %{params: params} do - assert {:ok, %AgentRelationshipRole{} = rel_role} = Domain.create(params) + test "with good params: creates an AgentRelationshipRole", %{params: params} do + assert {:ok, %AgentRelationshipRole{} = new} = Domain.create(params) - assert rel_role.role_behavior_id == params.role_behavior_id - assert rel_role.role_label == params.role_label - assert rel_role.inverse_role_label == params.inverse_role_label - assert rel_role.note == params.note + assert new.role_behavior_id == params.role_behavior_id + assert new.role_label == params.role_label + assert new.inverse_role_label == params.inverse_role_label + assert new.note == params.note end - test "doesn't create an AgentRelationshipRole with invalid params" do + test "with bad params: doesn't create an AgentRelationshipRole" do assert {:error, %Changeset{}} = Domain.create(%{}) end end describe "update/2" do - test "updates an AgentRelationshipRole with valid params", %{params: params, agent_relationship_role: old} do + test "with good params: updates the AgentRelationshipRole", %{params: params, inserted: old} do assert {:ok, %AgentRelationshipRole{} = new} = Domain.update(old.id, params) - assert new.role_behavior_id == params.role_behavior_id assert new.role_label == params.role_label assert new.inverse_role_label == params.inverse_role_label assert new.note == params.note end - test "doesn't update an AgentRelationshipRole", %{agent_relationship_role: old} do + test "with bad params: doesn't update the AgentRelationshipRole", %{inserted: old} do assert {:ok, %AgentRelationshipRole{} = new} = Domain.update(old.id, %{}) - assert new.role_behavior_id == old.role_behavior_id assert new.role_label == old.role_label assert new.inverse_role_label == old.inverse_role_label @@ -72,8 +76,14 @@ describe "update/2" do end end -test "delete/1 deletes an AgentRelationshipRole", %{agent_relationship_role: %{id: id}} do - assert {:ok, %AgentRelationshipRole{id: ^id}} = Domain.delete(id) - assert Domain.by_id(id) == nil +describe "delete/1" do + test "with good id: deletes the AgentRelationshipRole", %{inserted: %{id: id}} do + assert {:ok, %AgentRelationshipRole{id: ^id}} = Domain.delete(id) + assert {:error, "not found"} = Domain.one(id) + end + + test "with bad id: doesn't delete the AgentRelationshipRole" do + assert {:error, "not found"} = Domain.delete(Factory.id()) + end end end diff --git a/test/vf/agent_relationship_role/type.test.exs b/test/vf/agent_relationship_role/type.test.exs @@ -21,95 +21,86 @@ use ZenflowsTest.Help.AbsinCase, async: true setup do %{ params: %{ - role_behavior_id: Factory.insert!(:role_behavior).id, - role_label: Factory.uniq("role label"), - inverse_role_label: Factory.uniq("inverse role label"), - note: Factory.uniq("note"), + "roleBehavior" => Factory.insert!(:role_behavior).id, + "roleLabel" => Factory.uniq("role label"), + "inverseRoleLabel" => Factory.uniq("inverse role label"), + "note" => Factory.uniq("note"), }, - agent_relationship_role: Factory.insert!(:agent_relationship_role), + inserted: Factory.insert!(:agent_relationship_role), } end +@frag """ +fragment agentRelationshipRole on AgentRelationshipRole { + id + roleBehavior { id } + roleLabel + inverseRoleLabel + note +} +""" + describe "Query" do - test "agentRelationshipRole()", %{agent_relationship_role: rel_role} do + test "agentRelationshipRole", %{inserted: new} do assert %{data: %{"agentRelationshipRole" => data}} = - query!(""" - agentRelationshipRole(id: "#{rel_role.id}") { - id - roleBehavior { id } - roleLabel - inverseRoleLabel - note + run!(""" + #{@frag} + query ($id: ID!) { + agentRelationshipRole(id: $id) {...agentRelationshipRole} } - """) + """, vars: %{"id" => new.id}) - assert data["id"] == rel_role.id - assert data["roleBehavior"]["id"] == rel_role.role_behavior_id - assert data["roleLabel"] == rel_role.role_label - assert data["inverseRoleLabel"] == rel_role.inverse_role_label - assert data["note"] == rel_role.note + assert data["id"] == new.id + assert data["roleBehavior"]["id"] == new.role_behavior_id + assert data["roleLabel"] == new.role_label + assert data["inverseRoleLabel"] == new.inverse_role_label + assert data["note"] == new.note end end describe "Mutation" do - test "createAgentRelationshipRole()", %{params: params} do + test "createAgentRelationshipRole", %{params: params} do assert %{data: %{"createAgentRelationshipRole" => %{"agentRelationshipRole" => data}}} = - mutation!(""" - createAgentRelationshipRole(agentRelationshipRole: { - roleBehavior: "#{params.role_behavior_id}" - roleLabel: "#{params.role_label}" - inverseRoleLabel: "#{params.inverse_role_label}" - note: "#{params.note}" - }) { - agentRelationshipRole { - id - roleBehavior { id } - roleLabel - inverseRoleLabel - note + run!(""" + #{@frag} + mutation ($agentRelationshipRole: AgentRelationshipRoleCreateParams!) { + createAgentRelationshipRole(agentRelationshipRole: $agentRelationshipRole) { + agentRelationshipRole {...agentRelationshipRole} } } - """) + """, vars: %{"agentRelationshipRole" => params}) assert {:ok, _} = Zenflows.DB.ID.cast(data["id"]) - assert data["roleBehavior"]["id"] == params.role_behavior_id - assert data["roleLabel"] == params.role_label - assert data["inverseRoleLabel"] == params.inverse_role_label - assert data["note"] == params.note + assert data["roleBehavior"]["id"] == params["roleBehavior"] + assert data["roleLabel"] == params["roleLabel"] + assert data["inverseRoleLabel"] == params["inverseRoleLabel"] + assert data["note"] == params["note"] end - test "updateAgentRelationshipRole()", %{params: params, agent_relationship_role: rel_role} do + test "updateAgentRelationshipRole", %{params: params, inserted: old} do assert %{data: %{"updateAgentRelationshipRole" => %{"agentRelationshipRole" => data}}} = - mutation!(""" - updateAgentRelationshipRole(agentRelationshipRole: { - id: "#{rel_role.id}" - roleBehavior: "#{params.role_behavior_id}" - roleLabel: "#{params.role_label}" - inverseRoleLabel: "#{params.inverse_role_label}" - note: "#{params.note}" - }) { - agentRelationshipRole { - id - roleBehavior { id } - roleLabel - inverseRoleLabel - note + run!(""" + #{@frag} + mutation ($agentRelationshipRole: AgentRelationshipRoleUpdateParams!) { + updateAgentRelationshipRole(agentRelationshipRole: $agentRelationshipRole) { + agentRelationshipRole {...agentRelationshipRole} } } - """) - - assert data["id"] == rel_role.id - assert data["roleBehavior"]["id"] == params.role_behavior_id - assert data["roleLabel"] == params.role_label - assert data["inverseRoleLabel"] == params.inverse_role_label - assert data["note"] == params.note + """, vars: %{"agentRelationshipRole" => Map.put(params, "id", old.id)}) + assert data["id"] == old.id + assert data["roleBehavior"]["id"] == params["roleBehavior"] + assert data["roleLabel"] == params["roleLabel"] + assert data["inverseRoleLabel"] == params["inverseRoleLabel"] + assert data["note"] == params["note"] end - test "deleteAgentRelationshipRole()", %{agent_relationship_role: %{id: id}} do + test "deleteAgentRelationshipRole()", %{inserted: %{id: id}} do assert %{data: %{"deleteAgentRelationshipRole" => true}} = - mutation!(""" - deleteAgentRelationshipRole(id: "#{id}") - """) + run!(""" + mutation ($id: ID!) { + deleteAgentRelationshipRole(id: $id) + } + """, vars: %{"id" => id}) end end end