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 CASCADE , CharField , ForeignKey , Index , Q
7
7
from django .db .models .functions import Lower
8
8
from django .test import (
@@ -398,9 +398,9 @@ def test_partial_index(self):
398
398
),
399
399
),
400
400
)
401
- self .assertIn (
402
- "WHERE %s" % editor . quote_name ( "pub_date" ) ,
403
- str ( index .create_sql (Article , schema_editor = editor ) ),
401
+ self .assertEqual (
402
+ { "pub_date" : { "$gt" : datetime . datetime ( 2015 , 1 , 1 , 6 , 0 )}} ,
403
+ index ._get_condition_mql (Article , schema_editor = editor ),
404
404
)
405
405
editor .add_index (index = index , model = Article )
406
406
with connection .cursor () as cursor :
@@ -417,12 +417,13 @@ def test_integer_restriction_partial(self):
417
417
with connection .schema_editor () as editor :
418
418
index = Index (
419
419
name = "recent_article_idx" ,
420
- fields = ["id" ],
420
+ # This is changed
421
+ fields = ["headline" ],
421
422
condition = Q (pk__gt = 1 ),
422
423
)
423
- self .assertIn (
424
- "WHERE %s" % editor . quote_name ( "id" ) ,
425
- str ( index .create_sql (Article , schema_editor = editor ) ),
424
+ self .assertEqual (
425
+ { "_id" : { "$gt" : 1 }} ,
426
+ index ._get_condition_mql (Article , schema_editor = editor ),
426
427
)
427
428
editor .add_index (index = index , model = Article )
428
429
with connection .cursor () as cursor :
@@ -442,9 +443,9 @@ def test_boolean_restriction_partial(self):
442
443
fields = ["published" ],
443
444
condition = Q (published = True ),
444
445
)
445
- self .assertIn (
446
- "WHERE %s" % editor . quote_name ( "published" ) ,
447
- str ( index .create_sql (Article , schema_editor = editor ) ),
446
+ self .assertEqual (
447
+ { "published" : { "$eq" : True }} ,
448
+ index ._get_condition_mql (Article , schema_editor = editor ),
448
449
)
449
450
editor .add_index (index = index , model = Article )
450
451
with connection .cursor () as cursor :
@@ -472,15 +473,24 @@ def test_multiple_conditions(self):
472
473
tzinfo = timezone .get_current_timezone (),
473
474
)
474
475
)
475
- & Q (headline__contains = "China" )
476
+ & Q (headline = "China" )
476
477
),
477
478
)
478
- sql = str (index .create_sql (Article , schema_editor = editor ))
479
- where = sql .find ("WHERE" )
480
- self .assertIn ("WHERE (%s" % editor .quote_name ("pub_date" ), sql )
479
+ sql = index ._get_condition_mql (Article , schema_editor = editor )
480
+ self .assertEqual (
481
+ sql ,
482
+ {
483
+ "$and" : [
484
+ {"pub_date" : {"$gt" : datetime .datetime (2015 , 1 , 1 , 6 , 0 )}},
485
+ {"headline" : {"$eq" : "China" }},
486
+ ]
487
+ },
488
+ )
489
+ # where = sql.find("WHERE")
490
+ # self.assertIn("WHERE (%s" % editor.quote_name("pub_date"), sql)
481
491
# Because each backend has different syntax for the operators,
482
492
# check ONLY the occurrence of headline in the SQL.
483
- self .assertGreater (sql .rfind ("headline" ), where )
493
+ # self.assertGreater(sql.rfind("headline"), where)
484
494
editor .add_index (index = index , model = Article )
485
495
with connection .cursor () as cursor :
486
496
self .assertIn (
@@ -493,26 +503,17 @@ def test_multiple_conditions(self):
493
503
editor .remove_index (index = index , model = Article )
494
504
495
505
def test_is_null_condition (self ):
496
- with connection .schema_editor () as editor :
497
- index = Index (
498
- name = "recent_article_idx" ,
499
- fields = ["pub_date" ],
500
- condition = Q (pub_date__isnull = False ),
501
- )
502
- self .assertIn (
503
- "WHERE %s IS NOT NULL" % editor .quote_name ("pub_date" ),
504
- str (index .create_sql (Article , schema_editor = editor )),
505
- )
506
- editor .add_index (index = index , model = Article )
507
- with connection .cursor () as cursor :
508
- self .assertIn (
509
- index .name ,
510
- connection .introspection .get_constraints (
511
- cursor = cursor ,
512
- table_name = Article ._meta .db_table ,
513
- ),
514
- )
515
- editor .remove_index (index = index , model = Article )
506
+ msg = "MongoDB does not support the 'isnull' lookup in indexes."
507
+ index = Index (
508
+ name = "recent_article_idx" ,
509
+ fields = ["pub_date" ],
510
+ condition = Q (pub_date__isnull = False ),
511
+ )
512
+ with (
513
+ self .assertRaisesMessage (NotSupportedError , msg ),
514
+ connection .schema_editor () as editor ,
515
+ ):
516
+ index ._get_condition_mql (Article , schema_editor = editor )
516
517
517
518
@skipUnlessDBFeature ("supports_expression_indexes" )
518
519
def test_partial_func_index (self ):
0 commit comments