2014-07-24 14:57:24 +02:00
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
|
2015-02-09 14:15:26 +01:00
|
|
|
from django import forms
|
2014-07-24 14:57:24 +02:00
|
|
|
from django.core import exceptions, serializers
|
|
|
|
from django.db import models
|
2015-04-17 23:38:20 +02:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
2014-07-24 14:57:24 +02:00
|
|
|
|
2015-01-02 02:47:49 +01:00
|
|
|
from .models import DurationModel, NullDurationModel
|
2014-07-24 14:57:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestSaveLoad(TestCase):
|
|
|
|
def test_simple_roundtrip(self):
|
2017-12-28 23:35:41 +01:00
|
|
|
duration = datetime.timedelta(microseconds=8999999999999999)
|
2014-07-24 14:57:24 +02:00
|
|
|
DurationModel.objects.create(field=duration)
|
|
|
|
loaded = DurationModel.objects.get()
|
|
|
|
self.assertEqual(loaded.field, duration)
|
|
|
|
|
2015-01-02 02:47:49 +01:00
|
|
|
def test_create_empty(self):
|
|
|
|
NullDurationModel.objects.create()
|
|
|
|
loaded = NullDurationModel.objects.get()
|
2016-06-16 20:19:18 +02:00
|
|
|
self.assertIsNone(loaded.field)
|
2015-01-02 02:47:49 +01:00
|
|
|
|
2016-03-10 15:22:09 +01:00
|
|
|
def test_fractional_seconds(self):
|
|
|
|
value = datetime.timedelta(seconds=2.05)
|
|
|
|
d = DurationModel.objects.create(field=value)
|
|
|
|
d.refresh_from_db()
|
|
|
|
self.assertEqual(d.field, value)
|
|
|
|
|
2014-07-24 14:57:24 +02:00
|
|
|
|
|
|
|
class TestQuerying(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.objs = [
|
|
|
|
DurationModel.objects.create(field=datetime.timedelta(days=1)),
|
|
|
|
DurationModel.objects.create(field=datetime.timedelta(seconds=1)),
|
|
|
|
DurationModel.objects.create(field=datetime.timedelta(seconds=-1)),
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_exact(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
DurationModel.objects.filter(field=datetime.timedelta(days=1)),
|
|
|
|
[self.objs[0]],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_gt(self):
|
2022-04-14 12:12:13 +02:00
|
|
|
self.assertCountEqual(
|
2014-07-24 14:57:24 +02:00
|
|
|
DurationModel.objects.filter(field__gt=datetime.timedelta(days=0)),
|
|
|
|
[self.objs[0], self.objs[1]],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-04-17 23:38:20 +02:00
|
|
|
class TestSerialization(SimpleTestCase):
|
2014-07-24 14:57:24 +02:00
|
|
|
test_data = (
|
|
|
|
'[{"fields": {"field": "1 01:00:00"}, "model": "model_fields.durationmodel", '
|
|
|
|
'"pk": null}]'
|
2022-02-04 08:08:27 +01:00
|
|
|
)
|
2014-07-24 14:57:24 +02:00
|
|
|
|
|
|
|
def test_dumping(self):
|
|
|
|
instance = DurationModel(field=datetime.timedelta(days=1, hours=1))
|
|
|
|
data = serializers.serialize("json", [instance])
|
|
|
|
self.assertEqual(json.loads(data), json.loads(self.test_data))
|
|
|
|
|
|
|
|
def test_loading(self):
|
|
|
|
instance = list(serializers.deserialize("json", self.test_data))[0].object
|
|
|
|
self.assertEqual(instance.field, datetime.timedelta(days=1, hours=1))
|
|
|
|
|
|
|
|
|
2015-04-17 23:38:20 +02:00
|
|
|
class TestValidation(SimpleTestCase):
|
2014-07-24 14:57:24 +02:00
|
|
|
def test_invalid_string(self):
|
|
|
|
field = models.DurationField()
|
|
|
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
|
|
|
field.clean("not a datetime", None)
|
|
|
|
self.assertEqual(cm.exception.code, "invalid")
|
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message % cm.exception.params,
|
2019-06-27 18:39:47 +02:00
|
|
|
"“not a datetime” value has an invalid format. "
|
|
|
|
"It must be in [DD] [[HH:]MM:]ss[.uuuuuu] format.",
|
2014-07-24 14:57:24 +02:00
|
|
|
)
|
2015-02-09 14:15:26 +01:00
|
|
|
|
|
|
|
|
2015-04-17 23:38:20 +02:00
|
|
|
class TestFormField(SimpleTestCase):
|
2015-02-09 14:15:26 +01:00
|
|
|
# Tests for forms.DurationField are in the forms_tests app.
|
|
|
|
|
|
|
|
def test_formfield(self):
|
|
|
|
field = models.DurationField()
|
|
|
|
self.assertIsInstance(field.formfield(), forms.DurationField)
|