Improve JSONEncoder test coverage (#47935)
This commit is contained in:
parent
7ed9e5b2c2
commit
389891d13d
1 changed files with 9 additions and 3 deletions
|
@ -11,11 +11,17 @@ def test_json_encoder(hass):
|
||||||
ha_json_enc = JSONEncoder()
|
ha_json_enc = JSONEncoder()
|
||||||
state = core.State("test.test", "hello")
|
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()
|
assert ha_json_enc.default(state) == state.as_dict()
|
||||||
|
|
||||||
# Default method raises TypeError if non HA object
|
# Default method raises TypeError if non HA object
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
ha_json_enc.default(1)
|
ha_json_enc.default(1)
|
||||||
|
|
||||||
now = dt_util.utcnow()
|
|
||||||
assert ha_json_enc.default(now) == now.isoformat()
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue