Ejemplo n.º 1
0
    def test_write_layers(self):
        output = io.BytesIO()
        document = Document(header=Header(encoding="utf-8"))

        layer_1 = Layer(0, "Layer 1")
        layer_2 = Layer(1, "Layer 2")

        document.add_layer(layer_1)
        document.add_layer(layer_2)

        objs_layer_1 = [
            LayerObject(0, 0),
            LayerObject(1, 0),
            LayerObject(2, 0),
            LayerObject(3, 0),
        ]

        objs_layer_2 = [
            LayerObject(0, 1),
            LayerObject(1, 1),
            LayerObject(2, 1),
            LayerObject(3, 1),
        ]

        layer_1.add_objects(objs_layer_1)
        layer_2.add_objects(objs_layer_2)

        writer = _Writer(document, output)
        writer._write_layers()

        assert output.getvalue() == b'\x03' \
                                    b'\x00\x00\x00\x02' \
                                    b'\x09Layer 1\x00\x00\x00\x00\x04\x0A' \
                                    b'\x09Layer 2\x00\x00\x00\x00\x04\x0A' \
                                    b'\x04'
Ejemplo n.º 2
0
    def test_write_header(self):
        output = io.BytesIO()
        document = Document(header=Header(encoding="utf-8"))

        writer = _Writer(document, output)
        writer._write_header()

        assert output.getvalue() == b'\x01utf-8\x00\x02'
Ejemplo n.º 3
0
    def test_write_flag(self):
        output = io.BytesIO()
        document = Document(header=Header(encoding="utf-8"))

        writer = _Writer(document, output)

        writer._write_flag(HEADER_START)
        assert output.getvalue() == b'\x01'
Ejemplo n.º 4
0
    def test_write_string(self):
        output = io.BytesIO()
        document = Document(header=Header(encoding="utf-8"))

        writer = _Writer(document, output)

        # We assert 4 bytes per int here
        writer._write_string('Hello')
        assert output.getvalue() == b'Hello'
Ejemplo n.º 5
0
    def test_write_relations(self):
        output = io.BytesIO()
        document = Document(header=Header(encoding="utf-8"))

        layer_1 = Layer(0, "Layer 1")
        layer_2 = Layer(1, "Layer 2")

        document.add_layer(layer_1)
        document.add_layer(layer_2)

        objs_layer_1 = [
            LayerObject(0, 0),
            LayerObject(1, 0),
            LayerObject(2, 0),
            LayerObject(3, 0),
        ]

        objs_layer_2 = [
            LayerObject(0, 1),
            LayerObject(1, 1),
            LayerObject(2, 1),
            LayerObject(3, 1),
        ]

        layer_1.add_objects(objs_layer_1)
        layer_2.add_objects(objs_layer_2)

        # Add some relations
        objs_layer_1[0].add_child(objs_layer_2[1])
        objs_layer_1[1].add_child(objs_layer_2[0])
        objs_layer_1[2].add_child(objs_layer_2[3])
        objs_layer_1[3].add_child(objs_layer_2[2])

        writer = _Writer(document, output)
        writer._prepare_relations_for_writing()
        writer._write_relations()

        assert output.getvalue() == b'\x07' \
                                    b'\x00\x00\x00\x01' \
                                    b'\x0e' \
                                    b'\x00\x00\x00\x00' \
                                    b'\x00\x00\x00\x01' \
                                    b'\x00\x00\x00\x04' \
                                    b'\x00\x00\x00\x00' \
                                    b'\x00\x00\x00\x01' \
                                    b'\x00\x00\x00\x01' \
                                    b'\x00\x00\x00\x00' \
                                    b'\x00\x00\x00\x02' \
                                    b'\x00\x00\x00\x03' \
                                    b'\x00\x00\x00\x03' \
                                    b'\x00\x00\x00\x02' \
                                    b'\x0f' \
                                    b'\x08'
Ejemplo n.º 6
0
    def test_write_attrs(self):
        output = io.BytesIO()
        document = Document(header=Header(encoding="utf-8"))

        layer_1 = Layer(0, "Layer 1")
        layer_2 = Layer(1, "Layer 2")

        document.add_layer(layer_1)
        document.add_layer(layer_2)

        objs_layer_1 = [
            LayerObject(0, 0, attrs={'key1': 'val2'}),
        ]

        objs_layer_2 = [
            LayerObject(0, 1, attrs={'key3': 'val'}),
            LayerObject(1, 1, attrs={'key4': 'val2'}),
        ]

        layer_1.add_objects(objs_layer_1)
        layer_2.add_objects(objs_layer_2)

        writer = _Writer(document, output)
        writer._prepare_attrs_for_writing()
        writer._write_attrs()

        # All chunks will be full
        # The ordering may vary, so we simply that the correct chunks are there.
        output_value = output.getvalue()
        assert b'\x05' \
               b'\x00\x00\x00\x03' in output_value
        assert b'\x0B' \
               b'\x00\x00\x00\x00' \
               b'key1\x00' \
               b'val2\x00' \
               b'\x0D' in output_value
        assert b'\x0B' \
               b'\x00\x00\x00\x01' \
               b'key3\x00' \
               b'val\x00\x00' \
               b'\x0D' in output_value
        assert b'\x0B' \
               b'\x00\x00\x00\x01' \
               b'key4\x00' \
               b'\x00val2\x00' \
               b'\x0D'in output_value
        assert b'\x06' in output_value[-1:]
Ejemplo n.º 7
0
    def test_prepare_relations_for_writing(self):
        output = io.BytesIO()
        document = Document(header=Header(encoding="utf-8"))

        layer_1 = Layer(0, "Layer 1")
        layer_2 = Layer(1, "Layer 2")

        document.add_layer(layer_1)
        document.add_layer(layer_2)

        objs_layer_1 = [
            LayerObject(0, 0),
            LayerObject(1, 0),
            LayerObject(2, 0),
            LayerObject(3, 0),
        ]

        objs_layer_2 = [
            LayerObject(0, 1),
            LayerObject(1, 1),
            LayerObject(2, 1),
            LayerObject(3, 1),
        ]

        layer_1.add_objects(objs_layer_1)
        layer_2.add_objects(objs_layer_2)

        # Add some relations
        objs_layer_1[0].add_child(objs_layer_2[1])
        objs_layer_1[1].add_child(objs_layer_2[0])
        objs_layer_1[2].add_child(objs_layer_2[3])
        objs_layer_1[3].add_child(objs_layer_2[2])

        writer = _Writer(document, output)
        writer._prepare_relations_for_writing()

        assert 0 in writer._temp_relations
        assert 1 in writer._temp_relations[0]

        rel_tuples = writer._temp_relations[0][1]
        assert (0, 1) in rel_tuples
        assert (1, 0) in rel_tuples
        assert (2, 3) in rel_tuples
        assert (3, 2) in rel_tuples

        assert writer._temp_num_of_relations == 1
Ejemplo n.º 8
0
    def test_prepare_attrs_for_writing(self):
        output = io.BytesIO()
        document = Document(header=Header(encoding="utf-8"))

        layer_1 = Layer(0, "Layer 1")
        layer_2 = Layer(1, "Layer 2")

        document.add_layer(layer_1)
        document.add_layer(layer_2)

        objs_layer_1 = [
            LayerObject(0, 0, attrs={'key1': 'val2'}),
            LayerObject(1, 0, attrs={'key2': 'val'}),
            LayerObject(2, 0, attrs={'key1': 'val4', 'key2': 'val3'}),
            LayerObject(3, 0, attrs={'key2': 'val'}),
        ]

        objs_layer_2 = [
            LayerObject(0, 1, attrs={'key3': 'val'}),
            LayerObject(1, 1, attrs={'key4': 'val6'}),
            LayerObject(2, 1, attrs={'key2': 'val7'}),
            LayerObject(3, 1, attrs={'key4': 'val2'}),
        ]

        layer_1.add_objects(objs_layer_1)
        layer_2.add_objects(objs_layer_2)

        # Add some relations
        objs_layer_1[0].add_child(objs_layer_2[1])
        objs_layer_1[1].add_child(objs_layer_2[0])
        objs_layer_1[2].add_child(objs_layer_2[3])
        objs_layer_1[3].add_child(objs_layer_2[2])

        writer = _Writer(document, output)
        writer._prepare_attrs_for_writing()

        assert 0 in writer._temp_attributes
        assert 1 in writer._temp_attributes

        layer_0_attrs = writer._temp_attributes[0]
        assert 'key1' in layer_0_attrs
        assert 'key2' in layer_0_attrs

        assert 0 in layer_0_attrs['key1']
        assert layer_0_attrs['key1'][0] == 'val2'
        assert 2 in layer_0_attrs['key1']
        assert layer_0_attrs['key1'][2] == 'val4'

        assert 1 in layer_0_attrs['key2']
        assert layer_0_attrs['key2'][1] == 'val'
        assert 2 in layer_0_attrs['key2']
        assert layer_0_attrs['key2'][2] == 'val3'
        assert 3 in layer_0_attrs['key2']
        assert layer_0_attrs['key2'][3] == 'val'

        layer_1_attrs = writer._temp_attributes[1]
        assert 'key2' in layer_1_attrs
        assert 'key3' in layer_1_attrs
        assert 'key4' in layer_1_attrs

        assert 2 in layer_1_attrs['key2']
        assert layer_1_attrs['key2'][2] == 'val7'

        assert 0 in layer_1_attrs['key3']
        assert layer_1_attrs['key3'][0] == 'val'

        assert 1 in layer_1_attrs['key4']
        assert layer_1_attrs['key4'][1] == 'val6'
        assert 3 in layer_1_attrs['key4']
        assert layer_1_attrs['key4'][3] == 'val2'