lostplaces-backend/django_lostplaces/lostplaces/tests/models/test_abstract_models.py

149 lines
4.0 KiB
Python
Raw Permalink Normal View History

2020-09-19 22:50:07 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2020-09-13 10:27:01 +02:00
2020-10-04 22:12:24 +02:00
import datetime
from django.utils import timezone
2020-09-13 10:27:01 +02:00
from django.test import TestCase
from django.db import models
from django.contrib.auth.models import User
from lostplaces.models import (
2020-09-13 19:12:32 +02:00
Taggable,
2020-09-14 15:18:21 +02:00
Mapable,
Squashed commit of the following: commit 0d62e72d72922a84e41c9f2cc21977b794784d1c Merge: 79fee63 85f2a81 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:55:18 2020 +0200 Merge branch 'develop' into refactor/models commit 79fee631d7ac28509067ecdd74078f1a2f6e0be2 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:54:32 2020 +0200 Updating references for related name commit 8e07e79df2de2601f2e2eadfdd37eb7c719c51b0 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:53:31 2020 +0200 Generating of related names fix commit 5fd804f37a805ae4707e13c3d941bdde3660afea Merge: 8cc1d3e 3b526c9 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:01:48 2020 +0200 Merge branch 'develop' into refactor/models commit 8cc1d3e690211dba6451e86569f00078b23e0621 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 20:21:08 2020 +0200 Tests commit 7c0591e5397f892b1f6fb80725a693c21f90468a Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 23:53:39 2020 +0200 Testing PlaceAsset commit 2e7b49ad1a15173565c81e7eb8bb3f35b9f622a6 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:25:08 2020 +0200 Restructuring models commit eb7d03b08b326f9115e70d0fd9ed5d0fc229a362 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:01:54 2020 +0200 Abstract class Expireable commit 2b51e741bb5734c5a578beeadef7819fe58b2223 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 21:54:07 2020 +0200 Abstract Model for PlaceAsset (i.e. Photoalbums)
2020-09-22 21:56:51 +02:00
Submittable,
PlaceAsset,
2020-10-04 22:12:24 +02:00
Expireable,
Voucher
2020-09-13 19:12:32 +02:00
)
from lostplaces.tests.models import ModelTestCase
2020-09-13 10:27:01 +02:00
from taggit.managers import TaggableManager
2020-09-13 18:37:21 +02:00
class TaggableTestCase(ModelTestCase):
2020-09-13 10:27:01 +02:00
model = Taggable
def test_tags(self):
2020-09-13 13:29:27 +02:00
self.assertField('tags', TaggableManager)
2020-09-13 10:27:01 +02:00
2020-09-14 15:18:21 +02:00
class MapableTestCase(ModelTestCase):
2020-09-13 10:27:01 +02:00
2020-09-14 15:18:21 +02:00
model = Mapable
2020-09-13 10:27:01 +02:00
def test_name(self):
2020-09-13 13:30:11 +02:00
self.assertCharField(
2020-09-13 10:27:01 +02:00
field_name='name',
min_length=10,
max_length=100
)
def test_latitude(self):
2020-09-13 13:30:47 +02:00
self.assertFloatField(
2020-09-13 10:27:01 +02:00
field_name='latitude',
min_value=-90,
max_value=90
)
def test_longitude(self):
2020-09-13 13:30:47 +02:00
self.assertFloatField(
2020-09-13 10:27:01 +02:00
field_name='longitude',
min_value=-180,
max_value=180
)
2020-09-13 19:12:32 +02:00
class SubmittableTestCase(ModelTestCase):
model = Submittable
def test_submitted_when(self):
self.assertField(
field_name='submitted_when',
field_class=models.DateTimeField,
must_have={'auto_now_add': True}
)
def test_submitted_by(self):
submitted_by = self.assertField(
field_name='submitted_by',
field_class=models.ForeignKey
)
self.assertEqual(
submitted_by.remote_field.related_name,
Squashed commit of the following: commit 0d62e72d72922a84e41c9f2cc21977b794784d1c Merge: 79fee63 85f2a81 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:55:18 2020 +0200 Merge branch 'develop' into refactor/models commit 79fee631d7ac28509067ecdd74078f1a2f6e0be2 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:54:32 2020 +0200 Updating references for related name commit 8e07e79df2de2601f2e2eadfdd37eb7c719c51b0 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:53:31 2020 +0200 Generating of related names fix commit 5fd804f37a805ae4707e13c3d941bdde3660afea Merge: 8cc1d3e 3b526c9 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:01:48 2020 +0200 Merge branch 'develop' into refactor/models commit 8cc1d3e690211dba6451e86569f00078b23e0621 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 20:21:08 2020 +0200 Tests commit 7c0591e5397f892b1f6fb80725a693c21f90468a Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 23:53:39 2020 +0200 Testing PlaceAsset commit 2e7b49ad1a15173565c81e7eb8bb3f35b9f622a6 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:25:08 2020 +0200 Restructuring models commit eb7d03b08b326f9115e70d0fd9ed5d0fc229a362 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:01:54 2020 +0200 Abstract class Expireable commit 2b51e741bb5734c5a578beeadef7819fe58b2223 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 21:54:07 2020 +0200 Abstract Model for PlaceAsset (i.e. Photoalbums)
2020-09-22 21:56:51 +02:00
'%(class)ss',
msg='Expecting the related_name of %s to be \'%%(class)ss\', got %s' % (
2020-09-13 19:12:32 +02:00
str(submitted_by),
submitted_by.remote_field.related_name
)
)
self.assertTrue(
submitted_by.null,
2020-09-13 19:17:04 +02:00
msg='Expecting %s to has null=True' % (
str(submitted_by)
2020-09-13 19:12:32 +02:00
)
2020-09-13 19:17:04 +02:00
)
self.assertTrue(
submitted_by.blank,
msg='Expecting %s to has blank=True' % (
str(submitted_by)
)
)
2020-09-13 19:12:32 +02:00
self.assertEqual(
2020-09-13 19:17:04 +02:00
submitted_by.remote_field.on_delete,
2020-09-13 19:29:30 +02:00
models.SET_NULL,
2020-09-13 19:17:04 +02:00
msg='Expecting %s to be null when reference is delete (models.SET_NULL)' % (
str(submitted_by)
)
)
2020-09-13 19:12:32 +02:00
Squashed commit of the following: commit 0d62e72d72922a84e41c9f2cc21977b794784d1c Merge: 79fee63 85f2a81 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:55:18 2020 +0200 Merge branch 'develop' into refactor/models commit 79fee631d7ac28509067ecdd74078f1a2f6e0be2 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:54:32 2020 +0200 Updating references for related name commit 8e07e79df2de2601f2e2eadfdd37eb7c719c51b0 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:53:31 2020 +0200 Generating of related names fix commit 5fd804f37a805ae4707e13c3d941bdde3660afea Merge: 8cc1d3e 3b526c9 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:01:48 2020 +0200 Merge branch 'develop' into refactor/models commit 8cc1d3e690211dba6451e86569f00078b23e0621 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 20:21:08 2020 +0200 Tests commit 7c0591e5397f892b1f6fb80725a693c21f90468a Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 23:53:39 2020 +0200 Testing PlaceAsset commit 2e7b49ad1a15173565c81e7eb8bb3f35b9f622a6 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:25:08 2020 +0200 Restructuring models commit eb7d03b08b326f9115e70d0fd9ed5d0fc229a362 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:01:54 2020 +0200 Abstract class Expireable commit 2b51e741bb5734c5a578beeadef7819fe58b2223 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 21:54:07 2020 +0200 Abstract Model for PlaceAsset (i.e. Photoalbums)
2020-09-22 21:56:51 +02:00
class PlaceAssetTestCase(ModelTestCase):
model = PlaceAsset
def test_place(self):
field = self.assertField('place', models.ForeignKey)
self.assertEqual(field.remote_field.on_delete, models.CASCADE,
msg='Expecting the deletion of %s to be cascading' % (
str(field)
)
)
expected_related_name = '%(class)ss'
self.assertEqual(field.remote_field.related_name, expected_related_name,
msg='Expecting the related name of %s to be %s' % (
str(field),
expected_related_name
)
)
class ExpireableTestCase(ModelTestCase):
2020-10-04 22:12:24 +02:00
model = Expireable
def test_fields(self):
self.assertField(
field_name='created_when',
field_class=models.DateTimeField,
must_have={'auto_now_add': True}
)
self.assertField(
field_name='expires_when',
field_class=models.DateTimeField
)
def test_is_expired(self):
valid_voucher = Voucher.objects.create(
code='Test123',
expires_when=timezone.now() + datetime.timedelta(minutes=2)
)
self.assertFalse(
valid_voucher.is_expired,
msg='Expecing the expirable object to not be expired'
)
invalid_voucher = Voucher.objects.create(
code='Test1234',
expires_when=timezone.now() - datetime.timedelta(minutes=2)
)
self.assertTrue(
invalid_voucher.is_expired,
msg='Expecing the expirable object to be expired'
)