-
Notifications
You must be signed in to change notification settings - Fork 46
/
test_zodbconvert.py
161 lines (127 loc) · 4.54 KB
/
test_zodbconvert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
##############################################################################
#
# Copyright (c) 2009 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from __future__ import print_function
from contextlib import contextmanager
import os
import tempfile
import transaction
import unittest
from relstorage.zodbconvert import main
class AbstractZODBConvertBase(unittest.TestCase):
cfgfile = None
def _create_src_storage(self):
raise NotImplementedError()
def _create_dest_storage(self):
raise NotImplementedError()
def _create_src_db(self):
from ZODB.DB import DB
return DB(self._create_src_storage())
def _create_dest_db(self):
from ZODB.DB import DB
return DB(self._create_dest_storage())
@contextmanager
def __conn(self, name):
db = getattr(self, '_create_' + name + '_db')()
conn = db.open()
try:
yield conn
finally:
conn.close()
db.close()
def _src_conn(self):
return self.__conn('src')
def _dest_conn(self):
return self.__conn('dest')
def _write_value_for_x_in_src(self, x):
with self._src_conn() as conn:
conn.root()['x'] = x
transaction.commit()
def _check_value_of_x_in_dest(self, x):
with self._dest_conn() as conn2:
db_x = conn2.root().get('x')
self.assertEqual(db_x, x)
def test_convert(self):
self._write_value_for_x_in_src(10)
main(['', self.cfgfile])
self._check_value_of_x_in_dest(10)
def test_dry_run(self):
self._write_value_for_x_in_src(10)
main(['', '--dry-run', self.cfgfile])
self._check_value_of_x_in_dest(None)
def test_incremental(self):
x = 10
self._write_value_for_x_in_src(x)
main(['', self.cfgfile])
self._check_value_of_x_in_dest(x)
x = "hi"
self._write_value_for_x_in_src(x)
main(['', '--incremental', self.cfgfile])
self._check_value_of_x_in_dest(x)
def test_no_overwrite(self):
db = self._create_src_db() # create the root object
db.close()
db = self._create_dest_db() # create the root object
db.close()
self.assertRaises(SystemExit, main, ['', self.cfgfile])
class FSZODBConvertTests(AbstractZODBConvertBase):
def setUp(self):
super(FSZODBConvertTests, self).setUp()
fd, self.srcfile = tempfile.mkstemp()
os.close(fd)
os.remove(self.srcfile)
fd, self.destfile = tempfile.mkstemp()
os.close(fd)
os.remove(self.destfile)
cfg = """
<filestorage source>
path %s
</filestorage>
<filestorage destination>
path %s
</filestorage>
""" % (self.srcfile, self.destfile)
self._write_cfg(cfg)
def _write_cfg(self, cfg):
fd, self.cfgfile = tempfile.mkstemp()
os.write(fd, cfg)
os.close(fd)
def tearDown(self):
if os.path.exists(self.destfile):
os.remove(self.destfile)
if os.path.exists(self.srcfile):
os.remove(self.srcfile)
if os.path.exists(self.cfgfile):
os.remove(self.cfgfile)
super(FSZODBConvertTests, self).tearDown()
def _create_src_storage(self):
from ZODB.FileStorage import FileStorage
return FileStorage(self.srcfile)
def _create_dest_storage(self):
from ZODB.FileStorage import FileStorage
return FileStorage(self.destfile)
def test_storage_has_data(self):
from ZODB.DB import DB
from relstorage.zodbconvert import storage_has_data
from ZODB.FileStorage import FileStorage
src = FileStorage(self.srcfile, create=True)
self.assertFalse(storage_has_data(src))
db = DB(src) # add the root object
db.close()
self.assertTrue(storage_has_data(src))
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(FSZODBConvertTests))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')