server/test/unittest_rql2sql.py
changeset 3890 d7a270f50f54
parent 3777 3ef8cdb5fb1c
parent 3852 03121ca1f85e
child 3998 94cc7cad3d2d
--- a/server/test/unittest_rql2sql.py	Sun Nov 08 21:53:18 2009 +0100
+++ b/server/test/unittest_rql2sql.py	Fri Nov 20 19:35:54 2009 +0100
@@ -10,13 +10,13 @@
 
 import sys
 
-from logilab.common.testlib import TestCase, unittest_main
+from logilab.common.testlib import TestCase, unittest_main, mock_object
 
 from rql import BadRQLQuery
 from indexer import get_indexer
 
 #from cubicweb.server.sources.native import remove_unused_solutions
-from cubicweb.server.sources.rql2sql import SQLGenerator
+from cubicweb.server.sources.rql2sql import SQLGenerator, remove_unused_solutions
 
 from rql.utils import register_function, FunctionDescr
 # add a dumb registered procedure
@@ -165,6 +165,9 @@
      '''SELECT _X.cw_eid
 FROM cw_Personne AS _X
 WHERE _X.cw_prenom=lulu AND NOT EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, in_group_relation AS rel_in_group1, cw_CWGroup AS _G WHERE rel_owned_by0.eid_from=_X.cw_eid AND rel_in_group1.eid_from=rel_owned_by0.eid_to AND rel_in_group1.eid_to=_G.cw_eid AND ((_G.cw_name=lulufanclub) OR (_G.cw_name=managers)))'''),
+
+
+
 ]
 
 ADVANCED= [
@@ -868,10 +871,21 @@
     ]
 
 VIRTUAL_VARS = [
-    ("Personne P WHERE P travaille S, S tel T, S fax T, S is Societe;",
+
+    ('Any X WHERE X is CWUser, X creation_date > D1, Y creation_date D1, Y login "SWEB09"',
+     '''SELECT _X.cw_eid
+FROM cw_CWUser AS _X, cw_CWUser AS _Y
+WHERE _X.cw_creation_date>_Y.cw_creation_date AND _Y.cw_login=SWEB09'''),
+
+    ('Any X WHERE X is CWUser, Y creation_date D1, Y login "SWEB09", X creation_date > D1',
+     '''SELECT _X.cw_eid
+FROM cw_CWUser AS _X, cw_CWUser AS _Y
+WHERE _Y.cw_login=SWEB09 AND _X.cw_creation_date>_Y.cw_creation_date'''),
+
+    ('Personne P WHERE P travaille S, S tel T, S fax T, S is Societe',
      '''SELECT rel_travaille0.eid_from
 FROM cw_Societe AS _S, travaille_relation AS rel_travaille0
-WHERE rel_travaille0.eid_to=_S.cw_eid AND _S.cw_fax=_S.cw_tel'''),
+WHERE rel_travaille0.eid_to=_S.cw_eid AND _S.cw_tel=_S.cw_fax'''),
 
     ("Personne P where X eid 0, X creation_date D, P datenaiss < D, X is Affaire",
      '''SELECT _P.cw_eid
@@ -1590,7 +1604,26 @@
 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_Affaire AS _P WHERE rel_owned_by0.eid_from=_P.cw_eid AND rel_owned_by0.eid_to=1 UNION SELECT 1 FROM owned_by_relation AS rel_owned_by1, cw_Note AS _P WHERE rel_owned_by1.eid_from=_P.cw_eid AND rel_owned_by1.eid_to=1)''')
 
 
+class removeUnsusedSolutionsTC(TestCase):
+    def test_invariant_not_varying(self):
+        rqlst = mock_object(defined_vars={})
+        rqlst.defined_vars['A'] = mock_object(scope=rqlst, stinfo={'optrelations':False}, _q_invariant=True)
+        rqlst.defined_vars['B'] = mock_object(scope=rqlst, stinfo={'optrelations':False}, _q_invariant=False)
+        self.assertEquals(remove_unused_solutions(rqlst, [{'A': 'RugbyGroup', 'B': 'RugbyTeam'},
+                                                          {'A': 'FootGroup', 'B': 'FootTeam'}], {}, None),
+                          ([{'A': 'RugbyGroup', 'B': 'RugbyTeam'},
+                            {'A': 'FootGroup', 'B': 'FootTeam'}],
+                           {}, set('B'))
+                          )
 
+    def test_invariant_varying(self):
+        rqlst = mock_object(defined_vars={})
+        rqlst.defined_vars['A'] = mock_object(scope=rqlst, stinfo={'optrelations':False}, _q_invariant=True)
+        rqlst.defined_vars['B'] = mock_object(scope=rqlst, stinfo={'optrelations':False}, _q_invariant=False)
+        self.assertEquals(remove_unused_solutions(rqlst, [{'A': 'RugbyGroup', 'B': 'RugbyTeam'},
+                                                          {'A': 'FootGroup', 'B': 'RugbyTeam'}], {}, None),
+                          ([{'A': 'RugbyGroup', 'B': 'RugbyTeam'}], {}, set())
+                          )
 
 if __name__ == '__main__':
     unittest_main()