2
2
from unittest import skipUnless
3
3
4
4
from django .conf import settings
5
- from django .db import connection
5
+ from django .db import NotSupportedError , connection
6
6
from django .db .models import (
7
7
CASCADE ,
8
8
CharField ,
@@ -405,9 +405,9 @@ def test_partial_index(self):
405
405
),
406
406
),
407
407
)
408
- self .assertIn (
409
- "WHERE %s" % editor . quote_name ( "pub_date" ) ,
410
- str ( index .create_sql (Article , schema_editor = editor ) ),
408
+ self .assertEqual (
409
+ { "pub_date" : { "$gt" : datetime . datetime ( 2015 , 1 , 1 , 6 , 0 )}} ,
410
+ index ._get_condition_mql (Article , schema_editor = editor ),
411
411
)
412
412
editor .add_index (index = index , model = Article )
413
413
with connection .cursor () as cursor :
@@ -424,12 +424,13 @@ def test_integer_restriction_partial(self):
424
424
with connection .schema_editor () as editor :
425
425
index = Index (
426
426
name = "recent_article_idx" ,
427
- fields = ["id" ],
427
+ # This is changed
428
+ fields = ["headline" ],
428
429
condition = Q (pk__gt = 1 ),
429
430
)
430
- self .assertIn (
431
- "WHERE %s" % editor . quote_name ( "id" ) ,
432
- str ( index .create_sql (Article , schema_editor = editor ) ),
431
+ self .assertEqual (
432
+ { "_id" : { "$gt" : 1 }} ,
433
+ index ._get_condition_mql (Article , schema_editor = editor ),
433
434
)
434
435
editor .add_index (index = index , model = Article )
435
436
with connection .cursor () as cursor :
@@ -449,9 +450,9 @@ def test_boolean_restriction_partial(self):
449
450
fields = ["published" ],
450
451
condition = Q (published = True ),
451
452
)
452
- self .assertIn (
453
- "WHERE %s" % editor . quote_name ( "published" ) ,
454
- str ( index .create_sql (Article , schema_editor = editor ) ),
453
+ self .assertEqual (
454
+ { "published" : { "$eq" : True }} ,
455
+ index ._get_condition_mql (Article , schema_editor = editor ),
455
456
)
456
457
editor .add_index (index = index , model = Article )
457
458
with connection .cursor () as cursor :
@@ -479,15 +480,24 @@ def test_multiple_conditions(self):
479
480
tzinfo = timezone .get_current_timezone (),
480
481
)
481
482
)
482
- & Q (headline__contains = "China" )
483
+ & Q (headline = "China" )
483
484
),
484
485
)
485
- sql = str (index .create_sql (Article , schema_editor = editor ))
486
- where = sql .find ("WHERE" )
487
- self .assertIn ("WHERE (%s" % editor .quote_name ("pub_date" ), sql )
486
+ sql = index ._get_condition_mql (Article , schema_editor = editor )
487
+ self .assertEqual (
488
+ sql ,
489
+ {
490
+ "$and" : [
491
+ {"pub_date" : {"$gt" : datetime .datetime (2015 , 1 , 1 , 6 , 0 )}},
492
+ {"headline" : {"$eq" : "China" }},
493
+ ]
494
+ },
495
+ )
496
+ # where = sql.find("WHERE")
497
+ # self.assertIn("WHERE (%s" % editor.quote_name("pub_date"), sql)
488
498
# Because each backend has different syntax for the operators,
489
499
# check ONLY the occurrence of headline in the SQL.
490
- self .assertGreater (sql .rfind ("headline" ), where )
500
+ # self.assertGreater(sql.rfind("headline"), where)
491
501
editor .add_index (index = index , model = Article )
492
502
with connection .cursor () as cursor :
493
503
self .assertIn (
@@ -500,26 +510,17 @@ def test_multiple_conditions(self):
500
510
editor .remove_index (index = index , model = Article )
501
511
502
512
def test_is_null_condition (self ):
503
- with connection .schema_editor () as editor :
504
- index = Index (
505
- name = "recent_article_idx" ,
506
- fields = ["pub_date" ],
507
- condition = Q (pub_date__isnull = False ),
508
- )
509
- self .assertIn (
510
- "WHERE %s IS NOT NULL" % editor .quote_name ("pub_date" ),
511
- str (index .create_sql (Article , schema_editor = editor )),
512
- )
513
- editor .add_index (index = index , model = Article )
514
- with connection .cursor () as cursor :
515
- self .assertIn (
516
- index .name ,
517
- connection .introspection .get_constraints (
518
- cursor = cursor ,
519
- table_name = Article ._meta .db_table ,
520
- ),
521
- )
522
- editor .remove_index (index = index , model = Article )
513
+ msg = "MongoDB does not support the 'isnull' lookup in indexes."
514
+ index = Index (
515
+ name = "recent_article_idx" ,
516
+ fields = ["pub_date" ],
517
+ condition = Q (pub_date__isnull = False ),
518
+ )
519
+ with (
520
+ self .assertRaisesMessage (NotSupportedError , msg ),
521
+ connection .schema_editor () as editor ,
522
+ ):
523
+ index ._get_condition_mql (Article , schema_editor = editor )
523
524
524
525
@skipUnlessDBFeature ("supports_expression_indexes" )
525
526
def test_partial_func_index (self ):
0 commit comments