Compare commits

...

3 Commits

Author SHA1 Message Date
64c0c5f8e6 Setting Up Test Data mor in a Unit way 2020-09-12 08:39:06 +02:00
18a597c726 Merge branch 'develop' into testing 2020-09-12 08:38:37 +02:00
Leonhard Strohmidel
baca596603 sync 2020-09-11 23:07:19 +02:00
10 changed files with 153 additions and 75 deletions

View File

@ -19,6 +19,7 @@ easy-thumbnails = "*"
image = "*" image = "*"
django-widget-tweaks = "*" django-widget-tweaks = "*"
django-taggit = "*" django-taggit = "*"
# Commented out to not explicitly specify Python 3 subversion. # Commented out to not explicitly specify Python 3 subversion.
# [requires] [requires]
# python_version = "3.8" python_version = "3.7"

View File

@ -1,13 +1,11 @@
from django.db import models as django_models from django.test import TestCase
from django.contrib.auth.models import User from django.contrib.auth.models import User
class BaseData(TestCase):
def mock_user(): @classmethod
explorer_list = User.objects.all() def setUpTestData(cls):
if len(explorer_list) <= 0: User.objects.create_user(
return User.objects.create_user(
username='testpeter', username='testpeter',
password='Develop123' password='Develop123'
) ).save()
else:
return explorer_list[0]

View File

@ -1,14 +1,21 @@
from django.db import models from django.db import models
from django.contrib.auth.models import User
from django.core.exceptions import FieldDoesNotExist from django.core.exceptions import FieldDoesNotExist
from django.test import TestCase from django.test import TestCase
# Creating a test user
class ModelTestCase: class ModelTestCase:
''' '''
Base class for Lostplaces models Base class for Lostplaces models
''' '''
model = None
model_name = 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={}): def _test_field(self, field_name, field_class, must_have={}, must_not_have={}):
''' '''
Tests if a field exists under the given name and Tests if a field exists under the given name and

View File

@ -3,12 +3,15 @@ from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from lostplaces_app.models import Explorer from lostplaces_app.models import Explorer
from lostplaces_app.tests import mock_user
class ExplorerTestCase(TestCase): class ExplorerTestCase(TestCase):
def setUp(self): @classmethod
mock_user() def setUpTestData(self):
User.objects.create_user(
username='testpeter',
password='Develop123'
)
def test_epxlorer_creation(self): def test_epxlorer_creation(self):
''' '''
@ -43,7 +46,7 @@ class ExplorerTestCase(TestCase):
Tests if the Explorer objects get's deleted when the User instance is deleted 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 explorer_id = user.explorer.id
user.delete() user.delete()
with self.assertRaises(models.ObjectDoesNotExist, with self.assertRaises(models.ObjectDoesNotExist,

View File

@ -7,37 +7,50 @@ from django.test import TestCase
from django.db import models from django.db import models
from django.core.files import File from django.core.files import File
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User
from lostplaces_app.models import PlaceImage from lostplaces_app.models import PlaceImage, Place
from lostplaces_app.tests.models import SubmittableTestCase from lostplaces_app.tests.models import SubmittableTestCase
from lostplaces_app.tests import mock_user from lostplaces_app.tests.models.test_place_model import PlaceTestCase
from lostplaces_app.tests.models.test_place_model import mock_place
from easy_thumbnails.fields import ThumbnailerImageField from easy_thumbnails.fields import ThumbnailerImageField
def mock_place_image(): class TestPlaceImage(SubmittableTestCase, TestCase):
all_place_images = PlaceImage.objects.all() model = PlaceImage
if len(all_place_images) <= 0:
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
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()
current_dir = os.path.dirname(os.path.abspath(__file__)) 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')): if not os.path.isfile(os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')):
shutil.copyfile( shutil.copyfile(
os.path.join(current_dir, 'im_a_image.jpeg'), os.path.join(current_dir, 'im_a_image.jpeg'),
os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg') os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')
) )
return PlaceImage.objects.create(
PlaceImage.objects.create(
description='Im a description', description='Im a description',
filename=os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg'), filename=os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg'),
place=mock_place(), place=place,
submitted_when=datetime.datetime.now(), submitted_when=datetime.datetime.now(),
submitted_by=mock_user().explorer submitted_by=user.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): def test_description(self):
self._test_field('description', models.TextField) self._test_field('description', models.TextField)

View File

