Skip to content

Commit

Permalink
Update QueryBuilder tests with new query syntax
Browse files Browse the repository at this point in the history
The QueryBuilder tests have been updated to use the newly introduced
`translated` macro.
  • Loading branch information
crbelaus committed Mar 30, 2017
1 parent b68fb7f commit a682ec1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 62 deletions.
25 changes: 0 additions & 25 deletions test/new_query_builder_test.exs

This file was deleted.

70 changes: 33 additions & 37 deletions test/query_builder_test.exs
Original file line number Diff line number Diff line change
@@ -1,94 +1,90 @@
alias Trans.Article
alias Trans.TestRepo, as: Repo

import Trans.Factory
import Trans.QueryBuilder
import Ecto.Query, only: [from: 2]

defmodule QueryBuilderTest do
use ExUnit.Case

setup_all do
{:ok,
translated_article: insert(:article),
untranslated_article: insert(:article, test_translation_container: %{}),
untranslated_article: insert(:article, translations: %{})
}
end

test "should find only one article translated to ES" do
matches =
Article
|> Article.with_translations(:es)
|> Repo.all
assert Enum.count(matches) == 1
count = Repo.one(from a in Article,
where: not is_nil(translated(a, locale: :es)),
select: count(a.id)
)
assert count == 1
end

test "should not find any article translated to DE" do
matches =
Article
|> Article.with_translations(:de)
|> Repo.all
assert Enum.empty?(matches)
count = Repo.one(from a in Article,
where: not is_nil(translated(a, locale: :de)),
select: count(a.id))
assert count == 0
end

test "should find an article by its FR title",
%{translated_article: article} do
fr_title = article.test_translation_container["fr"]["title"]
matches =
Article
|> Article.with_translation(:fr, :title, fr_title)
|> Repo.all
matches = Repo.all(from a in Article,
where: translated(a.title, locale: :fr) == ^article.translations["fr"]["title"])
assert Enum.count(matches) == 1
assert hd(matches).id == article.id
end

test "should not find an article by a non existant translation" do
matches =
Article
|> Article.with_translation(:es, :title, "FAKE TITLE")
|> Repo.all
assert Enum.empty?(matches)
count = Repo.one(from a in Article,
select: count(a.id),
where: translated(a.title, locale: :es) == "FAKE TITLE")
assert count == 0
end

test "should find an article by partial and case sensitive translation",
%{translated_article: article} do
first_words =
article.test_translation_container["es"]["body"]
article.translations["es"]["body"]
|> String.split
|> Enum.take(3)
|> Enum.join(" ")
matches =
Article
|> Article.with_translation(:es, :body, "#{first_words}%", type: :like)
|> Repo.all
|> Kernel.<>("%")
matches = Repo.all(from a in Article,
where: ilike(translated(a.body, locale: :es), ^first_words))
assert Enum.count(matches) == 1
assert hd(matches).id == article.id
end

test "should not find an article by incorrect case using case sensitive translation",
%{translated_article: article} do
first_words =
article.test_translation_container["fr"]["body"]
article.translations["fr"]["body"]
|> String.split
|> Enum.take(3)
|> Enum.join(" ")
|> String.upcase
matches =
Article
|> Article.with_translation(:fr, :body, "#{first_words}%", type: :like)
|> Repo.all
assert Enum.empty?(matches)
|> Kernel.<>("%")
count = Repo.one(from a in Article,
select: count(a.id),
where: like(translated(a.body, locale: :fr), ^first_words))
assert count == 0
end

test "should find an article by incorrect case using case insensitive translation",
%{translated_article: article} do
first_words =
article.test_translation_container["fr"]["body"]
article.translations["fr"]["body"]
|> String.split
|> Enum.take(3)
|> Enum.join(" ")
|> String.upcase
matches =
Article
|> Article.with_translation(:fr, :body, "#{first_words}%", type: :ilike)
|> Repo.all
|> Kernel.<>("%")
matches = Repo.all(from a in Article,
where: ilike(translated(a.body, locale: :fr), ^first_words))
assert Enum.count(matches) == 1
assert hd(matches).id == article.id
end
Expand Down

0 comments on commit a682ec1

Please sign in to comment.