|
| 1 | +defmodule Localize.Ecto.PostgresTest do |
| 2 | + # `Localize.Ecto` exposes the same macros for backwards compatibility |
| 3 | + # and is covered by test/localize/ecto_test.exs. This suite drives |
| 4 | + # Localize.Ecto.Postgres directly so the named adapter module is |
| 5 | + # exercised on its own terms rather than by inference from the alias. |
| 6 | + use ExUnit.Case, async: true |
| 7 | + |
| 8 | + import Ecto.Query |
| 9 | + import Localize.Ecto.Postgres |
| 10 | + |
| 11 | + require Localize.Ecto |
| 12 | + |
| 13 | + doctest Localize.Ecto.Postgres |
| 14 | + |
| 15 | + defp to_sql(query) do |
| 16 | + {query, _cast_params, _dump_params} = |
| 17 | + Ecto.Adapter.Queryable.plan_query(:all, Ecto.Adapters.Postgres, query) |
| 18 | + |
| 19 | + IO.iodata_to_binary(Ecto.Adapters.Postgres.Connection.all(query)) |
| 20 | + end |
| 21 | + |
| 22 | + describe "collate/1,2" do |
| 23 | + test "collate/1 resolves the current locale" do |
| 24 | + query = from p in "products", order_by: collate(p.name), select: p.name |
| 25 | + |
| 26 | + assert to_sql(query) =~ ~s[ORDER BY p0."name" COLLATE "en-x-icu"] |
| 27 | + end |
| 28 | + |
| 29 | + test "collate/2 emits the quoted collation name" do |
| 30 | + query = from p in "products", order_by: collate(p.name, "sv"), select: p.name |
| 31 | + |
| 32 | + assert to_sql(query) =~ ~s[ORDER BY p0."name" COLLATE "sv-x-icu"] |
| 33 | + end |
| 34 | + |
| 35 | + test "accepts a pinned runtime locale" do |
| 36 | + locale = "de-DE" |
| 37 | + query = from p in "products", order_by: collate(p.name, ^locale), select: p.name |
| 38 | + |
| 39 | + assert to_sql(query) =~ ~s[COLLATE "de-x-icu"] |
| 40 | + end |
| 41 | + |
| 42 | + test "accepts a collation name given directly" do |
| 43 | + query = |
| 44 | + from p in "products", |
| 45 | + order_by: collate(p.name, collation: "german_phonebook"), |
| 46 | + select: p.name |
| 47 | + |
| 48 | + assert to_sql(query) =~ ~s[COLLATE "german_phonebook"] |
| 49 | + end |
| 50 | + |
| 51 | + test "carries a BCP 47 collation type into the name" do |
| 52 | + query = from p in "products", order_by: collate(p.name, "de-u-co-phonebk"), select: p.name |
| 53 | + |
| 54 | + assert to_sql(query) =~ ~s[COLLATE "de-u-co-phonebk-x-icu"] |
| 55 | + end |
| 56 | + |
| 57 | + test "collates a comparison" do |
| 58 | + query = from p in "products", where: collate(p.name < "münchen", "de"), select: p.name |
| 59 | + |
| 60 | + assert to_sql(query) =~ ~s[WHERE (p0."name" < 'münchen' COLLATE "de-x-icu")] |
| 61 | + end |
| 62 | + |
| 63 | + test "collates each comparison operator" do |
| 64 | + equality = from p in "products", select: collate(p.name == p.description, "de") |
| 65 | + inequality = from p in "products", select: collate(p.name != p.description, "de") |
| 66 | + greater = from p in "products", select: collate(p.name >= p.description, "de") |
| 67 | + |
| 68 | + assert to_sql(equality) =~ ~s[p0."name" = p0."description" COLLATE "de-x-icu"] |
| 69 | + assert to_sql(inequality) =~ ~s[p0."name" <> p0."description" COLLATE "de-x-icu"] |
| 70 | + assert to_sql(greater) =~ ~s[p0."name" >= p0."description" COLLATE "de-x-icu"] |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + describe "case mapping" do |
| 75 | + # PostgreSQL takes case mapping from the collation of the argument, |
| 76 | + # which is what distinguishes these from the SQLite expansions. |
| 77 | + test "lower/1 and lower/2 collate the argument" do |
| 78 | + assert to_sql(from p in "products", select: lower(p.name)) =~ |
| 79 | + ~s[lower(p0."name" COLLATE "en-x-icu")] |
| 80 | + |
| 81 | + assert to_sql(from p in "products", select: lower(p.name, "tr")) =~ |
| 82 | + ~s[lower(p0."name" COLLATE "tr-x-icu")] |
| 83 | + end |
| 84 | + |
| 85 | + test "upper/1 and upper/2 collate the argument" do |
| 86 | + assert to_sql(from p in "products", select: upper(p.name)) =~ |
| 87 | + ~s[upper(p0."name" COLLATE "en-x-icu")] |
| 88 | + |
| 89 | + assert to_sql(from p in "products", select: upper(p.name, "tr")) =~ |
| 90 | + ~s[upper(p0."name" COLLATE "tr-x-icu")] |
| 91 | + end |
| 92 | + |
| 93 | + test "initcap/1 and initcap/2 collate the argument" do |
| 94 | + assert to_sql(from p in "products", select: initcap(p.name)) =~ |
| 95 | + ~s[initcap(p0."name" COLLATE "en-x-icu")] |
| 96 | + |
| 97 | + assert to_sql(from p in "products", select: initcap(p.name, "nl")) =~ |
| 98 | + ~s[initcap(p0."name" COLLATE "nl-x-icu")] |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + describe "at_time_zone/2" do |
| 103 | + test "canonicalizes the zone" do |
| 104 | + query = from e in "events", select: at_time_zone(e.starts_at, "Australia/Sydney") |
| 105 | + |
| 106 | + assert to_sql(query) =~ ~s[AT TIME ZONE] |
| 107 | + end |
| 108 | + |
| 109 | + test "accepts a pinned zone" do |
| 110 | + zone = "Australia/Sydney" |
| 111 | + query = from e in "events", select: at_time_zone(e.starts_at, ^zone) |
| 112 | + |
| 113 | + assert to_sql(query) =~ ~s[AT TIME ZONE] |
| 114 | + end |
| 115 | + |
| 116 | + test "raises on an unknown zone when the query is built" do |
| 117 | + assert_raise Localize.UnknownTimezoneError, fn -> |
| 118 | + Localize.Ecto.Type.TimeZone.canonicalize!("Mars/Olympus_Mons") |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + describe "ts_match/2,3" do |
| 124 | + test "ts_match/2 uses the current locale's configuration" do |
| 125 | + query = from p in "products", where: ts_match(p.description, "chair"), select: p.id |
| 126 | + |
| 127 | + assert to_sql(query) =~ "to_tsvector" |
| 128 | + assert to_sql(query) =~ "websearch_to_tsquery" |
| 129 | + end |
| 130 | + |
| 131 | + test "ts_match/3 resolves the locale's configuration" do |
| 132 | + query = from p in "products", where: ts_match(p.description, "stuhl", "de"), select: p.id |
| 133 | + |
| 134 | + assert to_sql(query) =~ "to_tsvector" |
| 135 | + end |
| 136 | + |
| 137 | + test "ts_match/3 accepts a configuration name directly" do |
| 138 | + query = |
| 139 | + from p in "products", |
| 140 | + where: ts_match(p.description, "stuhl", config: "german"), |
| 141 | + select: p.id |
| 142 | + |
| 143 | + assert to_sql(query) =~ "to_tsvector" |
| 144 | + end |
| 145 | + |
| 146 | + # A pinned locale is deliberately not tested here: for a literal |
| 147 | + # binary the compiler can prove the macro's is_list/1 branch dead |
| 148 | + # and warns. The pin itself goes through Builder.unpin/1, which the |
| 149 | + # collate/2 pinned-locale test above already covers. |
| 150 | + end |
| 151 | + |
| 152 | + describe "parity with Localize.Ecto" do |
| 153 | + test "the compatibility module emits identical SQL" do |
| 154 | + via_alias = |
| 155 | + from p in "products", order_by: Localize.Ecto.collate(p.name, "sv"), select: p.name |
| 156 | + |
| 157 | + via_module = from p in "products", order_by: collate(p.name, "sv"), select: p.name |
| 158 | + |
| 159 | + assert to_sql(via_alias) == to_sql(via_module) |
| 160 | + end |
| 161 | + end |
| 162 | +end |
0 commit comments