Removing option to make tags lowercase
[mediagoblin.git] / mediagoblin / tests / tools.py
CommitLineData
c5678c1a
CAW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011 Free Software Foundation, Inc
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
18import pkg_resources
19import os, shutil
20
623bee73 21from paste.deploy import loadapp
c5678c1a
CAW
22from webtest import TestApp
23
9ea5c28b 24from mediagoblin import util
421129b6 25from mediagoblin.init.config import read_mediagoblin_config
3aa4c668 26from mediagoblin.decorators import _make_safe
c5678c1a
CAW
27from mediagoblin.db.open import setup_connection_and_db_from_config
28
29
cfd2cbf3 30MEDIAGOBLIN_TEST_DB_NAME = u'__mediagoblin_tests__'
623bee73 31TEST_SERVER_CONFIG = pkg_resources.resource_filename(
5c441e75 32 'mediagoblin.tests', 'test_paste.ini')
c5678c1a 33TEST_APP_CONFIG = pkg_resources.resource_filename(
623bee73 34 'mediagoblin.tests', 'test_mgoblin_app.ini')
c5678c1a
CAW
35TEST_USER_DEV = pkg_resources.resource_filename(
36 'mediagoblin.tests', 'test_user_dev')
37MGOBLIN_APP = None
38
39USER_DEV_DIRECTORIES_TO_SETUP = [
40 'media/public', 'media/queue',
41 'beaker/sessions/data', 'beaker/sessions/lock']
42
29f1333e
CAW
43BAD_CELERY_MESSAGE = """\
44Sorry, you *absolutely* must run nosetests with the
073b61fe
E
45mediagoblin.init.celery.from_tests module. Like so:
46$ CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_tests ./bin/nosetests"""
29f1333e 47
c5678c1a
CAW
48
49class BadCeleryEnviron(Exception): pass
50
51
29f1333e 52def suicide_if_bad_celery_environ():
eaca7874 53 if not os.environ.get('CELERY_CONFIG_MODULE') == \
073b61fe 54 'mediagoblin.init.celery.from_tests':
29f1333e
CAW
55 raise BadCeleryEnviron(BAD_CELERY_MESSAGE)
56
57
58def get_test_app(dump_old_app=True):
59 suicide_if_bad_celery_environ()
623bee73 60
9ea5c28b 61 # Leave this imported as it sets up celery.
073b61fe 62 from mediagoblin.init.celery import from_tests
9ea5c28b 63
623bee73 64 global MGOBLIN_APP
c5678c1a
CAW
65
66 # Just return the old app if that exists and it's okay to set up
67 # and return
68 if MGOBLIN_APP and not dump_old_app:
69 return MGOBLIN_APP
70
71 # Remove and reinstall user_dev directories
72 if os.path.exists(TEST_USER_DEV):
73 shutil.rmtree(TEST_USER_DEV)
74
75 for directory in USER_DEV_DIRECTORIES_TO_SETUP:
76 full_dir = os.path.join(TEST_USER_DEV, directory)
77 os.makedirs(full_dir)
78
79 # Get app config
623bee73
CAW
80 global_config, validation_result = read_mediagoblin_config(TEST_APP_CONFIG)
81 app_config = global_config['mediagoblin']
c5678c1a
CAW
82
83 # Wipe database
84 # @@: For now we're dropping collections, but we could also just
85 # collection.remove() ?
623bee73 86 connection, db = setup_connection_and_db_from_config(app_config)
cfd2cbf3 87 assert db.name == MEDIAGOBLIN_TEST_DB_NAME
c5678c1a
CAW
88
89 collections_to_wipe = [
90 collection
91 for collection in db.collection_names()
92 if not collection.startswith('system.')]
93
94 for collection in collections_to_wipe:
95 db.drop_collection(collection)
96
c5678c1a
CAW
97 # TODO: Drop and recreate indexes
98
99 # setup app and return
0a791a94 100 test_app = loadapp(
623bee73
CAW
101 'config:' + TEST_SERVER_CONFIG)
102
103 app = TestApp(test_app)
104 MGOBLIN_APP = app
105
623bee73 106 return app
3aa4c668
CAW
107
108
109def setup_fresh_app(func):
110 """
111 Decorator to setup a fresh test application for this function.
112
113 Cleans out test buckets and passes in a new, fresh test_app.
114 """
115 def wrapper(*args, **kwargs):
116 test_app = get_test_app()
117 util.clear_test_buckets()
118 return func(test_app, *args, **kwargs)
119
120 return _make_safe(wrapper, func)
85663692
CAW
121
122
123def install_fixtures_simple(db, fixtures):
124 """
125 Very simply install fixtures in the database
126 """
127 for collection_name, collection_fixtures in fixtures.iteritems():
128 collection = db[collection_name]
129 for fixture in collection_fixtures:
130 collection.insert(fixture)
131
132
133def assert_db_meets_expected(db, expected):
134 """
135 Assert a database contains the things we expect it to.
136
137 Objects are found via '_id', so you should make sure your document
138 has an _id.
139
140 Args:
141 - db: pymongo or mongokit database connection
142 - expected: the data we expect. Formatted like:
143 {'collection_name': [
144 {'_id': 'foo',
145 'some_field': 'some_value'},]}
146 """
147 for collection_name, collection_data in expected.iteritems():
148 collection = db[collection_name]
149 for expected_document in collection_data:
150 document = collection.find_one({'_id': expected_document['_id']})
151 assert document is not None # make sure it exists
152 assert document == expected_document # make sure it matches