Commits

Branko Vukelic committed e92972e

Added complete test suite

Comments (0)

Files changed (4)

+#!/usr/bin/env python
+
+from django.conf import settings, global_settings
+
+if not settings.configured:
+    settings.configure(
+        DATABASES={
+            'default': {
+                'ENGINE': 'django.db.backends.sqlite3',
+            }
+        },
+        INSTALLED_APPS=[
+            'related',
+            'tests',
+        ],
+        ROOT_URLCONF='',
+        DEBUG=False,
+        SITE_ID=1,
+    )
+
+
+import sys
+from os.path import dirname, abspath
+from optparse import OptionParser
+from django.test.simple import DjangoTestSuiteRunner
+
+def runtests(*test_args, **kwargs):
+    if 'south' in settings.INSTALLED_APPS:
+        from south.management.commands import patch_for_test_db_setup
+        patch_for_test_db_setup()
+
+    if not test_args:
+        test_args = ['tests']
+    parent = dirname(abspath(__file__))
+    sys.path.insert(0, parent)
+    test_runner = DjangoTestSuiteRunner(verbosity=kwargs.get('verbosity', 1),
+                                        interactive=kwargs.get('interactive',
+                                                               False),
+                                        failfast=kwargs.get('failfast'))
+    failures = test_runner.run_tests(test_args)
+    sys.exit(failures)
+
+if __name__ == '__main__':
+    parser = OptionParser()
+    parser.add_option('--failfast', action='store_true', default=False, dest='failfast')
+
+    (options, args) = parser.parse_args()
+
+    runtests(failfast=options.failfast, *args)
+

tests/__init__.py

Empty file added.

tests/models.py

