Пример #1
0
        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() == '[]'
Пример #2
0
    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() == '[]'
Пример #3
0
        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
Пример #4
0
            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"]'
Пример #5
0
            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"}]'
Пример #6
0
        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()
Пример #7
0
        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"}]'
Пример #8
0
            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"]'
Пример #9
0
            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"}]'
Пример #10
0
        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))
Пример #11
0
            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"
                        ]""")
Пример #12
0
        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"]]'
Пример #13
0
        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"}]'
Пример #14
0
            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"}
                        ]""")
Пример #15
0
            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"]'
Пример #16
0
            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"}]'
Пример #17
0
            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]'
Пример #18
0
        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"
                        ]
                    ]""")
Пример #19
0
        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
Пример #20
0
        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"
                        }
                    ]""")
Пример #21
0
        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
                ]""")
Пример #22
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
                        }
                    }
                ]""")
Пример #23
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')
Пример #24
0
    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() == '['