File: test_bob.py

package info (click to toggle)
python-sponge 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 1,300 kB
  • ctags: 411
  • sloc: python: 2,870; makefile: 130
file content (354 lines) | stat: -rw-r--r-- 9,610 bytes parent folder | download | duplicates (2)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
# -*- coding: utf-8; -*-
#
# Copyright (C) 2009 Gabriel Falcão <gabriel@nacaolivre.org>
# Copyright (C) 2009 Bernardo Heynemann <heynemann@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import sys
import yaml
import cherrypy
import optparse

from os.path import abspath, join, dirname
from mox import Mox
from nose.tools import assert_equals
from utils import assert_raises

from sponge import bob
from StringIO import StringIO

basic_config = {
    'run-as': 'wsgi',
    'host': '0.0.0.0',
    'port': 4000,
    'autoreload': True,
    'application': {
        'classes': {
            'HelloWorldController': '/',
            'AjaxController': '/ajax',
        },
        'image-dir': None,
        'path': None,
        'template-dir': None,
        'static': {
            '/media': None,
        },
    },
}

def test_can_create_bob():
    b = bob.Bob()
    assert b
    assert isinstance(b, bob.Bob)

def test_run_fails_with_unknown_args():
    mox = Mox()

    mock_parser = mox.CreateMockAnything()
    mock_parser.parse_args().AndReturn(("options", ["args"]))

    mox.ReplayAll()

    b = bob.Bob(parser=mock_parser)
    sys.stderr = StringIO()
    assert_raises(SystemExit, b.run)
    assert_equals(sys.stderr.getvalue(),
                  '\nargs is an invalid argument, choose one ' \
                  'in create, go, start\n')
    sys.stderr = sys.__stderr__
    mox.VerifyAll()

def test_run_fails_without_args():
    mox = Mox()

    mock_parser = mox.CreateMockAnything()
    mock_parser.parse_args().AndReturn(("options", []))

    mox.ReplayAll()

    b = bob.Bob(parser=mock_parser)
    sys.stderr = StringIO()
    assert_raises(SystemExit, b.run)
    assert_equals(sys.stderr.getvalue(),
                  '\nmissing argument, choose one ' \
                  'in create, go, start\n')
    sys.stderr = sys.__stderr__
    mox.VerifyAll()

def test_run_calls_create_with_second_argument():
    mox = Mox()

    mock_parser = mox.CreateMockAnything()
    mock_parser.parse_args().AndReturn(("options", ['create', 'some']))
    b = bob.Bob(parser=mock_parser)
    b.create = mox.CreateMockAnything()
    b.create('some')

    mox.ReplayAll()
    b.run()
    mox.VerifyAll()

def test_run_calls_go():
    mox = Mox()

    mock_parser = mox.CreateMockAnything()
    mock_parser.parse_args().AndReturn(("options", ['go']))
    b = bob.Bob(parser=mock_parser)
    b.go = mox.CreateMockAnything()
    b.go()

    mox.ReplayAll()
    b.run()
    mox.VerifyAll()

def test_run_calls_start_with_second_argument():
    mox = Mox()

    mock_parser = mox.CreateMockAnything()
    mock_parser.parse_args().AndReturn(("options", ['start', 'some']))
    b = bob.Bob(parser=mock_parser)
    b.start = mox.CreateMockAnything()
    b.start('some')

    mox.ReplayAll()
    b.run()
    mox.VerifyAll()

def test_go_through_main_run():
    mox = Mox()
    bobby = bob.Bob
    old_sys = bob.sys

    mock_parser = mox.CreateMockAnything()
    file_system = mox.CreateMockAnything()

    bob.sys = mox.CreateMockAnything()

    bob_mock = mox.CreateMockAnything()
    bob_instance_mock = mox.CreateMockAnything()
    bob_instance_mock.run = mox.CreateMockAnything()

    bob_instance_mock.run().AndReturn(0)
    bob_mock.__call__(parser=mock_parser, fs=file_system).AndReturn(bob_instance_mock)
    bob.sys.exit(0)
    bob.Bob = bob_mock
    mox.ReplayAll()

    try:
        got = bob.run(parser=mock_parser, fs=file_system)
        mox.VerifyAll()
    finally:
        mox.UnsetStubs()
        bob.Bob = bobby
        bob.sys = old_sys

def test_exit_without_args():
    mox = Mox()
    mock_parser = mox.CreateMockAnything()

    b = bob.Bob(parser=mock_parser)
    assert_raises(SystemExit, b.exit, exc_pattern=r'1')

def test_exit_with_specific_exit_code():
    mox = Mox()
    mock_parser = mox.CreateMockAnything()

    b = bob.Bob(parser=mock_parser)
    assert_raises(SystemExit, b.exit, 100, exc_pattern=r'100')

