From a682ec1f313a1a88d5d3a05c4cf4634d663bb735 Mon Sep 17 00:00:00 2001 From: belaustegui Date: Thu, 30 Mar 2017 21:52:31 +0200 Subject: [PATCH] Update QueryBuilder tests with new query syntax The QueryBuilder tests have been updated to use the newly introduced `translated` macro. --- test/new_query_builder_test.exs | 25 ------------ test/query_builder_test.exs | 70 ++++++++++++++++----------------- 2 files changed, 33 insertions(+), 62 deletions(-) delete mode 100644 test/new_query_builder_test.exs diff --git a/test/new_query_builder_test.exs b/test/new_query_builder_test.exs deleted file mode 100644 index 0031234..0000000 --- a/test/new_query_builder_test.exs +++ /dev/null @@ -1,25 +0,0 @@ -alias Trans.Article -alias Trans.TestRepo, as: Repo -import Trans.Factory -import Ecto.Query, only: [from: 2] -import Trans.QueryBuilder - -defmodule QueryBuilderTest do - use ExUnit.Case - - setup_all do - {:ok, - translated_article: insert(:article), - untranslated_article: insert(:article, test_translation_container: %{}), - } - end - - test "should find an article by its FR title", - %{translated_article: article} do - fr_title = article.test_translation_container["fr"]["title"] - matches = Repo.all(from a in Article, - where: translated(Trans.Article, a.title, locale: :fr) == ^fr_title) - assert Enum.count(matches) == 1 - assert hd(matches).id == article.id - end -end diff --git a/test/query_builder_test.exs b/test/query_builder_test.exs index 5a1c56f..0587764 100644 --- a/test/query_builder_test.exs +++ b/test/query_builder_test.exs @@ -1,6 +1,9 @@ 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 @@ -8,56 +11,50 @@ defmodule QueryBuilderTest do 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 @@ -65,30 +62,29 @@ defmodule QueryBuilderTest do 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