[extensions-web] extensions: Fix ordering



commit e9b546ca954ddd4696588366a1cdcea85da6dc55
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Sun Mar 4 15:50:13 2012 -0500

    extensions: Fix ordering
    
    Add a test, as well

 sweettooth/extensions/tests.py |   41 +++++++++++++++++++++++++++++++++++++--
 sweettooth/extensions/views.py |    2 +-
 2 files changed, 39 insertions(+), 4 deletions(-)
---
diff --git a/sweettooth/extensions/tests.py b/sweettooth/extensions/tests.py
index 3ba69a5..d823795 100644
--- a/sweettooth/extensions/tests.py
+++ b/sweettooth/extensions/tests.py
@@ -455,10 +455,16 @@ class QueryExtensionsTest(BasicUserTestCase, TestCase):
         return json.loads(response.content)
 
     def gather_uuids(self, params):
-        return sorted(details['uuid'] for details in self.get_response(params))
+        if 'sort' not in params:
+            params['sort'] = 'name'
 
-    def create_extension(self, name):
-        return models.Extension.objects.create_from_metadata(dict(uuid=name + "@mecheye.net", name=name), creator=self.user)
+        return [details['uuid'] for details in self.get_response(params)]
+
+    def create_extension(self, name, **kwargs):
+        metadata = dict(uuid=name + "@mecheye.net", name=name)
+        return models.Extension.objects.create_from_metadata(metadata,
+                                                             creator=self.user,
+                                                             **kwargs)
 
     def test_basic(self):
         one = self.create_extension("one")
@@ -524,3 +530,32 @@ class QueryExtensionsTest(BasicUserTestCase, TestCase):
         # has this shell version is NEW.
         uuids = self.gather_uuids(dict(shell_version="3.3.90"))
         self.assertEqual(uuids, [])
+
+    def test_sort(self):
+        one = self.create_extension("one", downloads=50, popularity=15)
+        models.ExtensionVersion.objects.create(extension=one, status=models.STATUS_ACTIVE)
+
+        two = self.create_extension("two", downloads=40, popularity=20)
+        models.ExtensionVersion.objects.create(extension=two, status=models.STATUS_ACTIVE)
+
+        uuids = self.gather_uuids(dict(sort="name"))
+        self.assertEqual(uuids, [one.uuid, two.uuid])
+        # name gets asc sort by default
+        uuids = self.gather_uuids(dict(sort="name", order="asc"))
+        self.assertEqual(uuids, [one.uuid, two.uuid])
+        uuids = self.gather_uuids(dict(sort="name", order="desc"))
+        self.assertEqual(uuids, [two.uuid, one.uuid])
+
+        uuids = self.gather_uuids(dict(sort="popularity"))
+        self.assertEqual(uuids, [two.uuid, one.uuid])
+        uuids = self.gather_uuids(dict(sort="popularity", order="desc"))
+        self.assertEqual(uuids, [two.uuid, one.uuid])
+        uuids = self.gather_uuids(dict(sort="popularity", order="asc"))
+        self.assertEqual(uuids, [one.uuid, two.uuid])
+
+        uuids = self.gather_uuids(dict(sort="downloads"))
+        self.assertEqual(uuids, [one.uuid, two.uuid])
+        uuids = self.gather_uuids(dict(sort="downloads", order="desc"))
+        self.assertEqual(uuids, [one.uuid, two.uuid])
+        uuids = self.gather_uuids(dict(sort="downloads", order="asc"))
+        self.assertEqual(uuids, [two.uuid, one.uuid])
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index 1d440bc..453d04c 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -126,7 +126,7 @@ def ajax_query_params_query(request):
         default_order = 'desc'
 
     order = request.GET.get('order', default_order)
-    queryset.query.standard_order = (order == 'asc')
+    queryset.query.standard_ordering = (order == 'asc')
 
     return queryset
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]