zf

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

commit 516e210ac7a3d2250131d2d4edd9e2bb52f8dec4
parent 9dbf6f3dd25c0119fc8a249d09790819cb5ce9e0
Author: srfsh <dev@srf.sh>
Date:   Tue, 23 Aug 2022 12:12:50 +0300

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

Diffstat:
Msrc/zenflows/vf/unit/domain.ex | 29+++++++++++++++++------------
Msrc/zenflows/vf/unit/resolv.ex | 14+++++++++-----
Msrc/zenflows/vf/unit/type.ex | 26+++++++++++++++++++++-----
Mtest/vf/unit/domain.test.exs | 9++++-----
Mtest/vf/unit/type.test.exs | 90++++++++++++++++++++++++++++++++++++++++----------------------------------------
5 files changed, 96 insertions(+), 72 deletions(-)

diff --git a/src/zenflows/vf/unit/domain.ex b/src/zenflows/vf/unit/domain.ex @@ -20,6 +20,7 @@ defmodule Zenflows.VF.Unit.Domain do alias Ecto.Multi alias Zenflows.DB.Repo +alias Zenflows.GQL.Paging alias Zenflows.VF.Unit @typep repo() :: Ecto.Repo.t() @@ -27,20 +28,23 @@ alias Zenflows.VF.Unit @typep id() :: Zenflows.DB.Schema.id() @typep params() :: Zenflows.DB.Schema.params() -@spec one(repo(), id()) :: {:ok, Unit.t()} | {:error, String.t()} -def one(repo \\ Repo, id) do - one_by(repo, id: id) -end - -@spec one_by(repo(), map() | Keyword.t()) +@spec one(repo(), id() | map() | Keyword.t()) :: {:ok, Unit.t()} | {:error, String.t()} -def one_by(repo \\ Repo, clauses) do +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(Unit, clauses) do nil -> {:error, "not found"} found -> {:ok, found} end end +@spec all(Paging.params()) :: Paging.result(Unit.t()) +def all(params) do + Paging.page(Unit, params) +end + + # `repo` is needed since we use that in a migration script. @spec create(repo(), params()) :: {:ok, Unit.t()} | {:error, chgset()} def create(repo \\ Repo, params) do @@ -53,11 +57,12 @@ def create(repo \\ Repo, params) do end end -@spec update(id(), params()) :: {:ok, Unit.t()} | {:error, chgset()} +@spec update(id(), params()) + :: {:ok, Unit.t()} | {:error, String.t() | chgset()} def update(id, params) do Multi.new() |> Multi.put(:id, id) - |> Multi.run(:one, &one_by/2) + |> Multi.run(:one, &one/2) |> Multi.update(:update, &Unit.chgset(&1.one, params)) |> Repo.transaction() |> case do @@ -66,12 +71,12 @@ def update(id, params) do end end -@spec delete(id()) :: {:ok, Unit.t()} | {:error, chgset()} +@spec delete(id()) :: {:ok, Unit.t()} | {:error, String.t() | chgset()} def delete(id) do Multi.new() |> Multi.put(:id, id) - |> Multi.run(:one, &one_by/2) - |> Multi.delete(:delete, &(&1.one)) + |> Multi.run(:one, &one/2) + |> Multi.delete(:delete, & &1.one) |> Repo.transaction() |> case do {:ok, %{delete: u}} -> {:ok, u} diff --git a/src/zenflows/vf/unit/resolv.ex b/src/zenflows/vf/unit/resolv.ex @@ -20,23 +20,27 @@ defmodule Zenflows.VF.Unit.Resolv do alias Zenflows.VF.Unit.Domain -def unit(%{id: id}, _info) do - Domain.one(id) +def unit(params, _) do + Domain.one(params) end -def create_unit(%{unit: params}, _info) do +def units(params, _) do + Domain.all(params) +end + +def create_unit(%{unit: params}, _) do with {:ok, unit} <- Domain.create(params) do {:ok, %{unit: unit}} end end -def update_unit(%{unit: %{id: id} = params}, _info) do +def update_unit(%{unit: %{id: id} = params}, _) do with {:ok, unit} <- Domain.update(id, params) do {:ok, %{unit: unit}} end end -def delete_unit(%{id: id}, _info) do +def delete_unit(%{id: id}, _) do with {:ok, _} <- Domain.delete(id) do {:ok, true} end diff --git a/src/zenflows/vf/unit/type.ex b/src/zenflows/vf/unit/type.ex @@ -39,10 +39,6 @@ object :unit do field :symbol, non_null(:string) end -object :unit_response do - field :unit, :unit -end - input_object :unit_create_params do @desc @label field :label, non_null(:string) @@ -61,13 +57,33 @@ input_object :unit_update_params do field :symbol, :string end +object :unit_response do + field :unit, non_null(:unit) +end + +object :unit_edge do + field :cursor, non_null(:id) + field :node, non_null(:unit) +end + +object :unit_connection do + field :page_info, non_null(:page_info) + field :edges, non_null(list_of(non_null(:unit_edge))) +end + object :query_unit do field :unit, :unit do arg :id, non_null(:id) resolve &Resolv.unit/2 end - #units(start: ID, limit: Int): [Unit!] + field :units, :unit_connection do + arg :first, :integer + arg :after, :id + arg :last, :integer + arg :before, :id + resolve &Resolv.units/2 + end end object :mutation_unit do diff --git a/test/vf/unit/domain.test.exs b/test/vf/unit/domain.test.exs @@ -28,7 +28,6 @@ setup do symbol: Factory.str("symbol"), }, inserted: Factory.insert!(:unit), - id: Factory.id(), } end @@ -37,8 +36,8 @@ describe "one/1" do assert {:ok, %Unit{}} = Domain.one(id) end - test "with bad id: doesn't find the Unit", %{id: id} do - assert {:error, "not found"} = Domain.one(id) + test "with bad id: doesn't find the Unit" do + assert {:error, "not found"} = Domain.one(Factory.id()) end end @@ -74,8 +73,8 @@ describe "delete/1" do assert {:error, "not found"} = Domain.one(id) end - test "with bad id: doesn't delete the Unit", %{id: id} do - assert {:error, "not found"} = Domain.delete(id) + test "with bad id: doesn't delete the Unit" do + assert {:error, "not found"} = Domain.delete(Factory.id()) end end end diff --git a/test/vf/unit/type.test.exs b/test/vf/unit/type.test.exs @@ -21,77 +21,77 @@ use ZenflowsTest.Help.AbsinCase, async: true setup do %{ params: %{ - label: Factory.uniq("label"), - symbol: Factory.uniq("symbol"), + "label" => Factory.uniq("label"), + "symbol" => Factory.uniq("symbol"), }, - unit: Factory.insert!(:unit), + inserted: Factory.insert!(:unit), } end +@frag """ +fragment unit on Unit { + id + label + symbol +} +""" + describe "Query" do - test "unit()", %{unit: unit} do + test "unit", %{inserted: new} do assert %{data: %{"unit" => data}} = - query!(""" - unit(id: "#{unit.id}") { - id - label - symbol + run!(""" + #{@frag} + query ($id: ID!) { + unit(id: $id) {...unit} } - """) + """, vars: %{"id" => new.id}) - assert data["id"] == unit.id - assert data["label"] == unit.label - assert data["symbol"] == unit.symbol + assert data["id"] == new.id + assert data["label"] == new.label + assert data["symbol"] == new.symbol end end describe "Mutation" do - test "createUnit()", %{params: params} do + test "createUnit", %{params: params} do assert %{data: %{"createUnit" => %{"unit" => data}}} = - mutation!(""" - createUnit(unit: { - label: "#{params.label}" - symbol: "#{params.symbol}" - }) { - unit { - id - label - symbol + run!(""" + #{@frag} + mutation ($unit: UnitCreateParams!) { + createUnit(unit: $unit) { + unit {...unit} } } - """) + """, vars: %{"unit" => params}) assert {:ok, _} = Zenflows.DB.ID.cast(data["id"]) - assert data["label"] == params.label - assert data["symbol"] == params.symbol + assert data["label"] == params["label"] + assert data["symbol"] == params["symbol"] end - test "updateUnit()", %{params: params, unit: unit} do + test "updateUnit", %{params: params, inserted: old} do assert %{data: %{"updateUnit" => %{"unit" => data}}} = - mutation!(""" - updateUnit(unit: { - id: "#{unit.id}" - label: "#{params.label}" - symbol: "#{params.symbol}" - }) { - unit { - id - label - symbol + run!(""" + #{@frag} + mutation ($unit: UnitUpdateParams!) { + updateUnit(unit: $unit) { + unit {...unit} } } - """) + """, vars: %{"unit" => Map.put(params, "id", old.id)}) - assert data["id"] == unit.id - assert data["label"] == params.label - assert data["symbol"] == params.symbol + assert data["id"] == old.id + assert data["label"] == params["label"] + assert data["symbol"] == params["symbol"] end - test "deleteUnit()", %{unit: %{id: id}} do + test "deleteUnit", %{inserted: %{id: id}} do assert %{data: %{"deleteUnit" => true}} = - mutation!(""" - deleteUnit(id: "#{id}") - """) + run!(""" + mutation ($id: ID!) { + deleteUnit(id: $id) + } + """, vars: %{"id" => id}) end end end