def test_decimal_decode(self): assert (DataClassWithDecimal.from_dict( self.dc_decimal_json) == DataClassWithDecimal( Decimal(self.decimal_s)))
def test_decimal_decode(self): assert DataClassWithDecimal.from_json( self.dec_ts_json_in) == self.dec_ts
def test_decimal_encode(self): assert (DataClassWithDecimal(Decimal( self.decimal_s)).to_json() == self.dc_decimal_json)
class TestTypes: uuid_s = 'd1d61dd7-c036-47d3-a6ed-91cc2e885fc8' dc_uuid_json = f'{{"id": "{uuid_s}"}}' def test_uuid_encode(self): assert (DataClassWithUuid(UUID( self.uuid_s)).to_json() == self.dc_uuid_json) def test_uuid_decode(self): assert (DataClassWithUuid.from_json( self.dc_uuid_json) == DataClassWithUuid(UUID(self.uuid_s))) dt = datetime(2018, 11, 17, 16, 55, 28, 456753, tzinfo=timezone.utc) tz = timezone.utc ts = dt.timestamp() dc_ts_json = f'{{"created_at": {ts}}}' dc_ts = DataClassWithDatetime(datetime.fromtimestamp(ts, tz=tz)) iso = dt.isoformat() dc_iso_json = f'{{"created_at": "{iso}"}}' dc_iso = DataClassWithIsoDatetime(datetime.fromisoformat(iso)) dec = Decimal("0.04") dec_ts_json_in = f'{{"price": "{dec}"}}' dec_ts_json_out = f'{{"price": {dec}}}' dec_ts = DataClassWithDecimal(dec) def test_decimal_encode(self): assert self.dec_ts.to_json() == self.dec_ts_json_out def test_decimal_decode(self): assert DataClassWithDecimal.from_json( self.dec_ts_json_in) == self.dec_ts def test_datetime_encode(self): assert (self.dc_ts.to_json() == self.dc_ts_json) def test_datetime_decode(self): assert (DataClassWithDatetime.from_json(self.dc_ts_json) == self.dc_ts) def test_datetime_override_encode(self): assert (self.dc_iso.to_json() == self.dc_iso_json) def test_datetime_override_decode(self): assert (DataClassWithIsoDatetime.from_json( self.dc_iso_json) == self.dc_iso) def test_datetime_schema_encode(self): assert (DataClassWithDatetime.schema().dumps( self.dc_ts) == self.dc_ts_json) def test_datetime_schema_decode(self): assert (DataClassWithDatetime.schema().loads( self.dc_ts_json) == self.dc_ts) def test_datetime_override_schema_encode(self): assert (DataClassWithIsoDatetime.schema().dumps( self.dc_iso) == self.dc_iso_json) def test_datetime_override_schema_decode(self): iso = DataClassWithIsoDatetime.schema().loads(self.dc_iso_json) # FIXME bug in marshmallow currently returns datetime-naive instead of # datetime-aware. also seems to drop microseconds? # #955 iso.created_at = iso.created_at.replace(microsecond=456753, tzinfo=self.tz) assert (iso == self.dc_iso) def test_datetime_custom_iso_fieldoverride_schema_encode(self): assert (DataClassWithCustomIsoDatetime.schema().dumps( self.dc_iso) == self.dc_iso_json) def test_datetime_custom_iso_field_override_schema_decode(self): iso = DataClassWithCustomIsoDatetime.schema().loads(self.dc_iso_json) assert (iso == DataClassWithCustomIsoDatetime(self.dt))