Commit 0d244355 authored by ivan's avatar ivan

measure and proposal paginated queries tested

parent 0463e32a
......@@ -36,8 +36,8 @@ defmodule Measurement.Hydration do
unit: [
resolve: &Measurement.Unit.GraphQL.unit/2
],
measures: [
resolve: &Measurement.Measure.GraphQL.measures/2
measures_pages: [
resolve: &Measurement.Measure.GraphQL.measures_pages/2
],
# all_measures: [
# resolve: &Measurement.Measure.GraphQL.all_measures/2
......
......@@ -29,7 +29,7 @@ defmodule Measurement.Measure.GraphQL do
})
end
def measures(page_opts, info) do
def measures_pages(page_opts, info) do
ResolveRootPage.run(%ResolveRootPage{
module: __MODULE__,
fetcher: :fetch_measures,
......@@ -53,10 +53,10 @@ defmodule Measurement.Measure.GraphQL do
FetchPage.run(%FetchPage{
queries: Measurement.Measure.Queries,
query: Measurement.Measure,
# cursor_fn: measures.cursor(:followers),
cursor_fn: & &1.id,
page_opts: page_opts,
base_filters: [user: GraphQL.current_user(info)],
data_filters: [:dafault]
data_filters: [:default]
})
end
......
......@@ -85,6 +85,7 @@ defmodule Measurement.Measure.Queries do
## by field values
def filter(q, {:id, id}) when is_binary(id) do
where(q, [measure: c], c.id == ^id)
end
......@@ -120,30 +121,6 @@ defmodule Measurement.Measure.Queries do
select(q, [measure: c], {field(c, ^key), count(c.id)})
end
# pagination
def filter(q, {:limit, limit}) do
limit(q, ^limit)
end
def filter(q, {:paginate_id, %{after: a, limit: limit}}) do
limit = limit + 2
q
|> where([measure: c], c.id >= ^a)
|> limit(^limit)
end
def filter(q, {:paginate_id, %{before: b, limit: limit}}) do
q
|> where([measure: c], c.id <= ^b)
|> filter(limit: limit + 2)
end
def filter(q, {:paginate_id, %{limit: limit}}) do
filter(q, limit: limit + 1)
end
# defp page(q, %{limit: limit}, _), do: filter(q, limit: limit + 1)
def inc_quantity(id, amount) do
......
......@@ -140,11 +140,11 @@ type MeasuresPage {
type MeasurementQuery {
unit(id: ID): Unit
units(start: ID, limit: Int): [Unit!]
unitsPages(limit: Int, before: [Cursor], after: [Cursor]): [UnitsPage!]
unitsPages(limit: Int, before: [Cursor], after: [Cursor]): UnitsPage!
measure(id: ID): Measure
measures(start: ID, limit: Int): [Measure!]
measuresPages(limit: Int, before: [Cursor], after: [Cursor]): [MeasuresPage!]
measuresPages(limit: Int, before: [Cursor], after: [Cursor]): MeasuresPage!
}
type MeasurementMutation {
......
......@@ -48,7 +48,7 @@ defmodule Measurement.GraphQLTest do
q = units_query()
conn = user_conn(user)
vars = %{after: after_unit.id, limit: 2}
assert [%{"edges" => fetched}] = grumble_post_key(q, conn, :unitsPages, vars)
assert %{"edges" => fetched} = grumble_post_key(q, conn, :units_pages, vars)
assert Enum.count(fetched) == 2
assert List.first(fetched)["id"] == after_unit.id
end
......@@ -119,4 +119,21 @@ defmodule Measurement.GraphQLTest do
assert_measure(grumble_post_key(q, conn, :measure, %{id: measure.id}))
end
end
describe "measuresPages" do
test "fetches a page of measures" do
user = fake_user!()
unit = fake_unit!(user)
measures = some(5, fn -> fake_measure!(user, unit) end)
after_measure = List.first(measures)
q = measures_pages_query()
conn = user_conn(user)
vars = %{after: after_measure.id, limit: 2}
assert %{"edges" => fetched} = grumble_post_key(q, conn, :measures_pages, vars)
assert Enum.count(fetched) == 2
assert List.first(fetched)["id"] == after_measure.id
end
end
end
......@@ -145,6 +145,32 @@ defmodule Measurement.Test.Faking do
gen_query(:id, &measure_subquery/1, options)
end
def measures_pages_query(options \\ []) do
params =
[
after: list_type(:cursor),
before: list_type(:cursor),
limit: :int
] ++ Keyword.get(options, :params, [])
gen_query(&measures_pages_subquery/1, [{:params, params} | options])
end
def measures_pages_subquery(options \\ []) do
args = [
after: var(:after),
before: var(:before),
limit: var(:limit)
]
page_subquery(
:measures_pages,
&measure_fields/1,
[{:args, args} | options]
)
end
def create_measure_mutation(options \\ []) do
[measure: type!(:measure_create_params)]
|> gen_mutation(&create_measure_submutation/1, options)
......
......@@ -467,6 +467,32 @@ defmodule ValueFlows.Test.Faking do
gen_subquery(:id, :proposal, &proposal_fields/1, options)
end
def proposals_pages_subquery(options \\ []) do
args = [
after: var(:after),
before: var(:before),
limit: var(:limit)
]
page_subquery(
:proposals_pages,
&proposal_fields/1,
[{:args, args} | options]
)
end
def proposals_pages_query(options \\ []) do
params =
[
after: list_type(:cursor),
before: list_type(:cursor),
limit: :int
] ++ Keyword.get(options, :params, [])
gen_query(&proposals_pages_subquery/1, [{:params, params} | options])
end
def create_proposal_mutation(options \\ []) do
[proposal: type!(:proposal_create_params)]
|> gen_mutation(&create_proposal_submutation/1, options)
......
......@@ -141,7 +141,19 @@ defmodule ValueFlows.Proposal.GraphQLTest do
end
end
describe "proposals" do
describe "proposalPages" do
test "fetches a page of proposals" do
user = fake_user!()
proposals = some(5, fn -> fake_proposal!(user) end)
after_proposal = List.first(proposals)
q = proposals_pages_query()
conn = user_conn(user)
vars = %{after: after_proposal.id, limit: 2}
assert %{"edges" => fetched} = grumble_post_key(q, conn, :proposalsPages, vars)
assert Enum.count(fetched) == 2
assert List.first(fetched)["id"] == after_proposal.id
end
end
describe "createProposal" do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment