def test_read_annotations_wrong_class():
    with pytest.raises(ValueError):
        try:
            csv_generator.read_annotations(csv_str('a.png,0,1,2,3,g'),
                                           {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: unknown class name:")
            raise
def test_read_annotations_wrong_y2():
    with pytest.raises(ValueError):
        try:
            csv_generator.read_annotations(csv_str('a.png,0,1,2,a,a'),
                                           {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: malformed y2:")
            raise
def test_read_annotations_empty_image():
    # Check that images without annotations are parsed.
    assert csv_generator.read_annotations(csv_str('a.png,,,,,\nb.png,,,,,'),
                                          {'a': 1}) == {
                                              'a.png': [],
                                              'b.png': []
                                          }

    # Check that lines without annotations don't clear earlier annotations.
    assert csv_generator.read_annotations(
        csv_str('a.png,0,1,2,3,a\na.png,,,,,'), {'a': 1}) == {
            'a.png': [annotation(0, 1, 2, 3, 'a')]
        }
def test_read_annotations_invalid_bb_y():
    with pytest.raises(ValueError):
        try:
            csv_generator.read_annotations(csv_str('a.png,1,2,3,2,a'),
                                           {'a': 1})
        except ValueError as e:
            assert str(e).startswith(
                "line 1: y2 (2) must be higher than y1 (2)")
            raise
    with pytest.raises(ValueError):
        try:
            csv_generator.read_annotations(csv_str('a.png,1,8,3,5,a'),
                                           {'a': 1})
        except ValueError as e:
            assert str(e).startswith(
                "line 1: y2 (5) must be higher than y1 (8)")
            raise
def test_read_annotations_invalid_bb_x():
    with pytest.raises(ValueError):
        try:
            csv_generator.read_annotations(csv_str('a.png,1,2,1,3,g'),
                                           {'a': 1})
        except ValueError as e:
            assert str(e).startswith(
                "line 1: x2 (1) must be higher than x1 (1)")
            raise
    with pytest.raises(ValueError):
        try:
            csv_generator.read_annotations(csv_str('a.png,9,2,5,3,g'),
                                           {'a': 1})
        except ValueError as e:
            assert str(e).startswith(
                "line 1: x2 (5) must be higher than x1 (9)")
            raise
def test_read_annotations_wrong_format():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    with pytest.raises(ValueError):
        try:
            csv_generator.read_annotations(csv_str('a.png,1,2,3,a'), classes)
        except ValueError as e:
            assert str(e).startswith("line 1: format should be")
            raise

    with pytest.raises(ValueError):
        try:
            csv_generator.read_annotations(
                csv_str('a.png,0,1,2,3,a'
                        '\n'
                        'a.png,1,2,3,a'
                        '\n'), classes)
        except ValueError as e:
            assert str(e).startswith("line 2: format should be")
            raise
def test_read_annotations_multiple():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    annotations = csv_generator.read_annotations(
        csv_str('a.png,0,1,2,3,a'
                '\n'
                'b.png,4,5,6,7,b'
                '\n'
                'a.png,8,9,10,11,c'
                '\n'), classes)
    assert annotations == {
        'a.png': [
            annotation(0, 1, 2, 3, 'a'),
            annotation(8, 9, 10, 11, 'c'),
        ],
        'b.png': [annotation(4, 5, 6, 7, 'b')],
    }
def test_read_annotations():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    annotations = csv_generator.read_annotations(
        csv_str('a.png,0,1,2,3,a'
                '\n'
                'b.png,4,5,6,7,b'
                '\n'
                'c.png,8,9,10,11,c'
                '\n'
                'd.png,12,13,14,15,d'
                '\n'), classes)
    assert annotations == {
        'a.png': [annotation(0, 1, 2, 3, 'a')],
        'b.png': [annotation(4, 5, 6, 7, 'b')],
        'c.png': [annotation(8, 9, 10, 11, 'c')],
        'd.png': [annotation(12, 13, 14, 15, 'd')],
    }