Skip to content

Commit 5c30020

Browse files
Viktor Dickviktordick
authored andcommitted
Streamline tests using an autouse fixture and add test for pick operation
1 parent 526fc3f commit 5c30020

File tree

3 files changed

+115
-42
lines changed

3 files changed

+115
-42
lines changed

conftest.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

perfact/zodbsync/tests/environment.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ def cleanup(self):
2929
class Repository():
3030
def __init__(self):
3131
self.path = tempfile.mkdtemp()
32-
subprocess.check_call(['git', '-C', self.path, 'init'])
32+
commands = [
33+
['init'],
34+
['config', 'user.email', 'test@zodbsync.org'],
35+
['config', 'user.name', 'testrepo'],
36+
]
37+
for cmd in commands:
38+
subprocess.check_call(['git', '-C', self.path] + cmd)
3339

3440
def cleanup(self):
3541
shutil.rmtree(self.path)
Lines changed: 108 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,122 @@
1-
import perfact.zodbsync.main
21
import os
32
import os.path
3+
import subprocess
4+
import pytest
5+
6+
import perfact.zodbsync.main
7+
import perfact.zodbsync.tests.environment as env
8+
9+
10+
ZEOPORT = 9011
411

512

613
class TestSync():
714
'''
815
All tests defined in this class share the same environment fixture (i.e.,
916
same ZEO, same repo etc.)
1017
'''
11-
def test_record(self, environment):
12-
env = environment
13-
perfact.zodbsync.main.create_runner([
14-
'--config', env.config.path, 'record', '/',
15-
]).run()
16-
assert os.path.isfile(env.repo.path + '/__root__/acl_users/__meta__')
17-
18-
def test_playback(self, environment):
19-
env = environment
20-
self.test_record(environment)
21-
path = env.repo.path + '/__root__/index_html/__source-utf8__.html'
18+
19+
@pytest.fixture(scope='class', autouse=True)
20+
def environment(self, request):
21+
'''
22+
Fixture that is automatically used by all tests. Initializes
23+
environment and injects the elements of it into the class.
24+
'''
25+
myenv = {
26+
'zeo': env.ZeoInstance(port=ZEOPORT),
27+
'repo': env.Repository(),
28+
'zopeconfig': env.ZopeConfig(zeoport=ZEOPORT),
29+
}
30+
myenv['config'] = env.ZODBSyncConfig(env=myenv)
31+
32+
# inject items into class so methods can use them
33+
for key, value in myenv.items():
34+
setattr(request.cls, key, value)
35+
36+
# at this point, all tests are called
37+
yield
38+
39+
# clean up items
40+
for item in myenv.values():
41+
item.cleanup()
42+
43+
def runner(self, *cmd):
44+
'''
45+
Create runner for given zodbsync command
46+
'''
47+
return perfact.zodbsync.main.create_runner(
48+
['--config', self.config.path] + list(cmd)
49+
)
50+
51+
def gitrun(self, *cmd):
52+
'''
53+
Run git command.
54+
'''
55+
subprocess.check_call(
56+
['git', '-C', self.repo.path] + list(cmd)
57+
)
58+
59+
def gitoutput(self, *cmd):
60+
'''
61+
Run git command, returning output.
62+
'''
63+
return subprocess.check_output(
64+
['git', '-C', self.repo.path] + list(cmd),
65+
universal_newlines=True,
66+
)
67+
68+
def record_all(self, commitmsg=None):
69+
'''
70+
Record everything
71+
'''
72+
self.runner('record', '/').run()
73+
if commitmsg is not None:
74+
self.gitrun('add', '.')
75+
self.gitrun('commit', '-m', commitmsg)
76+
77+
def test_record(self):
78+
'''
79+
Record everything and make sure acl_users exists.
80+
'''
81+
self.record_all()
82+
assert os.path.isfile(
83+
self.repo.path + '/__root__/acl_users/__meta__'
84+
)
85+
86+
def test_playback(self):
87+
'''
88+
Record everything, change /index_html, play it back and check if the
89+
contents are correct.
90+
'''
91+
self.record_all()
92+
path = self.repo.path + '/__root__/index_html/__source-utf8__.html'
2293
content = '<html></html>'
2394
with open(path, 'w') as f:
2495
f.write(content)
25-
runner = perfact.zodbsync.main.create_runner([
26-
'--config', env.config.path, 'playback', '/index_html'
27-
])
96+
runner = self.runner('playback', '/index_html')
2897
runner.run()
2998
assert runner.sync.app.index_html() == content
99+
100+
def test_pick(self):
101+
# Record everything, commit it
102+
self.record_all(commitmsg='First commit')
103+
104+
# Add a folder, commit it
105+
folder = self.repo.path + '/__root__/TestFolder'
106+
os.mkdir(folder)
107+
with open(folder + '/__meta__', 'w') as f:
108+
f.write('''[
109+
('props', []),
110+
('title', ''),
111+
('type', 'Folder'),
112+
]''')
113+
self.gitrun('add', '.')
114+
self.gitrun('commit', '-m', 'Second commit')
115+
commit = self.gitoutput('show-ref', '--head', '--hash', 'HEAD').strip()
116+
117+
# Reset the commit and pick it again
118+
self.gitrun('reset', '--hard', 'HEAD~')
119+
runner = self.runner('pick', commit)
120+
runner.run()
121+
122+
assert 'TestFolder' in runner.sync.app.objectIds()

0 commit comments

Comments
 (0)