Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albertored committed May 25, 2020
1 parent 4ee5423 commit 20445f3
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 29 deletions.
8 changes: 8 additions & 0 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"skip_files": [
"test"
],
"coverage_options": {
"treat_no_relevant_lines_as_covered": true
}
}
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule ExSieve.Mixfile do
description: "Build filtred and sorted Ecto.Query struct from object based queries.",
aliases: aliases(),
deps: deps(),
test_coverage: [tool: ExCoveralls],
dialyzer: [plt_add_deps: :transitive],

# Docs
Expand Down Expand Up @@ -43,7 +44,8 @@ defmodule ExSieve.Mixfile do
{:ex_doc, "~> 0.21", only: :dev},
{:ex_machina, "~> 2.0", only: :test},
{:ecto_sql, "~> 3.0", only: :test},
{:postgrex, "~> 0.15", only: :test}
{:postgrex, "~> 0.15", only: :test},
{:excoveralls, "~> 0.10", only: :test}
]
end

Expand Down
10 changes: 9 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
"credo": {:hex, :credo, "1.4.0", "92339d4cbadd1e88b5ee43d427b639b68a11071b6f73854e33638e30a0ea11f5", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1fd3b70dce216574ce3c18bdf510b57e7c4c85c2ec9cad4bff854abaf7e58658"},
"db_connection": {:hex, :db_connection, "2.2.2", "3bbca41b199e1598245b716248964926303b5d4609ff065125ce98bcd368939e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "642af240d8a8affb93b4ba5a6fcd2bbcbdc327e1a524b825d383711536f8070c"},
Expand All @@ -11,11 +12,18 @@
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.22.1", "9bb6d51508778193a4ea90fa16eac47f8b67934f33f8271d5e1edec2dc0eee4c", [:mix], [{:earmark, "~> 1.4.0", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "d957de1b75cb9f78d3ee17820733dc4460114d8b1e11f7ee4fd6546e69b1db60"},
"ex_machina": {:hex, :ex_machina, "2.4.0", "09a34c5d371bfb5f78399029194a8ff67aff340ebe8ba19040181af35315eabb", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "a20bc9ddc721b33ea913b93666c5d0bdca5cbad7a67540784ae277228832d72c"},
"excoveralls": {:hex, :excoveralls, "0.12.3", "2142be7cb978a3ae78385487edda6d1aff0e482ffc6123877bb7270a8ffbcfe0", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "568a3e616c264283f5dea5b020783ae40eef3f7ee2163f7a67cbd7b35bcadada"},
"hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "e0100f8ef7d1124222c11ad362c857d3df7cb5f4204054f9f0f4a728666591fc"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "4bdd305eb64e18b0273864920695cb18d7a2021f31a11b9c5fbcd9a253f936e2"},
"jason": {:hex, :jason, "1.2.1", "12b22825e22f468c02eb3e4b9985f3d0cb8dc40b9bd704730efa11abd2708c44", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "b659b8571deedf60f79c5a608e15414085fa141344e2716fbd6988a084b5f993"},
"makeup": {:hex, :makeup, "1.0.1", "82f332e461dc6c79dbd82fbe2a9c10d48ed07146f0a478286e590c83c52010b5", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49736fe5b66a08d8575bf5321d716bac5da20c8e6b97714fec2bcd6febcfa1f8"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"postgrex": {:hex, :postgrex, "0.15.4", "5d691c25fc79070705a2ff0e35ce0822b86a0ee3c6fdb7a4fb354623955e1aed", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "306515b9d975fcb2478dc337a1d27dc3bf8af7cd71017c333fe9db3a3d211b0a"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"},
"telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
}
42 changes: 39 additions & 3 deletions test/ex_sieve/builder/join_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,61 @@ defmodule ExSieve.Builder.JoinTest do
use ExUnit.Case
import Ecto.Query

alias ExSieve.Post
alias ExSieve.{Post, User}
alias ExSieve.Builder.Join
alias ExSieve.Node.{Attribute, Condition, Grouping, Sort}

defp sample_grouping(attrs) do
%Grouping{
conditions: [
%Condition{
attributes: Enum.map(attrs, fn {name, parent} -> %Attribute{name: name, parent: parent} end),
combinator: :and,
predicate: :eq,
values: ["foo"]
}
],
combinator: :and,
groupings: []
}
end

describe "ExSieve.Builder.Join.build/2" do
test "return Ecto.Query post join with comments" do
grouping = sample_grouping(body: [:comments])

original = Post |> join(:inner, [p], c in assoc(p, :comments), as: :comments) |> inspect()
built = Post |> Join.build([:comments]) |> inspect()
built = Post |> Join.build(grouping, []) |> inspect()

assert original == built
end

test "return Ecto.Query post and user join with comments" do
grouping = sample_grouping(body: [:comments], name: [:user])

original =
Post
|> join(:inner, [p], c in assoc(p, :comments), as: :comments)
|> join(:inner, [p], c in assoc(p, :user), as: :user)
|> inspect()

built = Post |> Join.build([:comments, :user]) |> inspect()
built = Post |> Join.build(grouping, []) |> inspect()

assert original == built
end

test "correctly handle nested relations" do
sorts = [%Sort{direction: :asc, attribute: %Attribute{name: :title, parent: [:posts]}}]
grouping = sample_grouping(body: [:posts, :comments], body: [:comments])

original =
User
|> join(:inner, [u], p in assoc(u, :posts), as: :posts)
|> join(:inner, [u], c in assoc(u, :comments), as: :comments)
|> join(:inner, [posts: p], c in assoc(p, :comments), as: :posts_comments)
|> inspect()

built = User |> Join.build(grouping, sorts) |> inspect()

assert original == built
end
Expand Down
23 changes: 19 additions & 4 deletions test/ex_sieve/builder/order_by_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ defmodule ExSieve.Builder.OrderByTest do
use ExUnit.Case
import Ecto.Query

alias ExSieve.{Comment, Post}
alias ExSieve.{Comment, Post, User}
alias ExSieve.Builder.OrderBy
alias ExSieve.Node.{Sort, Attribute}

describe "ExSieve.Builder.OrderBy.build/3" do
test "return Ecto.Query with order by asc id" do
sorts = [%Sort{direction: :asc, attribute: %Attribute{name: :id, parent: :query}}]
sorts = [%Sort{direction: :asc, attribute: %Attribute{name: :id, parent: []}}]

original = Comment |> order_by(asc: :id) |> inspect()
built = Comment |> OrderBy.build(sorts) |> inspect()
Expand All @@ -17,8 +17,8 @@ defmodule ExSieve.Builder.OrderByTest do

test "return Ecto.Query order by asc id and post body" do
sorts = [
%Sort{direction: :asc, attribute: %Attribute{name: :id, parent: :query}},
%Sort{direction: :desc, attribute: %Attribute{name: :body, parent: :comments}}
%Sort{direction: :asc, attribute: %Attribute{name: :id, parent: []}},
%Sort{direction: :desc, attribute: %Attribute{name: :body, parent: [:comments]}}
]

base = from(from(p in Post, join: c in assoc(p, :comments), as: :comments))
Expand All @@ -27,5 +27,20 @@ defmodule ExSieve.Builder.OrderByTest do

assert original == built
end

test "correctly handle nested relations" do
sorts = [%Sort{direction: :desc, attribute: %Attribute{name: :body, parent: [:posts, :comments]}}]

base =
User
|> join(:inner, [u], p in assoc(u, :posts), as: :posts)
|> join(:inner, [posts: p], c in assoc(p, :comments), as: :posts_comments)

original = base |> order_by([posts_comments: pc], desc: pc.body) |> inspect()

built = base |> OrderBy.build(sorts) |> inspect()

assert original == built
end
end
end
21 changes: 20 additions & 1 deletion test/ex_sieve/builder/where_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ExSieve.Builder.WhereTest do
use ExUnit.Case
import Ecto.Query

alias ExSieve.{Post, Config}
alias ExSieve.{Config, Post, User}
alias ExSieve.Builder.Where
alias ExSieve.Node.Grouping

Expand Down Expand Up @@ -64,5 +64,24 @@ defmodule ExSieve.Builder.WhereTest do

assert ecto == query
end

test "return Ecto.Query for nested relations" do
params = %{"m" => "or", "posts_comments_body_eq" => "test", "id_eq" => 1}
grouping = Grouping.extract(params, User, %Config{ignore_errors: true})

base =
User
|> join(:inner, [u], p in assoc(u, :posts), as: :posts)
|> join(:inner, [posts: p], c in assoc(p, :comments), as: :posts_comments)

ecto =
base
|> where([u, posts_comments: c], field(c, :body) == ^"test" or field(u, :id) == ^1)
|> inspect()

query = base |> Where.build(grouping) |> inspect()

assert ecto == query
end
end
end
18 changes: 11 additions & 7 deletions test/ex_sieve/node/attribute_test.exs
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
defmodule ExSieve.Node.AttributeTest do
use ExUnit.Case

alias ExSieve.{Node.Attribute, Post, Comment}
alias ExSieve.{Node.Attribute, Post, Comment, User}

describe "ExSieve.Node.Attribute.extract/2" do
test "return Attribute with parent belongs_to" do
assert %Attribute{parent: :post, name: :body} == Attribute.extract("post_body_eq", Comment)
assert %Attribute{parent: [:post], name: :body} == Attribute.extract("post_body_eq", Comment)
end

test "return Attribute with has_many parent" do
assert %Attribute{parent: :comments, name: :id} == Attribute.extract("comments_id_in", Post)
assert %Attribute{parent: [:comments], name: :id} == Attribute.extract("comments_id_in", Post)
end

test "return Attribute without parent" do
assert %Attribute{name: :id, parent: :query} == Attribute.extract("id_eq", Comment)
assert %Attribute{name: :id, parent: []} == Attribute.extract("id_eq", Comment)
end

test "return Attributes for schema with similar fields names" do
assert %Attribute{name: :published, parent: :query} == Attribute.extract("published_eq", Post)
assert %Attribute{name: :published_at, parent: :query} == Attribute.extract("published_at_eq", Post)
assert %Attribute{name: :published, parent: []} == Attribute.extract("published_eq", Post)
assert %Attribute{name: :published_at, parent: []} == Attribute.extract("published_at_eq", Post)
end

test "return Attribute with belongs_to parent" do
assert %Attribute{name: :name, parent: :user} == Attribute.extract("user_name_cont_all", Comment)
assert %Attribute{name: :name, parent: [:user]} == Attribute.extract("user_name_cont_all", Comment)
end

test "return Attribute with nested parents" do
assert %Attribute{name: :body, parent: [:posts, :comments]} == Attribute.extract("posts_comments_body_cont", User)
end

test "return {:error, :attribute_not_found}" do
Expand Down
3 changes: 2 additions & 1 deletion test/ex_sieve/node/condition_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExSieve.Node.ConditionTest do
use ExUnit.Case

alias ExSieve.{Node.Condition, Comment}
alias ExSieve.{Node.Condition, Comment, User}

describe "ExSieve.Node.Condition.extract/3" do
test "return Condition with without combinator" do
Expand Down Expand Up @@ -50,6 +50,7 @@ defmodule ExSieve.Node.ConditionTest do

test "return {:error, :attribute_not_found}" do
assert {:error, :attribute_not_found} == Condition.extract("tid_eq", 1, Comment)
assert {:error, :attribute_not_found} == Condition.extract("posts_comments_foo_eq", 1, User)
end

test "return {:error, :value_is_empty}" do
Expand Down
11 changes: 8 additions & 3 deletions test/ex_sieve/node/sort_test.exs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
defmodule ExSieve.Node.SortTest do
use ExUnit.Case, async: true

alias ExSieve.Comment
alias ExSieve.{Comment, User}
alias ExSieve.Node.{Sort, Attribute}

describe "Sort.extract/2" do
test "return list(Sort.t) for String.t value" do
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: :post}}
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: [:post]}}
assert [sort] == Sort.extract("post_body asc", Comment)
end

