Skip to content

Commit 5ba3a96

Browse files
committed
indexes
1 parent 8270a6b commit 5ba3a96

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

tests/indexes/tests.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest import skipUnless
33

44
from django.conf import settings
5-
from django.db import connection
5+
from django.db import NotSupportedError, connection
66
from django.db.models import CASCADE, CharField, ForeignKey, Index, Q
77
from django.db.models.functions import Lower
88
from django.test import (
@@ -398,9 +398,9 @@ def test_partial_index(self):
398398
),
399399
),
400400
)
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),
404404
)
405405
editor.add_index(index=index, model=Article)
406406
with connection.cursor() as cursor:
@@ -417,12 +417,13 @@ def test_integer_restriction_partial(self):
417417
with connection.schema_editor() as editor:
418418
index = Index(
419419
name="recent_article_idx",
420-
fields=["id"],
420+
# This is changed
421+
fields=["headline"],
421422
condition=Q(pk__gt=1),
422423
)
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),
426427
)
427428
editor.add_index(index=index, model=Article)
428429
with connection.cursor() as cursor:
@@ -442,9 +443,9 @@ def test_boolean_restriction_partial(self):
442443
fields=["published"],
443444
condition=Q(published=True),
444445
)
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),
448449
)
449450
editor.add_index(index=index, model=Article)
450451
with connection.cursor() as cursor:
@@ -472,15 +473,24 @@ def test_multiple_conditions(self):
472473
tzinfo=timezone.get_current_timezone(),
473474
)
474475
)
475-
& Q(headline__contains="China")
476+
& Q(headline="China")
476477
),
477478
)
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)
481491
# Because each backend has different syntax for the operators,
482492
# check ONLY the occurrence of headline in the SQL.
483-
self.assertGreater(sql.rfind("headline"), where)
493+
# self.assertGreater(sql.rfind("headline"), where)
484494
editor.add_index(index=index, model=Article)
485495
with connection.cursor() as cursor:
486496
self.assertIn(
@@ -493,26 +503,17 @@ def test_multiple_conditions(self):
493503
editor.remove_index(index=index, model=Article)
494504

495505
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)
516517

517518
@skipUnlessDBFeature("supports_expression_indexes")
518519
def test_partial_func_index(self):

0 commit comments

Comments
 (0)