Created
May 17, 2019 23:24
-
-
Save erayerdin/c8c27dfdfa44bcc3278589e958c53e1c to your computer and use it in GitHub Desktop.
Abstract base model testing classes for pytest and Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
import pytest | |
DATETIME_FIELDS = (models.DateTimeField, models.DateField, models.TimeField) | |
class BaseModelTest: | |
model: models.Model = None | |
def test_issubclass_model(self): | |
assert issubclass(self.model, models.Model) | |
class BaseModelFieldTest: | |
model: models.Model = None | |
field_name: str = None | |
field_type: models.Field = None | |
is_null: bool = False | |
is_blank: bool = False | |
default_value = models.fields.NOT_PROVIDED | |
is_auto_now: bool = True | |
is_auto_now_add: bool = True | |
@property | |
def field(self): | |
return self.model._meta.get_field(self.field_name) | |
def test_field_type(self): | |
assert isinstance(self.field, self.field_type) | |
def test_is_null(self): | |
assert self.field.null == self.is_null | |
def test_is_blank(self): | |
assert self.field.blank == self.is_blank | |
def test_default_value(self): | |
assert self.field.default == self.default_value | |
def test_auto_now(self): | |
if self.field.__class__ not in DATETIME_FIELDS: | |
pytest.skip( | |
f"{self.model.__name__}->{self.field_name} is not a date/time model type." | |
) | |
assert self.field.auto_now == self.is_auto_now | |
def test_auto_now_add(self): | |
if self.field.__class__ not in DATETIME_FIELDS: | |
pytest.skip( | |
f"{self.model.__name__}->{self.field_name} is not a date/time model type." | |
) | |
assert self.field.auto_now_add == self.is_auto_now_add |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment