コード例 #1
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
    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() == '{}'
コード例 #2
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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
コード例 #3
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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')
コード例 #4
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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"}}'
コード例 #5
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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"}'
コード例 #6
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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() == '{}'
コード例 #7
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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"]}'
コード例 #8
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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}
コード例 #9
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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)}
コード例 #10
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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"}}'
コード例 #11
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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"}'
コード例 #12
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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]}'
コード例 #13
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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"
                        }""")
コード例 #14
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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"}
                        }""")
コード例 #15
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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"]}'
コード例 #16
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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"}}'
コード例 #17
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
            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"}'
コード例 #18
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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"
                        ]
                    }""")
コード例 #19
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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
コード例 #20
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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"
                        ]
                    }""")
コード例 #21
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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
                }""")
コード例 #22
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
        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
                        }
                    }
                }""")
コード例 #23
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
    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() == '{'
コード例 #24
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
 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')
コード例 #25
0
ファイル: test_stream.py プロジェクト: calsev/jsonstreams
 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')