From baca596603cf61781b605730059b64c9a8687e17 Mon Sep 17 00:00:00 2001 From: Leonhard Strohmidel Date: Fri, 11 Sep 2020 23:07:19 +0200 Subject: [PATCH] sync --- Pipfile | 5 +- lostplaces/lostplaces_app/tests/__init__.py | 14 +++--- .../lostplaces_app/tests/models/__init__.py | 7 +++ .../tests/models/test_explorer_model.py | 15 +++--- .../tests/models/test_place_image_model.py | 29 ++++++------ .../tests/models/test_place_model.py | 46 ++++++++++--------- .../tests/models/test_voucher_model.py | 15 +++--- .../lostplaces_app/tests/test_models.py | 0 .../tests/views/test_base_views.py | 7 +-- .../tests/views/test_place_views.py | 5 -- 10 files changed, 70 insertions(+), 73 deletions(-) delete mode 100644 lostplaces/lostplaces_app/tests/test_models.py diff --git a/Pipfile b/Pipfile index d2312fc..e7e2fd6 100644 --- a/Pipfile +++ b/Pipfile @@ -19,6 +19,7 @@ easy-thumbnails = "*" image = "*" django-widget-tweaks = "*" django-taggit = "*" + # Commented out to not explicitly specify Python 3 subversion. -# [requires] -# python_version = "3.8" +[requires] +python_version = "3.7" diff --git a/lostplaces/lostplaces_app/tests/__init__.py b/lostplaces/lostplaces_app/tests/__init__.py index 7eae89c..c06a02a 100644 --- a/lostplaces/lostplaces_app/tests/__init__.py +++ b/lostplaces/lostplaces_app/tests/__init__.py @@ -1,13 +1,11 @@ -from django.db import models as django_models +from django.test import TestCase from django.contrib.auth.models import User +class BaseData(TestCase): -def mock_user(): - explorer_list = User.objects.all() - if len(explorer_list) <= 0: - return User.objects.create_user( + @classmethod + def setUpTestData(cls): + User.objects.create_user( username='testpeter', password='Develop123' - ) - else: - return explorer_list[0] \ No newline at end of file + ).save() \ No newline at end of file diff --git a/lostplaces/lostplaces_app/tests/models/__init__.py b/lostplaces/lostplaces_app/tests/models/__init__.py index 26914a2..f822493 100644 --- a/lostplaces/lostplaces_app/tests/models/__init__.py +++ b/lostplaces/lostplaces_app/tests/models/__init__.py @@ -1,14 +1,21 @@ from django.db import models +from django.contrib.auth.models import User from django.core.exceptions import FieldDoesNotExist from django.test import TestCase +# Creating a test user class ModelTestCase: ''' Base class for Lostplaces models ''' + model = None model_name = None + def setUp(self): + self.object = self.model.objects.get(id=1) + self.model_name = type(self.model).__name__ + def _test_field(self, field_name, field_class, must_have={}, must_not_have={}): ''' Tests if a field exists under the given name and diff --git a/lostplaces/lostplaces_app/tests/models/test_explorer_model.py b/lostplaces/lostplaces_app/tests/models/test_explorer_model.py index 825251d..0b39171 100644 --- a/lostplaces/lostplaces_app/tests/models/test_explorer_model.py +++ b/lostplaces/lostplaces_app/tests/models/test_explorer_model.py @@ -3,13 +3,16 @@ from django.db import models from django.contrib.auth.models import User from lostplaces_app.models import Explorer -from lostplaces_app.tests import mock_user class ExplorerTestCase(TestCase): - - def setUp(self): - mock_user() - + + @classmethod + def setUpTestData(self): + User.objects.create_user( + username='testpeter', + + ) + def test_epxlorer_creation(self): ''' Tests if the explorer profile will be automticly @@ -43,7 +46,7 @@ class ExplorerTestCase(TestCase): Tests if the Explorer objects get's deleted when the User instance is deleted ''' - user = User.objects.get(id=1) + user = User.objects.get(username='testpeter') explorer_id = user.explorer.id user.delete() with self.assertRaises(models.ObjectDoesNotExist, diff --git a/lostplaces/lostplaces_app/tests/models/test_place_image_model.py b/lostplaces/lostplaces_app/tests/models/test_place_image_model.py index baa2dc6..f3d9f85 100644 --- a/lostplaces/lostplaces_app/tests/models/test_place_image_model.py +++ b/lostplaces/lostplaces_app/tests/models/test_place_image_model.py @@ -8,36 +8,33 @@ from django.db import models from django.core.files import File from django.conf import settings -from lostplaces_app.models import PlaceImage +from lostplaces_app.models import PlaceImage, Place from lostplaces_app.tests.models import SubmittableTestCase -from lostplaces_app.tests import mock_user -from lostplaces_app.tests.models.test_place_model import mock_place +from lostplaces_app.tests.models.test_place_model Test from easy_thumbnails.fields import ThumbnailerImageField -def mock_place_image(): - all_place_images = PlaceImage.objects.all() - if len(all_place_images) <= 0: +class TestPlaceImage(SubmittableTestCase, TestCase): + model = PlaceImage + + @classmethod + def setUpTestData(cls): + + Place.setUpTestData() + current_dir = os.path.dirname(os.path.abspath(__file__)) if not os.path.isfile(os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')): shutil.copyfile( os.path.join(current_dir, 'im_a_image.jpeg'), os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg') ) - return PlaceImage.objects.create( + PlaceImage.objects.create( description='Im a description', filename=os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg'), - place=mock_place(), + place=Place.objects.get(id=1), submitted_when=datetime.datetime.now(), - submitted_by=mock_user().explorer + submitted_by=User.objects.get(username='testpeter').explorer ) - return all_place_images[0] - -class TestPlaceImage(SubmittableTestCase, TestCase): - model_name = 'PlaceImage' - - def setUp(self): - self.object = mock_place_image() def test_description(self): self._test_field('description', models.TextField) diff --git a/lostplaces/lostplaces_app/tests/models/test_place_model.py b/lostplaces/lostplaces_app/tests/models/test_place_model.py index 2ca5ef5..de34ec9 100644 --- a/lostplaces/lostplaces_app/tests/models/test_place_model.py +++ b/lostplaces/lostplaces_app/tests/models/test_place_model.py @@ -3,34 +3,36 @@ import datetime from django.test import TestCase from django.db import models +from django.contrib.auth.models import User + from lostplaces_app.models import Place +from lostplaces_app.tests import BaseData from lostplaces_app.tests.models import SubmittableTestCase -from lostplaces_app.tests import mock_user + from taggit.managers import TaggableManager -def mock_place(): - place = Place.objects.create( - name='Im a place', - submitted_when=datetime.datetime.now(), - submitted_by=mock_user().explorer, - location='Testtown', - latitude=50.5, - longitude=7.0, - description='This is just a test, do not worry' - ) - place.tags.add('I a tag', 'testlocation') - - return place - class PlaceTestCase(SubmittableTestCase, TestCase): - model_name = 'Place' + model = Place related_name = 'places' nullable = True - def setUp(self): - self.place = mock_place() - self.object = self.place + @classmethod + def setUpTestData(cls): + + BaseData.setUpTestData() + + place = Place.objects.create( + name='Im a place', + submitted_when=datetime.datetime.now(), + submitted_by=User.objects.get(username='testpeter').explorer, + location='Testtown', + latitude=50.5, + longitude=7.0, + description='This is just a test, do not worry' + ) + place.tags.add('I a tag', 'testlocation') + place.save() def test_name_field(self): self._test_char_field( @@ -72,10 +74,12 @@ class PlaceTestCase(SubmittableTestCase, TestCase): of 10 places ''' place_list = [] + place = Place.objects.get(id=1) + place.id = None for i in range(10): - place = mock_place() place.latitude = i+1 place.longitude = i+10 + place.save() place_list.append(place) avg_latlon = Place.average_latlon(place_list) @@ -95,7 +99,7 @@ class PlaceTestCase(SubmittableTestCase, TestCase): Tests the average latitude/longitude calculation of a list of one place ''' - place = mock_place() + place = Place.objects.get(id=1) avg_latlon = Place.average_latlon([place]) self.assertEqual(avg_latlon[0], place.latitude, msg='%s:(one place) average latitude missmatch' % ( diff --git a/lostplaces/lostplaces_app/tests/models/test_voucher_model.py b/lostplaces/lostplaces_app/tests/models/test_voucher_model.py index 89c733e..ffc0c93 100644 --- a/lostplaces/lostplaces_app/tests/models/test_voucher_model.py +++ b/lostplaces/lostplaces_app/tests/models/test_voucher_model.py @@ -7,19 +7,16 @@ from lostplaces_app.models import Voucher from lostplaces_app.tests.models import ModelTestCase -def mock_voucher(): - return Voucher.objects.create( +class VoucheTestCase(ModelTestCase, TestCase): + model = Voucher + + @classmethod + def setUpTestData(cls): + Voucher.objects.create( code='ayDraJCCwfhcFiYmSR5GrcjcchDfcahv', expires=datetime.datetime.now() + datetime.timedelta(days=1) ) - -class VoucheTestCase(ModelTestCase, TestCase): - model_name = 'Voucher' - - def setUp(self): - self.object = mock_voucher() - def test_voucher_code(self): self._test_char_field( 'code', diff --git a/lostplaces/lostplaces_app/tests/test_models.py b/lostplaces/lostplaces_app/tests/test_models.py deleted file mode 100644 index e69de29..0000000 diff --git a/lostplaces/lostplaces_app/tests/views/test_base_views.py b/lostplaces/lostplaces_app/tests/views/test_base_views.py index 60a03d4..ab6ae96 100644 --- a/lostplaces/lostplaces_app/tests/views/test_base_views.py +++ b/lostplaces/lostplaces_app/tests/views/test_base_views.py @@ -4,14 +4,11 @@ from django.urls import reverse_lazy from lostplaces_app.models import Place from django.contrib.auth.models import User -from lostplaces_app.tests.models.test_place_model import mock_place -from lostplaces_app.tests import mock_user class TestIsAuthenticated(TestCase): + def setUp(self): self. client = Client() - mock_place() - mock_user() def test_logged_in(self): self.client.login(username='testpeter', password='Develop123') @@ -38,8 +35,6 @@ class TestIsPlaceSubmitter(TestCase): def setUp(self): self. client = Client() - mock_place() - mock_user() def test_is_submitter(self): self.client.login(username='testpeter', password='Develop123') diff --git a/lostplaces/lostplaces_app/tests/views/test_place_views.py b/lostplaces/lostplaces_app/tests/views/test_place_views.py index ae9c671..3ae6769 100644 --- a/lostplaces/lostplaces_app/tests/views/test_place_views.py +++ b/lostplaces/lostplaces_app/tests/views/test_place_views.py @@ -3,15 +3,10 @@ from django.urls import reverse_lazy from lostplaces_app.models import Place -from lostplaces_app.tests.models.test_place_model import mock_place -from lostplaces_app.tests import mock_user - class TestPlaceCreateView(TestCase): def setUp(self): self. client = Client() - mock_place() - mock_user() def test_url_logged_in(self): self.client.login(username='testpeter', password='Develop123')