Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_json_encoder_failure():
class Dummy:
pass
with pytest.raises(TypeError) as x:
encoder(Dummy())
def test_encode_decimal():
d = decimal.Decimal("6.7")
doc = json.dumps({"d": d}, default=encoder)
assert str(d) in doc
def test_json_encoder_decimal():
d = Decimal('1.38')
assert encoder(d) == '1.38'
def test_json_encoder_datetime():
now = datetime.utcnow()
assert encoder(now) == now.isoformat()
def test_json_encoder_uuid():
u = uuid.uuid4()
assert encoder(u) == str(u)
def test_encode_date_and_datetime():
now = datetime.now()
utcnow = datetime.utcnow()
today = datetime.today()
d = {"now": now, "utcnow": utcnow, "today": today}
doc = json.dumps(d, default=encoder)
assert now.isoformat() in doc
assert utcnow.isoformat() in doc
assert today.isoformat() in doc