forked from Submitty/Submitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_create.py
155 lines (132 loc) · 5.57 KB
/
test_create.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
"""Test the create command."""
from argparse import Namespace
from io import StringIO
from pathlib import Path
import shutil
import sys
import tempfile
import unittest
import migrator
import migrator.main
class TestCreate(unittest.TestCase):
"""Test the migrator."""
def setUp(self):
"""Set up the temp directory to use for the tests."""
self.stdout = sys.stdout
sys.stdout = StringIO()
self.dir = tempfile.mkdtemp()
self.old_path = migrator.MIGRATIONS_PATH
migrator.MIGRATIONS_PATH = Path(self.dir)
def tearDown(self):
"""Remove the temp directory we used for the tests."""
sys.stdout = self.stdout
shutil.rmtree(self.dir)
migrator.MIGRATIONS_PATH = self.old_path
def create_test_runner(self, module_text, parameters, parameter_text, environment):
"""Run create test for a given environment."""
args = Namespace()
args.name = 'test'
args.environments = [environment]
migrator_dir = Path(self.dir, environment)
migrator_dir.mkdir()
migrator.main.create(args)
expected = """\"\"\"{0}\"\"\"
def up({1}):
\"\"\"
Run up migration.
{2}
\"\"\"
pass
def down({1}):
\"\"\"
Run down migration (rollback).
{2}
\"\"\"
pass
""".format(module_text, ', '.join(parameters), parameter_text)
found_files = 0
for entry in migrator_dir.iterdir():
with entry.open() as open_file:
self.assertEqual(expected, open_file.read())
found_files += 1
self.assertEqual(1, found_files)
def test_create_system(self):
"""Test the create command for system environment."""
parameters = ['config']
environment = 'system'
module_text = 'Migration for the Submitty system.'
parameter_text = """:param config: Object holding configuration details about Submitty
:type config: migrator.config.Config"""
self.create_test_runner(module_text, parameters, parameter_text, environment)
self.assertRegex(sys.stdout.getvalue(), r'Created migration: system\/[0-9]{14}_test.py')
def test_create_master(self):
"""Test the create command for the master environment."""
parameters = ['config', 'database']
environment = 'master'
module_text = 'Migration for the Submitty master database.'
parameter_text = """:param config: Object holding configuration details about Submitty
:type config: migrator.config.Config
:param database: Object for interacting with given database for environment
:type database: migrator.db.Database"""
self.create_test_runner(module_text, parameters, parameter_text, environment)
self.assertRegex(sys.stdout.getvalue(), r'Created migration: master\/[0-9]{14}_test.py')
def test_create_course(self):
"""Test the create command for the course environment."""
parameters = ['config', 'database', 'semester', 'course']
environment = 'course'
module_text = 'Migration for a given Submitty course database.'
parameter_text = """:param config: Object holding configuration details about Submitty
:type config: migrator.config.Config
:param database: Object for interacting with given database for environment
:type database: migrator.db.Database
:param semester: Semester of the course being migrated
:type semester: str
:param course: Code of course being migrated
:type course: str"""
self.create_test_runner(module_text, parameters, parameter_text, environment)
self.assertRegex(sys.stdout.getvalue(), r'Created migration: course\/[0-9]{14}_test.py')
def test_create_master_and_system(self):
args = Namespace()
args.name = 'test'
args.environments = ['system', 'master']
for environment in args.environments:
Path(self.dir, environment).mkdir()
migrator.main.create(args)
for environment in args.environments:
found_files = 0
for entry in Path(self.dir, environment).iterdir():
with entry.open() as open_file:
self.assertTrue(len(open_file.read()) > 0)
found_files += 1
self.assertEqual(1, found_files)
regex = r"""Created migration: master\/[0-9]{14}_test.py
Created migration: system\/[0-9]{14}_test.py"""
self.assertRegex(sys.stdout.getvalue(), regex)
def test_create_all(self):
args = Namespace()
args.name = 'test'
args.environments = ['course', 'master', 'system']
for environment in args.environments:
Path(self.dir, environment).mkdir()
migrator.main.create(args)
for environment in args.environments:
found_files = 0
for entry in Path(self.dir, environment).iterdir():
with entry.open() as open_file:
self.assertTrue(len(open_file.read()) > 0)
found_files += 1
self.assertEqual(1, found_files)
regex = r"""Created migration: master\/[0-9]{14}_test.py
Created migration: system\/[0-9]{14}_test.py
Created migration: course\/[0-9]{14}_test.py"""
self.assertRegex(sys.stdout.getvalue(), regex)
def test_create_bad_name(self):
args = Namespace()
args.name = 'invalid#!!!'
args.environments = ['system']
with self.assertRaises(ValueError) as cm:
migrator.main.create(args)
self.assertEqual(
"Invalid migration name (must only contain alphanumeric and _): invalid#!!!",
str(cm.exception)
)