|
1 | 1 | import json |
2 | 2 |
|
3 | 3 | from django.test import TestCase |
4 | | -from facility_profile.models import MyUser, TestModel |
| 4 | +from facility_profile.models import Facility, MyUser, TestModel |
5 | 5 |
|
6 | 6 | from morango.models.core import Store |
7 | 7 | from morango.models.manager import SyncableModelManager |
8 | 8 | from morango.models.query import SyncableModelQuerySet |
| 9 | +from morango.registry import syncable_models |
9 | 10 | from morango.sync.controller import MorangoProfileController |
10 | 11 |
|
11 | 12 |
|
@@ -140,3 +141,40 @@ def test_hidden_models_deletion_during_deserialization(self): |
140 | 141 |
|
141 | 142 | # The store record should still exist but marked as deleted |
142 | 143 | self.assertTrue(Store.objects.filter(id=hidden_obj.id, deleted=True).exists()) |
| 144 | + |
| 145 | + def test_get_model_querysets_applies_morango_ordering(self): |
| 146 | + old_ordering = Facility.morango_ordering |
| 147 | + Facility.morango_ordering = ("-name",) |
| 148 | + try: |
| 149 | + Facility.objects.create(name="a-facility") |
| 150 | + Facility.objects.create(name="z-facility") |
| 151 | + queryset = next( |
| 152 | + qs |
| 153 | + for qs in syncable_models.get_model_querysets(Facility.morango_profile) |
| 154 | + if qs.model is Facility |
| 155 | + ) |
| 156 | + self.assertEqual( |
| 157 | + list(queryset.values_list("name", flat=True)), |
| 158 | + ["z-facility", "a-facility"], |
| 159 | + ) |
| 160 | + finally: |
| 161 | + Facility.morango_ordering = old_ordering |
| 162 | + |
| 163 | + def test_get_model_querysets_uses_nulls_last_for_string_ordering(self): |
| 164 | + old_ordering = Facility.morango_ordering |
| 165 | + Facility.morango_ordering = ("parent_id", "name") |
| 166 | + try: |
| 167 | + root_b = Facility.objects.create(name="root-b", parent=None) |
| 168 | + root_a = Facility.objects.create(name="root-a", parent=None) |
| 169 | + child = Facility.objects.create(name="child", parent=root_a) |
| 170 | + queryset = next( |
| 171 | + qs |
| 172 | + for qs in syncable_models.get_model_querysets(Facility.morango_profile) |
| 173 | + if qs.model is Facility |
| 174 | + ) |
| 175 | + self.assertEqual( |
| 176 | + list(queryset.values_list("id", flat=True)), |
| 177 | + [child.id, root_a.id, root_b.id], |
| 178 | + ) |
| 179 | + finally: |
| 180 | + Facility.morango_ordering = old_ordering |
0 commit comments