web/test/unittest_facet.py
changeset 6380 63d5dbaef999
parent 6340 470d8e828fda
child 6417 d2361abe8505
--- a/web/test/unittest_facet.py	Fri Oct 01 16:07:03 2010 +0200
+++ b/web/test/unittest_facet.py	Fri Oct 01 16:53:35 2010 +0200
@@ -14,26 +14,33 @@
         self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
         return req, rset, rqlst, mainvar
 
-    def test_relation_simple(self):
+    def _in_group_facet(self, cls=facet.RelationFacet, no_relation=False):
         req, rset, rqlst, mainvar = self.prepare_rqlst()
-        f = facet.RelationFacet(req, rset=rset,
-                                rqlst=rqlst.children[0],
-                                filtered_variable=mainvar)
+        cls.no_relation = no_relation
+        f = cls(req, rset=rset, rqlst=rqlst.children[0],
+                filtered_variable=mainvar)
+        f.__regid__ = 'in_group'
         f.rtype = 'in_group'
         f.role = 'subject'
         f.target_attr = 'name'
         guests, managers = [eid for eid, in self.execute('CWGroup G ORDERBY GN '
                                                          'WHERE G name GN, G name IN ("guests", "managers")')]
+        groups = [eid for eid, in self.execute('CWGroup G ORDERBY GN '
+                                               'WHERE G name GN, G name IN ("guests", "managers")')]
+        return f, groups
+
+    def test_relation_simple(self):
+        f, (guests, managers) = self._in_group_facet()
         self.assertEqual(f.vocabulary(),
-                          [(u'guests', guests), (u'managers', managers)])
+                      [(u'guests', guests), (u'managers', managers)])
         # ensure rqlst is left unmodified
-        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
+        self.assertEqual(f.rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
         #rqlst = rset.syntax_tree()
         self.assertEqual(f.possible_values(),
                           [str(guests), str(managers)])
         # ensure rqlst is left unmodified
-        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
-        req.form[f.__regid__] = str(guests)
+        self.assertEqual(f.rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
+        f._cw.form[f.__regid__] = str(guests)
         f.add_rql_restrictions()
         # selection is cluttered because rqlst has been prepared for facet (it
         # is not in real life)
@@ -72,25 +79,47 @@
         self.assertEqual(f.rqlst.as_string(),
                           'DISTINCT Any  GROUPBY X WHERE X in_group G?, G name GN, NOT G name "users", X in_group D, D eid %s' % guests)
 
+    def test_relation_no_relation_1(self):
+        f, (guests, managers) = self._in_group_facet(no_relation=True)
+        self.assertEqual(f.vocabulary(),
+                          [(u'guests', guests), (u'managers', managers)])
+        self.assertEqual(f.possible_values(),
+                          [str(guests), str(managers)])
+        f._cw.create_entity('CWUser', login=u'hop', upassword='toto')
+        self.assertEqual(f.vocabulary(),
+                          [(u'no relation', ''), (u'guests', guests), (u'managers', managers)])
+        self.assertEqual(f.possible_values(),
+                          [str(guests), str(managers)])
+        f._cw.form[f.__regid__] = ''
+        f.add_rql_restrictions()
+        self.assertEqual(f.rqlst.as_string(),
+                          'DISTINCT Any  WHERE X is CWUser, NOT X in_group G')
+
+    def test_relation_no_relation_2(self):
+        f, (guests, managers) = self._in_group_facet(no_relation=True)
+        f._cw.form[f.__regid__] = ['', guests]
+        f.rqlst.save_state()
+        f.add_rql_restrictions()
+        self.assertEqual(f.rqlst.as_string(),
+                          'DISTINCT Any  WHERE X is CWUser, (NOT X in_group B) OR (X in_group A, A eid %s)' % guests)
+        f.rqlst.recover()
+        self.assertEqual(f.rqlst.as_string(),
+                          'DISTINCT Any  WHERE X is CWUser')
+
+
 
     def test_relationattribute(self):
-        req, rset, rqlst, mainvar = self.prepare_rqlst()
-        f = facet.RelationAttributeFacet(req, rset=rset,
-                                         rqlst=rqlst.children[0],
-                                         filtered_variable=mainvar)
-        f.rtype = 'in_group'
-        f.role = 'subject'
-        f.target_attr = 'name'
+        f, (guests, managers) = self._in_group_facet(cls=facet.RelationAttributeFacet)
         self.assertEqual(f.vocabulary(),
                           [(u'guests', u'guests'), (u'managers', u'managers')])
         # ensure rqlst is left unmodified
-        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
+        self.assertEqual(f.rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
         #rqlst = rset.syntax_tree()
         self.assertEqual(f.possible_values(),
                           ['guests', 'managers'])
         # ensure rqlst is left unmodified
-        self.assertEqual(rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
-        req.form[f.__regid__] = 'guests'
+        self.assertEqual(f.rqlst.as_string(), 'DISTINCT Any  WHERE X is CWUser')
+        f._cw.form[f.__regid__] = 'guests'
         f.add_rql_restrictions()
         # selection is cluttered because rqlst has been prepared for facet (it
         # is not in real life)