Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add more tests.

* add test for app specification
* add test for option passing
  • Loading branch information...
commit 8cc49c17b234385f504f18cdffffc4cd7a719bf2 1 parent a13cf03
@willkg authored
View
72 eadred/tests/test_generate.py
@@ -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)
View
3  examples/testproject/settings.py
@@ -1,3 +1,4 @@
INSTALLED_APPS = [
- 'testproject.testapp'
+ 'testproject.testapp',
+ 'testproject.testapp2',
]
View
13 examples/testproject/testapp/sampledata.py
@@ -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
View
0  examples/testproject/testapp2/__init__.py
No changes.
View
15 examples/testproject/testapp2/sampledata.py
@@ -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']
Please sign in to comment.
Something went wrong with that request. Please try again.