def test_configure():
    mox = Mox()
    mock_parser = mox.CreateMockAnything()

    mox.StubOutWithMock(bob, 'yaml')
    config_validator = bob.ConfigValidator
    sponge_config = bob.SpongeConfig

    bob.ConfigValidator = mox.CreateMockAnything()
    bob.SpongeConfig = mox.CreateMockAnything()

    b = bob.Bob(parser=mock_parser)
    b.fs = mox.CreateMockAnything()

    b.fs.current_dir().AndReturn('should_be_current_dir')
    b.fs.current_dir('settings.yml'). \
         AndReturn('/current/path/settings-yaml')

    file_mock = mox.CreateMockAnything()
    b.fs.open('/current/path/settings-yaml', 'r'). \
         AndReturn(file_mock)

    file_mock.read().AndReturn('should-be-raw-yaml-text')
    bob.yaml.load('should-be-raw-yaml-text'). \
        AndReturn('should-be-config-dict')

    bob.ConfigValidator('should-be-config-dict'). \
        AndReturn('should-be-validator')

    config_mock = mox.CreateMockAnything()
    bob.SpongeConfig(cherrypy.config, 'should-be-validator'). \
        AndReturn(config_mock)

    config_mock.setup_all('should_be_current_dir')

    mox.ReplayAll()
    try:
        b.configure()
        mox.VerifyAll()
    finally:
        mox.UnsetStubs()
        bob.ConfigValidator = config_validator
        bob.SpongeConfig = sponge_config

def test_go():
    mox = Mox()
    mox.StubOutWithMock(bob, 'cherrypy')

    b = bob.Bob()
    b.configure = mox.CreateMockAnything()
    b.configure()
    bob.cherrypy.quickstart()
    mox.ReplayAll()
    try:
        b.go()
        mox.VerifyAll()
    finally:
        mox.UnsetStubs()

def test_create_fails_without_argument():
    b = bob.Bob()
    sys.stderr = StringIO()
    assert_raises(SystemExit, b.create, None)
    assert_equals(sys.stderr.getvalue(),
                  '\nmissing project name, try something like ' \
                  '"bob create foobar"\n')
    sys.stderr = sys.__stderr__

def test_create_fails_if_path_already_exists():
    mox = Mox()
    b = bob.Bob()
    b.fs = mox.CreateMockAnything()

    b.fs.current_dir('my-project'). \
         AndReturn('/full/path/to/my-project')

    b.fs.exists('/full/path/to/my-project'). \
         AndReturn(True)

    mox.ReplayAll()
    try:
        sys.stderr = StringIO()
        assert_raises(SystemExit, b.create, 'my-project')
        assert_equals(sys.stderr.getvalue(),
                      '\nThe path "/full/path/to/my-project" ' \
                      'already exists. Maybe you could choose ' \
                      'another name for your project ?\n')
    finally:
        sys.stderr = sys.__stderr__

def test_create_success():
    mox = Mox()
    b = bob.Bob()
    b.fs = mox.CreateMockAnything()
    b.fs.join = join

    mox.StubOutWithMock(bob, 'SpongeData')
    mox.StubOutWithMock(bob, 'yaml')

    full_path = '/full/path/to/my-project'
    b.fs.current_dir('my-project'). \
         AndReturn(full_path)

    b.fs.exists(full_path). \
         AndReturn(False)

    b.fs.mkdir(full_path)

    file_mock = mox.CreateMockAnything()
    b.fs.open(join(full_path, 'settings.yml'), 'w'). \
         AndReturn(file_mock)

    expected_dict = basic_config.copy()
    expected_dict['application'].update({
        'static': {
            '/media': join('media')
        },
        'path': join('app', 'controllers.py'),
        'image-dir': join('media', 'img'),
        'template-dir': join('templates'),
    })

    bob.yaml.dump(expected_dict, indent=True).AndReturn('should-be-a-yaml')
    file_mock.write('should-be-a-yaml')
    file_mock.close()

    bob.SpongeData.get_file('project.zip'). \
        AndReturn('should-be-path-to-zip-file')

    b.fs.extract_zip('should-be-path-to-zip-file', full_path)

    mox.ReplayAll()
    b.create('my-project')
    mox.VerifyAll()

def test_start():
    mox = Mox()
    b = bob.Bob()
    b.fs = mox.CreateMockAnything()
    b.create = mox.CreateMockAnything()
    b.go = mox.CreateMockAnything()

    b.create('foo-bar')
    b.fs.pushd('foo-bar')
    b.go()

    mox.ReplayAll()

    b.start('foo-bar')

    mox.VerifyAll()

def test_fix_yml():
    expected = """
test:
  with:
    Items: here
    And: Here
"""
    wrong = """
test:
  with: {Items: here, And: Here}
"""

    b = bob.Bob()

    got = b.fix_yml(wrong)

    assert_equals(got, expected)

def test_bob_help():
    b = bob.Bob()
    assert_equals(b.get_help(), "\n Sponge Bob is the responsible for " \
                  "managing\n    the user's application and its modules. " \
                  "\n\nTo use type %prog [options] or %prog -h (--help) " \
                  "for help with the available options\n\nACTIONS:\n\ncreate " \
                  "<projectname> - creates a new project, which means " \
                  "creating a new folder in current directory, named " \
                  "projectname\ngo start the cherrypy server using the " \
                  "configuration file settings.yml in current directory." \
                  "\nstart <projectname> executes both bob create and bob go")