caellion-python-commons: tests/test_serializers_datetime_serializer.py Source File

caellion-python-commons

test_serializers_datetime_serializer.py
Go to the documentation of this file.
1 # test framework
2 import unittest
3 import pytest
4 import datetime
5
6 # package
7 from caellion.pycommons.serializers.datetime_serializer import DateTimeUnixSerializer
8 from caellion.pycommons.serializers.datetime_serializer import DateTimeUnixMillisSerializer
9 from caellion.pycommons.serializers.datetime_serializer import DateTimeUnixMicrosSerializer
10 from caellion.pycommons.serializers.datetime_serializer import DateTimeIsoTimeSerializer
11
12
13 # test cases
14 class TestSerializersDateTimeSerializer():
15 @pytest.mark.parametrize( "input, expected",[ ("2022-01-01T00:00:00+00:00", "2022-01-01T00:00:00+00:00"), ("2022-01-01T00:00:00-01:00", "2022-01-01T01:00:00+00:00"),]
16 )
17 def test_serialize_isoformat(self, input, expected):
18 input = datetime.datetime.fromisoformat(input)
19 assert DateTimeIsoTimeSerializer.serialize(input) == expected
20
21 @pytest.mark.parametrize( "input, expected",[ ("2022-01-01T00:00:00.000000+00:00", 1640995200), ("2022-01-01T00:00:00.000000-01:00", 1640998800),]
22 )
23 def test_serialize_unixseconds(self, input, expected):
24 input = datetime.datetime.fromisoformat(input)
25 assert DateTimeUnixSerializer.serialize(input) == expected
26
27 @pytest.mark.parametrize( "input, expected",[ ("2022-01-01T00:00:00.000000+00:00", 1640995200000), ("2022-01-01T00:00:00.000000-01:00", 1640998800000),]
28 )
29 def test_serialize_unixmillis(self, input, expected):
30 input = datetime.datetime.fromisoformat(input)
31 assert DateTimeUnixMillisSerializer.serialize(input) == expected
32
33 @pytest.mark.parametrize( "input, expected",[ ("2022-01-01T00:00:00.000000+00:00", 1640995200000000), ("2022-01-01T00:00:00.000000-01:00", 1640998800000000),]
34 )
35 def test_serialize_unixmicros(self, input, expected):
36 input = datetime.datetime.fromisoformat(input)
37 assert DateTimeUnixMicrosSerializer.serialize(input) == expected
38
39 @pytest.mark.parametrize( "input, expected",[ ("2022-01-01T00:00:00+00:00", "2022-01-01T00:00:00+00:00"), ("2022-01-01T01:00:00+00:00", "2022-01-01T01:00:00+00:00"),]
40 )
41 def test_deserialize_isoformat(self, input, expected):
42 assert DateTimeIsoTimeSerializer.deserialize(input) == datetime.datetime.fromisoformat(expected)
43
44 @pytest.mark.parametrize( "input, expected",[ (1640995200, "2022-01-01T00:00:00+00:00"), (1640998800, "2022-01-01T01:00:00+00:00"),]
45 )
46 def test_deserialize_unixseconds(self, input, expected):
47 assert DateTimeUnixSerializer.deserialize(input).isoformat() == expected
48
49 @pytest.mark.parametrize( "input, expected",[ (1640995200001, "2022-01-01T00:00:00.001000+00:00"), (1640998800001, "2022-01-01T01:00:00.001000+00:00"),]
50 )
51 def test_deserialize_unixmillis(self, input, expected):
52 assert DateTimeUnixMillisSerializer.deserialize(input).isoformat() == expected
53
54 @pytest.mark.parametrize( "input, expected",[ (1640995200000001, "2022-01-01T00:00:00.000001+00:00"), (1640998800000001, "2022-01-01T01:00:00.000001+00:00"),]
55 )
56 def test_deserialize_unixmicros(self, input, expected):
57 assert DateTimeUnixMicrosSerializer.deserialize(input).isoformat() == expected
58
59
60 if __name__ == "__main__":
61 unittest.main()
62
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer
Definition: test_serializers_datetime_serializer.py:14
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer.test_serialize_unixmicros
def test_serialize_unixmicros(self, input, expected)
Definition: test_serializers_datetime_serializer.py:35
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer.test_serialize_unixmillis
def test_serialize_unixmillis(self, input, expected)
Definition: test_serializers_datetime_serializer.py:29
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer.test_deserialize_unixseconds
def test_deserialize_unixseconds(self, input, expected)
Definition: test_serializers_datetime_serializer.py:46
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer.test_deserialize_unixmicros
def test_deserialize_unixmicros(self, input, expected)
Definition: test_serializers_datetime_serializer.py:56
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer.test_serialize_isoformat
def test_serialize_isoformat(self, input, expected)
Definition: test_serializers_datetime_serializer.py:17
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer.test_serialize_unixseconds
def test_serialize_unixseconds(self, input, expected)
Definition: test_serializers_datetime_serializer.py:23
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer.test_deserialize_unixmillis
def test_deserialize_unixmillis(self, input, expected)
Definition: test_serializers_datetime_serializer.py:51
tests.test_serializers_datetime_serializer.TestSerializersDateTimeSerializer.test_deserialize_isoformat
def test_deserialize_isoformat(self, input, expected)
Definition: test_serializers_datetime_serializer.py:41
caellion.pycommons.serializers.datetime_serializer
This module provides various serializers for datetime.datetime object.
Definition: datetime_serializer.py:1