Empty file added.
+from django.test import TestCase
+from mock import Mock, patch
+
+from related.views import GetExistingMixin, CreateWithRelatedMixin
+
+
+class GetExistingMixinTestCase(TestCase):
+    def setUp(self):
+        self.request = Mock()
+        self.request.REQUEST = Mock()
+        self.request.REQUEST.get = Mock()
+        self.view = GetExistingMixin()
+        self.view.request = self.request
+
+    def test_get_existing_redirect_url(self):
+        self.view.existing_redirect_url = '/foo'
+        self.assertEqual(self.view.get_existing_redirect_url(Mock()), '/foo')
+
+    def test_get_existing_kwargs_pk(self):
+        kwargs = self.view.get_existing_kwargs()
+        self.request.REQUEST.get.assert_called_once_with('pk')
+        self.assertEqual(kwargs['pk'], self.request.REQUEST.get.return_value)
+
+    def test_get_existing_kwargs_slug(self):
+        def get_mock(key):
+            if key == 'slug':
+                return 'foo'
+
+        self.request.REQUEST.get = get_mock
+        kwargs = self.view.get_existing_kwargs()
+        self.assertEqual(kwargs['slug'], 'foo')
+        self.assertFalse(kwargs.has_key('pk'))
+
+    def test_get_existing_from_request(self):
+        self.view.model = Mock()
+        self.view.model.objects = Mock()
+        self.view.model.objects.get = Mock()
+        self.view.model.objects.get
+        existing = self.view.get_existing_from_request()
+        self.assertEqual(existing, self.view.model.objects.get.return_value)
+
+    def test_get_existing_form_kwargs(self):
+        self.request.method = 'GET'
+        kwargs = self.view.get_existing_form_kwargs()
+        self.assertEqual(kwargs, dict(initial=dict()))
+
+        self.request.method = 'POST'
+        self.request.POST = 'foo'
+        self.assertEqual(
+            self.view.get_existing_form_kwargs(),
+            dict(data='foo', initial=dict())
+        )
+
+        self.request.method = 'PUT'
+        self.request.POST = 'bar'
+        self.assertEqual(
+            self.view.get_existing_form_kwargs(),
+            dict(data='bar', initial=dict())
+        )
+
+    def test_existing_form(self):
+        self.request.method = 'POST'
+        self.request.POST = dict(foo='bar')
+        self.view.existing_form_class = Mock()
+        form = self.view.get_existing_form()
+        self.view.existing_form_class.assert_called_once_with(
+            initial=dict(), data=dict(foo='bar')
+        )
+
+    def test_existing_form_with_override(self):
+        self.request.method = 'POST'
+        self.request.POST = dict(foo='bar')
+        self.view.existing_form_class = Mock()
+        form = self.view.get_existing_form(data=dict(fam='dam'))
+        self.view.existing_form_class.assert_called_once_with(
+            initial=dict(), data=dict(fam='dam')
+        )
+
+    def test_get_existing_from_form(self):
+        mock_form_instance = Mock()
+        mock_form_instance.cleaned_data = dict(foo=123)
+        mock_form_instance.is_valid = Mock()
+        mock_form_instance.is_valid.return_value = True
+
+        self.view.existing_form_field = 'foo'
+        self.view.existing_form_class = Mock()
+        self.view.existing_form_class.return_value = mock_form_instance
+
+        existing = self.view.get_existing_from_form()
+        self.assertEqual(existing, 123)
+
+    def test_get_existing(self):
+        self.view.get_existing_from_form = Mock()
+        self.view.get_existing_from_request = Mock()
+
+        self.view.get_existing()
+        self.view.get_existing_from_form.assert_not_called()
+        self.view.get_existing_from_request.assert_called()
+
+        self.view.existing_form_class = True
+
+        self.view.get_existing()
+        self.view.get_existing_from_form.assert_called()
+        self.view.get_existing_from_request.assert_not_called()
+
+    def test_get_context(self):
+        class Super(object):
+            def get_context_data(self):
+                return dict(foo='bar')
+
+        class View(GetExistingMixin, Super):
+            pass
+
+        view = View()
+
+        view.request = Mock()
+        view.request.GET = dict(fam='dam')
+        view.get_existing_form = Mock()
+
+        ctx = view.get_context_data()
+
+        self.assertTrue(ctx.has_key('existing_form'))
+
+    def test_post(self):
+        class Super(object):
+            def post(self, request):
+                return 'foo'
+
+        class View(GetExistingMixin, Super):
+            pass
+
+        view = View()
+        view.get_existing = Mock()
+        view.get_existing.return_value = None
+
+        response = view.post(Mock())
+
+        self.assertEqual(response, 'foo')
+
+    def test_post_with_existing(self):
+        from django.http import HttpResponseRedirect
+
+        class Super(object):
+            post = Mock()
+
+        class View(GetExistingMixin, Super):
+            pass
+
+        view = View()
+        view.get_existing = Mock()
+        view.get_existing.return_value = True
+        view.get_existing_redirect_url = lambda x: '/foo'
+
+        response = view.post(Mock())
+        self.assertIsInstance(response, HttpResponseRedirect)
+
+
+class CreateWithRelatedMixinTestCase(TestCase):
+    def setUp(self):
+        self.request = Mock()
+        self.request.GET = Mock()
+        self.request.POST = Mock()
+        self.request.GET.get = Mock()
+        self.request.POST.get = Mock()
+
+        self.view = CreateWithRelatedMixin()
+        self.view.request = self.request
+
+    def test_get_related_field(self):
+        self.view.related_model = Mock()
+        self.view.related_model._meta = Mock()
+        self.view.related_model._meta.verbose_name = 'foo'
+
+        rf = self.view.get_related_field()
+        self.assertEqual(rf, 'foo')
+
+        self.view.related_field = 'bar'
+        rf = self.view.get_related_field()
+        self.assertEqual(rf, 'bar')
+
+    def test_get_object_kwargs_with_pk(self):
+        self.view.kwargs = dict(pk='foo')
+        kwargs = self.view.get_object_kwargs()
+        self.assertEqual(kwargs, dict(pk='foo'))
+
+    def test_get_object_kwargs_with_slug(self):
+        self.view.kwargs = dict(slug='bar')
+        kwargs = self.view.get_object_kwargs()
+        self.assertEqual(kwargs, dict(slug='bar'))
+
+    def test_get_related_obj(self):
+        self.view.related_model = Mock()
+        self.view.related_model.objects = Mock()
+        self.view.related_model.objects.get = Mock()
+        self.view.get_object_kwargs = Mock()
+        self.view.get_object_kwargs.return_value = dict(pk='foo')
+
+        obj = self.view.get_related_object()
+        self.assertEqual(obj, self.view.related_model.objects.get.return_value)
+
+    def test_related_object_not_found(self):
+        from django.http import Http404, HttpResponseRedirect
+
+        self.view.related_404_redirect_url = '/foo'
+        response = self.view.related_object_not_found()
+        self.assertIsInstance(response, HttpResponseRedirect)
+
+        self.view.related_404_redirect_url = None
+
+        with self.assertRaises(Http404):
+            self.view.related_object_not_found()
+
+    def test_form_valid(self):
+        self.view.get_related_field = Mock()
+        self.view.get_related_field.return_value = 'foo'
+        self.view.get_related_object = Mock()
+        self.view.get_related_object.return_value = True
+        self.view.get_success_url = Mock()
+        self.view.get_success_url.return_value = '/foo'
+
+        mock_form = Mock()
+        mock_form.cleaned_data = dict(fam='dam')
+
+        mock_object = Mock()
+        mock_object.save = Mock()
+
+        self.view.model = Mock()
+        self.view.model.return_value = mock_object
+
+        self.view.form_valid(mock_form)
+
+        self.view.model.assert_called_once_with(fam='dam')
+        self.view.object = mock_object
+
+        mock_object.save.assert_called_once()
+        self.view.object.foo = self.view.get_related_object.return_value