diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..1e5073c --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,6 @@ +[ + inputs: [ + "{lib,config,test}/**/*.{ex,exs}", + "mix.exs" + ] +] diff --git a/.travis.yml b/.travis.yml index c790410..7ab57d3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,11 @@ language: elixir otp_release: - - 20.1 + - 20.2 elixir: - - 1.5.2 + - 1.6.0 before_script: - export SCRIVENER_ECTO_DB_USER=postgres - MIX_ENV=test mix db.reset +script: + - mix test + - mix format --check-formatted diff --git a/config/config.exs b/config/config.exs index c9c59bb..8233fe9 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,3 +1,3 @@ use Mix.Config -import_config "#{Mix.env}.exs" +import_config "#{Mix.env()}.exs" diff --git a/config/test.exs b/config/test.exs index 8be8359..7f55256 100644 --- a/config/test.exs +++ b/config/test.exs @@ -8,5 +8,4 @@ config :scrivener_ecto, Scrivener.Ecto.Repo, database: "scrivener_test", username: System.get_env("SCRIVENER_ECTO_DB_USER") || System.get_env("USER") -config :logger, :console, - level: :error +config :logger, :console, level: :error diff --git a/lib/scrivener/paginater/atom.ex b/lib/scrivener/paginater/atom.ex index b89f471..b498f52 100644 --- a/lib/scrivener/paginater/atom.ex +++ b/lib/scrivener/paginater/atom.ex @@ -1,10 +1,10 @@ defimpl Scrivener.Paginater, for: Atom do @moduledoc false - @spec paginate(atom, Scrivener.Config.t) :: Scrivener.Page.t + @spec paginate(atom, Scrivener.Config.t()) :: Scrivener.Page.t() def paginate(atom, config) do atom - |> Ecto.Queryable.to_query + |> Ecto.Queryable.to_query() |> Scrivener.Paginater.paginate(config) end end diff --git a/lib/scrivener/paginater/ecto/query.ex b/lib/scrivener/paginater/ecto/query.ex index db823ef..53b1eb5 100644 --- a/lib/scrivener/paginater/ecto/query.ex +++ b/lib/scrivener/paginater/ecto/query.ex @@ -5,9 +5,17 @@ defimpl Scrivener.Paginater, for: Ecto.Query do @moduledoc false - @spec paginate(Ecto.Query.t, Scrivener.Config.t) :: Scrivener.Page.t - def paginate(query, %Config{page_size: page_size, page_number: page_number, module: repo, caller: caller, options: options}) do - total_entries = Keyword.get_lazy(options, :total_entries, fn -> total_entries(query, repo, caller) end) + @spec paginate(Ecto.Query.t(), Scrivener.Config.t()) :: Scrivener.Page.t() + def paginate(query, %Config{ + page_size: page_size, + page_number: page_number, + module: repo, + caller: caller, + options: options + }) do + total_entries = + Keyword.get_lazy(options, :total_entries, fn -> total_entries(query, repo, caller) end) + total_pages = total_pages(total_entries, page_size) page_number = min(total_pages, page_number) @@ -42,20 +50,22 @@ defimpl Scrivener.Paginater, for: Ecto.Query do end defp prepare_select( - %{ - group_bys: [ - %Ecto.Query.QueryExpr{ - expr: [ - {{:., [], [{:&, [], [source_index]}, field]}, [], []} | _ - ] - } | _ - ] - } = query - ) do + %{ + group_bys: [ + %Ecto.Query.QueryExpr{ + expr: [ + {{:., [], [{:&, [], [source_index]}, field]}, [], []} | _ + ] + } + | _ + ] + } = query + ) do query |> exclude(:select) |> select([x: source_index], struct(x, ^[field])) end + defp prepare_select(query) do query |> exclude(:select) @@ -70,6 +80,6 @@ defimpl Scrivener.Paginater, for: Ecto.Query do defp total_pages(0, _), do: 1 defp total_pages(total_entries, page_size) do - (total_entries / page_size) |> Float.ceil |> round + (total_entries / page_size) |> Float.ceil() |> round end end diff --git a/mix.exs b/mix.exs index 62b4aa9..df6ac11 100644 --- a/mix.exs +++ b/mix.exs @@ -6,7 +6,7 @@ defmodule Scrivener.Ecto.Mixfile do app: :scrivener_ecto, version: "1.3.0", elixir: "~> 1.3", - elixirc_paths: elixirc_paths(Mix.env), + elixirc_paths: elixirc_paths(Mix.env()), package: package(), description: "Paginate your Ecto queries with Scrivener", deps: deps(), @@ -32,7 +32,7 @@ defmodule Scrivener.Ecto.Mixfile do def application do [ - applications: applications(Mix.env) + applications: applications(Mix.env()) ] end diff --git a/mix.lock b/mix.lock index c7bd758..2c1cb6a 100644 --- a/mix.lock +++ b/mix.lock @@ -1,12 +1,14 @@ -%{"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [], [], "hexpm"}, - "db_connection": {:hex, :db_connection, "1.1.2", "2865c2a4bae0714e2213a0ce60a1b12d76a6efba0c51fbda59c9ab8d1accc7a8", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, +%{ + "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [], [], "hexpm"}, + "db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, "decimal": {:hex, :decimal, "1.4.1", "ad9e501edf7322f122f7fc151cce7c2a0c9ada96f2b0155b8a09a795c2029770", [:mix], [], "hexpm"}, "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"}, "dialyze": {:hex, :dialyze, "0.2.1"}, - "earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], [], "hexpm"}, - "ecto": {:hex, :ecto, "2.2.6", "3fd1067661d6d64851a0d4db9acd9e884c00d2d1aa41cc09da687226cf894661", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, + "earmark": {:hex, :earmark, "1.2.4", "99b637c62a4d65a20a9fb674b8cffb8baa771c04605a80c911c4418c69b75439", [:mix], [], "hexpm"}, + "ecto": {:hex, :ecto, "2.2.8", "a4463c0928b970f2cee722cd29aaac154e866a15882c5737e0038bbfcf03ec2c", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, "ex_doc": {:hex, :ex_doc, "0.18.1", "37c69d2ef62f24928c1f4fdc7c724ea04aecfdf500c4329185f8e3649c915baf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, "ex_spec": {:hex, :ex_spec, "1.0.0", "b1e791072fecbf80c725adf45e7cbdf3d96af3765638a1f1547824706ece4bc9", [:mix], []}, "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"}, "postgrex": {:hex, :postgrex, "0.13.3", "c277cfb2a9c5034d445a722494c13359e361d344ef6f25d604c2353185682bfc", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"}, - "scrivener": {:hex, :scrivener, "2.4.0", "c9431804b13ac6a5c4b01eb32188c1ff926898a2d684244d021706841f022e66", [:mix], [], "hexpm"}} + "scrivener": {:hex, :scrivener, "2.4.0", "c9431804b13ac6a5c4b01eb32188c1ff926898a2d684244d021706841f022e66", [:mix], [], "hexpm"}, +} diff --git a/test/scrivener/paginator/ecto/query_test.exs b/test/scrivener/paginator/ecto/query_test.exs index ee2d5bb..48a766b 100644 --- a/test/scrivener/paginator/ecto/query_test.exs +++ b/test/scrivener/paginator/ecto/query_test.exs @@ -4,17 +4,20 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do alias Scrivener.Ecto.{Comment, KeyValue, Post} defp create_posts do - unpublished_post = %Post{ - title: "Title unpublished", - body: "Body unpublished", - published: false - } |> Scrivener.Ecto.Repo.insert! + unpublished_post = + %Post{ + title: "Title unpublished", + body: "Body unpublished", + published: false + } + |> Scrivener.Ecto.Repo.insert!() Enum.map(1..2, fn i -> %Comment{ body: "Body #{i}", post_id: unpublished_post.id - } |> Scrivener.Ecto.Repo.insert! + } + |> Scrivener.Ecto.Repo.insert!() end) Enum.map(1..6, fn i -> @@ -22,7 +25,8 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do title: "Title #{i}", body: "Body #{i}", published: true - } |> Scrivener.Ecto.Repo.insert! + } + |> Scrivener.Ecto.Repo.insert!() end) end @@ -30,8 +34,9 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do Enum.map(1..10, fn i -> %KeyValue{ key: "key_#{i}", - value: (rem(i, 2) |> to_string) - } |> Scrivener.Ecto.Repo.insert! + value: rem(i, 2) |> to_string + } + |> Scrivener.Ecto.Repo.insert!() end) end @@ -39,7 +44,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do test "paginates an unconstrained query" do create_posts() - page = Post |> Scrivener.Ecto.Repo.paginate + page = Post |> Scrivener.Ecto.Repo.paginate() assert page.page_size == 5 assert page.page_number == 1 @@ -48,7 +53,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do end test "page information is correct with no results" do - page = Post |> Scrivener.Ecto.Repo.paginate + page = Post |> Scrivener.Ecto.Repo.paginate() assert page.page_size == 5 assert page.page_number == 1 @@ -61,8 +66,8 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published - |> Scrivener.Ecto.Repo.paginate + |> Post.published() + |> Scrivener.Ecto.Repo.paginate() assert page.page_size == 5 assert page.page_number == 1 @@ -76,9 +81,9 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> preload(:comments) - |> Scrivener.Ecto.Repo.paginate + |> Scrivener.Ecto.Repo.paginate() assert page.page_size == 5 assert page.page_number == 1 @@ -93,7 +98,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do |> join(:left, [p], c in assoc(p, :comments)) |> group_by([p], p.id) |> select([p], sum(p.id)) - |> Scrivener.Ecto.Repo.paginate + |> Scrivener.Ecto.Repo.paginate() assert page.total_entries == 7 end @@ -105,7 +110,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do Post |> select([p], fragment("? as aliased_title", p.title)) |> order_by([p], fragment("aliased_title")) - |> Scrivener.Ecto.Repo.paginate + |> Scrivener.Ecto.Repo.paginate() assert page.total_entries == 7 end @@ -115,7 +120,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.Ecto.Repo.paginate(%{"page" => "2", "page_size" => "3"}) assert page.page_size == 3 @@ -129,7 +134,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.Ecto.Repo.paginate(page: 2, page_size: 3) assert page.page_size == 3 @@ -142,10 +147,11 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do create_posts() parent = self() - task = Task.async(fn -> - Post - |> Scrivener.Ecto.Repo.paginate(caller: parent) - end) + task = + Task.async(fn -> + Post + |> Scrivener.Ecto.Repo.paginate(caller: parent) + end) page = Task.await(task) @@ -160,10 +166,11 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do parent = self() - task = Task.async(fn -> - Post - |> Scrivener.Ecto.Repo.paginate(%{"caller" => parent}) - end) + task = + Task.async(fn -> + Post + |> Scrivener.Ecto.Repo.paginate(%{"caller" => parent}) + end) page = Task.await(task) @@ -178,7 +185,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.Ecto.Repo.paginate(%{"page" => "1", "page_size" => "20"}) assert page.page_size == 10 @@ -196,7 +203,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.paginate(config) assert page.total_entries == 130 @@ -207,7 +214,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.Ecto.Repo.paginate(options: [total_entries: 130]) assert page.total_entries == 130 @@ -225,7 +232,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.paginate(config) assert page.page_number == 1 @@ -237,7 +244,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = KeyValue - |> KeyValue.zero + |> KeyValue.zero() |> Scrivener.Ecto.Repo.paginate(page_size: 2) assert page.total_entries == 5 @@ -251,7 +258,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do Post |> join(:left, [p], c in assoc(p, :comments)) |> group_by([p], p.id) - |> Scrivener.Ecto.Repo.paginate + |> Scrivener.Ecto.Repo.paginate() assert page.total_entries == 7 end @@ -262,8 +269,8 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post |> group_by([p], p.body) - |> select([p], (p.body)) - |> Scrivener.Ecto.Repo.paginate + |> select([p], p.body) + |> Scrivener.Ecto.Repo.paginate() assert page.total_entries == 7 end @@ -275,8 +282,8 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do Post |> join(:inner, [p], c in assoc(p, :comments)) |> group_by([p, c], c.body) - |> select([p, c], ({c.body, count("*")})) - |> Scrivener.Ecto.Repo.paginate + |> select([p, c], {c.body, count("*")}) + |> Scrivener.Ecto.Repo.paginate() assert page.total_entries == 2 end @@ -288,8 +295,8 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do Post |> join(:inner, [p], c in assoc(p, :comments)) |> group_by([p, c], [c.body, p.title]) - |> select([p, c], ({c.body, p.title, count("*")})) - |> Scrivener.Ecto.Repo.paginate + |> select([p, c], {c.body, p.title, count("*")}) + |> Scrivener.Ecto.Repo.paginate() assert page.total_entries == 2 end @@ -306,7 +313,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.paginate(config) assert page.page_size == 4 @@ -320,7 +327,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.paginate(module: Scrivener.Ecto.Repo, page: 2, page_size: 4) assert page.page_size == 4 @@ -334,7 +341,7 @@ defmodule Scrivener.Paginator.Ecto.QueryTest do page = Post - |> Post.published + |> Post.published() |> Scrivener.paginate(%{"module" => Scrivener.Ecto.Repo, "page" => 2, "page_size" => 4}) assert page.page_size == 4 diff --git a/test/support/comment.ex b/test/support/comment.ex index 4c26680..747bfb9 100644 --- a/test/support/comment.ex +++ b/test/support/comment.ex @@ -2,9 +2,9 @@ defmodule Scrivener.Ecto.Comment do use Ecto.Schema schema "comments" do - field :body, :string + field(:body, :string) - belongs_to :post, Scrivener.Ecto.Post + belongs_to(:post, Scrivener.Ecto.Post) timestamps() end diff --git a/test/support/keyvalue.ex b/test/support/keyvalue.ex index 5893117..adc7f70 100644 --- a/test/support/keyvalue.ex +++ b/test/support/keyvalue.ex @@ -6,7 +6,7 @@ defmodule Scrivener.Ecto.KeyValue do @primary_key {:key, :string, autogenerate: false} schema "key_values" do - field :value, :string + field(:value, :string) end def zero(query) do diff --git a/test/support/post.ex b/test/support/post.ex index 9c5e59e..1159e44 100644 --- a/test/support/post.ex +++ b/test/support/post.ex @@ -4,11 +4,11 @@ defmodule Scrivener.Ecto.Post do import Ecto.Query schema "posts" do - field :title, :string - field :body, :string - field :published, :boolean + field(:title, :string) + field(:body, :string) + field(:published, :boolean) - has_many :comments, Scrivener.Ecto.Comment + has_many(:comments, Scrivener.Ecto.Comment) timestamps() end diff --git a/test/test_helper.exs b/test/test_helper.exs index ce1d2f3..17627d6 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,7 +1,7 @@ defmodule Scrivener.Ecto.TestCase do use ExUnit.CaseTemplate - using(opts) do + using opts do quote do use ExUnit.Case, unquote(opts) import Ecto.Query @@ -13,7 +13,7 @@ defmodule Scrivener.Ecto.TestCase do end end -Scrivener.Ecto.Repo.start_link +Scrivener.Ecto.Repo.start_link() Ecto.Adapters.SQL.Sandbox.mode(Scrivener.Ecto.Repo, :manual) ExUnit.start()