archesproject / arches

Arches is a web platform for creating, managing, & visualizing geospatial data. Arches was inspired by the needs of the Cultural Heritage community, particularly the widespread need of organizations to build & manage cultural heritage inventories
GNU Affero General Public License v3.0
210 stars 139 forks source link

Fix flaky test_json_export() #11065

Closed jacobtylerwalls closed 2 weeks ago

jacobtylerwalls commented 2 weeks ago

Types of changes

Description of Change

In this test we had indeterminate sorting of lists holding dictionaries. To sort a list of dictionaries consistently, we need to define a sort key. We had a sort key for the innermost array of tiles (tileid), but not the outer array of resources.

Testing instructions

With this diff applied:

diff --git a/tests/exporter/resource_export_tests.py b/tests/exporter/resource_export_tests.py
index 764829e9f5..51799cab88 100644
--- a/tests/exporter/resource_export_tests.py
+++ b/tests/exporter/resource_export_tests.py
@@ -95,6 +95,7 @@ class BusinessDataExportTests(ArchesTestCase):
         self.assertDictEqual(dict(csv_input), dict(csv_output))

     def test_json_export(self):
+        import random
         def deep_sort(obj):
             """
             Recursively sort list or dict nested lists. Taken from
@@ -117,6 +118,7 @@ class BusinessDataExportTests(ArchesTestCase):
                     _sorted = sorted(
                         new_list,
                         key=lambda item: item["resourceinstance"]["resourceinstanceid"],
+                        reverse=random.random() > 0.5,
                     )
                 else:
                     try:

you can run python3 manage.py test tests -k test_json_export --settings=tests.test_settings --keepdb and see the failure occasionally seen on CI.