releaf-i18n_database 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,175 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::TranslationsUtilities do
4
- def postgresql?
5
- ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
6
- end
7
-
8
- describe ".search" do
9
- before do
10
- allow(described_class).to receive(:filter_by_text).with("aaa", "xxxx").and_return("bbb")
11
- allow(described_class).to receive(:filter_only_blank_translations).with("bbb").and_return("ccc")
12
- allow(described_class).to receive(:filter_only_blank_translations).with("ddd").and_return("eee")
13
- end
14
-
15
- context "when search string is present" do
16
- it "applies search filter to given collection" do
17
- expect(described_class.search("aaa", "xxxx", true)).to eq("ccc")
18
- end
19
- end
20
-
21
- context "when search string is empty" do
22
- it "does not apply search filter to given collection" do
23
- expect(described_class.search("ddd", "", true)).to eq("eee")
24
- end
25
- end
26
-
27
- context "when `only blank` option is true" do
28
- it "applies `only blank` filter to given collection" do
29
- expect(described_class.search("aaa", "xxxx", true)).to eq("ccc")
30
- end
31
- end
32
-
33
- context "when `only blank` option is other than true" do
34
- it "does not apply `only blank` filter to given collection" do
35
- expect(described_class.search("aaa", "xxxx", false)).to eq("bbb")
36
- end
37
- end
38
- end
39
-
40
- describe ".filter_only_blank_translations" do
41
- it "returns given collection applied with empty translation data search" do
42
- collection = Releaf::I18nDatabase::I18nEntry.where("id > 1")
43
- allow(described_class).to receive(:search_columns).and_return([
44
- Releaf::I18nDatabase::I18nEntry.arel_table[:key],
45
- Releaf::I18nDatabase::I18nEntry.arel_table.alias("lv_data")[:text],
46
- ])
47
- if postgresql?
48
- result = "WHERE (id > 1) AND ((\"releaf_i18n_entries\".\"key\" = '' OR \"releaf_i18n_entries\".\"key\" IS NULL)"
49
- result += " OR (\"lv_data\".\"text\" = '' OR \"lv_data\".\"text\" IS NULL))"
50
- else
51
- result = "WHERE (id > 1) AND ((`releaf_i18n_entries`.`key` = '' OR `releaf_i18n_entries`.`key` IS NULL)"
52
- result += " OR (`lv_data`.`text` = '' OR `lv_data`.`text` IS NULL))"
53
- end
54
- expect(described_class.filter_only_blank_translations(collection).to_sql).to end_with(result)
55
- end
56
- end
57
-
58
- describe ".filter_by_text" do
59
- it "returns collection with grouped column searches" do
60
- collection = Releaf::I18nDatabase::I18nEntry.where("id > 1")
61
- allow(described_class).to receive(:column_searches).with("redx").and_return([
62
- "id = 8 AND id = 2",
63
- "id = 9 AND id = 19",
64
- ])
65
- result = "WHERE (id > 1) AND ((id = 8 AND id = 2) OR (id = 9 AND id = 19))"
66
- expect(described_class.filter_by_text(collection, "redx").to_sql).to end_with(result)
67
- end
68
- end
69
-
70
- describe ".column_searches" do
71
- it "return array with column based searches" do
72
- allow(described_class).to receive(:search_columns).and_return([
73
- Releaf::I18nDatabase::I18nEntry.arel_table[:key],
74
- Releaf::I18nDatabase::I18nEntry.arel_table.alias("lv_data")[:text],
75
- ])
76
- allow(described_class).to receive(:escape_search_string).with("red").twice.and_return("escaped_red")
77
- allow(described_class).to receive(:escape_search_string).with("car").twice.and_return("escaped_car")
78
- if postgresql?
79
- result = ["\"releaf_i18n_entries\".\"key\" ILIKE '%escaped_red%' AND \"releaf_i18n_entries\".\"key\" ILIKE '%escaped_car%'",
80
- "\"lv_data\".\"text\" ILIKE '%escaped_red%' AND \"lv_data\".\"text\" ILIKE '%escaped_car%'"]
81
- else
82
- result = ["`releaf_i18n_entries`.`key` LIKE '%escaped_red%' AND `releaf_i18n_entries`.`key` LIKE '%escaped_car%'",
83
- "`lv_data`.`text` LIKE '%escaped_red%' AND `lv_data`.`text` LIKE '%escaped_car%'"]
84
- end
85
- expect(described_class.column_searches(" red car ")).to eq(result)
86
- end
87
- end
88
-
89
- describe ".search_columns" do
90
- it "returns array with translation key arel attribute and locale tables localization attributes" do
91
- allow(described_class).to receive(:locale_tables).and_return(
92
- de: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("de_data"),
93
- lv: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("lv_data"),
94
- en: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("en_data")
95
- )
96
-
97
- result = [
98
- Releaf::I18nDatabase::I18nEntry.arel_table[:key],
99
- Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("de_data")[:text],
100
- Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("lv_data")[:text],
101
- Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("en_data")[:text]
102
- ]
103
- expect(described_class.search_columns).to eq(result)
104
- end
105
- end
106
-
107
- describe ".escape_search_string" do
108
- it "returns escaped search string with escaped `%` and `_` chars" do
109
- expect(described_class.escape_search_string("k % _ x")).to eq("k \\% \\_ x")
110
- end
111
- end
112
-
113
- describe ".locale_tables" do
114
- it "returns array with arel aliased locale tables" do
115
- allow(Releaf.application.config).to receive(:all_locales).and_return([:de, :en])
116
- result = {
117
- de: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("de_data"),
118
- en: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("en_data")
119
- }
120
- expect(described_class.locale_tables).to eq(result)
121
- end
122
- end
123
-
124
- describe ".include_localizations" do
125
- it "returns given collection with included joins and overrided selects" do
126
- allow(described_class).to receive(:localization_include_joins).and_return(["a", "b"])
127
- allow(described_class).to receive(:localization_include_selects).and_return("x")
128
- if postgresql?
129
- result = "SELECT x FROM \"releaf_i18n_entries\" a b"
130
- else
131
- result = "SELECT x FROM `releaf_i18n_entries` a b"
132
- end
133
- expect(described_class.include_localizations(Releaf::I18nDatabase::I18nEntry).to_sql).to eq(result)
134
- end
135
- end
136
-
137
- describe ".localization_include_joins" do
138
- it "returns array with locales translation table joins" do
139
- allow(described_class).to receive(:locale_tables).and_return(
140
- de: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("de_data"),
141
- lv: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("lv_data"),
142
- en: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("en_data")
143
- )
144
- result = ["LEFT JOIN releaf_i18n_entry_translations AS de_data ON de_data.i18n_entry_id = releaf_i18n_entries.id AND de_data.locale = 'de'",
145
- "LEFT JOIN releaf_i18n_entry_translations AS lv_data ON lv_data.i18n_entry_id = releaf_i18n_entries.id AND lv_data.locale = 'lv'",
146
- "LEFT JOIN releaf_i18n_entry_translations AS en_data ON en_data.i18n_entry_id = releaf_i18n_entries.id AND en_data.locale = 'en'"]
147
- expect(described_class.localization_include_joins).to eq(result)
148
- end
149
- end
150
-
151
- describe ".localization_include_selects" do
152
- it "returns all base table columns and each locale translated values and ids" do
153
- allow(described_class).to receive(:localization_include_locales_columns).and_return([
154
- "de_data.localization AS de_localization",
155
- "de_data.id AS de_localization_id"
156
- ])
157
- result = "releaf_i18n_entries.*, de_data.localization AS de_localization, de_data.id AS de_localization_id"
158
- expect(described_class.localization_include_selects).to eq(result)
159
- end
160
- end
161
-
162
- describe ".localization_include_locales_columns" do
163
- it "returns locales translated values columns and ids" do
164
- allow(described_class).to receive(:locale_tables).and_return(
165
- de: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("de_data"),
166
- lv: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("lv_data"),
167
- en: Releaf::I18nDatabase::I18nEntryTranslation.arel_table.alias("en_data")
168
- )
169
- result = ["de_data.text AS de_localization", "de_data.id AS de_localization_id",
170
- "lv_data.text AS lv_localization", "lv_data.id AS lv_localization_id",
171
- "en_data.text AS en_localization", "en_data.id AS en_localization_id"]
172
- expect(described_class.localization_include_locales_columns).to eq(result)
173
- end
174
- end
175
- end
@@ -1,50 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::I18nEntry do
4
- it { is_expected.to validate_presence_of(:key) }
5
- it { is_expected.to validate_length_of(:key).is_at_most(255) }
6
- it { subject.key = "a"; is_expected.to validate_uniqueness_of(:key) }
7
- it { is_expected.to have_many(:i18n_entry_translation).dependent(:destroy) }
8
- it { is_expected.to accept_nested_attributes_for(:i18n_entry_translation).allow_destroy(true) }
9
-
10
- describe "#locale_value" do
11
- it "returns translated value for given locale" do
12
- subject.i18n_entry_translation.build(text: 'apple', locale: "en")
13
- subject.i18n_entry_translation.build(text: 'apfel', locale: "de")
14
-
15
- expect(subject.locale_value("en")).to eq("apple")
16
- expect(subject.locale_value(:de)).to eq("apfel")
17
- expect(subject.locale_value("lt")).to be nil
18
- end
19
- end
20
-
21
- describe "#find_or_initialize_translation" do
22
- before do
23
- subject.key = "xx"
24
- subject.i18n_entry_translation.build(text: 'apple', locale: "en")
25
- subject.i18n_entry_translation.build(text: 'ābols', locale: "lv")
26
- subject.save
27
- allow(subject.i18n_entry_translation).to receive(:build).with(locale: "de").and_return(:new)
28
- end
29
-
30
- context "when translation exists for given locale (given as string or symbol)" do
31
- it "returns existing translation instance" do
32
- expect(subject.find_or_initialize_translation(:en).text).to eq("apple")
33
- expect(subject.find_or_initialize_translation("en").text).to eq("apple")
34
- end
35
-
36
- it "uses AR cache to prevent multiple db hit for multiple locales lookup" do
37
- expect {
38
- subject.find_or_initialize_translation("en")
39
- subject.find_or_initialize_translation("lv")
40
- }.to make_database_queries(count: 1)
41
- end
42
- end
43
-
44
- context "when translation does not exists for given locale" do
45
- it "returns newly builded translation instance" do
46
- expect(subject.find_or_initialize_translation("de")).to eq(:new)
47
- end
48
- end
49
- end
50
- end
@@ -1,9 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::I18nEntryTranslation do
4
- it { is_expected.to validate_presence_of(:i18n_entry) }
5
- it { is_expected.to validate_presence_of(:locale) }
6
- it { is_expected.to validate_length_of(:locale).is_at_most(5) }
7
- it { subject.locale = "de"; subject.i18n_entry_id = 1; is_expected.to validate_uniqueness_of(:i18n_entry_id).scoped_to([:locale]) }
8
- it { is_expected.to belong_to(:i18n_entry) }
9
- end