Ticket #1763: test_env.2.py

File test_env.2.py, 1.7 KB (added by crankycoder@…, 18 years ago)

A module that has an init_ramdb() function that will replace your database with a SQLite memory version

Line 
1import os, glob, sys
2
3rootpath = os.getcwd()
4while True:
5 files = [s for s in list(glob.glob("%s/*" % rootpath)) if s.endswith('settings.py')]
6 print files
7 if len(files) == 1:
8 rootpath, site_name = os.path.split(rootpath)
9 break
10 rootpath = os.path.split(rootpath)[0]
11
12rootpath = os.path.split(rootpath)[0]
13
14sys.path.append(rootpath)
15
16settings_module = '%s.settings' % site_name
17os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
18__import__(settings_module)
19
20def init_ramdb():
21 # Change Settings
22 from django.conf import settings
23 settings.DATABASE_NAME = ':memory:'
24 settings.DATABASE_ENGINE = 'sqlite3'
25
26 # Reload DB module
27 import django.db
28 import django.db.transaction
29
30 # Make sure we kill the old connection
31 del django.db.connection
32 reload(django.db)
33
34 # Force the transaction code to have a new connection
35 django.db.transaction.connection = django.db.connection
36
37 # Install Models
38 from django.core import management
39 from django.contrib.auth.management import create_superuser
40 from django.dispatch import dispatcher
41 from django.db.models import signals
42 from django.contrib.auth import models as auth_app
43
44 # We need to disconnect the create_superuser command
45 receivers = dispatcher.getReceivers(sender=auth_app, \
46 signal=signals.post_syncdb)
47
48 # Receivers are a list of weak references, so we need to resolve those refs
49 # to strong references
50 receivers = [r() for r in receivers]
51 if create_superuser in receivers:
52 dispatcher.disconnect(create_superuser, \
53 sender=auth_app, \
54 signal=signals.post_syncdb)
55 management.syncdb()
56
57init_ramdb()
Back to Top