def test_dict_to_json(): # None in keys is converted to "null". roundtrip_to_json({None: 'foo'}, '{\n "null": "foo"\n}') # Keys in resulting objects are sorted. roundtrip_to_json({ None: 'baz', 'foo': 'bar' }, '{\n "foo": "bar",\n "null": "baz"\n}') # Nested dicts are processed too. roundtrip_to_json({ 'foo': { 'bar': 'baz', None: 'qux' }, None: 'quux' }, '{\n "foo": {\n "bar": "baz",\n "null": "qux"\n },\n "null": "quux"\n}' ) # Input isn't mutated. actual = {'foo': 'bar', None: {'baz': 'qux', None: 'quux'}} expected = copy.deepcopy(actual) release.to_json(actual) assert actual == expected
def test_to_json(): roundtrip_to_json('foo', '"foo"') roundtrip_to_json(['foo', 'bar'], '[\n "foo",\n "bar"\n]') roundtrip_to_json(('foo', 'bar'), '[\n "foo",\n "bar"\n]', ['foo', 'bar']) roundtrip_to_json({'foo': 'bar', 'baz': 'qux'}, '{\n "baz": "qux",\n "foo": "bar"\n}') # Sets aren't JSON serializable. with pytest.raises(TypeError): release.to_json({'foo', 'bar'})
def test_to_json(): roundtrip_to_json("foo", '"foo"') roundtrip_to_json(["foo", "bar"], '[\n "foo",\n "bar"\n]') roundtrip_to_json(("foo", "bar"), '[\n "foo",\n "bar"\n]', ["foo", "bar"]) roundtrip_to_json({"foo": "bar", "baz": "qux"}, '{\n "baz": "qux",\n "foo": "bar"\n}') # Sets aren't JSON serializable. with pytest.raises(TypeError): release.to_json({"foo", "bar"})
def roundtrip_to_json(data, mid_state, new_end_state=None): assert release.to_json(data) == mid_state if new_end_state is not None: assert release.from_json(mid_state) == new_end_state else: assert release.from_json(mid_state) == data
def test_dict_to_json(): # None in keys is converted to "null". roundtrip_to_json({None: 'foo'}, '{\n "null": "foo"\n}') # Keys in resulting objects are sorted. roundtrip_to_json({None: 'baz', 'foo': 'bar'}, '{\n "foo": "bar",\n "null": "baz"\n}') # Nested dicts are processed too. roundtrip_to_json( {'foo': {'bar': 'baz', None: 'qux'}, None: 'quux'}, '{\n "foo": {\n "bar": "baz",\n "null": "qux"\n },\n "null": "quux"\n}') # Input isn't mutated. actual = {'foo': 'bar', None: {'baz': 'qux', None: 'quux'}} expected = copy.deepcopy(actual) release.to_json(actual) assert actual == expected