Finished splitting util.py into separate files.
[mediagoblin.git] / mediagoblin / tests / tools.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 import pkg_resources
19 import os, shutil
20
21 from paste.deploy import loadapp
22 from webtest import TestApp
23
24 from mediagoblin.tools import testing
25 from mediagoblin.init.config import read_mediagoblin_config
26 from mediagoblin.decorators import _make_safe
27 from mediagoblin.db.open import setup_connection_and_db_from_config
28
29
30 MEDIAGOBLIN_TEST_DB_NAME = u'__mediagoblin_tests__'
31 TEST_SERVER_CONFIG = pkg_resources.resource_filename(
32 'mediagoblin.tests', 'test_paste.ini')
33 TEST_APP_CONFIG = pkg_resources.resource_filename(
34 'mediagoblin.tests', 'test_mgoblin_app.ini')
35 TEST_USER_DEV = pkg_resources.resource_filename(
36 'mediagoblin.tests', 'test_user_dev')
37 MGOBLIN_APP = None
38
39 USER_DEV_DIRECTORIES_TO_SETUP = [
40 'media/public', 'media/queue',
41 'beaker/sessions/data', 'beaker/sessions/lock']
42
43 BAD_CELERY_MESSAGE = """\
44 Sorry, you *absolutely* must run nosetests with the
45 mediagoblin.init.celery.from_tests module. Like so:
46 $ CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_tests ./bin/nosetests"""
47
48
49 class BadCeleryEnviron(Exception): pass
50
51
52 def suicide_if_bad_celery_environ():
53 if not os.environ.get('CELERY_CONFIG_MODULE') == \
54 'mediagoblin.init.celery.from_tests':
55 raise BadCeleryEnviron(BAD_CELERY_MESSAGE)
56
57
58 def get_test_app(dump_old_app=True):
59 suicide_if_bad_celery_environ()
60
61 # Make sure we've turned on testing
62 testing._activate_testing()
63
64 # Leave this imported as it sets up celery.
65 from mediagoblin.init.celery import from_tests
66
67 global MGOBLIN_APP
68
69 # Just return the old app if that exists and it's okay to set up
70 # and return
71 if MGOBLIN_APP and not dump_old_app:
72 return MGOBLIN_APP
73
74 # Remove and reinstall user_dev directories
75 if os.path.exists(TEST_USER_DEV):
76 shutil.rmtree(TEST_USER_DEV)
77
78 for directory in USER_DEV_DIRECTORIES_TO_SETUP:
79 full_dir = os.path.join(TEST_USER_DEV, directory)
80 os.makedirs(full_dir)
81
82 # Get app config
83 global_config, validation_result = read_mediagoblin_config(TEST_APP_CONFIG)
84 app_config = global_config['mediagoblin']
85
86 # Wipe database
87 # @@: For now we're dropping collections, but we could also just
88 # collection.remove() ?
89 connection, db = setup_connection_and_db_from_config(app_config)
90 assert db.name == MEDIAGOBLIN_TEST_DB_NAME
91
92 collections_to_wipe = [
93 collection
94 for collection in db.collection_names()
95 if not collection.startswith('system.')]
96
97 for collection in collections_to_wipe:
98 db.drop_collection(collection)
99
100 # TODO: Drop and recreate indexes
101
102 # setup app and return
103 test_app = loadapp(
104 'config:' + TEST_SERVER_CONFIG)
105
106 app = TestApp(test_app)
107 MGOBLIN_APP = app
108
109 return app
110
111
112 def setup_fresh_app(func):
113 """
114 Decorator to setup a fresh test application for this function.
115
116 Cleans out test buckets and passes in a new, fresh test_app.
117 """
118 def wrapper(*args, **kwargs):
119 test_app = get_test_app()
120 testing.clear_test_buckets()
121 return func(test_app, *args, **kwargs)
122
123 return _make_safe(wrapper, func)
124
125
126 def install_fixtures_simple(db, fixtures):
127 """
128 Very simply install fixtures in the database
129 """
130 for collection_name, collection_fixtures in fixtures.iteritems():
131 collection = db[collection_name]
132 for fixture in collection_fixtures:
133 collection.insert(fixture)
134
135
136 def assert_db_meets_expected(db, expected):
137 """
138 Assert a database contains the things we expect it to.
139
140 Objects are found via '_id', so you should make sure your document
141 has an _id.
142
143 Args:
144 - db: pymongo or mongokit database connection
145 - expected: the data we expect. Formatted like:
146 {'collection_name': [
147 {'_id': 'foo',
148 'some_field': 'some_value'},]}
149 """
150 for collection_name, collection_data in expected.iteritems():
151 collection = db[collection_name]
152 for expected_document in collection_data:
153 document = collection.find_one({'_id': expected_document['_id']})
154 assert document is not None # make sure it exists
155 assert document == expected_document # make sure it matches