def test_context_manager(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as _: pass with open('foo', 'r') as f: assert f.read() == '{}'
def test_types(self, key, value, expected): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as s: s.write(key, value) with open('foo', 'r') as f: assert f.read() == expected
def test_subobject(self): with open('foo', 'w') as f: test = jsonstreams.Object(f, 0, 0, _ENCODER) test.close() with pytest.raises(jsonstreams.StreamClosedError): test.subobject('foo')
def test_complex(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as s: s.write('foo', {"1": 'bar'}) with open('foo', 'r') as f: assert f.read() == '{"foo": {"1": "bar"}}'
def test_write_one(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as s: s.write('foo', 'bar') with open('foo', 'r') as f: assert f.read() == '{"foo": "bar"}'
def test_basic(self): with open('foo', 'w') as f: test = jsonstreams.Object(f, 0, 0, _ENCODER) test.close() with open('foo', 'r') as f: assert f.read() == '{}'
def test_context_manager(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as s: with s.subarray('ook') as p: p.write('foo') with open('foo', 'r') as f: assert f.read() == '{"ook": ["foo"]}'
def test_basic(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as a: a.iterwrite(six.iteritems({'a': 1, '2': 2, 'foo': None})) with open('foo', 'r') as f: actual = json.load(f) assert actual == {"a": 1, "2": 2, "foo": None}
def test_pretty_multiple(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 4, 0, _ENCODER, pretty=True) as a: a.iterwrite((str(i), i) for i in range(5)) with open('foo', 'r') as f: actual = json.load(f) assert actual == {str(i): i for i in range(5)}
def test_outer_inner(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as s: s.write('foo', 'bar') with s.subobject('ook') as p: p.write("1", 'bar') with open('foo', 'r') as f: assert f.read() == '{"foo": "bar", "ook": {"1": "bar"}}'
def test_inner_outer(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as s: with s.subarray('ook') as p: p.write(1) s.write('foo', 'bar') with open('foo', 'r') as f: assert f.read() == '{"ook": [1], "foo": "bar"}'
def test_inner(self): with open('foo', 'w') as f: s = jsonstreams.Object(f, 0, 0, _ENCODER) p = s.subarray("2") p.write(1) p.close() s.close() with open('foo', 'r') as f: assert f.read() == '{"2": [1]}'
def test_write_one(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 4, 0, _ENCODER) as s: s.write('foo', 'bar') with open('foo', 'r') as f: assert f.read() == textwrap.dedent("""\ { "foo": "bar" }""")
def test_complex(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 4, 0, _ENCODER) as s: s.write('foo', {"1": 'bar'}) with open('foo', 'r') as f: assert f.read() == textwrap.dedent("""\ { "foo": {"1": "bar"} }""")
def test_basic(self): with open('foo', 'w') as f: s = jsonstreams.Object(f, 0, 0, _ENCODER) p = s.subarray('ook') p.write('foo') p.close() s.close() with open('foo', 'r') as f: assert f.read() == '{"ook": ["foo"]}'
def test_inner(self): with open('foo', 'w') as f: s = jsonstreams.Object(f, 0, 0, _ENCODER) p = s.subobject("2") p.write("1", 'bar') p.close() s.close() with open('foo', 'r') as f: assert f.read() == '{"2": {"1": "bar"}}'
def test_outer_inner_outer(self): with open('foo', 'w') as f: s = jsonstreams.Object(f, 0, 0, _ENCODER) s.write('1', 'foo') p = s.subarray('2') p.write(1) p.close() s.write('3', 'foo') s.close() with open('foo', 'r') as f: assert f.read() == '{"1": "foo", "2": [1], "3": "foo"}'
def test_context_manager_indent(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 4, 0, _ENCODER) as s: with s.subarray('ook') as p: p.write('foo') with open('foo', 'r') as f: assert f.read() == textwrap.dedent("""\ { "ook": [ "foo" ] }""")
def test_pretty_subarray(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 4, 0, _ENCODER, pretty=True) as a: a.iterwrite((str(i), i) for i in range(5)) with a.subarray('foo') as b: b.iterwrite(range(2)) expected = {str(i): i for i in range(5)} expected['foo'] = list(range(2)) with open('foo', 'r') as f: actual = json.load(f) assert actual == expected
def test_indent(self): with open('foo', 'w') as f: s = jsonstreams.Object(f, 4, 0, _ENCODER) p = s.subarray('ook') p.write('foo') p.close() s.close() with open('foo', 'r') as f: assert f.read() == textwrap.dedent("""\ { "ook": [ "foo" ] }""")
def test_pretty_one(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 4, 0, _ENCODER, pretty=True) as a: a.iterwrite((str(i), i) for i in range(1)) with open('foo', 'r') as f: actual = json.load(f) assert actual == {str(i): i for i in range(1)} with open('foo', 'r') as f: actual = f.read() assert actual == textwrap.dedent("""\ { "0": 0 }""")
def test_pretty(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 4, 0, json.JSONEncoder(indent=4), pretty=True) as s: s.write("1", {'bar': {"b": 0}}) s.write("2", {'fob': {"f": 0}}) with open('foo', 'r') as f: actual = f.read() assert actual == textwrap.dedent("""\ { "1": { "bar": { "b": 0 } }, "2": { "fob": { "f": 0 } } }""")
def test_init(self): with open('foo', 'w') as f: jsonstreams.Object(f, 0, 0, _ENCODER) with open('foo', 'r') as f: assert f.read() == '{'
def test_invalid_key_types(self, key): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as s: with pytest.raises(jsonstreams.InvalidTypeError): s.write(key, 'foo')
def test_subarray(self): with open('foo', 'w') as f: with jsonstreams.Object(f, 0, 0, _ENCODER) as a: with a.subarray('foo') as b: with pytest.raises(jsonstreams.ModifyWrongStreamError): a.write('foo', 'bar')