|
1 """cubicweb.common.migration unit tests""" |
|
2 |
|
3 from os.path import abspath |
|
4 from logilab.common.testlib import TestCase, unittest_main |
|
5 |
|
6 from cubicweb.devtools import TestServerConfiguration |
|
7 from cubicweb.devtools.apptest import TestEnvironment |
|
8 |
|
9 from cubicweb.cwconfig import CubicWebConfiguration |
|
10 from cubicweb.common.migration import migration_files, filter_scripts |
|
11 |
|
12 |
|
13 class Schema(dict): |
|
14 def has_entity(self, e_type): |
|
15 return self.has_key(e_type) |
|
16 |
|
17 SMIGRDIR = abspath('data/server_migration') + '/' |
|
18 TMIGRDIR = abspath('data/migration') + '/' |
|
19 |
|
20 class MigrTestConfig(TestServerConfiguration): |
|
21 verbosity = 0 |
|
22 def migration_scripts_dir(cls): |
|
23 return SMIGRDIR |
|
24 |
|
25 def cube_migration_scripts_dir(cls, cube): |
|
26 return TMIGRDIR |
|
27 |
|
28 class MigrationToolsTC(TestCase): |
|
29 def setUp(self): |
|
30 self.config = MigrTestConfig('data') |
|
31 from yams.schema import Schema |
|
32 self.config.load_schema = lambda expand_cubes=False: Schema('test') |
|
33 |
|
34 def test_migration_files_base(self): |
|
35 self.assertListEquals(migration_files(self.config, [('cubicweb', (2,3,0), (2,4,0)), |
|
36 ('TEMPLATE', (0,0,2), (0,0,3))]), |
|
37 [SMIGRDIR+'bootstrapmigration_repository.py', |
|
38 TMIGRDIR+'0.0.3_Any.py']) |
|
39 self.assertListEquals(migration_files(self.config, [('cubicweb', (2,4,0), (2,5,0)), |
|
40 ('TEMPLATE', (0,0,2), (0,0,3))]), |
|
41 [SMIGRDIR+'bootstrapmigration_repository.py', |
|
42 SMIGRDIR+'2.5.0_Any.sql', |
|
43 TMIGRDIR+'0.0.3_Any.py']) |
|
44 self.assertListEquals(migration_files(self.config, [('cubicweb', (2,5,0), (2,6,0)), |
|
45 ('TEMPLATE', (0,0,3), (0,0,4))]), |
|
46 [SMIGRDIR+'bootstrapmigration_repository.py', |
|
47 SMIGRDIR+'2.6.0_Any.sql', |
|
48 TMIGRDIR+'0.0.4_Any.py']) |
|
49 |
|
50 ## def test_migration_files_overlap(self): |
|
51 ## self.assertListEquals(migration_files(self.config, (2,4,0), (2,10,2), |
|
52 ## (0,0,2), (0,1,2)), |
|
53 ## [SMIGRDIR+'bootstrapmigration_repository.py', |
|
54 ## TMIGRDIR+'0.0.3_Any.py', |
|
55 ## TMIGRDIR+'0.0.4_Any.py', |
|
56 ## SMIGRDIR+'2.4.0_2.5.0_Any.sql', |
|
57 ## SMIGRDIR+'2.5.1_2.6.0_Any.sql', |
|
58 ## TMIGRDIR+'0.1.0_Any.py', |
|
59 ## TMIGRDIR+'0.1.0_common.py', |
|
60 ## TMIGRDIR+'0.1.0_repository.py', |
|
61 ## TMIGRDIR+'0.1.2_Any.py', |
|
62 ## SMIGRDIR+'2.10.1_2.10.2_Any.sql']) |
|
63 |
|
64 def test_migration_files_for_mode(self): |
|
65 from cubicweb.server.migractions import ServerMigrationHelper |
|
66 self.assertIsInstance(self.config.migration_handler(), ServerMigrationHelper) |
|
67 from cubicweb.common.migration import MigrationHelper |
|
68 config = CubicWebConfiguration('data') |
|
69 config.verbosity = 0 |
|
70 self.assert_(not isinstance(config.migration_handler(), ServerMigrationHelper)) |
|
71 self.assertIsInstance(config.migration_handler(), MigrationHelper) |
|
72 config = self.config |
|
73 config.__class__.name = 'twisted' |
|
74 self.assertListEquals(migration_files(config, [('TEMPLATE', (0,0,4), (0,1,0))]), |
|
75 [TMIGRDIR+'0.1.0_common.py', |
|
76 TMIGRDIR+'0.1.0_web.py']) |
|
77 config.__class__.name = 'repository' |
|
78 self.assertListEquals(migration_files(config, [('TEMPLATE', (0,0,4), (0,1,0))]), |
|
79 [SMIGRDIR+'bootstrapmigration_repository.py', |
|
80 TMIGRDIR+'0.1.0_Any.py', |
|
81 TMIGRDIR+'0.1.0_common.py', |
|
82 TMIGRDIR+'0.1.0_repository.py']) |
|
83 config.__class__.name = 'all-in-one' |
|
84 self.assertListEquals(migration_files(config, [('TEMPLATE', (0,0,4), (0,1,0))]), |
|
85 [SMIGRDIR+'bootstrapmigration_repository.py', |
|
86 TMIGRDIR+'0.1.0_Any.py', |
|
87 TMIGRDIR+'0.1.0_common.py', |
|
88 TMIGRDIR+'0.1.0_repository.py', |
|
89 TMIGRDIR+'0.1.0_web.py']) |
|
90 config.__class__.name = 'repository' |
|
91 |
|
92 def test_filter_scripts(self): |
|
93 self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,4,0), (2,5,0)), |
|
94 [((2, 5, 0), SMIGRDIR+'2.5.0_Any.sql')]) |
|
95 self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,4,0), (2,6,0)), |
|
96 [((2, 5, 0), SMIGRDIR+'2.5.0_Any.sql'), |
|
97 ((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')]) |
|
98 self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,5,1)), |
|
99 []) |
|
100 self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,6,0)), |
|
101 [((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')]) |
|
102 self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,10,2)), |
|
103 [((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql'), |
|
104 ((2, 10, 2), SMIGRDIR+'2.10.2_Any.sql')]) |
|
105 self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,1), (2,6,0)), |
|
106 [((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')]) |
|
107 self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,1), (2,10,2)), |
|
108 [((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql'), |
|
109 ((2, 10, 2), SMIGRDIR+'2.10.2_Any.sql')]) |
|
110 |
|
111 |
|
112 from cubicweb.devtools import ApptestConfiguration, init_test_database, cleanup_sqlite |
|
113 |
|
114 class BaseCreationTC(TestCase): |
|
115 |
|
116 def test_db_creation(self): |
|
117 """make sure database can be created""" |
|
118 config = ApptestConfiguration('data') |
|
119 source = config.sources()['system'] |
|
120 self.assertEquals(source['db-driver'], 'sqlite') |
|
121 cleanup_sqlite(source['db-name'], removecube=True) |
|
122 init_test_database(driver=source['db-driver'], config=config) |
|
123 |
|
124 |
|
125 if __name__ == '__main__': |
|
126 unittest_main() |