Yet another class to simplify field choices creation. Keeps order, allows i18n.
Before:
ONLINE = 0
OFFLINE = 1
STATES = (
(ONLINE, _('online')),
(OFFLINE, _('offline'))
)
state = models.IntegerField(choices=STATES, default=OFFLINE)
After:
STATES = Choices(
('ONLINE', _('online')),
('OFFLINE', _('offline'))
)
state = models.IntegerField(choices=STATES, default=STATES.OFFLINE)
This TestSettingsManager class takes some of the pain out of making temporary changes to settings for the purposes of a unittest or doctest. It will keep track of the original settings and let you easily revert them back when you're done.
It also handles re-syncing the DB if you modify INSTALLED_APPS, which is especially handy if you have some test-only models in tests/models.py. This makes it easy to dynamically get those models synced to the DB before running your tests.
Sample doctest usage, for testing an app called "app_under_test," that has a tests/ sub-module containing a urls.py for testing URLs, a models.py with some testing models, and a templates/ directory with test templates:
>>> from test_utils import TestManager; mgr = TestManager()
>>> import os
>>> mgr.set(INSTALLED_APPS=('django.contrib.contenttypes',
... 'django.contrib.sessions',
... 'django.contrib.auth',
... 'app_under_test',
... 'app_under_test.tests'),
... ROOT_URLCONF='app_under_test.tests.urls',
... TEMPLATE_DIRS=(os.path.join(os.path.dirname(__file__),
... 'templates'),))
...do your doctests...
>>> mgr.revert()