def test_should_not_merge_right_adjacent_block_with_same_different_tag(
            self):
        block1 = AnnotationBlock(TAG1, DEFAULT_BOUNDING_BOX)
        block2 = AnnotationBlock(
            TAG2, block1.bounding_box.move_by(block1.bounding_box.width, 0))

        merged_blocks = merge_blocks([block1, block2])
        assert [b.tag for b in merged_blocks] == [TAG1, TAG2]
    def test_should_merge_right_adjacent_block_with_same_tag(self):
        block1 = AnnotationBlock(TAG1, DEFAULT_BOUNDING_BOX)
        block2 = AnnotationBlock(
            TAG1, block1.bounding_box.move_by(block1.bounding_box.width, 0))

        merged_blocks = merge_blocks([block1, block2])
        assert [b.tag for b in merged_blocks] == [TAG1]
        assert merged_blocks[0].bounding_box == block1.bounding_box.include(
            block2.bounding_box)
    def test_should_not_merge_too_far_away_block_with_same_tag(self):
        block1 = AnnotationBlock(TAG1, DEFAULT_BOUNDING_BOX)
        block2 = AnnotationBlock(
            TAG1,
            block1.bounding_box.move_by(
                block1.bounding_box.width + DEFAULT_NEARBY_TOLERANCE + 1, 0))

        merged_blocks = merge_blocks([block1, block2],
                                     nearby_tolerance=DEFAULT_NEARBY_TOLERANCE)
        assert [b.tag for b in merged_blocks] == [TAG1, TAG1]
    def test_should_merge_right_nearby_block_with_same_tag(self):
        block1 = AnnotationBlock(TAG1, DEFAULT_BOUNDING_BOX)
        block2 = AnnotationBlock(
            TAG1,
            block1.bounding_box.move_by(
                block1.bounding_box.width + DEFAULT_NEARBY_TOLERANCE, 0))

        merged_blocks = merge_blocks([block1, block2],
                                     nearby_tolerance=DEFAULT_NEARBY_TOLERANCE)
        assert [b.tag for b in merged_blocks] == [TAG1]
        assert merged_blocks[0].bounding_box == block1.bounding_box.include(
            block2.bounding_box)
    def test_should_merge_multiple_sequential_right_adjacent_blocks(self):
        block1 = AnnotationBlock(TAG1, DEFAULT_BOUNDING_BOX)
        block2 = AnnotationBlock(
            TAG1, block1.bounding_box.move_by(block1.bounding_box.width, 0))
        block3 = AnnotationBlock(
            TAG1, block2.bounding_box.move_by(block2.bounding_box.width, 0))

        merged_blocks = merge_blocks([block1, block2, block3])
        assert [b.tag for b in merged_blocks] == [TAG1]

        assert merged_blocks[0].bounding_box == (block1.bounding_box.include(
            block2.bounding_box).include(block3.bounding_box))
    def test_should_ignore_unmapped_tag(self):
        blocks = [AnnotationBlock(TAG1, BoundingBox(0, 0, 1, 1))]

        image = annotated_blocks_to_image(blocks,
                                          color_map={},
                                          width=3,
                                          height=3)
        assert image.getpixel((0, 0)) == (255, 255, 255, 0)
    def test_should_convert_rect_color_name(self):
        blocks = [AnnotationBlock(TAG1, BoundingBox(0, 0, 1, 1))]

        image = annotated_blocks_to_image(blocks,
                                          color_map={TAG1: 'green'},
                                          width=3,
                                          height=3)
        assert image.getpixel((0, 0)) == (0, 128, 0, 255)
    def test_should_accept_float_image_size(self):
        blocks = [AnnotationBlock(TAG1, BoundingBox(0, 0, 1, 1))]

        image = annotated_blocks_to_image(blocks,
                                          color_map={TAG1: (0, 255, 0)},
                                          width=3.1,
                                          height=3.9)
        assert image.size == (4, 4)
    def test_should_create_rect_for_single_annotated_block(self):
        blocks = [AnnotationBlock(TAG1, BoundingBox(0, 0, 1, 1))]

        image = annotated_blocks_to_image(blocks,
                                          color_map={TAG1: (0, 255, 0)},
                                          width=3,
                                          height=3)
        assert image.getpixel((0, 0)) == (0, 255, 0, 255)
    def test_should_add_background(self):
        blocks = [AnnotationBlock(TAG1, DEFAULT_BOUNDING_BOX)]

        result_svg = annotated_blocks_to_svg(blocks,
                                             color_map={TAG1: '#123'},
                                             width=100,
                                             height=100,
                                             background=DEFAULT_COLOR)
        background_elements = result_svg.xpath('svg:rect[@class="background"]',
                                               namespaces={'svg': SVG_NS})
        assert len(background_elements) == 1
        assert background_elements[0].attrib['fill'] == DEFAULT_COLOR
    def test_should_create_rect_for_single_annotated_block(self):
        blocks = [AnnotationBlock(TAG1, DEFAULT_BOUNDING_BOX)]

        result_svg = annotated_blocks_to_svg(blocks,
                                             color_map={TAG1: DEFAULT_COLOR},
                                             width=100,
                                             height=100)
        result_rect_elements = result_svg.xpath('svg:rect',
                                                namespaces={'svg': SVG_NS})
        assert len(result_rect_elements) == 1
        assert result_rect_elements[0].attrib['class'] == TAG1
        assert result_rect_elements[0].attrib['fill'] == DEFAULT_COLOR
    def test_should_return_same_single_blocks(self):
        block = AnnotationBlock(TAG1, DEFAULT_BOUNDING_BOX)

        merged_blocks = merge_blocks([block])
        assert merged_blocks == [block]