|
1 # copyright 2003-2014 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 """cubicweb.migration unit tests""" |
|
19 |
|
20 from os.path import abspath, dirname, join |
|
21 from logilab.common.testlib import TestCase, unittest_main |
|
22 |
|
23 from cubicweb.devtools import TestServerConfiguration |
|
24 from cubicweb.cwconfig import CubicWebConfiguration |
|
25 from cubicweb.migration import MigrationHelper, filter_scripts, version_strictly_lower |
|
26 from cubicweb.server.migractions import ServerMigrationHelper |
|
27 |
|
28 |
|
29 class Schema(dict): |
|
30 def has_entity(self, e_type): |
|
31 return e_type in self |
|
32 |
|
33 SMIGRDIR = join(dirname(__file__), 'data', 'server_migration') + '/' |
|
34 TMIGRDIR = join(dirname(__file__), 'data', 'migration') + '/' |
|
35 |
|
36 class MigrTestConfig(TestServerConfiguration): |
|
37 verbosity = 0 |
|
38 def migration_scripts_dir(cls): |
|
39 return SMIGRDIR |
|
40 |
|
41 def cube_migration_scripts_dir(cls, cube): |
|
42 return TMIGRDIR |
|
43 |
|
44 class MigrationToolsTC(TestCase): |
|
45 def setUp(self): |
|
46 self.config = MigrTestConfig('data') |
|
47 from yams.schema import Schema |
|
48 self.config.load_schema = lambda expand_cubes=False: Schema('test') |
|
49 self.config.__class__.cubicweb_appobject_path = frozenset() |
|
50 self.config.__class__.cube_appobject_path = frozenset() |
|
51 |
|
52 def test_filter_scripts_base(self): |
|
53 self.assertListEqual(filter_scripts(self.config, SMIGRDIR, (2,3,0), (2,4,0)), |
|
54 []) |
|
55 self.assertListEqual(filter_scripts(self.config, SMIGRDIR, (2,4,0), (2,5,0)), |
|
56 [((2, 5, 0), SMIGRDIR+'2.5.0_Any.sql')]) |
|
57 self.assertListEqual(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,6,0)), |
|
58 [((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')]) |
|
59 self.assertListEqual(filter_scripts(self.config, SMIGRDIR, (2,4,0), (2,6,0)), |
|
60 [((2, 5, 0), SMIGRDIR+'2.5.0_Any.sql'), |
|
61 ((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')]) |
|
62 self.assertListEqual(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,5,1)), |
|
63 []) |
|
64 self.assertListEqual(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,10,2)), |
|
65 [((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql'), |
|
66 ((2, 10, 2), SMIGRDIR+'2.10.2_Any.sql')]) |
|
67 self.assertListEqual(filter_scripts(self.config, SMIGRDIR, (2,5,1), (2,6,0)), |
|
68 [((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')]) |
|
69 |
|
70 self.assertListEqual(filter_scripts(self.config, TMIGRDIR, (0,0,2), (0,0,3)), |
|
71 [((0, 0, 3), TMIGRDIR+'0.0.3_Any.py')]) |
|
72 self.assertListEqual(filter_scripts(self.config, TMIGRDIR, (0,0,2), (0,0,4)), |
|
73 [((0, 0, 3), TMIGRDIR+'0.0.3_Any.py'), |
|
74 ((0, 0, 4), TMIGRDIR+'0.0.4_Any.py')]) |
|
75 |
|
76 def test_filter_scripts_for_mode(self): |
|
77 config = CubicWebConfiguration('data') |
|
78 config.verbosity = 0 |
|
79 config = self.config |
|
80 config.__class__.name = 'repository' |
|
81 self.assertListEqual(filter_scripts(config, TMIGRDIR, (0,0,4), (0,1,0)), |
|
82 [((0, 1 ,0), TMIGRDIR+'0.1.0_Any.py'), |
|
83 ((0, 1 ,0), TMIGRDIR+'0.1.0_common.py'), |
|
84 ((0, 1 ,0), TMIGRDIR+'0.1.0_repository.py')]) |
|
85 config.__class__.name = 'all-in-one' |
|
86 self.assertListEqual(filter_scripts(config, TMIGRDIR, (0,0,4), (0,1,0)), |
|
87 [((0, 1 ,0), TMIGRDIR+'0.1.0_Any.py'), |
|
88 ((0, 1 ,0), TMIGRDIR+'0.1.0_common.py'), |
|
89 ((0, 1 ,0), TMIGRDIR+'0.1.0_repository.py')]) |
|
90 config.__class__.name = 'repository' |
|
91 |
|
92 def test_version_strictly_lower(self): |
|
93 self.assertTrue(version_strictly_lower(None, '1.0.0')) |
|
94 self.assertFalse(version_strictly_lower('1.0.0', None)) |
|
95 |
|
96 |
|
97 from cubicweb.devtools import ApptestConfiguration, get_test_db_handler |
|
98 |
|
99 class BaseCreationTC(TestCase): |
|
100 |
|
101 def test_db_creation(self): |
|
102 """make sure database can be created""" |
|
103 config = ApptestConfiguration('data', apphome=self.datadir) |
|
104 source = config.system_source_config |
|
105 self.assertEqual(source['db-driver'], 'sqlite') |
|
106 handler = get_test_db_handler(config) |
|
107 handler.init_test_database() |
|
108 handler.build_db_cache() |
|
109 repo, cnx = handler.get_repo_and_cnx() |
|
110 with cnx: |
|
111 self.assertEqual(cnx.execute('Any SN WHERE X is CWUser, X login "admin", X in_state S, S name SN').rows, |
|
112 [['activated']]) |
|
113 repo.shutdown() |
|
114 |
|
115 if __name__ == '__main__': |
|
116 unittest_main() |