Improve JSONEncoder test coverage (#47935)

pull/48001/head
Erik Montnemery 2021-03-16 17:12:51 +01:00 committed by GitHub
parent 7ed9e5b2c2
commit 389891d13d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 3 deletions

View File

@ -11,11 +11,17 @@ def test_json_encoder(hass):
ha_json_enc = JSONEncoder()
state = core.State("test.test", "hello")
# Test serializing a datetime
now = dt_util.utcnow()
assert ha_json_enc.default(now) == now.isoformat()
# Test serializing a set()
data = {"milk", "beer"}
assert sorted(ha_json_enc.default(data)) == sorted(list(data))
# Test serializing an object which implements as_dict
assert ha_json_enc.default(state) == state.as_dict()
# Default method raises TypeError if non HA object
with pytest.raises(TypeError):
ha_json_enc.default(1)
now = dt_util.utcnow()
assert ha_json_enc.default(now) == now.isoformat()