|
2 | 2 | These tests are adapted from Django's tests/raw_query/tests.py.
|
3 | 3 | """
|
4 | 4 |
|
5 |
| -from datetime import date |
| 5 | +from datetime import date, datetime |
6 | 6 |
|
7 |
| -from django.db import connection |
8 | 7 | from django.core.exceptions import FieldDoesNotExist
|
| 8 | +from django.db import connection |
9 | 9 | from django.test import TestCase
|
10 | 10 |
|
11 | 11 | from django_mongodb_backend.queryset import RawQuerySet
|
@@ -171,29 +171,23 @@ def test_order_handler(self):
|
171 | 171 | authors = Author.objects.all()
|
172 | 172 | self.assertSuccessfulRawQuery(Author, query, authors)
|
173 | 173 |
|
174 |
| - def test_different_ordered_in_database(self): |
175 |
| - """Documents in MongoDB are not required to maintain key order as |
176 |
| - a means to improve write efficiency. Documents can be returned |
177 |
| - to Django out of order. This can lead to incorrect information being placed |
178 |
| - in a RawQueryset object. |
| 174 | + def test_different_db_key_order(self): |
179 | 175 | """
|
180 |
| - database = connection["<TEST_DATABASE>"].database |
181 |
| - raw_insert = Author(first_name="Out of", last_name="Order", dob=date(1950, 9, 20)) |
182 |
| - try: |
183 |
| - # Insert a document into the database in reverse |
184 |
| - database[Author._meta.db_table].insert_one( |
185 |
| - { |
186 |
| - field.name: getattr(field.name, raw_insert) |
187 |
| - for field in reversed(Author._meta.get_fields()) |
188 |
| - } |
189 |
| - ) |
190 |
| - query = [] |
191 |
| - authors = Author.objects.all() |
192 |
| - self.assertSuccessfulRawQuery(Author, query, authors) |
193 |
| - finally: |
194 |
| - database[Author._meta.db_table].delete_one( |
195 |
| - {"first_name": raw_insert.first_name, "last_name": raw_insert.last_name} |
196 |
| - ) |
| 176 | + A raw query correctly associates document keys to model fields when the |
| 177 | + document key order is different than the order of model fields. |
| 178 | + """ |
| 179 | + db = connection.database |
| 180 | + author = Author(first_name="Out of", last_name="Order", dob=datetime(1950, 9, 20)) |
| 181 | + # Insert a document with the keys reversd. |
| 182 | + db.get_collection(Author._meta.db_table).insert_one( |
| 183 | + { |
| 184 | + field.name: getattr(author, field.name) |
| 185 | + for field in reversed(Author._meta.concrete_fields) |
| 186 | + } |
| 187 | + ) |
| 188 | + query = [] |
| 189 | + authors = Author.objects.all() |
| 190 | + self.assertSuccessfulRawQuery(Author, query, authors) |
197 | 191 |
|
198 | 192 | def test_query_representation(self):
|
199 | 193 | """Test representation of raw query."""
|
|
0 commit comments