(checkpoint)
authorJulien Jehannet <julien.jehannet@logilab.fr>
Mon, 19 Oct 2009 21:40:38 +0200
changeset 72 c47d1759317c
parent 71 178f11d7f7d7
child 73 b3146a828192
(checkpoint)
test/unittest_classification.py
test/unittest_descendant_of.py
test/unittest_keyword.py
--- a/test/unittest_classification.py	Mon Oct 19 21:39:58 2009 +0200
+++ b/test/unittest_classification.py	Mon Oct 19 21:40:38 2009 +0200
@@ -12,12 +12,12 @@
         self.kw2 = self.execute('INSERT Keyword K: K name "kwuser", K included_in C WHERE C name "classif2"')[0][0]
         #self.zone = self.add_entity('Zone', name=u"Paris")
 
-    def aest_application_of_bad_keyword_fails(self):
+    def test_application_of_bad_keyword_fails(self):
         self.assertRaises(ValidationError, self.execute,
                           'SET K applied_to G WHERE G is CWGroup, K name "kwuser"')
 
 
-    def aest_creating_a_new_subkeyword_sets_included_in(self):
+    def test_creating_a_new_subkeyword_sets_included_in(self):
         self.execute('INSERT Keyword SK: SK name "kwgroup2", SK subkeyword_of K '
                      'WHERE K name "kwgroup"')
         self.commit()
@@ -26,12 +26,12 @@
         self.assertEqual(rset[0][0], 'classif1')
 
 
-    def aest_cannot_create_subkeyword_from_other_classification(self):
+    def test_cannot_create_subkeyword_from_other_classification(self):
         self.execute('SET X subkeyword_of Y WHERE X eid %s, Y eid %s' % (self.kw1, self.kw2))
         self.assertRaises(ValidationError, self.commit)
 
 
-    def aest_cannot_create_cycles(self):
+    def test_cannot_create_cycles(self):
         # direct obvious cycle
         self.assertRaises(ValidationError, self.execute,
                           'SET X subkeyword_of Y WHERE X eid %s, Y eid %s' % (self.kw1, self.kw1))
@@ -41,21 +41,6 @@
         self.execute('SET X subkeyword_of Y WHERE X eid %s, Y eid %s' % (self.kw1, kw3))
         self.assertRaises(ValidationError, self.commit)
 
