def test_basic(self): with open('foo', 'w') as f: test = jsonstreams.Array(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.Array(f, 0, 0, _ENCODER) as _: pass with open('foo', 'r') as f: assert f.read() == '[]'
def test_types(self, value, expected): with open('foo', 'w') as f: with jsonstreams.Array(f, 0, 0, _ENCODER) as s: s.write(value) with open('foo', 'r') as f: assert f.read() == expected
def test_write_one(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 0, 0, _ENCODER) as s: s.write('foo') with open('foo', 'r') as f: assert f.read() == '["foo"]'
def test_complex(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 0, 0, _ENCODER) as s: s.write({"1": 'bar'}) with open('foo', 'r') as f: assert f.read() == '[{"1": "bar"}]'
def test_subobject(self): with open('foo', 'w') as f: test = jsonstreams.Array(f, 0, 0, _ENCODER) test.close() with pytest.raises(jsonstreams.StreamClosedError): test.subobject()
def test_context_manager(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 0, 0, _ENCODER) as s: with s.subobject() as p: p.write('foo', 'bar') with open('foo', 'r') as f: assert f.read() == '[{"foo": "bar"}]'
def test_inner_outer(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 0, 0, _ENCODER) as s: with s.subarray() as p: p.write('bar') s.write('foo') with open('foo', 'r') as f: assert f.read() == '[["bar"], "foo"]'
def test_outer_inner(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 0, 0, _ENCODER) as s: s.write('foo') with s.subobject() as p: p.write("1", 'bar') with open('foo', 'r') as f: assert f.read() == '["foo", {"1": "bar"}]'
def test_basic(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 0, 0, _ENCODER) as a: a.iterwrite(range(5)) with open('foo', 'r') as f: actual = json.load(f) assert actual == list(range(5))
def test_write_one(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 4, 0, _ENCODER) as s: s.write('foo') with open('foo', 'r') as f: assert f.read() == textwrap.dedent("""\ [ "foo" ]""")
def test_basic(self): with open('foo', 'w') as f: s = jsonstreams.Array(f, 0, 0, _ENCODER) p = s.subarray() p.write('foo') p.close() s.close() with open('foo', 'r') as f: assert f.read() == '[["foo"]]'
def test_basic(self): with open('foo', 'w') as f: s = jsonstreams.Array(f, 0, 0, _ENCODER) p = s.subobject() p.write('foo', 'bar') p.close() s.close() with open('foo', 'r') as f: assert f.read() == '[{"foo": "bar"}]'
def test_complex(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 4, 0, _ENCODER) as s: s.write({"1": 'bar'}) with open('foo', 'r') as f: assert f.read() == textwrap.dedent("""\ [ {"1": "bar"} ]""")
def test_inner_outer(self): with open('foo', 'w') as f: s = jsonstreams.Array(f, 0, 0, _ENCODER) p = s.subarray() p.write('foo') p.close() s.write('bar') s.close() with open('foo', 'r') as f: assert f.read() == '[["foo"], "bar"]'
def test_outer_inner(self): with open('foo', 'w') as f: s = jsonstreams.Array(f, 0, 0, _ENCODER) s.write('foo') p = s.subobject() p.write('1', 'bar') p.close() s.close() with open('foo', 'r') as f: assert f.read() == '["foo", {"1": "bar"}]'
def test_outer_inner_outer(self): with open('foo', 'w') as f: s = jsonstreams.Array(f, 0, 0, _ENCODER) s.write(1) p = s.subarray() p.write(1) p.close() s.write(2) s.close() with open('foo', 'r') as f: assert f.read() == '[1, [1], 2]'
def test_context_manager_indent(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 4, 0, _ENCODER) as s: with s.subarray() as p: p.write('foo') with open('foo', 'r') as f: assert f.read() == textwrap.dedent("""\ [ [ "foo" ] ]""")
def test_pretty_subarray(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 4, 0, _ENCODER, pretty=True) as a: a.iterwrite(range(5)) with a.subarray() as b: b.iterwrite(range(2)) expected = list(range(5)) expected.append(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.Array(f, 4, 0, _ENCODER) p = s.subobject() p.write('foo', 'bar') p.close() s.close() with open('foo', 'r') as f: assert f.read() == textwrap.dedent("""\ [ { "foo": "bar" } ]""")
def test_pretty_one(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 4, 0, _ENCODER, pretty=True) as a: a.iterwrite(range(1)) with open('foo', 'r') as f: actual = json.load(f) assert actual == list(range(1)) with open('foo', 'r') as f: actual = f.read() assert actual == textwrap.dedent("""\ [ 0 ]""")
def test_pretty(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 4, 0, json.JSONEncoder(indent=4), pretty=True) as s: s.write({'bar': {"b": 0}}) s.write({'fob': {"f": 0}}) with open('foo', 'r') as f: actual = f.read() assert actual == textwrap.dedent("""\ [ { "bar": { "b": 0 } }, { "fob": { "f": 0 } } ]""")
def test_subarray(self): with open('foo', 'w') as f: with jsonstreams.Array(f, 0, 0, _ENCODER) as a: with a.subarray() as b: with pytest.raises(jsonstreams.ModifyWrongStreamError): a.write('foo')
def test_init(self): with open('foo', 'w') as f: jsonstreams.Array(f, 0, 0, _ENCODER) with open('foo', 'r') as f: assert f.read() == '['