|
4 | 4 |
|
5 | 5 | from datetime import date
|
6 | 6 |
|
| 7 | +from django.db import connection |
7 | 8 | from django.core.exceptions import FieldDoesNotExist
|
8 | 9 | from django.test import TestCase
|
9 | 10 |
|
@@ -170,6 +171,30 @@ def test_order_handler(self):
|
170 | 171 | authors = Author.objects.all()
|
171 | 172 | self.assertSuccessfulRawQuery(Author, query, authors)
|
172 | 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. |
| 179 | + """ |
| 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 | + ) |
| 197 | + |
173 | 198 | def test_query_representation(self):
|
174 | 199 | """Test representation of raw query."""
|
175 | 200 | query = [{"$match": {"last_name": "foo"}}]
|
|
0 commit comments