test "return list(Sort.t) for list(String.t) value" do
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: :post}}
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: [:post]}}
assert [sort] == Sort.extract(["post_body asc"], Comment)
end

test "correctly handle nested relations" do
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: [:posts, :comments]}}
assert [sort] == Sort.extract(["posts_comments_body asc"], User)
end

test "return {:error, :direction_not_found}" do
assert [{:error, :direction_not_found}] == Sort.extract("post_body_asc", Comment)
end
Expand Down
16 changes: 8 additions & 8 deletions test/node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ defmodule ExSieve.NodeTest do

describe "ExSieve.Node.call/2" do
test "return {list(Grouping.t), list(Sort.t)}", %{config: config} do
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: :post}}
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: [:post]}}

grouping = %Grouping{
combinator: :and,
conditions: [
%Condition{
attributes: [%Attribute{name: :id, parent: :query}],
attributes: [%Attribute{name: :id, parent: []}],
combinator: :and,
predicate: :eq,
values: [1]
Expand All @@ -31,13 +31,13 @@ defmodule ExSieve.NodeTest do
end

test "return {list(Grouping.t), list(Sort.t)} for params with mixed keys", %{config: config} do
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: :post}}
sort = %Sort{direction: :asc, attribute: %Attribute{name: :body, parent: [:post]}}

grouping = %Grouping{
combinator: :and,
conditions: [
%Condition{
attributes: [%Attribute{name: :id, parent: :query}],
attributes: [%Attribute{name: :id, parent: []}],
combinator: :and,
predicate: :eq,
values: [1]
Expand All @@ -53,15 +53,15 @@ defmodule ExSieve.NodeTest do

test "return {list(Grouping.t), list(Sort.t)} with nested groupings", %{config: config} do
sorts = [
%Sort{direction: :desc, attribute: %Attribute{name: :id, parent: :query}},
%Sort{direction: :asc, attribute: %Attribute{name: :body, parent: :post}}
%Sort{direction: :desc, attribute: %Attribute{name: :id, parent: []}},
%Sort{direction: :asc, attribute: %Attribute{name: :body, parent: [:post]}}
]

grouping = %Grouping{
combinator: :or,
conditions: [
%Condition{
attributes: [%Attribute{name: :id, parent: :query}],
attributes: [%Attribute{name: :id, parent: []}],
combinator: :and,
predicate: :eq,
values: [1]
Expand All @@ -72,7 +72,7 @@ defmodule ExSieve.NodeTest do
combinator: :and,
conditions: [
%Condition{
attributes: [%Attribute{name: :title, parent: :post}],
attributes: [%Attribute{name: :title, parent: [:post]}],
combinator: :and,
predicate: :eq,
values: [1]
Expand Down

0 comments on commit 20445f3

Please sign in to comment.