def test_timestamp_in_dataclass(self): @dataclass(repr=False) class C(SerialisableDataclass): v: Timestamp c = C(Timestamp.from_string('2019-01-01T12:00:00Z')) with self.subTest('No conversion in asdict'): self.assertIsInstance(c.asdict()['v'], Timestamp) with self.subTest('Conversion in str'): self.assertEqual(str(c), '{"v": "2019-01-01T12:00:00Z"}')
def __post_init__(self): self.added_at = Timestamp.from_string(self.added_at) self.added_by = PublicUser(**self.added_by) if self.video_thumbnail is not None: self.video_thumbnail = Image(**self.video_thumbnail) if self.track is not None: if self.is_local: self.track = LocalTrack(**self.track) else: self.track = FullTrack(**self.track)
def __post_init__(self): self.track = FullTrack(**self.track) self.played_at = Timestamp.from_string(self.played_at) if self.context is not None: self.context = Context(**self.context)
def __post_init__(self): self.added_at = Timestamp.from_string(self.added_at) self.show = SimpleShow(**self.show)
def __post_init__(self): self.added_at = Timestamp.from_string(self.added_at) self.album = FullAlbum(**self.album)
def __post_init__(self): self.added_at = Timestamp.from_string(self.added_at) self.track = FullTrack(**self.track)
def test_initialisable_with_millisecond_precision(self): Timestamp.from_string('2019-01-01T12:00:00.00Z')
def test_timestamp_formatted_back_to_string(self): time_str = '2019-01-01T12:00:00Z' t = Timestamp.from_string(time_str) self.assertEqual(str(t), time_str)
def test_incorrect_format_raises(self): with self.assertRaises(ValueError): Timestamp.from_string('2019-01-01')
def test_timestamp_initialisable_from_string(self): Timestamp.from_string('2019-01-01T12:00:00Z')
def test_timestamp_encoded_is_quoted_str(self): t = Timestamp.from_string('2019-01-01T12:00:00Z') encoded = JSONEncoder().encode(t) self.assertEqual(encoded, f'"{str(t)}"')