forked from benoitc/couchapp
-
Notifications
You must be signed in to change notification settings - Fork 101
/
test_generator.py
309 lines (232 loc) · 10.2 KB
/
test_generator.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# -*- coding: utf-8 -*-
from couchapp.errors import AppError
from couchapp.generator import copy_helper, find_template_dir
from couchapp.generator import init_basic, init_template, save_id
from couchapp.generator import generate, generate_vendor, generate_function
from mock import patch
from nose.tools import raises, with_setup
@patch('couchapp.generator.save_id')
@patch('couchapp.generator.setup_dirs')
@patch('couchapp.generator.setup_dir')
@patch('couchapp.generator.localdoc')
def test_init_basic(localdoc, setup_dir, setup_dirs, save_id):
init_basic('/mock/app')
assert setup_dir.called
assert setup_dirs.called
assert save_id.called
assert localdoc.document.called
@patch('couchapp.generator.open')
def test_save_id(open_):
save_id('/mock/app', 'someid')
open_.assert_called_with('/mock/app/_id', 'wb')
@patch('couchapp.generator.os.path.isdir', return_value=False)
def test_find_template_dir_not_found(isdir):
assert find_template_dir() is None
assert isdir.called
@patch('couchapp.generator.os.path.isdir', return_value=False)
def test_find_template_dir_not_found_raise(isdir):
@raises(AppError)
def f():
find_template_dir(raise_error=True)
f()
assert isdir.called
@patch('couchapp.generator.os.path.isdir', return_value=False)
def test_find_template_dir_template_type_error(isdir):
@raises(AppError)
def f():
find_template_dir(tmpl_type='mock_type')
f()
assert not isdir.called
@patch('couchapp.generator.user_path', return_value=['/mock/.couchapp'])
@patch('couchapp.generator.os.path.isdir', return_value=True)
def test_find_template_dir_user_dir_first(isdir, user_path):
ret = find_template_dir()
assert ret == '/mock/.couchapp/templates/', ret
assert user_path.called
assert isdir.called
@patch('couchapp.generator.user_path', return_value=['/mock/.couchapp'])
@patch('couchapp.generator.os.path.isdir', return_value=True)
def test_find_template_dir_user_dir_first_with_type(isdir, user_path):
ret = find_template_dir(tmpl_type='app')
assert ret == '/mock/.couchapp/templates/app', ret
assert user_path.called
assert isdir.called
@raises(OSError)
def test_copy_helper_invalid_src():
copy_helper('/mock', '/fake')
@patch('couchapp.generator.copytree')
@patch('couchapp.generator.copy2')
@patch('couchapp.generator.os.listdir', return_value=['foo', 'bar'])
@patch('couchapp.generator.os.path.isdir')
@patch('couchapp.generator.setup_dir')
def test_copy_helper_file_only(setup_dir, isdir, listdir, copy2, copytree):
def _isdir(p):
if p == '/mock':
return True
return False
isdir.side_effect = _isdir
copy_helper('/mock', '/fake')
assert setup_dir.called
assert copy2.called
assert not copytree.called
copy2.assert_any_call('/mock/foo', '/fake/foo')
copy2.assert_any_call('/mock/bar', '/fake/bar')
@patch('couchapp.generator.copytree')
@patch('couchapp.generator.copy2')
@patch('couchapp.generator.os.listdir', return_value=['foo', 'bar'])
@patch('couchapp.generator.os.path.isdir', return_value=True)
@patch('couchapp.generator.setup_dir')
def test_copy_helper_dir_only(setup_dir, isdir, listdir, copy2, copytree):
copy_helper('/mock', '/fake')
assert setup_dir.called
assert copytree.called
assert not copy2.called
copytree.assert_any_call('/mock/foo', '/fake/foo')
copytree.assert_any_call('/mock/bar', '/fake/bar')
@patch('couchapp.generator.copy_helper')
def test_init_template_invalid_name(copy_helper):
@raises(AppError)
def f():
init_template('/mock', template='app')
@raises(AppError)
def g():
init_template('/mock', template='functions')
@raises(AppError)
def h():
init_template('/mock', template='vendor')
f()
g()
h()
assert not copy_helper.called
@patch('couchapp.generator.localdoc')
@patch('couchapp.generator.save_id')
@patch('couchapp.generator.setup_dirs')
@patch('couchapp.generator.find_template_dir')
@patch('couchapp.generator.copy_helper')
def test_init_template_default(copy_helper, find_template_dir, setup_dirs,
save_id, localdoc):
def _find_dir(*args, **kwargs):
if (args, kwargs) == (('default', 'app'), dict(raise_error=True)):
# we will copy ``tempaltes/app`` dir
# and if the template not found in any paths,
# we will raise error.
return '/mock/.couchapp/templates/app'
elif (args, kwargs) == (('default',), dict(tmpl_type='vendor')):
# we will copy vendor dir
return '/mock/.couchapp/templates/vendor'
else:
raise AssertionError('invalid call {}'.format((args, kwargs)))
find_template_dir.side_effect = _find_dir
init_template('/fake', template='default')
assert copy_helper.called
assert find_template_dir.called
assert setup_dirs.called
assert save_id.called
assert localdoc.document.called
@patch('couchapp.generator.localdoc')
@patch('couchapp.generator.save_id')
@patch('couchapp.generator.setup_dirs')
@patch('couchapp.generator.find_template_dir')
@patch('couchapp.generator.copy_helper')
def test_init_template_with_tmpl_name(copy_helper, find_template_dir,
setup_dirs, save_id, localdoc):
def _find_dir(*args, **kwargs):
if (args, kwargs) == (('default', 'app'), dict(raise_error=True)):
# we will copy ``tempaltes/app`` dir
# and if the template not found in any paths,
# we will raise error.
return '/mock/.couchapp/templates/app'
elif (args, kwargs) == (('default',), dict(tmpl_type='vendor')):
# we will copy vendor dir
return None
elif (args, kwargs) == (tuple(), dict(tmpl_type='vendor')):
# ``vendor`` not found in the template set
# consider the case of fallback to ``default``
return '/opt/couchapp/templates/vendor'
else:
raise AssertionError('invalid call {}'.format((args, kwargs)))
find_template_dir.side_effect = _find_dir
init_template('/fake', template='default')
assert copy_helper.called
assert find_template_dir.called
assert setup_dirs.called
assert save_id.called
assert localdoc.document.called
@patch('couchapp.generator.generate_function')
@patch('couchapp.generator.generate_vendor')
def test_generate_dispatch_function(gen_vendor, gen_func):
generate('/mock/app', 'view', 'mock_view', template='default')
assert gen_func.called
assert not gen_vendor.called
@raises(AppError)
@patch('couchapp.generator.generate_function')
@patch('couchapp.generator.generate_vendor')
def test_generate_invalid_functions(gen_vendor, gen_func):
generate('/mock/app', 'strange', 'mock_func', template='default')
assert not gen_func.called
assert not gen_vendor.called
@patch('couchapp.generator.generate_function')
@patch('couchapp.generator.generate_vendor')
def test_generate_dispatch_to_vendor(gen_vendor, gen_func):
generate('/mock/app', 'vendor', 'myvendor', template='default')
assert not gen_func.called
assert gen_vendor.called
@patch('couchapp.generator.copy_helper')
@patch('couchapp.generator.setup_dir')
@patch('couchapp.generator.find_template_dir', return_value='/vendor/boostrap')
def test_generate_vendor(find_template_dir, setup_dir, copy_helper):
generate_vendor('/app', 'boostrap')
assert find_template_dir.called
setup_dir.assert_called_with('/app/vendor')
copy_helper.assert_called_with('/vendor/boostrap', '/app/vendor')
@patch('couchapp.generator.copy2')
@patch('couchapp.generator.setup_dir')
@patch('couchapp.generator.find_template_dir', return_value='/funcs/view')
def test_generate_function_view(find_template_dir, setup_dir, copy2):
generate_function('/app', 'view', 'mock')
assert find_template_dir.called
setup_dir.assert_called_with('/app/views/mock', require_empty=True)
copy2.assert_any_call('/funcs/view/map.js', '/app/views/mock/map.js')
copy2.assert_any_call('/funcs/view/reduce.js', '/app/views/mock/reduce.js')
@patch('couchapp.generator.copy2')
@patch('couchapp.generator.setup_dir')
@patch('couchapp.generator.find_template_dir', return_value='/funcs')
def test_generate_function_list(find_template_dir, setup_dir, copy2):
generate_function('/app', 'list', 'mock')
assert find_template_dir.called
setup_dir.assert_called_with('/app/lists', require_empty=False)
copy2.assert_called_with('/funcs/list.js', '/app/lists/mock.js')
@patch('couchapp.generator.copy2')
@patch('couchapp.generator.setup_dir')
@patch('couchapp.generator.find_template_dir', return_value='/funcs')
def test_generate_function_user_func(find_template_dir, setup_dir, copy2):
generate_function('/app', 'function', 'mock')
assert find_template_dir.called
setup_dir.assert_called_with('/app', require_empty=False)
copy2.assert_called_with('/funcs/mock.js', '/app/mock.js')
@patch('couchapp.generator.copy2')
@patch('couchapp.generator.setup_dir')
@patch('couchapp.generator.find_template_dir', return_value='/funcs')
def test_generate_function_spatial(find_template_dir, setup_dir, copy2):
generate_function('/app', 'spatial', 'mock')
assert find_template_dir.called
setup_dir.assert_called_with('/app/spatial', require_empty=False)
copy2.assert_called_with('/funcs/spatial.js', '/app/spatial/mock.js')
@patch('couchapp.generator.copy2')
@patch('couchapp.generator.setup_dir')
@patch('couchapp.generator.find_template_dir', return_value='/funcs')
def test_generate_function_validate_doc(find_template_dir, setup_dir, copy2):
generate_function('/app', 'validate_doc_update', 'mock')
assert find_template_dir.called
setup_dir.assert_called_with('/app', require_empty=False)
copy2.assert_called_with('/funcs/validate_doc_update.js',
'/app/validate_doc_update.js')
@raises(AppError)
@patch('couchapp.generator.copy2')
@patch('couchapp.generator.setup_dir')
@patch('couchapp.generator.find_template_dir', return_value='/funcs')
def test_generate_function_unkown(find_template_dir, setup_dir, copy2):
generate_function('/app', 'magic', 'mock')
assert find_template_dir.called
assert not setup_dir.called
assert not copy2.called