@ -3,34 +3,38 @@ import datetime
from django.test import TestCase from django.test import TestCase
from django.db import models from django.db import models
from django.contrib.auth.models import User
from lostplaces_app.models import Place from lostplaces_app.models import Place
from lostplaces_app.tests import BaseData
from lostplaces_app.tests.models import SubmittableTestCase from lostplaces_app.tests.models import SubmittableTestCase
from lostplaces_app.tests import mock_user
from taggit.managers import TaggableManager from taggit.managers import TaggableManager
def mock_place(): class PlaceTestCase(SubmittableTestCase, TestCase):
model = Place
related_name = 'places'
nullable = True
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
place = Place.objects.create( place = Place.objects.create(
name='Im a place', name='Im a place',
submitted_when=datetime.datetime.now(), submitted_when=datetime.datetime.now(),
submitted_by=mock_user().explorer, submitted_by=user.explorer,
location='Testtown', location='Testtown',
latitude=50.5, latitude=50.5,
longitude=7.0, longitude=7.0,
description='This is just a test, do not worry' description='This is just a test, do not worry'
) )
place.tags.add('I a tag', 'testlocation') place.tags.add('I a tag', 'testlocation')
place.save()
return place
class PlaceTestCase(SubmittableTestCase, TestCase):
model_name = 'Place'
related_name = 'places'
nullable = True
def setUp(self):
self.place = mock_place()
self.object = self.place
def test_name_field(self): def test_name_field(self):
self._test_char_field( self._test_char_field(
@ -73,9 +77,11 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
''' '''
place_list = [] place_list = []
for i in range(10): for i in range(10):
place = mock_place() place = Place.objects.get(id=1)
place.id = None
place.latitude = i+1 place.latitude = i+1
place.longitude = i+10 place.longitude = i+10
place.save()
place_list.append(place) place_list.append(place)
avg_latlon = Place.average_latlon(place_list) avg_latlon = Place.average_latlon(place_list)
@ -95,7 +101,7 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
Tests the average latitude/longitude calculation of a list Tests the average latitude/longitude calculation of a list
of one place of one place
''' '''
place = mock_place() place = Place.objects.get(id=1)
avg_latlon = Place.average_latlon([place]) avg_latlon = Place.average_latlon([place])
self.assertEqual(avg_latlon[0], place.latitude, self.assertEqual(avg_latlon[0], place.latitude,
msg='%s:(one place) average latitude missmatch' % ( msg='%s:(one place) average latitude missmatch' % (

View File

@ -8,19 +8,16 @@ from lostplaces_app.models import Voucher
from lostplaces_app.tests.models import ModelTestCase from lostplaces_app.tests.models import ModelTestCase
def mock_voucher(): class VoucheTestCase(ModelTestCase, TestCase):
return Voucher.objects.create( model = Voucher
@classmethod
def setUpTestData(cls):
Voucher.objects.create(
code='ayDraJCCwfhcFiYmSR5GrcjcchDfcahv', code='ayDraJCCwfhcFiYmSR5GrcjcchDfcahv',
expires_when=timezone.now() + datetime.timedelta(days=1) expires_when=timezone.now() + datetime.timedelta(days=1)
) )
class VoucherTestCase(ModelTestCase, TestCase):
model_name = 'Voucher'
def setUp(self):
self.object = mock_voucher()
def test_voucher_code(self): def test_voucher_code(self):
self._test_char_field( self._test_char_field(
'code', 'code',

View File

@ -1,17 +1,36 @@
import datetime
from django.test import TestCase, Client from django.test import TestCase, Client
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.contrib.auth.models import User
from lostplaces_app.models import Place from lostplaces_app.models import Place
from django.contrib.auth.models import User 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): class TestIsAuthenticated(TestCase):
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
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 setUp(self): def setUp(self):
self. client = Client() self. client = Client()
mock_place()
mock_user()
def test_logged_in(self): def test_logged_in(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')
@ -36,10 +55,27 @@ class TestIsAuthenticated(TestCase):
class TestIsPlaceSubmitter(TestCase): class TestIsPlaceSubmitter(TestCase):
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
place = Place.objects.create(
name='Im a place',
submitted_when=datetime.datetime.now(),
submitted_by=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')
place.save()
def setUp(self): def setUp(self):
self. client = Client() self. client = Client()
mock_place()
mock_user()
def test_is_submitter(self): def test_is_submitter(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')

View File

@ -1,17 +1,34 @@
import datetime
from django.test import TestCase, Client from django.test import TestCase, Client
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.contrib.auth.models import User
from lostplaces_app.models import Place 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): class TestPlaceCreateView(TestCase):
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
place = Place.objects.create(
name='Im a place',
submitted_when=datetime.datetime.now(),
submitted_by=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')
place.save()
def setUp(self): def setUp(self):
self. client = Client() self. client = Client()
mock_place()
mock_user()
def test_url_logged_in(self): def test_url_logged_in(self):
self.client.login(username='testpeter', password='Develop123') self.client.login(username='testpeter', password='Develop123')