2014-07-24 12:57:24 +00:00
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
|
2015-02-09 13:15:26 +00:00
|
|
|
from django import forms
|
2014-07-24 12:57:24 +00:00
|
|
|
from django.core import exceptions, serializers
|
|
|
|
from django.db import models
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
2014-07-24 12:57:24 +00:00
|
|
|
|
2015-01-02 01:47:49 +00:00
|
|
|
from .models import DurationModel, NullDurationModel
|
2014-07-24 12:57:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestSaveLoad(TestCase):
|
|
|
|
def test_simple_roundtrip(self):
|
2017-12-28 22:35:41 +00:00
|
|
|
duration = datetime.timedelta(microseconds=8999999999999999)
|
2014-07-24 12:57:24 +00:00
|
|
|
DurationModel.objects.create(field=duration)
|
|
|
|
loaded = DurationModel.objects.get()
|
|
|
|
self.assertEqual(loaded.field, duration)
|
|
|
|
|
2015-01-02 01:47:49 +00:00
|
|
|
def test_create_empty(self):
|
|
|
|
NullDurationModel.objects.create()
|
|
|
|
loaded = NullDurationModel.objects.get()
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(loaded.field)
|
2015-01-02 01:47:49 +00:00
|
|
|
|
2016-03-10 14:22:09 +00: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 12:57:24 +00: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 10:12:13 +00:00
|
|
|
self.assertCountEqual(
|
2014-07-24 12:57:24 +00:00
|
|
|
DurationModel.objects.filter(field__gt=datetime.timedelta(days=0)),
|
|
|
|
[self.objs[0], self.objs[1]],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class TestSerialization(SimpleTestCase):
|
2014-07-24 12:57:24 +00:00
|
|
|
test_data = (
|
|
|
|
'[{"fields": {"field": "1 01:00:00"}, "model": "model_fields.durationmodel", '
|
|
|
|
'"pk": null}]'
|
2022-02-04 07:08:27 +00:00
|
|
|
)
|
2014-07-24 12:57:24 +00: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 21:38:20 +00:00
|
|
|
class TestValidation(SimpleTestCase):
|
2014-07-24 12:57:24 +00: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 16:39:47 +00:00
|
|
|
"“not a datetime” value has an invalid format. "
|
|
|
|
"It must be in [DD] [[HH:]MM:]ss[.uuuuuu] format.",
|
2014-07-24 12:57:24 +00:00
|
|
|
)
|
2015-02-09 13:15:26 +00:00
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class TestFormField(SimpleTestCase):
|
2015-02-09 13:15:26 +00:00
|
|
|
# Tests for forms.DurationField are in the forms_tests app.
|
|
|
|
|
|
|
|
def test_formfield(self):
|
|
|
|
field = models.DurationField()
|
|
|
|
self.assertIsInstance(field.formfield(), forms.DurationField)
|