pg_search 2.3.2 → 2.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +11 -0
- data/.rubocop.yml +86 -4
- data/.travis.yml +4 -12
- data/CHANGELOG.md +25 -20
- data/LICENSE +1 -1
- data/README.md +5 -5
- data/lib/pg_search.rb +4 -4
- data/lib/pg_search/document.rb +1 -1
- data/lib/pg_search/features/dmetaphone.rb +4 -6
- data/lib/pg_search/features/tsearch.rb +13 -12
- data/lib/pg_search/migration/templates/add_pg_search_dmetaphone_support_functions.rb.erb +6 -6
- data/lib/pg_search/migration/templates/create_pg_search_documents.rb.erb +2 -2
- data/lib/pg_search/model.rb +2 -0
- data/lib/pg_search/multisearch.rb +10 -1
- data/lib/pg_search/multisearch/rebuilder.rb +2 -2
- data/lib/pg_search/scope_options.rb +2 -2
- data/lib/pg_search/tasks.rb +1 -1
- data/lib/pg_search/version.rb +1 -1
- data/pg_search.gemspec +8 -3
- data/spec/integration/.rubocop.yml +11 -0
- data/spec/integration/associations_spec.rb +17 -56
- data/spec/integration/deprecation_spec.rb +1 -1
- data/spec/integration/pg_search_spec.rb +62 -51
- data/spec/lib/pg_search/configuration/association_spec.rb +8 -6
- data/spec/lib/pg_search/features/dmetaphone_spec.rb +2 -2
- data/spec/lib/pg_search/features/trigram_spec.rb +15 -11
- data/spec/lib/pg_search/features/tsearch_spec.rb +16 -10
- data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +89 -55
- data/spec/lib/pg_search/multisearch_spec.rb +47 -28
- data/spec/lib/pg_search/multisearchable_spec.rb +148 -94
- data/spec/lib/pg_search/normalizer_spec.rb +12 -10
- data/spec/lib/pg_search_spec.rb +57 -41
- data/spec/spec_helper.rb +10 -4
- data/spec/support/database.rb +1 -1
- metadata +81 -8
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
|
+
# rubocop:disable RSpec/NestedGroups
|
5
6
|
describe PgSearch::Configuration::Association do
|
6
7
|
with_model :Avatar do
|
7
8
|
table do |t|
|
@@ -39,7 +40,7 @@ describe PgSearch::Configuration::Association do
|
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
42
|
-
context "has_one" do
|
43
|
+
context "with has_one" do
|
43
44
|
let(:association) { described_class.new(User, :avatar, :url) }
|
44
45
|
|
45
46
|
describe "#table_name" do
|
@@ -50,7 +51,7 @@ describe PgSearch::Configuration::Association do
|
|
50
51
|
|
51
52
|
describe "#join" do
|
52
53
|
let(:expected_sql) do
|
53
|
-
|
54
|
+
<<~SQL.squish
|
54
55
|
LEFT OUTER JOIN
|
55
56
|
(SELECT model_id AS id,
|
56
57
|
#{column_select} AS #{association.columns.first.alias}
|
@@ -70,7 +71,7 @@ describe PgSearch::Configuration::Association do
|
|
70
71
|
end
|
71
72
|
end
|
72
73
|
|
73
|
-
context "belongs_to" do
|
74
|
+
context "with belongs_to" do
|
74
75
|
let(:association) { described_class.new(User, :site, :title) }
|
75
76
|
|
76
77
|
describe "#table_name" do
|
@@ -81,7 +82,7 @@ describe PgSearch::Configuration::Association do
|
|
81
82
|
|
82
83
|
describe "#join" do
|
83
84
|
let(:expected_sql) do
|
84
|
-
|
85
|
+
<<~SQL.squish
|
85
86
|
LEFT OUTER JOIN
|
86
87
|
(SELECT model_id AS id,
|
87
88
|
#{column_select} AS #{association.columns.first.alias}
|
@@ -101,7 +102,7 @@ describe PgSearch::Configuration::Association do
|
|
101
102
|
end
|
102
103
|
end
|
103
104
|
|
104
|
-
context "has_many" do
|
105
|
+
context "with has_many" do
|
105
106
|
let(:association) { described_class.new(Site, :users, :name) }
|
106
107
|
|
107
108
|
describe "#table_name" do
|
@@ -112,7 +113,7 @@ describe PgSearch::Configuration::Association do
|
|
112
113
|
|
113
114
|
describe "#join" do
|
114
115
|
let(:expected_sql) do
|
115
|
-
|
116
|
+
<<~SQL.squish
|
116
117
|
LEFT OUTER JOIN
|
117
118
|
(SELECT model_id AS id,
|
118
119
|
string_agg(\"#{association.table_name}\".\"name\"::text, ' ') AS #{association.columns.first.alias}
|
@@ -138,3 +139,4 @@ describe PgSearch::Configuration::Association do
|
|
138
139
|
end
|
139
140
|
end
|
140
141
|
end
|
142
|
+
# rubocop:enable RSpec/NestedGroups
|
@@ -18,7 +18,7 @@ describe PgSearch::Features::DMetaphone do
|
|
18
18
|
PgSearch::Configuration::Column.new(:content, nil, Model)
|
19
19
|
]
|
20
20
|
options = {}
|
21
|
-
config =
|
21
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
22
22
|
normalizer = PgSearch::Normalizer.new(config)
|
23
23
|
|
24
24
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -43,7 +43,7 @@ describe PgSearch::Features::DMetaphone do
|
|
43
43
|
PgSearch::Configuration::Column.new(:content, nil, Model)
|
44
44
|
]
|
45
45
|
options = {}
|
46
|
-
config =
|
46
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
47
47
|
normalizer = PgSearch::Normalizer.new(config)
|
48
48
|
|
49
49
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -3,8 +3,10 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
require 'ostruct'
|
5
5
|
|
6
|
+
# rubocop:disable RSpec/MultipleMemoizedHelpers, RSpec/NestedGroups
|
6
7
|
describe PgSearch::Features::Trigram do
|
7
8
|
subject(:feature) { described_class.new(query, options, columns, Model, normalizer) }
|
9
|
+
|
8
10
|
let(:query) { 'lolwut' }
|
9
11
|
let(:options) { {} }
|
10
12
|
let(:columns) {
|
@@ -17,8 +19,10 @@ describe PgSearch::Features::Trigram do
|
|
17
19
|
let(:config) { OpenStruct.new(ignore: []) }
|
18
20
|
|
19
21
|
let(:coalesced_columns) do
|
20
|
-
|
21
|
-
coalesce(#{Model.quoted_table_name}."name"::text, '')
|
22
|
+
<<~SQL.squish
|
23
|
+
coalesce(#{Model.quoted_table_name}."name"::text, '')
|
24
|
+
|| ' '
|
25
|
+
|| coalesce(#{Model.quoted_table_name}."content"::text, '')
|
22
26
|
SQL
|
23
27
|
end
|
24
28
|
|
@@ -35,7 +39,7 @@ describe PgSearch::Features::Trigram do
|
|
35
39
|
expect(feature.conditions.to_sql).to eq("('#{query}' % (#{coalesced_columns}))")
|
36
40
|
end
|
37
41
|
|
38
|
-
context 'searching by word_similarity' do
|
42
|
+
context 'when searching by word_similarity' do
|
39
43
|
let(:options) do
|
40
44
|
{ word_similarity: true }
|
41
45
|
end
|
@@ -46,7 +50,7 @@ describe PgSearch::Features::Trigram do
|
|
46
50
|
end
|
47
51
|
end
|
48
52
|
|
49
|
-
context 'ignoring accents' do
|
53
|
+
context 'when ignoring accents' do
|
50
54
|
it 'escapes the search document and query, but not the accent function' do
|
51
55
|
config.ignore = [:accents]
|
52
56
|
expect(feature.conditions.to_sql).to eq("(unaccent('#{query}') % (unaccent(#{coalesced_columns})))")
|
@@ -54,7 +58,7 @@ describe PgSearch::Features::Trigram do
|
|
54
58
|
end
|
55
59
|
|
56
60
|
context 'when a threshold is specified' do
|
57
|
-
context 'searching by similarity' do
|
61
|
+
context 'when searching by similarity' do
|
58
62
|
let(:options) do
|
59
63
|
{ threshold: 0.5 }
|
60
64
|
end
|
@@ -66,7 +70,7 @@ describe PgSearch::Features::Trigram do
|
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
69
|
-
context 'searching by word_similarity' do
|
73
|
+
context 'when searching by word_similarity' do
|
70
74
|
let(:options) do
|
71
75
|
{ threshold: 0.5, word_similarity: true }
|
72
76
|
end
|
@@ -79,21 +83,20 @@ describe PgSearch::Features::Trigram do
|
|
79
83
|
end
|
80
84
|
end
|
81
85
|
|
82
|
-
context 'only certain columns are selected' do
|
83
|
-
context 'one column' do
|
86
|
+
context 'when only certain columns are selected' do
|
87
|
+
context 'with one column' do
|
84
88
|
let(:options) { { only: :name } }
|
85
89
|
|
86
90
|
it 'only searches against the select column' do
|
87
|
-
options = { only: :name }
|
88
91
|
coalesced_column = "coalesce(#{Model.quoted_table_name}.\"name\"::text, '')"
|
89
92
|
expect(feature.conditions.to_sql).to eq("('#{query}' % (#{coalesced_column}))")
|
90
93
|
end
|
91
94
|
end
|
92
|
-
|
95
|
+
|
96
|
+
context 'with multiple columns' do
|
93
97
|
let(:options) { { only: %i[name content] } }
|
94
98
|
|
95
99
|
it 'concatenates when multiples columns are selected' do
|
96
|
-
options = { only: %i[name content] }
|
97
100
|
expect(feature.conditions.to_sql).to eq("('#{query}' % (#{coalesced_columns}))")
|
98
101
|
end
|
99
102
|
end
|
@@ -106,3 +109,4 @@ describe PgSearch::Features::Trigram do
|
|
106
109
|
end
|
107
110
|
end
|
108
111
|
end
|
112
|
+
# rubocop:enable RSpec/MultipleMemoizedHelpers, RSpec/NestedGroups
|
@@ -19,7 +19,7 @@ describe PgSearch::Features::TSearch do
|
|
19
19
|
PgSearch::Configuration::Column.new(:content, nil, Model)
|
20
20
|
]
|
21
21
|
options = {}
|
22
|
-
config =
|
22
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
23
23
|
normalizer = PgSearch::Normalizer.new(config)
|
24
24
|
|
25
25
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -44,7 +44,7 @@ describe PgSearch::Features::TSearch do
|
|
44
44
|
PgSearch::Configuration::Column.new(:content, nil, Model)
|
45
45
|
]
|
46
46
|
options = {}
|
47
|
-
config =
|
47
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
48
48
|
normalizer = PgSearch::Normalizer.new(config)
|
49
49
|
|
50
50
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -61,7 +61,7 @@ describe PgSearch::Features::TSearch do
|
|
61
61
|
PgSearch::Configuration::Column.new(:content, nil, Model)
|
62
62
|
]
|
63
63
|
options = { negation: true }
|
64
|
-
config =
|
64
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
65
65
|
normalizer = PgSearch::Normalizer.new(config)
|
66
66
|
|
67
67
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -79,7 +79,7 @@ describe PgSearch::Features::TSearch do
|
|
79
79
|
PgSearch::Configuration::Column.new(:content, nil, Model)
|
80
80
|
]
|
81
81
|
options = { negation: false }
|
82
|
-
config =
|
82
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
83
83
|
normalizer = PgSearch::Normalizer.new(config)
|
84
84
|
|
85
85
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -97,7 +97,7 @@ describe PgSearch::Features::TSearch do
|
|
97
97
|
PgSearch::Configuration::Column.new(:content, nil, Model)
|
98
98
|
]
|
99
99
|
options = { tsvector_column: "my_tsvector" }
|
100
|
-
config =
|
100
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
101
101
|
normalizer = PgSearch::Normalizer.new(config)
|
102
102
|
|
103
103
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -115,7 +115,7 @@ describe PgSearch::Features::TSearch do
|
|
115
115
|
PgSearch::Configuration::Column.new(:content, nil, Model)
|
116
116
|
]
|
117
117
|
options = { tsvector_column: ["tsvector1", "tsvector2"] }
|
118
|
-
config =
|
118
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
119
119
|
normalizer = PgSearch::Normalizer.new(config)
|
120
120
|
|
121
121
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -141,7 +141,7 @@ describe PgSearch::Features::TSearch do
|
|
141
141
|
]
|
142
142
|
options = {}
|
143
143
|
|
144
|
-
config =
|
144
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
145
145
|
normalizer = PgSearch::Normalizer.new(config)
|
146
146
|
|
147
147
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -151,6 +151,7 @@ describe PgSearch::Features::TSearch do
|
|
151
151
|
end
|
152
152
|
|
153
153
|
context "when options[:dictionary] is passed" do
|
154
|
+
# rubocop:disable RSpec/ExampleLength
|
154
155
|
it 'uses the provided dictionary' do
|
155
156
|
query = "query"
|
156
157
|
columns = [
|
@@ -165,7 +166,7 @@ describe PgSearch::Features::TSearch do
|
|
165
166
|
}
|
166
167
|
}
|
167
168
|
|
168
|
-
config =
|
169
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
169
170
|
normalizer = PgSearch::Normalizer.new(config)
|
170
171
|
|
171
172
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -174,9 +175,11 @@ describe PgSearch::Features::TSearch do
|
|
174
175
|
|
175
176
|
expect(feature.highlight.to_sql).to eq(expected_sql)
|
176
177
|
end
|
178
|
+
# rubocop:enable RSpec/ExampleLength
|
177
179
|
end
|
178
180
|
|
179
181
|
context "when options[:highlight] has options set" do
|
182
|
+
# rubocop:disable RSpec/ExampleLength
|
180
183
|
it "passes the options to ts_headline" do
|
181
184
|
query = "query"
|
182
185
|
columns = [
|
@@ -195,7 +198,7 @@ describe PgSearch::Features::TSearch do
|
|
195
198
|
}
|
196
199
|
}
|
197
200
|
|
198
|
-
config =
|
201
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
199
202
|
normalizer = PgSearch::Normalizer.new(config)
|
200
203
|
|
201
204
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -204,7 +207,9 @@ describe PgSearch::Features::TSearch do
|
|
204
207
|
|
205
208
|
expect(feature.highlight.to_sql).to eq(expected_sql)
|
206
209
|
end
|
210
|
+
# rubocop:enable RSpec/ExampleLength
|
207
211
|
|
212
|
+
# rubocop:disable RSpec/ExampleLength
|
208
213
|
it "passes deprecated options to ts_headline" do
|
209
214
|
query = "query"
|
210
215
|
columns = [
|
@@ -223,7 +228,7 @@ describe PgSearch::Features::TSearch do
|
|
223
228
|
}
|
224
229
|
}
|
225
230
|
|
226
|
-
config =
|
231
|
+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
|
227
232
|
normalizer = PgSearch::Normalizer.new(config)
|
228
233
|
|
229
234
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -233,6 +238,7 @@ describe PgSearch::Features::TSearch do
|
|
233
238
|
|
234
239
|
expect(highlight_sql).to eq(expected_sql)
|
235
240
|
end
|
241
|
+
# rubocop:enable RSpec/ExampleLength
|
236
242
|
end
|
237
243
|
end
|
238
244
|
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
|
+
# rubocop:disable RSpec/NestedGroups
|
5
6
|
describe PgSearch::Multisearch::Rebuilder do
|
6
7
|
with_table "pg_search_documents", &DOCUMENTS_SCHEMA
|
7
8
|
|
@@ -10,7 +11,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
10
11
|
|
11
12
|
it 'raises an exception' do
|
12
13
|
expect {
|
13
|
-
|
14
|
+
described_class.new(NotMultisearchable)
|
14
15
|
}.to raise_exception(
|
15
16
|
PgSearch::Multisearch::ModelNotMultisearchable,
|
16
17
|
"NotMultisearchable is not multisearchable. See PgSearch::ClassMethods#multisearchable"
|
@@ -20,7 +21,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
20
21
|
|
21
22
|
describe "#rebuild" do
|
22
23
|
context "when the model defines .rebuild_pg_search_documents" do
|
23
|
-
context "
|
24
|
+
context "when multisearchable is not conditional" do
|
24
25
|
with_model :Model do
|
25
26
|
model do
|
26
27
|
include PgSearch::Model
|
@@ -31,14 +32,18 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
|
-
it "
|
35
|
-
rebuilder =
|
36
|
-
|
37
|
-
|
35
|
+
it "calls .rebuild_pg_search_documents" do
|
36
|
+
rebuilder = described_class.new(Model)
|
37
|
+
|
38
|
+
without_partial_double_verification do
|
39
|
+
allow(Model).to receive(:rebuild_pg_search_documents)
|
40
|
+
rebuilder.rebuild
|
41
|
+
expect(Model).to have_received(:rebuild_pg_search_documents)
|
42
|
+
end
|
38
43
|
end
|
39
44
|
end
|
40
45
|
|
41
|
-
context "
|
46
|
+
context "when multisearchable is conditional" do
|
42
47
|
%i[if unless].each do |conditional_key|
|
43
48
|
context "via :#{conditional_key}" do
|
44
49
|
with_model :Model do
|
@@ -55,10 +60,14 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
55
60
|
end
|
56
61
|
end
|
57
62
|
|
58
|
-
it "
|
59
|
-
rebuilder =
|
60
|
-
|
61
|
-
|
63
|
+
it "calls .rebuild_pg_search_documents" do
|
64
|
+
rebuilder = described_class.new(Model)
|
65
|
+
|
66
|
+
without_partial_double_verification do
|
67
|
+
allow(Model).to receive(:rebuild_pg_search_documents)
|
68
|
+
rebuilder.rebuild
|
69
|
+
expect(Model).to have_received(:rebuild_pg_search_documents)
|
70
|
+
end
|
62
71
|
end
|
63
72
|
end
|
64
73
|
end
|
@@ -66,7 +75,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
66
75
|
end
|
67
76
|
|
68
77
|
context "when the model does not define .rebuild_pg_search_documents" do
|
69
|
-
context "
|
78
|
+
context "when multisearchable is not conditional" do
|
70
79
|
context "when :against only includes columns" do
|
71
80
|
with_model :Model do
|
72
81
|
table do |t|
|
@@ -79,8 +88,8 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
79
88
|
end
|
80
89
|
end
|
81
90
|
|
82
|
-
it "
|
83
|
-
rebuilder =
|
91
|
+
it "does not call :rebuild_pg_search_documents" do
|
92
|
+
rebuilder = described_class.new(Model)
|
84
93
|
|
85
94
|
# stub respond_to? to return false since should_not_receive defines the method
|
86
95
|
original_respond_to = Model.method(:respond_to?)
|
@@ -92,24 +101,28 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
92
101
|
end
|
93
102
|
end
|
94
103
|
|
95
|
-
|
96
|
-
|
104
|
+
without_partial_double_verification do
|
105
|
+
allow(Model).to receive(:rebuild_pg_search_documents)
|
106
|
+
rebuilder.rebuild
|
107
|
+
expect(Model).not_to have_received(:rebuild_pg_search_documents)
|
108
|
+
end
|
97
109
|
end
|
98
110
|
|
99
|
-
|
111
|
+
# rubocop:disable RSpec/ExampleLength
|
112
|
+
it "executes the default SQL" do
|
100
113
|
time = Time.utc(2001, 1, 1, 0, 0, 0)
|
101
|
-
rebuilder =
|
102
|
-
|
103
|
-
expected_sql =
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
114
|
+
rebuilder = described_class.new(Model, -> { time })
|
115
|
+
|
116
|
+
expected_sql = <<~SQL.squish
|
117
|
+
INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
|
118
|
+
SELECT 'Model' AS searchable_type,
|
119
|
+
#{Model.quoted_table_name}.#{Model.primary_key} AS searchable_id,
|
120
|
+
(
|
121
|
+
coalesce(#{Model.quoted_table_name}."name"::text, '')
|
122
|
+
) AS content,
|
123
|
+
'2001-01-01 00:00:00' AS created_at,
|
124
|
+
'2001-01-01 00:00:00' AS updated_at
|
125
|
+
FROM #{Model.quoted_table_name}
|
113
126
|
SQL
|
114
127
|
|
115
128
|
executed_sql = []
|
@@ -124,8 +137,9 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
124
137
|
expect(executed_sql.length).to eq(1)
|
125
138
|
expect(executed_sql.first.strip).to eq(expected_sql.strip)
|
126
139
|
end
|
140
|
+
# rubocop:enable RSpec/ExampleLength
|
127
141
|
|
128
|
-
context "
|
142
|
+
context "with a model with a camel case column" do
|
129
143
|
with_model :ModelWithCamelCaseColumn do
|
130
144
|
table do |t|
|
131
145
|
t.string :camelName
|
@@ -139,12 +153,12 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
139
153
|
|
140
154
|
it "creates search document without PG error" do
|
141
155
|
time = Time.utc(2001, 1, 1, 0, 0, 0)
|
142
|
-
rebuilder =
|
156
|
+
rebuilder = described_class.new(Model, -> { time })
|
143
157
|
rebuilder.rebuild
|
144
158
|
end
|
145
159
|
end
|
146
160
|
|
147
|
-
context "
|
161
|
+
context "with a model with a non-standard primary key" do
|
148
162
|
with_model :ModelWithNonStandardPrimaryKey do
|
149
163
|
table primary_key: :non_standard_primary_key do |t|
|
150
164
|
t.string :name
|
@@ -156,20 +170,21 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
156
170
|
end
|
157
171
|
end
|
158
172
|
|
173
|
+
# rubocop:disable RSpec/ExampleLength
|
159
174
|
it "generates SQL with the correct primary key" do
|
160
175
|
time = Time.utc(2001, 1, 1, 0, 0, 0)
|
161
|
-
rebuilder =
|
162
|
-
|
163
|
-
expected_sql =
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
176
|
+
rebuilder = described_class.new(ModelWithNonStandardPrimaryKey, -> { time })
|
177
|
+
|
178
|
+
expected_sql = <<~SQL.squish
|
179
|
+
INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
|
180
|
+
SELECT 'ModelWithNonStandardPrimaryKey' AS searchable_type,
|
181
|
+
#{ModelWithNonStandardPrimaryKey.quoted_table_name}.non_standard_primary_key AS searchable_id,
|
182
|
+
(
|
183
|
+
coalesce(#{ModelWithNonStandardPrimaryKey.quoted_table_name}."name"::text, '')
|
184
|
+
) AS content,
|
185
|
+
'2001-01-01 00:00:00' AS created_at,
|
186
|
+
'2001-01-01 00:00:00' AS updated_at
|
187
|
+
FROM #{ModelWithNonStandardPrimaryKey.quoted_table_name}
|
173
188
|
SQL
|
174
189
|
|
175
190
|
executed_sql = []
|
@@ -184,6 +199,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
184
199
|
expect(executed_sql.length).to eq(1)
|
185
200
|
expect(executed_sql.first.strip).to eq(expected_sql.strip)
|
186
201
|
end
|
202
|
+
# rubocop:enable RSpec/ExampleLength
|
187
203
|
end
|
188
204
|
end
|
189
205
|
|
@@ -202,10 +218,11 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
202
218
|
end
|
203
219
|
end
|
204
220
|
|
221
|
+
# rubocop:disable RSpec/ExampleLength
|
205
222
|
it "calls update_pg_search_document on each record" do
|
206
223
|
record = Model.create!
|
207
224
|
|
208
|
-
rebuilder =
|
225
|
+
rebuilder = described_class.new(Model)
|
209
226
|
|
210
227
|
# stub respond_to? to return false since should_not_receive defines the method
|
211
228
|
original_respond_to = Model.method(:respond_to?)
|
@@ -216,12 +233,18 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
216
233
|
original_respond_to.call(method_name, *args)
|
217
234
|
end
|
218
235
|
end
|
219
|
-
expect(Model).not_to receive(:rebuild_pg_search_documents)
|
220
236
|
|
221
|
-
|
237
|
+
without_partial_double_verification do
|
238
|
+
allow(Model).to receive(:rebuild_pg_search_documents)
|
239
|
+
|
240
|
+
rebuilder.rebuild
|
241
|
+
|
242
|
+
expect(Model).not_to have_received(:rebuild_pg_search_documents)
|
243
|
+
end
|
222
244
|
|
223
245
|
expect(record.pg_search_document).to be_present
|
224
246
|
end
|
247
|
+
# rubocop:enable RSpec/ExampleLength
|
225
248
|
end
|
226
249
|
|
227
250
|
context "when only additional_attributes is set" do
|
@@ -243,7 +266,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
243
266
|
|
244
267
|
PgSearch::Document.delete_all
|
245
268
|
|
246
|
-
rebuilder =
|
269
|
+
rebuilder = described_class.new(Model)
|
247
270
|
rebuilder.rebuild
|
248
271
|
|
249
272
|
expect(record_1.reload.pg_search_document.additional_attribute_column).to eq("Model::1")
|
@@ -252,7 +275,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
252
275
|
end
|
253
276
|
end
|
254
277
|
|
255
|
-
context "
|
278
|
+
context "when multisearchable is conditional" do
|
256
279
|
context "via :if" do
|
257
280
|
with_model :Model do
|
258
281
|
table do |t|
|
@@ -265,11 +288,12 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
265
288
|
end
|
266
289
|
end
|
267
290
|
|
291
|
+
# rubocop:disable RSpec/ExampleLength
|
268
292
|
it "calls update_pg_search_document on each record" do
|
269
293
|
record_1 = Model.create!(active: true)
|
270
294
|
record_2 = Model.create!(active: false)
|
271
295
|
|
272
|
-
rebuilder =
|
296
|
+
rebuilder = described_class.new(Model)
|
273
297
|
|
274
298
|
# stub respond_to? to return false since should_not_receive defines the method
|
275
299
|
original_respond_to = Model.method(:respond_to?)
|
@@ -280,13 +304,17 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
280
304
|
original_respond_to.call(method_name, *args)
|
281
305
|
end
|
282
306
|
end
|
283
|
-
expect(Model).not_to receive(:rebuild_pg_search_documents)
|
284
307
|
|
285
|
-
|
308
|
+
without_partial_double_verification do
|
309
|
+
allow(Model).to receive(:rebuild_pg_search_documents)
|
310
|
+
rebuilder.rebuild
|
311
|
+
expect(Model).not_to have_received(:rebuild_pg_search_documents)
|
312
|
+
end
|
286
313
|
|
287
314
|
expect(record_1.pg_search_document).to be_present
|
288
315
|
expect(record_2.pg_search_document).not_to be_present
|
289
316
|
end
|
317
|
+
# rubocop:enable RSpec/ExampleLength
|
290
318
|
end
|
291
319
|
|
292
320
|
context "via :unless" do
|
@@ -301,11 +329,12 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
301
329
|
end
|
302
330
|
end
|
303
331
|
|
332
|
+
# rubocop:disable RSpec/ExampleLength
|
304
333
|
it "calls update_pg_search_document on each record" do
|
305
334
|
record_1 = Model.create!(inactive: true)
|
306
335
|
record_2 = Model.create!(inactive: false)
|
307
336
|
|
308
|
-
rebuilder =
|
337
|
+
rebuilder = described_class.new(Model)
|
309
338
|
|
310
339
|
# stub respond_to? to return false since should_not_receive defines the method
|
311
340
|
original_respond_to = Model.method(:respond_to?)
|
@@ -316,15 +345,20 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
316
345
|
original_respond_to.call(method_name, *args)
|
317
346
|
end
|
318
347
|
end
|
319
|
-
expect(Model).not_to receive(:rebuild_pg_search_documents)
|
320
348
|
|
321
|
-
|
349
|
+
without_partial_double_verification do
|
350
|
+
allow(Model).to receive(:rebuild_pg_search_documents)
|
351
|
+
rebuilder.rebuild
|
352
|
+
expect(Model).not_to have_received(:rebuild_pg_search_documents)
|
353
|
+
end
|
322
354
|
|
323
355
|
expect(record_1.pg_search_document).not_to be_present
|
324
356
|
expect(record_2.pg_search_document).to be_present
|
325
357
|
end
|
358
|
+
# rubocop:enable RSpec/ExampleLength
|
326
359
|
end
|
327
360
|
end
|
328
361
|
end
|
329
362
|
end
|
330
363
|
end
|
364
|
+
# rubocop:enable RSpec/NestedGroups
|