|
1 | 1 | from django.db import connection
|
2 |
| -from django.test import TransactionTestCase |
3 |
| -from django.test.utils import override_settings |
| 2 | +from django.test import TestCase |
4 | 3 |
|
5 | 4 | from django_mongodb_backend.indexes import AtlasSearchIndex
|
6 | 5 |
|
7 | 6 | from .models import Article
|
8 | 7 |
|
9 | 8 |
|
10 |
| -@override_settings(USE_TZ=True) |
11 |
| -class PartialIndexTests(TransactionTestCase): |
| 9 | +class PartialIndexTests(TestCase): |
12 | 10 | # Schema editor is used to create the index to test that it works.
|
13 | 11 | # available_apps = ["indexes"]
|
14 | 12 | available_apps = None
|
15 | 13 |
|
16 |
| - def test_partial_index(self): |
| 14 | + def assertAddRemoveIndex(self, editor, model, index): |
| 15 | + editor.add_index(index=index, model=model) |
| 16 | + self.assertIn( |
| 17 | + index.name, |
| 18 | + connection.introspection.get_constraints( |
| 19 | + cursor=None, |
| 20 | + table_name=model._meta.db_table, |
| 21 | + ), |
| 22 | + ) |
| 23 | + editor.remove_index(index=index, model=model) |
| 24 | + |
| 25 | + def test_simple_atlas_index(self): |
17 | 26 | with connection.schema_editor() as editor:
|
18 | 27 | index = AtlasSearchIndex(
|
19 | 28 | name="recent_article_idx",
|
20 | 29 | fields=["number"],
|
21 | 30 | )
|
22 | 31 | editor.add_index(index=index, model=Article)
|
23 |
| - # with connection.cursor() as cursor: |
24 |
| - # self.assertIn( |
25 |
| - # index.name, |
26 |
| - # connection.introspection.get_constraints( |
27 |
| - # cursor=cursor, |
28 |
| - # table_name=Article._meta.db_table, |
29 |
| - # ), |
30 |
| - # ) |
31 |
| - editor.remove_index(index=index, model=Article) |
| 32 | + self.assertAddRemoveIndex(editor, Article, index) |
| 33 | + |
| 34 | + def test_multiple_fields_atlas_index(self): |
| 35 | + with connection.schema_editor() as editor: |
| 36 | + index = AtlasSearchIndex( |
| 37 | + name="recent_article_idx", |
| 38 | + fields=["headline", "number", "body", "data", "embedded", "auto_now"], |
| 39 | + ) |
| 40 | + # editor.remove_index(index=index, model=Article) |
| 41 | + editor.add_index(index=index, model=Article) |
| 42 | + index_info = connection.introspection.get_constraints( |
| 43 | + cursor=None, |
| 44 | + table_name=Article._meta.db_table, |
| 45 | + ) |
| 46 | + expected_options = { |
| 47 | + "dynamic": False, |
| 48 | + "fields": { |
| 49 | + "auto_now": {"type": "date"}, |
| 50 | + "body": { |
| 51 | + "indexOptions": "offsets", |
| 52 | + "norms": "include", |
| 53 | + "store": True, |
| 54 | + "type": "string", |
| 55 | + }, |
| 56 | + "data": {"dynamic": False, "fields": {}, "type": "document"}, |
| 57 | + "embedded": {"dynamic": False, "fields": {}, "type": "embeddedDocuments"}, |
| 58 | + "headline": { |
| 59 | + "indexOptions": "offsets", |
| 60 | + "norms": "include", |
| 61 | + "store": True, |
| 62 | + "type": "string", |
| 63 | + }, |
| 64 | + "number": { |
| 65 | + "indexDoubles": True, |
| 66 | + "indexIntegers": True, |
| 67 | + "representation": "double", |
| 68 | + "type": "number", |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | + self.assertCountEqual(index_info[index.name]["columns"], index.fields) |
| 73 | + self.assertEqual(index_info[index.name]["options"], expected_options) |
| 74 | + |
| 75 | + self.assertEqual(index_info, {}) |
| 76 | + self.assertAddRemoveIndex(editor, Article, index) |
0 commit comments