Permalink
Browse files

Add more tests.

* add test for app specification
* add test for option passing
  • Loading branch information...
1 parent dcd7a2e commit c484f1e0bd170dfd9c764ed8654f5059fecb42c7 @willkg committed Sep 29, 2012
@@ -1,11 +1,69 @@
+import imp
+import unittest
+
+from django.test.utils import override_settings
+from django.utils.importlib import import_module
+
+from nose.tools import eq_
+
from eadred.management.commands import generatedata
-def test_generatedata():
- """Basic test to make sure function gets called."""
- from testproject.testapp import sampledata
+class TestGenerateData(unittest.TestCase):
+ def reset_app_data(self):
+ from django.conf import settings
+ for app in settings.INSTALLED_APPS:
+ try:
+ app_path = import_module(app).__path__
+ except AttributeError:
+ continue
+
+ try:
+ imp.find_module('sampledata', app_path)
+ except ImportError:
+ continue
+
+ module = import_module('%s.sampledata' % app)
+ module.reset_app()
+
+ def setUp(self):
+ self.reset_app_data()
+
+ @override_settings(INSTALLED_APPS=['testproject.testapp'])
+ def test_generatedata(self):
+ """Test to make sure function gets called."""
+ from testproject.testapp import sampledata
+
+ cmd = generatedata.Command()
+ cmd.run_from_argv(['manage.py', 'generatedata'])
+ eq_(sampledata.called, True)
+
+ @override_settings(INSTALLED_APPS=['testproject.testapp2'])
+ def test_generatedata_params(self):
+ """Test options passing."""
+ from testproject.testapp2 import sampledata
+
+ cmd = generatedata.Command()
+ cmd.run_from_argv(
+ ['manage.py', 'generatedata', '--with=dataset=random'])
+ eq_(sampledata.called_with_param, 'random')
+
+ self.reset_app_data()
+
+ cmd.run_from_argv(
+ ['manage.py', 'generatedata', '--with=dataset=basic'])
+ eq_(sampledata.called_with_param, 'basic')
+
+ @override_settings(
+ INSTALLED_APPS=[
+ 'testproject.testapp',
+ 'testproject.testapp2'])
+ def test_generatedata_specified_apps(self):
+ """Test app specification."""
+ from testproject.testapp import sampledata as testapp_sampledata
+ from testproject.testapp2 import sampledata as testapp2_sampledata
- assert sampledata.called == False
- cmd = generatedata.Command()
- cmd.run_from_argv(['manage.py', ''])
- assert sampledata.called == True
+ cmd = generatedata.Command()
+ cmd.run_from_argv(['manage.py', 'generatedata', 'testproject.testapp'])
+ eq_(testapp_sampledata.called, True)
+ eq_(testapp2_sampledata.called_with_param, testapp2_sampledata.DEFAULT)
@@ -1,3 +1,4 @@
INSTALLED_APPS = [
- 'testproject.testapp'
+ 'testproject.testapp',
+ 'testproject.testapp2',
]
@@ -1,10 +1,15 @@
-called = False
+DEFAULT = False
+
+called = DEFAULT
+
+
+def reset_app():
+ global called
+ called = DEFAULT
def generate_sampledata(options):
- # Generate your sample data here.
- print 'Generate sampledata from test app!'
- print 'options', options
global called
+ assert called == DEFAULT
called = True
No changes.
@@ -0,0 +1,15 @@
+DEFAULT = None
+
+called_with_param = DEFAULT
+
+
+def reset_app():
+ global called_with_param
+ called_with_param = DEFAULT
+
+
+def generate_sampledata(options):
+ global called_with_param
+
+ assert called_with_param == DEFAULT
+ called_with_param = options['dataset']

0 comments on commit c484f1e

Please sign in to comment.