From 5b9672e21ee9a0aa8e4557c28e082f25cccb7af7 Mon Sep 17 00:00:00 2001 From: reverend Date: Sun, 16 May 2021 13:03:41 +0200 Subject: [PATCH] Testing PlaceAssetCreateView --- django_lostplaces/lostplaces/models/place.py | 3 + .../lostplaces/templates/flat_form.html | 4 ++ .../lostplaces/tests/views/test_base_views.py | 58 +++++++++++++++++-- 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 django_lostplaces/lostplaces/templates/flat_form.html diff --git a/django_lostplaces/lostplaces/models/place.py b/django_lostplaces/lostplaces/models/place.py index 3feb78b..d11a4ab 100644 --- a/django_lostplaces/lostplaces/models/place.py +++ b/django_lostplaces/lostplaces/models/place.py @@ -98,6 +98,9 @@ class PlaceAsset(Submittable): null=True ) +class DummyAsset(PlaceAsset): + name = models.CharField(max_length=50) + class PlaceImage(PlaceAsset): """ PlaceImage defines an image file object that points to a file in uploads/. diff --git a/django_lostplaces/lostplaces/templates/flat_form.html b/django_lostplaces/lostplaces/templates/flat_form.html new file mode 100644 index 0000000..ae1dbf9 --- /dev/null +++ b/django_lostplaces/lostplaces/templates/flat_form.html @@ -0,0 +1,4 @@ +
+ {{form}} + +
\ No newline at end of file diff --git a/django_lostplaces/lostplaces/tests/views/test_base_views.py b/django_lostplaces/lostplaces/tests/views/test_base_views.py index 57e8ceb..169f217 100644 --- a/django_lostplaces/lostplaces/tests/views/test_base_views.py +++ b/django_lostplaces/lostplaces/tests/views/test_base_views.py @@ -3,14 +3,19 @@ from django.test import TestCase, RequestFactory, Client from django.urls import reverse_lazy +from django.db import models from django.contrib.auth.models import User, AnonymousUser from django.contrib.messages.storage.fallback import FallbackStorage from django.utils import timezone from django.shortcuts import render from lostplaces.models import Place -from lostplaces.views import IsAuthenticatedMixin +from lostplaces.views import ( + IsAuthenticatedMixin, + PlaceAssetCreateView +) from lostplaces.tests.views import ViewTestCase +from lostplaces.models import DummyAsset class TestIsAuthenticated(ViewTestCase): view = IsAuthenticatedMixin @@ -21,9 +26,6 @@ class TestIsAuthenticated(ViewTestCase): username='testpeter', password='Develop123' ) - - def setUp(self): - self.client = Client() def test_logged_in(self): request = RequestFactory().get('/') @@ -43,7 +45,6 @@ class TestIsAuthenticated(ViewTestCase): response = IsAuthenticatedMixin.as_view()(request) self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/'])) - response = self.client.get(response['Location']) self.assertTrue(len(messages) > 0) class TestIsPlaceSubmitterMixin(TestCase): @@ -88,4 +89,51 @@ class TestIsPlaceSubmitterMixin(TestCase): self.assertEqual(response.status_code, 403) self.assertTrue(response.context['messages']) self.assertTrue(len(response.context['messages']) > 0) + +class TestPlaceAssetCreateView(ViewTestCase): + view = PlaceAssetCreateView + + class DummyAssetCreateView(PlaceAssetCreateView): + model = DummyAsset + fields = ['name'] + template_name = 'flat_form.html' + + @classmethod + def setUpTestData(cls): + user = User.objects.create_user( + username='testpeter', + password='Develop123' + ) + + place = Place.objects.create( + name='Im a place', + submitted_when=timezone.now(), + submitted_by=user.explorer, + location='Testtown', + latitude=50.5, + longitude=7.0, + description='This is just a test, do not worry' + ) + + def test_create_asset(self): + request = RequestFactory().post( + '/', + { + 'name': 'im a place' + } + ) + request.user = User.objects.get(id=1) + + response = TestPlaceAssetCreateView.DummyAssetCreateView.as_view()(request, place_id=1) + + self.assertTrue( + DummyAsset.objects.all().count() >= 1, + msg='Expecting a dummy place asset to be created by the view' + ) + + self.assertEqual( + DummyAsset.objects.get(id=1).name, + 'im a place', + msg='Expecting the dummy place asset to have the correct data' + ) \ No newline at end of file