cubicweb/dataimport/test/test_stores.py
changeset 11057 0b59724cb3f2
parent 11033 63d860a14a17
child 11165 c6fe858f6b90
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """unittest for cubicweb.dataimport.stores"""
       
    19 
       
    20 import datetime as DT
       
    21 
       
    22 from cubicweb.dataimport import stores
       
    23 from cubicweb.devtools.testlib import CubicWebTC
       
    24 
       
    25 
       
    26 class RQLObjectStoreTC(CubicWebTC):
       
    27 
       
    28     def test_all(self):
       
    29         with self.admin_access.repo_cnx() as cnx:
       
    30             store = stores.RQLObjectStore(cnx)
       
    31             # Check data insertion
       
    32             group_eid = store.prepare_insert_entity('CWGroup', name=u'grp')
       
    33             user_eid = store.prepare_insert_entity('CWUser', login=u'lgn',
       
    34                                                    upassword=u'pwd')
       
    35             store.prepare_insert_relation(user_eid, 'in_group', group_eid)
       
    36             cnx.commit()
       
    37             users = cnx.execute('CWUser X WHERE X login "lgn"')
       
    38             self.assertEqual(1, len(users))
       
    39             self.assertEqual(user_eid, users.one().eid)
       
    40             groups = cnx.execute('CWGroup X WHERE U in_group X, U login "lgn"')
       
    41             self.assertEqual(1, len(users))
       
    42             self.assertEqual(group_eid, groups.one().eid)
       
    43             # Check data update
       
    44             self.set_description('Check data update')
       
    45             store.prepare_update_entity('CWGroup', group_eid, name=u'new_grp')
       
    46             cnx.commit()
       
    47             group = cnx.execute('CWGroup X WHERE X name "grp"')
       
    48             self.assertEqual(len(group), 0)
       
    49             group = cnx.execute('CWGroup X WHERE X name "new_grp"')
       
    50             self.assertEqual, len(group), 1
       
    51             # Check data update with wrong type
       
    52             with self.assertRaises(AssertionError):
       
    53                 store.prepare_update_entity('CWUser', group_eid, name=u'new_user')
       
    54             cnx.commit()
       
    55             group = cnx.execute('CWGroup X WHERE X name "new_user"')
       
    56             self.assertEqual(len(group), 0)
       
    57             group = cnx.execute('CWGroup X WHERE X name "new_grp"')
       
    58             self.assertEqual(len(group), 1)
       
    59 
       
    60 
       
    61 class MetaGeneratorTC(CubicWebTC):
       
    62 
       
    63     def test_dont_generate_relation_to_internal_manager(self):
       
    64         with self.admin_access.repo_cnx() as cnx:
       
    65             metagen = stores.MetaGenerator(cnx)
       
    66             self.assertIn('created_by', metagen.etype_rels)
       
    67             self.assertIn('owned_by', metagen.etype_rels)
       
    68         with self.repo.internal_cnx() as cnx:
       
    69             metagen = stores.MetaGenerator(cnx)
       
    70             self.assertNotIn('created_by', metagen.etype_rels)
       
    71             self.assertNotIn('owned_by', metagen.etype_rels)
       
    72 
       
    73     def test_dont_generate_specified_values(self):
       
    74         with self.admin_access.repo_cnx() as cnx:
       
    75             metagen = stores.MetaGenerator(cnx)
       
    76             # hijack gen_modification_date to ensure we don't go through it
       
    77             metagen.gen_modification_date = None
       
    78             md = DT.datetime.utcnow() - DT.timedelta(days=1)
       
    79             entity, rels = metagen.base_etype_dicts('CWUser')
       
    80             entity.cw_edited.update(dict(modification_date=md))
       
    81             with cnx.ensure_cnx_set:
       
    82                 metagen.init_entity(entity)
       
    83             self.assertEqual(entity.cw_edited['modification_date'], md)
       
    84 
       
    85 
       
    86 if __name__ == '__main__':
       
    87     from logilab.common.testlib import unittest_main
       
    88     unittest_main()