-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest__gql.py
More file actions
731 lines (608 loc) · 24.8 KB
/
Copy pathtest__gql.py
File metadata and controls
731 lines (608 loc) · 24.8 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import pytest
from google.cloud.ndb import _gql as gql_module
from google.cloud.ndb import exceptions, key, model
from google.cloud.ndb import query as query_module
GQL_QUERY = """
SELECT prop1, prop2 FROM SomeKind WHERE prop3>5 and prop2='xxx'
ORDER BY prop4, prop1 DESC LIMIT 10 OFFSET 5 HINT ORDER_FIRST
"""
class TestLiteral:
@staticmethod
def test_constructor():
literal = gql_module.Literal("abc")
assert literal.__dict__ == {"_value": "abc"}
@staticmethod
def test_Get():
literal = gql_module.Literal("abc")
assert literal.Get() == "abc"
@staticmethod
def test___repr__():
literal = gql_module.Literal("abc")
assert literal.__repr__() == "Literal('abc')"
@staticmethod
def test___eq__():
literal = gql_module.Literal("abc")
literal2 = gql_module.Literal("abc")
literal3 = gql_module.Literal("xyz")
assert literal.__eq__(literal2) is True
assert literal.__eq__(literal3) is False
assert literal.__eq__(42) is NotImplemented
class TestGQL:
@staticmethod
def test_constructor():
gql = gql_module.GQL(GQL_QUERY)
assert gql.kind() == "SomeKind"
@staticmethod
def test_constructor_with_namespace():
gql = gql_module.GQL(GQL_QUERY, namespace="test-namespace")
assert gql._namespace == "test-namespace"
@staticmethod
def test_constructor_bad_query():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("BAD, BAD QUERY")
@staticmethod
def test_constructor_incomplete_query():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT")
@staticmethod
def test_constructor_extra_query():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind; END")
@staticmethod
def test_constructor_empty_where():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE")
@staticmethod
def test_constructor_empty_where_condition():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE")
@staticmethod
def test_constructor_bad_where_condition():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE WE_ARE")
@staticmethod
def test_constructor_reserved_where_identifier():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE WHERE")
@staticmethod
def test_constructor_empty_where_condition_value():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=")
@staticmethod
def test_filters():
Literal = gql_module.Literal
gql = gql_module.GQL(GQL_QUERY)
assert gql.filters() == {
("prop2", "="): [("nop", [Literal("xxx")])],
("prop3", ">"): [("nop", [Literal(5)])],
}
@staticmethod
def test_hint():
gql = gql_module.GQL("SELECT * FROM SomeKind HINT ORDER_FIRST")
assert gql.hint() == "ORDER_FIRST"
gql = gql_module.GQL("SELECT * FROM SomeKind HINT FILTER_FIRST")
assert gql.hint() == "FILTER_FIRST"
gql = gql_module.GQL("SELECT * FROM SomeKind HINT ANCESTOR_FIRST")
assert gql.hint() == "ANCESTOR_FIRST"
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind HINT TAKE_THE_HINT")
@staticmethod
def test_limit():
gql = gql_module.GQL("SELECT * FROM SomeKind LIMIT 10")
assert gql.limit() == 10
gql = gql_module.GQL("SELECT * FROM SomeKind LIMIT 10, 5")
assert gql.limit() == 5
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind LIMIT 0")
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind LIMIT -1")
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind LIMIT -1, 10")
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind LIMIT THE_SKY")
@staticmethod
def test_offset():
gql = gql_module.GQL("SELECT * FROM SomeKind")
assert gql.offset() == 0
gql = gql_module.GQL("SELECT * FROM SomeKind OFFSET 10")
assert gql.offset() == 10
gql = gql_module.GQL("SELECT * FROM SomeKind LIMIT 10, 5")
assert gql.offset() == 10
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind OFFSET -1")
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind LIMIT 5, 10 OFFSET 8")
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind OFFSET ZERO")
@staticmethod
def test_orderings():
gql = gql_module.GQL(GQL_QUERY)
assert gql.orderings() == [("prop4", 1), ("prop1", 2)]
@staticmethod
def test_is_keys_only():
gql = gql_module.GQL(GQL_QUERY)
assert gql.is_keys_only() is False
gql = gql_module.GQL("SELECT __key__ from SomeKind")
assert gql.is_keys_only() is True
@staticmethod
def test_projection():
gql = gql_module.GQL(GQL_QUERY)
assert gql.projection() == ("prop1", "prop2")
@staticmethod
def test_is_distinct():
gql = gql_module.GQL(GQL_QUERY)
assert gql.is_distinct() is False
gql = gql_module.GQL("SELECT DISTINCT prop1 from SomeKind")
assert gql.is_distinct() is True
@staticmethod
def test_kind():
gql = gql_module.GQL(GQL_QUERY)
assert gql.kind() == "SomeKind"
assert gql._entity == "SomeKind"
@staticmethod
def test_cast():
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=user('js')")
assert gql.filters() == {("prop1", "="): [("user", [gql_module.Literal("js")])]}
@staticmethod
def test_in_list():
Literal = gql_module.Literal
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1 IN (1, 2, 3)")
assert gql.filters() == {
("prop1", "IN"): [("list", [Literal(1), Literal(2), Literal(3)])]
}
@staticmethod
def test_not_in_list():
Literal = gql_module.Literal
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1 NOT IN (1, 2, 3)")
assert gql.filters() == {
("prop1", "NOT_IN"): [("list", [Literal(1), Literal(2), Literal(3)])]
}
@staticmethod
def test_cast_list_no_in():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=(1, 2, 3)")
@staticmethod
def test_not_without_in():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE prop1 NOT=1")
@staticmethod
def test_reference():
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=:ref")
assert gql.filters() == {("prop1", "="): [("nop", ["ref"])]}
@staticmethod
def test_ancestor_is():
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE ANCESTOR IS 'AnyKind'")
assert gql.filters() == {(-1, "is"): [("nop", [gql_module.Literal("AnyKind")])]}
@staticmethod
def test_ancestor_multiple_ancestors():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL(
(
"SELECT * FROM SomeKind WHERE ANCESTOR IS 'AnyKind' AND "
"ANCESTOR IS 'OtherKind'"
)
)
@staticmethod
def test_ancestor_no_is():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE ANCESTOR='OtherKind'")
@staticmethod
def test_is_no_ancestor():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind WHERE prop1 IS 'OtherKind'")
@staticmethod
def test_func():
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=key(:1)")
assert gql.filters() == {("prop1", "="): [("key", [1])]}
@staticmethod
def test_null():
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=NULL")
assert gql.filters() == {("prop1", "="): [("nop", [gql_module.Literal(None)])]}
@staticmethod
def test_true():
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=TRUE")
assert gql.filters() == {("prop1", "="): [("nop", [gql_module.Literal(True)])]}
@staticmethod
def test_false():
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=FALSE")
assert gql.filters() == {("prop1", "="): [("nop", [gql_module.Literal(False)])]}
@staticmethod
def test_float():
gql = gql_module.GQL("SELECT * FROM SomeKind WHERE prop1=3.14")
assert gql.filters() == {("prop1", "="): [("nop", [gql_module.Literal(3.14)])]}
@staticmethod
def test_quoted_identifier():
gql = gql_module.GQL('SELECT * FROM SomeKind WHERE "prop1"=3.14')
assert gql.filters() == {("prop1", "="): [("nop", [gql_module.Literal(3.14)])]}
@staticmethod
def test_order_by_ascending():
gql = gql_module.GQL("SELECT * FROM SomeKind ORDER BY prop1 ASC")
assert gql.orderings() == [("prop1", 1)]
@staticmethod
def test_order_by_no_arg():
with pytest.raises(exceptions.BadQueryError):
gql_module.GQL("SELECT * FROM SomeKind ORDER BY")
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query():
class SomeKind(model.Model):
prop1 = model.StringProperty()
prop2 = model.StringProperty()
prop3 = model.IntegerProperty()
prop4 = model.IntegerProperty()
rep = (
"Query(namespace='test-namespace', kind='SomeKind', filters=AND(FilterNode('prop2', '=', {}"
"), FilterNode('prop3', '>', 5)), order_by=[PropertyOrder(name="
"'prop4', reverse=False), PropertyOrder(name='prop1', "
"reverse=True)], limit=10, offset=5, "
"projection=['prop1', 'prop2'])"
)
gql = gql_module.GQL(GQL_QUERY, namespace="test-namespace")
query = gql.get_query()
compat_rep = "'xxx'"
assert repr(query) == rep.format(compat_rep)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_distinct():
class SomeKind(model.Model):
prop1 = model.StringProperty()
gql = gql_module.GQL("SELECT DISTINCT prop1 FROM SomeKind")
query = gql.get_query()
assert query.distinct_on == ("prop1",)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_no_kind():
class SomeKind(model.Model):
prop1 = model.StringProperty()
gql = gql_module.GQL("SELECT *")
query = gql.get_query()
assert query.kind is None
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_in():
class SomeKind(model.Model):
prop1 = model.IntegerProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 IN (1, 2, 3)")
query = gql.get_query()
assert query.filters == query_module.OR(
query_module.FilterNode("prop1", "=", 1),
query_module.FilterNode("prop1", "=", 2),
query_module.FilterNode("prop1", "=", 3),
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_not_in():
class SomeKind(model.Model):
prop1 = model.IntegerProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 NOT IN (1, 2)")
query = gql.get_query()
assert query.filters == query_module.FilterNode("prop1", "not_in", [1, 2])
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_in_parameterized():
class SomeKind(model.Model):
prop1 = model.StringProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 IN (:1, :2, :3)")
query = gql.get_query()
assert "'in'," in str(query.filters)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_not_in_parameterized():
class SomeKind(model.Model):
prop1 = model.StringProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 NOT IN (:1, :2, :3)"
)
query = gql.get_query()
assert "'not_in'," in str(query.filters)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_keys_only():
class SomeKind(model.Model):
prop1 = model.StringProperty()
gql = gql_module.GQL("SELECT __key__ FROM SomeKind WHERE prop1='a'")
query = gql.get_query()
assert query.keys_only is True
assert "keys_only=True" in query.__repr__()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_date():
class SomeKind(model.Model):
prop1 = model.DateProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Date(2020, 3, 26)"
)
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", datetime.datetime(2020, 3, 26, 0, 0, 0)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_date_one_parameter():
class SomeKind(model.Model):
prop1 = model.DateProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Date('2020-03-26')"
)
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", datetime.datetime(2020, 3, 26, 0, 0, 0)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_date_parameterized():
class SomeKind(model.Model):
prop1 = model.DateProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = Date(:1)")
query = gql.get_query()
assert "'date'" in str(query.filters)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_date_one_parameter_bad_date():
class SomeKind(model.Model):
prop1 = model.DateProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Date('not a date')"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_date_one_parameter_bad_type():
class SomeKind(model.Model):
prop1 = model.DateProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = Date(42)")
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_date_too_many_values():
class SomeKind(model.Model):
prop1 = model.DateProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Date(1, 2, 3, 4)"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_date_bad_values():
class SomeKind(model.Model):
prop1 = model.DateProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Date(100, 200, 300)"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_datetime():
class SomeKind(model.Model):
prop1 = model.DateTimeProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = DateTime(2020, 3, 26,12, 45, 5)"
)
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", datetime.datetime(2020, 3, 26, 12, 45, 5)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_datetime_ome_parameter():
class SomeKind(model.Model):
prop1 = model.DateTimeProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = DateTime('2020-03-26 12:45:05')"
)
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", datetime.datetime(2020, 3, 26, 12, 45, 5)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_datetime_parameterized():
class SomeKind(model.Model):
prop1 = model.DateTimeProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = DateTime(:1)")
query = gql.get_query()
assert "'datetime'" in str(query.filters)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_datetime_one_parameter_bad_date():
class SomeKind(model.Model):
prop1 = model.DateTimeProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = DateTime('not a date')"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_datetime_one_parameter_bad_type():
class SomeKind(model.Model):
prop1 = model.DateTimeProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = DateTime(42)")
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_datetime_bad_values():
class SomeKind(model.Model):
prop1 = model.DateTimeProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = DateTime(100, 200, 300)"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_time():
class SomeKind(model.Model):
prop1 = model.TimeProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = Time(12, 45, 5)")
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", datetime.datetime(1970, 1, 1, 12, 45, 5)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_time_one_parameter():
class SomeKind(model.Model):
prop1 = model.TimeProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Time('12:45:05')"
)
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", datetime.datetime(1970, 1, 1, 12, 45, 5)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_time_one_parameter_int():
class SomeKind(model.Model):
prop1 = model.TimeProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = Time(12)")
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", datetime.datetime(1970, 1, 1, 12)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_time_parameterized():
class SomeKind(model.Model):
prop1 = model.TimeProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = Time(:1)")
query = gql.get_query()
assert "'time'" in str(query.filters)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_time_one_parameter_bad_time():
class SomeKind(model.Model):
prop1 = model.TimeProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Time('not a time')"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_time_one_parameter_bad_type():
class SomeKind(model.Model):
prop1 = model.TimeProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = Time(3.141592)")
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_time_too_many_values():
class SomeKind(model.Model):
prop1 = model.TimeProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Time(1, 2, 3, 4)"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_time_bad_values():
class SomeKind(model.Model):
prop1 = model.TimeProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Time(100, 200, 300)"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_geopt():
class SomeKind(model.Model):
prop1 = model.GeoPtProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = GeoPt(20.67, -100.32)"
)
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", model.GeoPt(20.67, -100.32)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_geopt_parameterized():
class SomeKind(model.Model):
prop1 = model.GeoPtProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = GeoPt(:1)")
query = gql.get_query()
assert "'geopt'" in str(query.filters)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_geopt_too_many_values():
class SomeKind(model.Model):
prop1 = model.GeoPtProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = GeoPt(20.67,-100.32, 1.5)"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_key():
class SomeKind(model.Model):
prop1 = model.KeyProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Key('parent', 'c', 'child', 42)"
)
query = gql.get_query()
assert query.filters == query_module.FilterNode(
"prop1", "=", key.Key("parent", "c", "child", 42)
)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_key_parameterized():
class SomeKind(model.Model):
prop1 = model.KeyProperty()
gql = gql_module.GQL("SELECT prop1 FROM SomeKind WHERE prop1 = Key(:1)")
query = gql.get_query()
assert "'key'" in str(query.filters)
@staticmethod
@pytest.mark.usefixtures("in_context")
def test_get_query_key_odd_values():
class SomeKind(model.Model):
prop1 = model.KeyProperty()
gql = gql_module.GQL(
"SELECT prop1 FROM SomeKind WHERE prop1 = Key(100, 200, 300)"
)
with pytest.raises(exceptions.BadQueryError):
gql.get_query()
class TestNotImplementedFUNCTIONS:
@staticmethod
def test_user():
with pytest.raises(NotImplementedError):
gql_module.FUNCTIONS["user"]("any arg")
@staticmethod
def test_nop():
with pytest.raises(NotImplementedError):
gql_module.FUNCTIONS["nop"]("any arg")
@staticmethod
def test_time_2_args_and_invalid():
import datetime
assert gql_module._time_function([10, 30]) == datetime.time(10, 30)
with pytest.raises(exceptions.BadQueryError):
gql_module._time_function([])
@staticmethod
def test_ancestor_condition_not_is():
gql = gql_module.GQL("SELECT * FROM Kind")
with pytest.raises(ValueError, match="condition must be 'is'"):
gql._AddProcessedParameterFilter("ancestor", "!=", "nop", ["param"])