Example #1
0
class TestUTCDateTimeAttribute:
    """
    Tests UTCDateTime attributes
    """
    def setup(self):
        self.attr = UTCDateTimeAttribute()
        self.dt = datetime(2047, 1, 6, 8, 21, 30, 2000, tzinfo=timezone.utc)

    def test_utc_datetime_attribute(self):
        """
        UTCDateTimeAttribute.default
        """
        attr = UTCDateTimeAttribute(default=self.dt)
        assert attr.attr_type == STRING
        assert attr.default == self.dt

    def test_utc_date_time_serialize(self):
        """
        UTCDateTimeAttribute.serialize
        """
        assert self.attr.serialize(
            self.dt) == '2047-01-06T08:21:30.002000+0000'

    def test_utc_date_time_serialize_pre_1000(self):
        dt = self.dt.replace(year=1)
        assert self.attr.serialize(dt) == '0001-01-06T08:21:30.002000+0000'

    def test_utc_date_time_deserialize(self):
        """
        UTCDateTimeAttribute.deserialize
        """
        assert self.attr.deserialize(
            '2047-01-06T08:21:30.002000+0000') == self.dt

    def test_utc_date_time_deserialize_pre_1000_not_padded(self):
        assert self.attr.deserialize(
            '1-01-06T08:21:30.002000+0000') == self.dt.replace(year=1)

    @pytest.mark.parametrize(
        "invalid_string",
        [
            '2047-01-06T08:21:30.002000',  # naive datetime
            '2047-01-06T08:21:30+0000',  # missing microseconds
            '2047-01-06T08:21:30.001+0000',  # shortened microseconds
            '2047-01-06T08:21:30.002000-0000'  # "negative" utc
            '2047-01-06T08:21:30.002000+0030'  # not utc
            '2047-01-06 08:21:30.002000+0000',  # missing separator
            '2.47-01-06T08:21:30.002000+0000',
            'abcd-01-06T08:21:30.002000+0000',
            '2047-ab-06T08:21:30.002000+0000',
            '2047-01-abT08:21:30.002000+0000',
            '2047-01-06Tab:21:30.002000+0000',
            '2047-01-06T08:ab:30.002000+0000',
            '2047-01-06T08:21:ab.002000+0000',
            '2047-01-06T08:21:30.a00000+0000',
        ])
    def test_utc_date_time_invalid(self, invalid_string):
        with pytest.raises(ValueError, match="does not match format"):
            self.attr.deserialize(invalid_string)
Example #2
0
 def test_utc_date_time_serialize(self):
     """
     UTCDateTimeAttribute.serialize
     """
     tstamp = datetime.now()
     attr = UTCDateTimeAttribute()
     self.assertEqual(attr.serialize(tstamp), Delorean(tstamp, timezone=UTC).datetime.strftime(DATETIME_FORMAT))
Example #3
0
 def test_utc_date_time_serialize(self):
     """
     UTCDateTimeAttribute.serialize
     """
     tstamp = datetime.now()
     attr = UTCDateTimeAttribute()
     assert attr.serialize(tstamp) == tstamp.replace(tzinfo=UTC).strftime(DATETIME_FORMAT)
Example #4
0
 def test_utc_date_time_serialize(self):
     """
     UTCDateTimeAttribute.serialize
     """
     tstamp = datetime.now()
     attr = UTCDateTimeAttribute()
     self.assertEqual(attr.serialize(tstamp), tstamp.replace(tzinfo=UTC).strftime(DATETIME_FORMAT))
Example #5
0
 def test_utc_date_time_serialize(self):
     """
     UTCDateTimeAttribute.serialize
     """
     tstamp = datetime.now()
     attr = UTCDateTimeAttribute()
     assert attr.serialize(tstamp) == tstamp.replace(tzinfo=timezone.utc).strftime(DATETIME_FORMAT)