test/unittest_classification.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 24 Jun 2009 15:26:32 +0200
changeset 34 8f2eff358816
parent 26 38795a236973
child 61 6e358556dfea
permissions -rw-r--r--
needs to specify which entity types can be classified

from cubicweb.devtools.apptest import EnvBasedTC

from cubicweb.common import ValidationError


class ClassificationHooksTC(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_application_of_bad_keyword_fails(self):
        self.assertRaises(ValidationError, self.execute,
                          'SET K applied_to G WHERE G is CWGroup, K name "kwuser"')


    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()
        rset = self.execute('Any N WHERE C name N, K included_in C, K name "kwgroup2"')
        self.assertEquals(len(rset), 1)
        self.assertEqual(rset[0][0], 'classif1')


    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 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))
        # 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 test_keyword_workflow_and_permissions(self):
        user = self.create_user('toto')
        cnx = self.login('toto')

        kw1 = self.execute('INSERT Keyword K: K name "xyz"')[0][0]
        self.execute('SET K included_in C WHERE K eid %(x)s, C name "classif1"',
                     {'x' : kw1})
        # no validation error
        self.commit()
        self.assertEquals(self.execute('Any N WHERE K included_in C, C name N, K eid %(x)s',
                                       {'x' : kw1})[0][0], "classif1")
        keywords = self.execute('Any K WHERE K included_in C, C name "classif1"')
        # 'xyz' should be  visible to 'owner'
        self.assertEquals(len(keywords), 2)
        self.restore_connection()
        user = self.create_user("zoubidou")
        self.login("zoubidou")
        keywords = self.execute('Any K WHERE K included_in C, C name "classif1"')
        # 'xyz' should not visible
        self.assertEquals(len(keywords), 1)
        self.restore_connection()
        keywords = self.execute('Any K WHERE K included_in C, C name "classif1"')
        # 'xyz' should be  visible to 'managers'
        self.assertEquals(len(keywords), 2)
        self.execute('SET X in_state S WHERE S name "keyword validated", X eid %(x)s',
                     {'x' : kw1})
        self.commit()
        self.login("zoubidou")
        keywords = self.execute('Any K WHERE K included_in C, C name "classif1"')
        # 'xyz' should now be visible
        self.assertEquals(len(keywords), 2)


class KeywordTC(EnvBasedTC):
    def setup_database(self):
        self.classif = self.add_entity(u'Classification', name=u'theclassif')
        self.execute('SET C classifies ET WHERE C eid %(c)s, ET name "CWUser"',
                     {'c' : self.classif.eid})


    def test_initial_state_for_admins(self):
        kweid = self.execute('INSERT Keyword K: K name "kw1", K included_in C WHERE C eid %(c)s',
                             {'c' : self.classif.eid})[0][0]
        self.commit()
        state = self.execute('Any SN WHERE K in_state S, S name SN, K eid %(k)s',
                             {'k' : kweid})[0][0]
        self.assertEquals(state, u'keyword validated')

    def test_initial_state_for_users(self):
        user = self.create_user('john')
        self.login('john')
        kweid = self.execute('INSERT Keyword K: K name "kw1", K included_in C WHERE C eid %(c)s',
                             {'c' : self.classif.eid})[0][0]
        self.commit()
        state = self.execute('Any SN WHERE K in_state S, S name SN, K eid %(k)s',
                             {'k' : kweid})[0][0]
        self.assertEquals(state, u'validation pending')




if __name__ == '__main__':
    from logilab.common.testlib import unittest_main
    unittest_main()