forked from prismicio-community/ruby-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredicates_spec.rb
More file actions
335 lines (301 loc) · 12.5 KB
/
predicates_spec.rb
File metadata and controls
335 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
# encoding: utf-8
require 'spec_helper'
Predicates = Prismic::Predicates
describe 'predicates' do
before do
@api = Prismic.api('https://micro.prismic.io/api', nil)
@master_ref = @api.master_ref
@link_resolver = Prismic.link_resolver('master'){|doc_link| "http://localhost/#{doc_link.id}" }
@boolean_api = Prismic.api("https://a-bool.prismic.io/api", nil)
@boolean_api_master_ref = @boolean_api.master_ref
end
describe 'boolean query' do
it "can query a boolean field with value true" do
form = @boolean_api.form('everything').query(['at', 'my.bools.set_to_true', true])
form.data['q'].should == ['[[:d = at(my.bools.set_to_true, true)]]']
end
it "can query a boolean field with value false" do
form = @boolean_api.form('everything').query(['at', 'my.bools.set_to_true', false])
form.data['q'].should == ['[[:d = at(my.bools.set_to_true, false)]]']
end
end
describe 'at predicate' do
it 'as an array serializes well' do
form = @api.form('everything').query(['at', 'document.id', 'UrjI1gEAALOCeO5i'])
form.data['q'].should == ['[[:d = at(document.id, "UrjI1gEAALOCeO5i")]]']
end
it 'with helper serializes well' do
form = @api.form('everything').query(Predicates.at('document.id', 'UrjI1gEAALOCeO5i'))
form.data['q'].should == ['[[:d = at(document.id, "UrjI1gEAALOCeO5i")]]']
end
it 'allows to reuse predicates', focus: true do
predicate = Predicates.at('document.id', 'UrjI1gEAALOCeO5i')
form = @api.form('everything').query(predicate)
form.data['q'].should == ['[[:d = at(document.id, "UrjI1gEAALOCeO5i")]]']
form2 = @api.form('everything').query(predicate)
form2.data['q'].should == ['[[:d = at(document.id, "UrjI1gEAALOCeO5i")]]']
end
end
describe 'not predicate' do
it 'as an array serializes well' do
form = @api.form('everything').query(['not', 'document.id', 'UrjI1gEAALOCeO5i'])
form.data['q'].should == ['[[:d = not(document.id, "UrjI1gEAALOCeO5i")]]']
end
it 'with helper serializes well' do
form = @api.form('everything').query(Predicates.not('document.id', 'UrjI1gEAALOCeO5i'))
form.data['q'].should == ['[[:d = not(document.id, "UrjI1gEAALOCeO5i")]]']
end
end
describe 'any predicate' do
it 'with helper serializes well' do
form = @api.form('everything').query(Predicates.any('document.type', ['article', 'blog-post']))
form.data['q'].should == ['[[:d = any(document.type, ["article", "blog-post"])]]']
end
end
describe 'fulltext predicate' do
it 'with helper serializes well' do
form = @api.form('everything').query(Predicates.fulltext('document.type', ['article', 'blog-post']))
form.data['q'].should == ['[[:d = fulltext(document.type, ["article", "blog-post"])]]']
end
end
describe 'similar predicate' do
it 'as an array serializes well' do
form = @api.form('everything').query(['similar', 'idOfSomeDocument', 10])
form.data['q'].should == ['[[:d = similar("idOfSomeDocument", 10)]]']
end
it 'with helpers serializes well' do
form = @api.form('everything').query(Predicates.similar('idOfSomeDocument', 10))
form.data['q'].should == ['[[:d = similar("idOfSomeDocument", 10)]]']
end
end
describe 'has predicate' do
it 'as an array serializes well' do
form = @api.form('everything').query(['has', 'my.blog-post.author'])
form.data['q'].should == ['[[:d = has(my.blog-post.author)]]']
end
it 'with helpers serializes well' do
form = @api.form('everything').query(Predicates.has('my.blog-post.author'))
form.data['q'].should == ['[[:d = has(my.blog-post.author)]]']
end
end
describe 'missing predicate' do
it 'as an array serializes well' do
form = @api.form('everything').query(['missing', 'my.blog-post.author'])
form.data['q'].should == ['[[:d = missing(my.blog-post.author)]]']
end
it 'with helpers serializes well' do
form = @api.form('everything').query(Predicates.missing('my.blog-post.author'))
form.data['q'].should == ['[[:d = missing(my.blog-post.author)]]']
end
end
describe 'multiple predicates' do
it 'as an array serializes well' do
form = @api.form('everything').query(
['date.month-after', 'my.blog-post.publication-date', 4],
['date.month-before', 'my.blog-post.publication-date', 'December']
)
form.data['q'].should == ['[[:d = date.month-after(my.blog-post.publication-date, 4)][:d = date.month-before(my.blog-post.publication-date, "December")]]']
end
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.month_after('my.blog-post.publication-date', 4),
Predicates.month_before('my.blog-post.publication-date', 'December')
)
form.data['q'].should == ['[[:d = date.month-after(my.blog-post.publication-date, 4)][:d = date.month-before(my.blog-post.publication-date, "December")]]']
end
end
describe 'number GT' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.gt('my.blog-post.publication-date', 4)
)
form.data['q'].should == ['[[:d = number.gt(my.blog-post.publication-date, 4)]]']
end
end
describe 'number LT' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.lt('my.blog-post.publication-date', 4)
)
form.data['q'].should == ['[[:d = number.lt(my.blog-post.publication-date, 4)]]']
end
end
describe 'number in range' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.in_range('my.product.price', 2, 4.5)
)
form.data['q'].should == ['[[:d = number.inRange(my.product.price, 2, 4.5)]]']
end
end
describe 'date is before' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.date_before('my.blog-post.publication-date', Date.parse('2016-03-02') )
)
form.data['q'].should == ["[[:d = date.before(my.blog-post.publication-date, #{Date.parse('2016-03-02').to_time.to_i * 1000})]]"]
end
end
describe 'date is after' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.date_after('my.blog-post.publication-date', Date.parse('2016-03-02') )
)
form.data['q'].should == ["[[:d = date.after(my.blog-post.publication-date, #{Date.parse('2016-03-02').to_time.to_i * 1000})]]"]
end
end
describe 'date is between' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.date_between('my.blog-post.publication-date', Date.parse('2016-03-02'), Date.parse('2016-03-04') )
)
form.data['q'].should == ["[[:d = date.between(my.blog-post.publication-date, #{Date.parse('2016-03-02').to_time.to_i * 1000}, #{Date.parse('2016-03-04').to_time.to_i * 1000})]]"]
end
end
describe 'day of month' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.day_of_month('my.blog-post.publication-date', 10)
)
form.data['q'].should == ['[[:d = date.day-of-month(my.blog-post.publication-date, 10)]]']
end
end
describe 'day of month after' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.day_of_month_after('my.blog-post.publication-date', 10)
)
form.data['q'].should == ['[[:d = date.day-of-month-after(my.blog-post.publication-date, 10)]]']
end
end
describe 'day of month before' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.day_of_month_before('my.blog-post.publication-date', 10)
)
form.data['q'].should == ['[[:d = date.day-of-month-before(my.blog-post.publication-date, 10)]]']
end
end
describe 'day of week' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.day_of_week('my.blog-post.publication-date', 7)
)
form.data['q'].should == ['[[:d = date.day-of-week(my.blog-post.publication-date, 7)]]']
end
end
describe 'day of week after' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.day_of_week_after('my.blog-post.publication-date', 7)
)
form.data['q'].should == ['[[:d = date.day-of-week-after(my.blog-post.publication-date, 7)]]']
end
end
describe 'day of week before' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.day_of_week_before('my.blog-post.publication-date', 7)
)
form.data['q'].should == ['[[:d = date.day-of-week-before(my.blog-post.publication-date, 7)]]']
end
end
describe 'month' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.month('my.blog-post.publication-date', 5)
)
form.data['q'].should == ['[[:d = date.month(my.blog-post.publication-date, 5)]]']
end
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.month('my.blog-post.publication-date', 'May')
)
form.data['q'].should == ['[[:d = date.month(my.blog-post.publication-date, "May")]]']
end
end
describe 'month after' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.month_after('my.blog-post.publication-date', 4)
)
form.data['q'].should == ['[[:d = date.month-after(my.blog-post.publication-date, 4)]]']
end
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.month_after('my.blog-post.publication-date', 'April')
)
form.data['q'].should == ['[[:d = date.month-after(my.blog-post.publication-date, "April")]]']
end
end
describe 'month before' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.month_before('my.blog-post.publication-date', 12)
)
form.data['q'].should == ['[[:d = date.month-before(my.blog-post.publication-date, 12)]]']
end
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.month_before('my.blog-post.publication-date', 'December')
)
form.data['q'].should == ['[[:d = date.month-before(my.blog-post.publication-date, "December")]]']
end
end
describe 'year' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.year('my.blog-post.publication-date', 2013)
)
form.data['q'].should == ['[[:d = date.year(my.blog-post.publication-date, 2013)]]']
end
end
describe 'year before' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.year_before('my.blog-post.publication-date', 2013)
)
form.data['q'].should == ['[[:d = date.year-before(my.blog-post.publication-date, 2013)]]']
end
end
describe 'year after' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.year_after('my.blog-post.publication-date', 2011)
)
form.data['q'].should == ['[[:d = date.year-after(my.blog-post.publication-date, 2011)]]']
end
end
describe 'hour' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.hour('my.blog-post.publication-date', 2)
)
form.data['q'].should == ['[[:d = date.hour(my.blog-post.publication-date, 2)]]']
end
end
describe 'hour before' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.hour_before('my.blog-post.publication-date', 12)
)
form.data['q'].should == ['[[:d = date.hour-before(my.blog-post.publication-date, 12)]]']
end
end
describe 'hour after' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.hour_after('my.blog-post.publication-date', 17)
)
form.data['q'].should == ['[[:d = date.hour-after(my.blog-post.publication-date, 17)]]']
end
end
describe 'geopoint near' do
it 'with helpers serializes well' do
form = @api.form('everything').query(
Predicates.near('my.store.coordinates', 40.689757, -74.0451453, 15)
)
form.data['q'].should == ['[[:d = geopoint.near(my.store.coordinates, 40.689757, -74.0451453, 15)]]']
end
end
end