16 # You should have received a copy of the GNU Lesser General Public License along |
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/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """unit tests for module cubicweb.server.migractions""" |
18 """unit tests for module cubicweb.server.migractions""" |
19 |
19 |
20 from datetime import date |
20 from datetime import date |
21 from os.path import join |
21 import os.path as osp |
22 from contextlib import contextmanager |
22 from contextlib import contextmanager |
23 |
23 |
24 from logilab.common.testlib import unittest_main, Tags, tag |
24 from logilab.common.testlib import unittest_main, Tags, tag |
25 |
25 |
26 from yams.constraints import UniqueConstraint |
26 from yams.constraints import UniqueConstraint |
27 |
27 |
28 from cubicweb import ConfigurationError, ValidationError |
28 from cubicweb import ConfigurationError, ValidationError, ExecutionError |
29 from cubicweb.devtools.testlib import CubicWebTC |
29 from cubicweb.devtools.testlib import CubicWebTC |
30 from cubicweb.server.sqlutils import SQL_PREFIX |
30 from cubicweb.server.sqlutils import SQL_PREFIX |
31 from cubicweb.server.migractions import ServerMigrationHelper |
31 from cubicweb.server.migractions import ServerMigrationHelper |
32 |
32 |
33 import cubicweb.devtools |
33 import cubicweb.devtools |
|
34 |
|
35 |
|
36 HERE = osp.dirname(osp.abspath(__file__)) |
34 |
37 |
35 migrschema = None |
38 migrschema = None |
36 def tearDownModule(*args): |
39 def tearDownModule(*args): |
37 global migrschema |
40 global migrschema |
38 del migrschema |
41 del migrschema |
39 if hasattr(MigrationCommandsTC, 'origschema'): |
42 if hasattr(MigrationCommandsTC, 'origschema'): |
40 del MigrationCommandsTC.origschema |
43 del MigrationCommandsTC.origschema |
41 |
44 if hasattr(MigrationCommandsComputedTC, 'origschema'): |
42 class MigrationCommandsTC(CubicWebTC): |
45 del MigrationCommandsComputedTC.origschema |
|
46 |
|
47 class MigrationTC(CubicWebTC): |
43 |
48 |
44 configcls = cubicweb.devtools.TestServerConfiguration |
49 configcls = cubicweb.devtools.TestServerConfiguration |
45 |
50 |
46 tags = CubicWebTC.tags | Tags(('server', 'migration', 'migractions')) |
51 tags = CubicWebTC.tags | Tags(('server', 'migration', 'migractions')) |
47 |
52 |
48 def _init_repo(self): |
53 def _init_repo(self): |
49 super(MigrationCommandsTC, self)._init_repo() |
54 super(MigrationTC, self)._init_repo() |
50 # we have to read schema from the database to get eid for schema entities |
55 # we have to read schema from the database to get eid for schema entities |
51 self.repo.set_schema(self.repo.deserialize_schema(), resetvreg=False) |
56 self.repo.set_schema(self.repo.deserialize_schema(), resetvreg=False) |
52 # hack to read the schema from data/migrschema |
57 # hack to read the schema from data/migrschema |
53 config = self.config |
58 config = self.config |
54 config.appid = join('data', 'migratedapp') |
59 config.appid = osp.join(self.appid, 'migratedapp') |
55 config._apphome = self.datapath('migratedapp') |
60 config._apphome = osp.join(HERE, config.appid) |
56 global migrschema |
61 global migrschema |
57 migrschema = config.load_schema() |
62 migrschema = config.load_schema() |
58 config.appid = 'data' |
63 config.appid = self.appid |
59 config._apphome = self.datadir |
64 config._apphome = osp.join(HERE, self.appid) |
60 assert 'Folder' in migrschema |
|
61 |
65 |
62 def setUp(self): |
66 def setUp(self): |
63 CubicWebTC.setUp(self) |
67 CubicWebTC.setUp(self) |
64 |
68 |
65 def tearDown(self): |
69 def tearDown(self): |
70 def mh(self): |
74 def mh(self): |
71 with self.admin_access.client_cnx() as cnx: |
75 with self.admin_access.client_cnx() as cnx: |
72 yield cnx, ServerMigrationHelper(self.repo.config, migrschema, |
76 yield cnx, ServerMigrationHelper(self.repo.config, migrschema, |
73 repo=self.repo, cnx=cnx, |
77 repo=self.repo, cnx=cnx, |
74 interactive=False) |
78 interactive=False) |
|
79 |
|
80 |
|
81 class MigrationCommandsTC(MigrationTC): |
|
82 |
|
83 def _init_repo(self): |
|
84 super(MigrationCommandsTC, self)._init_repo() |
|
85 assert 'Folder' in migrschema |
75 |
86 |
76 def test_add_attribute_bool(self): |
87 def test_add_attribute_bool(self): |
77 with self.mh() as (cnx, mh): |
88 with self.mh() as (cnx, mh): |
78 self.assertNotIn('yesno', self.schema) |
89 self.assertNotIn('yesno', self.schema) |
79 cnx.create_entity('Note') |
90 cnx.create_entity('Note') |
665 mh.cmd_add_relation_type('same_as') |
676 mh.cmd_add_relation_type('same_as') |
666 same_as_sql = mh.sqlexec("SELECT sql FROM sqlite_master WHERE type='table' " |
677 same_as_sql = mh.sqlexec("SELECT sql FROM sqlite_master WHERE type='table' " |
667 "and name='same_as_relation'") |
678 "and name='same_as_relation'") |
668 self.assertTrue(same_as_sql) |
679 self.assertTrue(same_as_sql) |
669 |
680 |
|
681 |
|
682 class MigrationCommandsComputedTC(MigrationTC): |
|
683 """ Unit tests for computed relations and attributes |
|
684 """ |
|
685 appid = 'datacomputed' |
|
686 |
|
687 def test_computed_relation_add_relation_definition(self): |
|
688 self.assertNotIn('works_for', self.schema) |
|
689 with self.mh() as (cnx, mh): |
|
690 with self.assertRaises(ExecutionError) as exc: |
|
691 mh.cmd_add_relation_definition('Employee', 'works_for', |
|
692 'Company') |
|
693 self.assertEqual(str(exc.exception), |
|
694 'Cannot add a relation definition for a computed ' |
|
695 'relation (works_for)') |
|
696 |
|
697 def test_computed_relation_drop_relation_definition(self): |
|
698 self.assertIn('notes', self.schema) |
|
699 with self.mh() as (cnx, mh): |
|
700 with self.assertRaises(ExecutionError) as exc: |
|
701 mh.cmd_drop_relation_definition('Company', 'notes', 'Note') |
|
702 self.assertEqual(str(exc.exception), |
|
703 'Cannot drop a relation definition for a computed ' |
|
704 'relation (notes)') |
|
705 |
|
706 |
670 if __name__ == '__main__': |
707 if __name__ == '__main__': |
671 unittest_main() |
708 unittest_main() |