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

143 lines
4.8 KiB
Python
Raw Permalink Normal View History

2020-09-19 22:50:07 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2020-09-03 22:22:04 +02:00
from django.db import models
2020-09-11 12:08:15 +02:00
from django.core.exceptions import FieldDoesNotExist
from django.test import TestCase
2020-09-03 22:22:04 +02:00
2020-09-11 12:08:15 +02:00
2020-09-13 18:37:21 +02:00
class ModelTestCase(TestCase):
2020-09-03 22:22:04 +02:00
'''
2020-09-13 18:37:21 +02:00
Base class for ModelTests.
Parameters:
- model : Class to test
2020-09-03 22:22:04 +02:00
'''
2020-09-11 23:07:19 +02:00
model = None
2020-09-13 13:29:27 +02:00
def assertField(self, field_name, field_class, must_have={}, must_not_have={}):
2020-09-03 22:22:04 +02:00
'''
Tests if a field exists under the given name and
2020-09-11 12:08:15 +02:00
if the field is of the right type.
Also checks if the field has the given must_have attributes
and does not have any of the must_not_have attributes. If you
dont care about the value of the attribute you can just set it to
something that fullfills value == False (i.e. '' or 0)
2020-09-03 22:22:04 +02:00
'''
2020-09-11 12:08:15 +02:00
try:
2020-09-13 10:27:01 +02:00
field = self.model._meta.get_field(field_name)
2020-09-11 12:08:15 +02:00
except FieldDoesNotExist:
self.fail(
'Expecting %s to have a field named \'%s\'' % (
2020-09-13 18:37:21 +02:00
self.model.__name__,
2020-09-11 12:08:15 +02:00
field_name
)
2020-09-03 22:22:04 +02:00
)
self.assertEqual(
2020-09-11 12:08:15 +02:00
type(field), field_class,
2020-09-13 19:12:32 +02:00
msg='Expecting type of %s to be %s' % (
str(field),
2020-09-11 12:08:15 +02:00
field_class.__name__
2020-09-03 22:22:04 +02:00
)
)
2020-09-11 12:08:15 +02:00
for key, value in must_have.items():
if value:
2020-09-13 19:12:32 +02:00
self.assertEqual(
getattr(field, key), value,
msg='Expeting the value of %s %s to be \'%s\'' % (
str(field),
2020-09-11 12:08:15 +02:00
key,
value
)
)
else:
2020-09-13 19:12:32 +02:00
self.assertTrue(
hasattr(field, key),
msg='Expeting %s to have \'%s\'' % (
str(field),
2020-09-11 12:08:15 +02:00
key
)
)
2020-09-03 22:22:04 +02:00
2020-09-11 12:08:15 +02:00
for key, value in must_not_have.items():
if value:
2020-09-13 19:12:32 +02:00
self.assertTrue(
getattr(field, key) != value,
msg='Expeting the value of %s %s to not be \'%s\'' % (
str(field),
2020-09-11 12:08:15 +02:00
key,
value
)
)
else:
2020-09-13 19:12:32 +02:00
self.assertFalse(
hasattr(field, value),
msg='Expeting %s to not have \'%s\'' % (
str(field),
2020-09-11 12:08:15 +02:00
key
)
)
return field
2020-09-13 13:30:11 +02:00
def assertCharField(self, field_name, min_length, max_length, must_have={}, must_hot_have={}):
2020-09-03 22:22:04 +02:00
'''
Tests if the given field is a char field and if its max_length
is in min_length and max_legth
'''
2020-09-13 13:29:27 +02:00
field = self.assertField(
2020-09-11 12:08:15 +02:00
field_name, models.CharField, must_have, must_hot_have)
2020-09-03 22:22:04 +02:00
self.assertTrue(
field.max_length in range(min_length, max_length),
2020-09-13 19:12:32 +02:00
msg='Expeting %s max_length to be in the range of %d and %d' % (
str(field),
2020-09-03 22:22:04 +02:00
min_length,
max_length
2020-09-11 12:08:15 +02:00
)
2020-09-03 22:22:04 +02:00
)
2020-09-11 12:08:15 +02:00
2020-09-13 13:30:47 +02:00
def assertFloatField(self, field_name, min_value=None, max_value=None, must_have={}, must_hot_have={}):
2020-09-03 22:22:04 +02:00
'''
Tests if the field is a floatfield. If min_value and/or max_value are passed,
the validators of the field are also checked. The validator list of the field should
look like
[MinValueValidator, MayValueValidator], if both values are passed,
[MinValueValidator] if only min_value is passed,
[MaxValueValidator] if only max_value is passed
'''
2020-09-13 13:29:27 +02:00
field = self.assertField(
2020-09-11 12:08:15 +02:00
field_name, models.FloatField, must_have, must_hot_have)
2020-09-03 22:22:04 +02:00
if min_value:
self.assertTrue(
len(field.validators) >= 1,
2020-09-13 19:12:32 +02:00
msg='Expecting the first valiator of %s to check the minimum' % (
str(field)
2020-09-03 22:22:04 +02:00
)
2020-09-11 12:08:15 +02:00
)
2020-09-03 22:22:04 +02:00
self.assertEqual(
field.validators[0].limit_value,
min_value,
2020-09-13 19:12:32 +02:00
msg='Expecting the min value of %s min to be at least %d' % (
str(field),
2020-09-11 12:08:15 +02:00
min_value
2020-09-03 22:22:04 +02:00
)
)
if max_value:
index = 0
if min_value:
index += 1
self.assertTrue(
len(field.validators) >= index+1,
2020-09-13 19:12:32 +02:00
msg='Expecting the second valiator of %s to check the maximum' % (
str(field)
2020-09-11 12:08:15 +02:00
)
2020-09-03 22:22:04 +02:00
)
self.assertEqual(
field.validators[1].limit_value,
max_value,
2020-09-13 19:12:32 +02:00
msg='Expecting the max value of %s min to be at most %d' % (
str(field),
2020-09-11 12:08:15 +02:00
max_value
2020-09-03 22:22:04 +02:00
)
)