-    def test_descendant_of(self):
-        cl = self.add_entity('Classification', name=u'cl')
-        kw1 = self.add_entity('Keyword', name=u'kw1')
-        kw2 = self.add_entity('Keyword', name=u'kw2')
-        kw3 = self.add_entity('Keyword', name=u'kw3')
-        self.execute('SET X included_in C WHERE X eid %(x)s, C eid %(c)s',
-                         {'c':cl.eid, 'x':kw1.eid } )
-        for pkw, kw in ((kw1, kw2), (kw2, kw3)):
-            self.execute('SET K subkeyword_of P WHERE P eid %(p)s, K eid %(k)s',
-                     {'p': pkw.eid, 'k': kw.eid})
-        self.commit()
-        descendants = kw1.descendant_of
-        print [e for e in self.execute('Any K WHERE K descendant_of P, P eid %(x)s ', {'x':kw1.eid}).entities()]
-        print descendants
-        self.assertEquals(set(descendants), set((kw2,kw3)))
 
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/unittest_descendant_of.py	Mon Oct 19 21:40:38 2009 +0200
@@ -0,0 +1,163 @@
+from cubicweb.devtools.apptest import EnvBasedTC
+
+from cubicweb.common import ValidationError
+
+
+class ClassificationHooksTC(EnvBasedTC):
+
+    @property
+    def nb_keywords(self):
+        return len(self.execute('Any K WHERE K is Keyword'))
+
+    def setup_database(self):
+        self.execute('INSERT Classification C: C name "my_groups", C classifies ET WHERE ET name "CWGroup"')
+        self.execute('INSERT Classification C: C name "my_users", C classifies ET WHERE ET name "CWUser"')
+
+        rql = 'INSERT Keyword K: K name "group1", K included_in C WHERE C name "my_groups"'
+        self.group1 = self.execute(rql).get_entity(0, 0)
+        rql = 'INSERT Keyword K: K name "user1", K included_in C WHERE C name "my_users"'
+        self.user1 = self.execute(rql).get_entity(0, 0)
+        rql = 'INSERT Keyword K: K name "user2", K included_in C WHERE C name "my_users"'
+        self.user2 = self.execute(rql).get_entity(0, 0)
+
+    def test_after_add_keyword_hook(self):
+        """test if ``descendant_of`` is reflexive"""
+        nb = len(self.execute('Any K WHERE K is Keyword'))
+        self.assertEquals(len(self.execute('Any K WHERE K is Keyword, K descendant_of K')), nb)
+
+    def test_after_add_relation_subkeyword_of(self):
+        """test if when *after_add_relation* of ``subkeyword_of`` an entity
+        Keyword K, we have to set a new ``descendant_of`` relation as well to
+        Keyword K and to its classification C
+        """
+        self.assertEqual(len(self.user1.subkeyword_of), 0)
+        self.execute("SET K subkeyword_of KK WHERE K eid %(user)s, KK eid %(group)s",
+                     {'user': self.user1.eid, 'group': self.group1.eid})
+        self.commit()
+        self.user1.clear_all_caches()
+        self.assertEqual(len(self.user1.subkeyword_of), 1)
+        self.assertEqual(len(self.user1.descendant_of), 3)
+        names = set([kw.name for kw in self.user1.descendant_of])
+        self.assertSetEquals(names, set([u'user1', u'group1', u'my_groups']))
+
+        # add an intermediate subgroup between user2 and group2
+        rql = 'INSERT Keyword K: K name "group2", K included_in C WHERE C name "my_groups"'
+        group2 = self.execute(rql).get_entity(0, 0)
+        rql = 'INSERT Keyword K: K name "subgroup", K included_in C, K subkeyword_of G WHERE C name "my_groups", G name "group2"'
+        subgroup = self.execute(rql).get_entity(0, 0)
+        self.execute("SET K subkeyword_of KK WHERE K eid %(user)s, KK eid %(group)s",
+                     {'user': self.user2.eid, 'group': subgroup.eid})
+        self.commit()
+        self.user2.clear_all_caches()
+        self.assertEqual(len(self.user2.subkeyword_of), 1)
+        names = set([kw.name for kw in self.user2.descendant_of])
+        self.assertSetEquals(names, set([u'user2', u'subgroup', u'group2', u'my_groups']))
+
+        # set group3 the new top-level group of user1
+        self.execute("SET K subkeyword_of KK WHERE K eid %(subgroup)s, KK eid %(group)s",
+                     {'subgroup': self.group1.eid, 'group': subgroup.eid})
+        self.commit()
+        self.user1.clear_all_caches()
+        self.assertEqual(len(self.user1.subkeyword_of), 1)
+        names = set([kw.name for kw in self.user1.descendant_of])
+        self.assertSetEquals(names, set([u'user1', u'group1', u'subgroup', u'group3', u'my_groups']))
+
+
+    def XXXtest_get_recursive_parents(self):
+        self.skip()
+
+    def XXXtest_hook_creation_descendant_of_keyword(self):
+        self.skip()
+
+    def XXXtest_hook_creation_descendant_of_classification(self):
+        self.skip()
+
+    def test_delete_one_remaining_descendants(self):
+        """removes one ``descendant_of`` relation for descendant (except itself)"""
+        self.assertEqual(len(self.kw1.subkeyword_of),  0)
+        self.execute("SET K subkeyword_of KK WHERE K eid %(k)s, KK eid %(kk)s",
+                     {'k': self.kw1.eid, 'kk': self.kw2.eid})
+        self.commit()
+        self.kw1.clear_all_caches()
+        self.assertEqual(len(self.kw1.subkeyword_of), 1)
+        self.assertEqual(len(self.kw1.descendant_of), 3)
+        self.execute("DELETE K subkeyword_of KK WHERE K eid %(eid)s",
+                     {'eid': self.kw1.eid})
+        self.kw1.clear_all_caches()
+        self.assertEqual(len(self.kw1.subkeyword_of), 0)
+        self.assertEqual(self.kw1.descendant_of[0].eid, self.kw1.eid)
+        self.assertEqual(len(self.kw1.descendant_of), 1)
+
+    def test_delete_one_remaining_descendants_with_parent(self):
+        """removes one ``descendant_of`` relation for descendant (except itself)
+        
+            kw -*-> self.kw1 -*-> self.kw2
+
+        -*-> : ``subkeyword_of``
+        """
+        rql = 'INSERT Keyword K: K name "kw", K included_in C WHERE C name "classif2"'
+        kw = self.execute(rql).get_entity(0, 0)
+        self.assertEqual(len(kw.subkeyword_of),  0)
+        self.assertEqual(len(self.kw1.subkeyword_of),  0)
+        self.execute("SET K subkeyword_of KK WHERE K eid %(k)s, KK eid %(kk)s",
+                     {'k': self.kw1.eid, 'kk': self.kw2.eid})
+        self.execute("SET K subkeyword_of KK WHERE K eid %(k)s, KK eid %(kk)s",
+                     {'k': kw.eid, 'kk': self.kw1.eid})
+        self.commit()
+        kw.clear_all_caches()
+        self.kw1.clear_all_caches()
+        self.assertEqual(len(kw.subkeyword_of), 1)
+        print kw.subkeyword_of
+        for e in kw.descendant_of:
+            print e.name, e.eid
+        return
+        self.assertEqual(len(self.kw1.descendant_of), 3)
+        self.execute("DELETE K subkeyword_of KK WHERE K eid %(eid)s",
+                     {'eid': self.kw1.eid})
+        self.kw1.clear_all_caches()
+        self.assertEqual(len(self.kw1.subkeyword_of), 0)
+        self.assertEqual(self.kw1.descendant_of[0].eid, self.kw1.eid)
+        self.assertEqual(len(self.kw1.descendant_of), 1)
+
+    def XXXtest_same_schema_as_subkeyword_of(self):
+        self.skip()
+
+    def XXXtest_cannot_create_cycle(self):
+        # direct obvious cycle
+        self.assertRaises(ValidationError, self.execute,
+                          'SET X subkeyword_of Y WHERE X eid %s, Y eid %s' % (self.kw1, self.kw1))
+        # testing indirect cycles
+        kw3 = self.execute('INSERT Keyword SK: SK name "kwgroup2", SK included_in C, '
+                           'SK subkeyword_of K WHERE C name "classif1", K eid %s' % self.kw1)[0][0]
+        self.execute('SET X subkeyword_of Y WHERE X eid %s, Y eid %s' % (self.kw1, kw3))
+        self.assertRaises(ValidationError, self.commit)
+
+    def XXXtest_descendant_of(self):
+        cl = self.add_entity('Classification', name=u'cl')
+        kw1 = self.add_entity('Keyword', name=u'kw1')
+        kw2 = self.add_entity('Keyword', name=u'kw2')
+        kw3 = self.add_entity('Keyword', name=u'kw3')
+        self.execute('SET X included_in C WHERE X eid %(x)s, C eid %(c)s',
+                         {'c':cl.eid, 'x':kw1.eid } )
+        for pkw, kw in ((kw1, kw2), (kw2, kw3)):
+            self.execute('SET K subkeyword_of P WHERE P eid %(p)s, K eid %(k)s',
+                     {'p': pkw.eid, 'k': kw.eid})
+        self.commit()
+        descendants = kw1.descendant_of
+        print [e for e in self.execute('Any K WHERE K descendant_of P, P eid %(x)s ', {'x':kw1.eid}).entities()]
+        print descendants
+        self.assertEquals(set(descendants), set((kw2,kw3)))
+
+    def XXXtest_setup_database(self):
+#        self.nb = 10
+#        for i in range(self.nb):
+#            rql = 'INSERT Keyword K: K name %(name)s, K included_in C, K subkeyword_of KK WHERE C name "classif1", KK eid %(eid)s'
+#            self.execute(rql, {'name': u"keyword%s" % i, 'eid': self.user1.eid})
+        self.assertEquals(len(self.execute('Any K WHERE K is Keyword, K subkeyword_of KK')), self.nb)
+        for e in self.execute('Any K,N WHERE K is Keyword, K subkeyword_of KK,K name N').entities():
+            pass
+
+
+if __name__ == '__main__':
+    from logilab.common.testlib import unittest_main
+    unittest_main()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/unittest_keyword.py	Mon Oct 19 21:40:38 2009 +0200
@@ -0,0 +1,35 @@
+from cubicweb.devtools.apptest import EnvBasedTC
+
+from cubicweb.common import ValidationError
+
+
+class KeywordHooksTC(EnvBasedTC):
+
+    def setup_database(self):
+        self.execute('INSERT Classification C: C name "classif1", C classifies ET WHERE ET name "CWGroup"')
+        self.execute('INSERT Classification C: C name "classif2", C classifies ET WHERE ET name "CWUser"')
+        self.kw1 = self.execute('INSERT Keyword K: K name "kwgroup", K included_in C WHERE C name "classif1"')[0][0]
+        self.kw2 = self.execute('INSERT Keyword K: K name "kwuser", K included_in C WHERE C name "classif2"')[0][0]
+        #self.zone = self.add_entity('Zone', name=u"Paris")
+
+    def test_keyword_iterparents_1(self):
+        parent = self.execute('INSERT Keyword K: K name "kw1", K included_in C WHERE C name "classif1"').get_entity(0, 0)
+        self.execute('INSERT Keyword K: K name "kw2", K included_in C, K subkeyword_of SK WHERE C name "classif1", SK name "kw1"')
+        self.execute('INSERT Keyword K: K name "kw3", K included_in C, K subkeyword_of SK WHERE C name "classif1", SK name "kw2"')
+        self.execute('INSERT Keyword K: K name "kw4", K included_in C, K subkeyword_of SK WHERE C name "classif1", SK name "kw3"')
+        kw = self.execute('INSERT Keyword K: K name "kw5", K included_in C, K subkeyword_of KK WHERE C name "classif1", KK name "kw4"')
+        keyword = kw.get_entity(0, 0)
+        self.assertEquals([kw.name for kw in keyword.iterparents()], ['kw4', 'kw3', 'kw2', 'kw1', 'classif1'])
+
+    def test_keyword_iterparents_2(self):
+        keyword = self.execute('INSERT Keyword K: K name "kw1", K included_in C WHERE C name "classif1"').get_entity(0, 0)
+        self.execute('INSERT Keyword K: K name "kw2", K included_in C, KK subkeyword_of K WHERE C name "classif1", KK name "kw1"')
+        self.execute('INSERT Keyword K: K name "kw3", K included_in C, KK subkeyword_of K WHERE C name "classif1", KK name "kw2"')
+        self.execute('INSERT Keyword K: K name "kw4", K included_in C, KK subkeyword_of K WHERE C name "classif1", KK name "kw3"')
+        self.execute('INSERT Keyword K: K name "kw5", K included_in C, KK subkeyword_of K WHERE C name "classif1", KK name "kw4"')
+        self.assertEquals([kw.name for kw in keyword.iterparents()], [u'kw2', u'kw3', u'kw4', u'kw5', u'classif1'])
+
+
+if __name__ == '__main__':
+    from logilab.common.testlib import unittest_main
+    unittest_main()