|
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 | 7 | from django.core.exceptions import FieldDoesNotExist
|
| 8 | +from django.db import connection |
8 | 9 | from django.test import TestCase
|
9 | 10 |
|
10 | 11 | from django_mongodb_backend.queryset import RawQuerySet
|
@@ -170,6 +171,23 @@ def test_order_handler(self):
|
170 | 171 | authors = Author.objects.all()
|
171 | 172 | self.assertSuccessfulRawQuery(Author, query, authors)
|
172 | 173 |
|
| 174 | + def test_different_db_key_order(self): |
| 175 | + """ |
| 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 | + author = Author(first_name="Out of", last_name="Order", dob=datetime(1950, 9, 20)) |
| 180 | + # Insert a document with the keys reversed. |
| 181 | + connection.database.get_collection(Author._meta.db_table).insert_one( |
| 182 | + { |
| 183 | + field.name: getattr(author, field.name) |
| 184 | + for field in reversed(Author._meta.concrete_fields) |
| 185 | + } |
| 186 | + ) |
| 187 | + query = [] |
| 188 | + authors = Author.objects.all() |
| 189 | + self.assertSuccessfulRawQuery(Author, query, authors) |
| 190 | + |
173 | 191 | def test_query_representation(self):
|
174 | 192 | """Test representation of raw query."""
|
175 | 193 | query = [{"$match": {"last_name": "foo"}}]
|
|
0 commit comments