[facet] fix argument inversion in date range facet
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 04 Jul 2011 16:46:29 +0200
changeset 7615 e5ad51352eb3
parent 7614 40f33b3d3ea6
child 7617 be5f68f9314e
[facet] fix argument inversion in date range facet
web/facet.py
web/test/unittest_facet.py
--- a/web/facet.py	Mon Jul 04 12:37:44 2011 +0200
+++ b/web/facet.py	Mon Jul 04 16:46:29 2011 +0200
@@ -1029,7 +1029,7 @@
         if supvalue != self.supvalue(max=True):
             self._add_restriction(supvalue, '<=')
 
-    def _add_restriction(self, operator, value):
+    def _add_restriction(self, value, operator):
         self.select.add_constant_restriction(self.filtered_variable,
                                              self.rtype,
                                              self.formatvalue(value),
--- a/web/test/unittest_facet.py	Mon Jul 04 12:37:44 2011 +0200
+++ b/web/test/unittest_facet.py	Mon Jul 04 16:46:29 2011 +0200
@@ -1,3 +1,4 @@
+from logilab.common.date import datetime2ticks
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.web import facet
 
@@ -125,6 +126,33 @@
         self.assertEqual(f.select.as_string(),
                           "DISTINCT Any  WHERE X is CWUser, X in_group E, E name 'guests'")
 
+    def test_daterange(self):
+        req, rset, rqlst, filtered_variable = self.prepare_rqlst()
+        f = facet.DateRangeFacet(req, rset=rset,
+                                 select=rqlst.children[0],
+                                 filtered_variable=filtered_variable)
+        f.rtype = 'creation_date'
+        dates = self.execute('Any CD ORDERBY CD WHERE X is CWUser, X creation_date CD')
+        self.assertEqual(f.vocabulary(),
+                          [(str(dates[0][0]), dates[0][0]),
+                           (str(dates[1][0]), dates[1][0])])
+        # ensure rqlst is left unmodified
+        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
+        #rqlst = rset.syntax_tree()
+        self.assertEqual(f.possible_values(),
+                          [str(dates[0][0]), str(dates[1][0])])
+        # ensure rqlst is left unmodified
+        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
+        req.form['%s_inf' % f.__regid__] = str(datetime2ticks(dates[0][0]))
+        req.form['%s_sup' % f.__regid__] = str(datetime2ticks(dates[0][0]))
+        f.add_rql_restrictions()
+        # selection is cluttered because rqlst has been prepared for facet (it
+        # is not in real life)
+        self.assertEqual(f.select.as_string(),
+                          'DISTINCT Any  WHERE X is CWUser, X creation_date >= "%s", '
+                         'X creation_date <= "%s"'
+                         % (dates[0][0].strftime('%Y/%m/%d'),
+                            dates[0][0].strftime('%Y/%m/%d')))
 
     def test_attribute(self):
         req, rset, rqlst, filtered_variable = self.prepare_rqlst()