lostplaces-backend/lostplaces/lostplaces_app/tests/models/test_voucher_model.py

50 lines
1.3 KiB
Python
Raw Normal View History

2020-09-11 12:08:27 +02:00
import datetime
from django.test import TestCase
from django.db import models
from django.utils import timezone
2020-09-11 12:08:27 +02:00
from lostplaces_app.models import Voucher
2020-09-13 13:31:41 +02:00
from lostplaces_app.tests.models import ModelTestCaseMixin
2020-09-11 12:08:27 +02:00
2020-09-13 13:31:41 +02:00
class VoucheTestCase(ModelTestCaseMixin, TestCase):
2020-09-11 23:07:19 +02:00
model = Voucher
@classmethod
def setUpTestData(cls):
Voucher.objects.create(
2020-09-11 12:08:27 +02:00
code='ayDraJCCwfhcFiYmSR5GrcjcchDfcahv',
expires_when=timezone.now() + datetime.timedelta(days=1)
2020-09-11 12:08:27 +02:00
)
2020-09-12 08:38:37 +02:00
2020-09-11 12:08:27 +02:00
def test_voucher_code(self):
2020-09-13 13:30:11 +02:00
self.assertCharField(
2020-09-11 12:08:27 +02:00
'code',
10,
100,
must_have={'unique': True}
)
def test_voucher_created(self):
2020-09-13 13:29:27 +02:00
self.assertField(
2020-09-11 12:08:27 +02:00
'created_when',
models.DateTimeField,
must_have={'auto_now_add': True}
)
def test_voucher_expires(self):
2020-09-13 13:29:27 +02:00
self.assertField(
2020-09-11 12:08:27 +02:00
'expires_when',
models.DateTimeField,
must_not_have={'auto_now_add': True}
)
def test_str(self):
voucher = self.object
self.assertTrue(voucher.code.lower() in str(voucher).lower(),
msg='Expecting %s.__str__ to contain the voucher code' % (
self.model_name
)
2020-09-11 19:32:07 +02:00
)