a51402e9d90c7b12cf8d7252f5fadbaf9e355b96
[mediagoblin.git] / mediagoblin / tests / tools.py
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
18 import pkg_resources
19 import os, shutil
20
21 from paste.deploy import appconfig, loadapp
22 from webtest import TestApp
23
24 from mediagoblin.db.open import setup_connection_and_db_from_config
25
26
27 MEDIAGOBLIN_TEST_DB_NAME = '__mediagoblinunittests__'
28 TEST_APP_CONFIG = pkg_resources.resource_filename(
29 'mediagoblin.tests', 'mgoblin_test_app.ini')
30 TEST_USER_DEV = pkg_resources.resource_filename(
31 'mediagoblin.tests', 'test_user_dev')
32 MGOBLIN_APP = None
33
34 USER_DEV_DIRECTORIES_TO_SETUP = [
35 'media/public', 'media/queue',
36 'beaker/sessions/data', 'beaker/sessions/lock']
37
38
39 class BadCeleryEnviron(Exception): pass
40
41
42 def get_test_app(dump_old_app=True):
43 if not os.environ.get('CELERY_CONFIG_MODULE') == \
44 'mediagoblin.celery_setup.from_tests':
45 raise BadCeleryEnviron(
46 u"Sorry, you *absolutely* must run nosetests with the\n"
47 u"mediagoblin.celery_setup.from_tests module. Like so:\n"
48 u"$ CELERY_CONFIG_MODULE=mediagoblin.celery_setup.from_tests ./bin/nosetests")
49
50 # Just return the old app if that exists and it's okay to set up
51 # and return
52 if MGOBLIN_APP and not dump_old_app:
53 return MGOBLIN_APP
54
55 # Remove and reinstall user_dev directories
56 if os.path.exists(TEST_USER_DEV):
57 shutil.rmtree(TEST_USER_DEV)
58
59 for directory in USER_DEV_DIRECTORIES_TO_SETUP:
60 full_dir = os.path.join(TEST_USER_DEV, directory)
61 os.makedirs(full_dir)
62
63 # Get app config
64 config = appconfig(
65 'config:' + os.path.basename(TEST_APP_CONFIG),
66 relative_to=os.path.dirname(TEST_APP_CONFIG),
67 name='mediagoblin')
68
69 # Wipe database
70 # @@: For now we're dropping collections, but we could also just
71 # collection.remove() ?
72 connection, db = setup_connection_and_db_from_config(
73 config.local_conf)
74
75 collections_to_wipe = [
76 collection
77 for collection in db.collection_names()
78 if not collection.startswith('system.')]
79
80 for collection in collections_to_wipe:
81 db.drop_collection(collection)
82
83 # Don't need these anymore...
84 del(connection)
85 del(db)
86
87 # TODO: Drop and recreate indexes
88
89 # setup app and return
90 test_app = loadapp(
91 'config:' + TEST_APP_CONFIG)
92
93 return TestApp(test_app)