forked from hackorum-dev/hackorum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_parser_spec.rb
More file actions
345 lines (301 loc) · 12.5 KB
/
query_parser_spec.rb
File metadata and controls
345 lines (301 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Search::QueryParser, type: :service do
let(:parser) { described_class.new }
describe '#parse' do
context 'with simple text' do
it 'parses a single word' do
result = parser.parse('postgresql')
expect(result[:type]).to eq(:text)
expect(result[:value]).to eq('postgresql')
expect(result[:negated]).to be false
end
it 'parses bracketed text as plain text' do
result = parser.parse('[proposal] plan')
expect(result[:type]).to eq(:and)
expect(result[:children].size).to eq(2)
expect(result[:children][0][:type]).to eq(:text)
expect(result[:children][0][:value]).to eq('[proposal]')
expect(result[:children][1][:value]).to eq('plan')
end
it 'parses multiple words as implicit AND' do
result = parser.parse('postgresql vacuum')
expect(result[:type]).to eq(:and)
expect(result[:children].size).to eq(2)
expect(result[:children][0][:value]).to eq('postgresql')
expect(result[:children][1][:value]).to eq('vacuum')
end
it 'parses quoted text' do
result = parser.parse('"query planning"')
expect(result[:type]).to eq(:text)
expect(result[:value]).to eq('query planning')
expect(result[:quoted]).to be true
end
end
context 'with selectors' do
it 'parses from: selector' do
result = parser.parse('from:john')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:from)
expect(result[:value]).to eq('john')
expect(result[:negated]).to be false
end
it 'parses title: selector with quoted value' do
result = parser.parse('title:"query planning"')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:title)
expect(result[:value]).to eq('query planning')
expect(result[:quoted]).to be true
end
it 'parses all date selectors' do
%w[first_after first_before messages_after messages_before last_after last_before].each do |selector|
result = parser.parse("#{selector}:2024-01-01")
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(selector.to_sym)
end
end
it 'parses all state selectors' do
%w[unread read reading new starred notes].each do |selector|
result = parser.parse("#{selector}:me")
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(selector.to_sym)
expect(result[:value]).to eq('me')
end
end
it 'parses count selectors' do
%w[messages participants contributors].each do |selector|
result = parser.parse("#{selector}:>10")
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(selector.to_sym)
expect(result[:value]).to eq('>10')
end
end
it 'parses has: selector' do
result = parser.parse('has:attachment')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:has)
expect(result[:value]).to eq('attachment')
end
it 'parses tag: selector' do
result = parser.parse('tag:review')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:tag)
expect(result[:value]).to eq('review')
end
it 'parses tag: selector with from: condition' do
result = parser.parse('tag:review[from:me]')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:tag)
expect(result[:value]).to eq('review')
expect(result[:conditions].size).to eq(1)
expect(result[:conditions][0][:key]).to eq(:from)
expect(result[:conditions][0][:value]).to eq('me')
end
it 'parses tag: selector with empty value and from: condition' do
result = parser.parse('tag:[from:me]')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:tag)
expect(result[:value]).to eq('')
expect(result[:conditions].size).to eq(1)
end
end
context 'with negation' do
it 'parses negated text' do
result = parser.parse('-spam')
expect(result[:type]).to eq(:text)
expect(result[:negated]).to be true
expect(result[:value]).to eq('spam')
end
it 'parses negated selector' do
result = parser.parse('-from:john')
expect(result[:type]).to eq(:selector)
expect(result[:negated]).to be true
expect(result[:key]).to eq(:from)
expect(result[:value]).to eq('john')
end
it 'parses negated has: selector' do
result = parser.parse('-has:contributor')
expect(result[:type]).to eq(:selector)
expect(result[:negated]).to be true
expect(result[:key]).to eq(:has)
expect(result[:value]).to eq('contributor')
end
end
context 'with boolean operators' do
it 'parses explicit OR' do
result = parser.parse('from:john OR from:jane')
expect(result[:type]).to eq(:or)
expect(result[:children].size).to eq(2)
expect(result[:children][0][:value]).to eq('john')
expect(result[:children][1][:value]).to eq('jane')
end
it 'parses case-insensitive OR' do
result = parser.parse('from:john or from:jane')
expect(result[:type]).to eq(:or)
end
it 'parses mixed-case OR' do
result = parser.parse('from:john Or from:jane')
expect(result[:type]).to eq(:or)
end
it 'parses mixed-case AND' do
result = parser.parse('from:john And unread:me')
expect(result[:type]).to eq(:and)
expect(result[:children].size).to eq(2)
end
it 'parses explicit AND' do
result = parser.parse('from:john AND unread:me')
expect(result[:type]).to eq(:and)
expect(result[:children].size).to eq(2)
end
it 'handles operator precedence (AND binds tighter than OR)' do
result = parser.parse('from:john unread:me OR from:jane')
expect(result[:type]).to eq(:or)
# First child should be the AND of john and unread:me
expect(result[:children][0][:type]).to eq(:and)
# Second child should be jane
expect(result[:children][1][:value]).to eq('jane')
end
end
context 'with parentheses' do
it 'parses grouped expression' do
result = parser.parse('(from:john OR from:jane) unread:me')
expect(result[:type]).to eq(:and)
expect(result[:children][0][:type]).to eq(:or)
expect(result[:children][1][:key]).to eq(:unread)
end
it 'parses negated grouped expression' do
result = parser.parse('-(from:john OR from:jane)')
expect(result[:type]).to eq(:or)
expect(result[:negated]).to be true
end
end
context 'with complex queries' do
it 'parses complex query with multiple selectors' do
result = parser.parse('from:john title:"postgresql" unread:me')
expect(result[:type]).to eq(:and)
expect(result[:children].size).to eq(3)
end
it 'parses query with mixed text and selectors' do
result = parser.parse('postgresql from:john vacuum')
expect(result[:type]).to eq(:and)
expect(result[:children].size).to eq(3)
end
end
context 'with dependent conditions (bracket notation)' do
it 'parses from: selector with single condition' do
result = parser.parse('from:bruce[messages:>=10]')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:from)
expect(result[:value]).to eq('bruce')
expect(result[:conditions]).to be_an(Array)
expect(result[:conditions].size).to eq(1)
expect(result[:conditions][0][:key]).to eq(:messages)
expect(result[:conditions][0][:value]).to eq('>=10')
end
it 'parses from: selector with multiple conditions' do
result = parser.parse('from:bruce[messages:>=10, last_before:1m]')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:from)
expect(result[:conditions].size).to eq(2)
expect(result[:conditions][0][:key]).to eq(:messages)
expect(result[:conditions][0][:value]).to eq('>=10')
expect(result[:conditions][1][:key]).to eq(:last_before)
expect(result[:conditions][1][:value]).to eq('1m')
end
it 'parses from: selector with body condition' do
result = parser.parse('from:bruce[body:"patch"]')
expect(result[:conditions].size).to eq(1)
expect(result[:conditions][0][:key]).to eq(:body)
expect(result[:conditions][0][:value]).to eq('patch')
expect(result[:conditions][0][:quoted]).to be true
end
it 'parses has:attachment with conditions' do
result = parser.parse('has:attachment[from:bruce,count:>=3]')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:has)
expect(result[:value]).to eq('attachment')
expect(result[:conditions].size).to eq(2)
expect(result[:conditions][0][:key]).to eq(:from)
expect(result[:conditions][0][:value]).to eq('bruce')
expect(result[:conditions][1][:key]).to eq(:count)
expect(result[:conditions][1][:value]).to eq('>=3')
end
it 'parses tag: selector with from condition' do
result = parser.parse('tag:important[from:me]')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:tag)
expect(result[:value]).to eq('important')
expect(result[:conditions].size).to eq(1)
expect(result[:conditions][0][:key]).to eq(:from)
expect(result[:conditions][0][:value]).to eq('me')
end
it 'parses tag: selector with empty value and condition' do
result = parser.parse('tag:[from:teamname]')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:tag)
expect(result[:value]).to eq('')
expect(result[:conditions].size).to eq(1)
expect(result[:conditions][0][:key]).to eq(:from)
end
it 'parses negated selector with conditions' do
result = parser.parse('-from:bruce[messages:>=10]')
expect(result[:type]).to eq(:selector)
expect(result[:negated]).to be true
expect(result[:key]).to eq(:from)
expect(result[:value]).to eq('bruce')
expect(result[:conditions].size).to eq(1)
end
it 'parses selector without conditions' do
result = parser.parse('from:bruce')
expect(result[:conditions]).to be_nil
end
it 'handles whitespace in condition list' do
result = parser.parse('from:bruce[ messages:>=10 , last_before:1m ]')
expect(result[:conditions].size).to eq(2)
expect(result[:conditions][0][:key]).to eq(:messages)
expect(result[:conditions][1][:key]).to eq(:last_before)
end
it 'parses has:patch with conditions' do
result = parser.parse('has:patch[from:tom,count:>=2]')
expect(result[:key]).to eq(:has)
expect(result[:value]).to eq('patch')
expect(result[:conditions].size).to eq(2)
end
it 'parses tag with added_before condition' do
result = parser.parse('tag:review[added_before:1w]')
expect(result[:conditions].size).to eq(1)
expect(result[:conditions][0][:key]).to eq(:added_before)
expect(result[:conditions][0][:value]).to eq('1w')
end
end
context 'with edge cases' do
it 'returns nil for blank query' do
expect(parser.parse('')).to be_nil
expect(parser.parse(' ')).to be_nil
expect(parser.parse(nil)).to be_nil
end
it 'handles selector with empty value' do
result = parser.parse('from:')
expect(result[:type]).to eq(:selector)
expect(result[:key]).to eq(:from)
expect(result[:value]).to eq('')
end
it 'handles emails in from selector' do
result = parser.parse('from:john@example.com')
expect(result[:type]).to eq(:selector)
expect(result[:value]).to eq('john@example.com')
end
end
end
describe '#valid?' do
it 'returns true for valid queries' do
expect(parser.valid?('from:john')).to be true
expect(parser.valid?('postgresql vacuum')).to be true
expect(parser.valid?('(from:john OR from:jane)')).to be true
end
it 'returns false for invalid syntax' do
expect(parser.valid?('((broken')).to be false
# Note: 'from:john OR OR' is valid - the second OR is parsed as plain text
end
end
end