Ejemplo n.º 1
0
def test_anno_image_input_octet_stream_bad_json_filename(img_file, json_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    image_file_bytes = open(str(img_file), 'rb').read()
    image_file_attr = {
        'filename': 'test_img.png',
        'read.return_value': image_file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(image_file_bytes),
    }
    image_file = mock.Mock(**image_file_attr)

    json_file_bytes = open(str(json_file), 'rb').read()
    json_file_attr = {
        'filename': 'annotations.jso',
        'read.return_value': json_file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(json_file_bytes),
    }
    json_file = mock.Mock(**json_file_attr)

    request.method = "POST"
    request.files = {"image_file": image_file, "json_file": json_file}

    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_and_json)

    assert "unexpected file" in str(e.value)
Ejemplo n.º 2
0
def test_anno_image_input_http_request_malformatted_input_wrong_input_name():
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)

    request.method = "POST"
    request.files = {"abc": None}
    request.headers = {}
    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_only)

    assert "unexpected HTTP request format" in str(e.value)
Ejemplo n.º 3
0
def test_anno_image_input_http_request_multipart_octet_streams(
        img_file, json_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    image_file_bytes = open(str(img_file), 'rb').read()
    image_file_attr = {
        'filename': 'test_img.png',
        'read.return_value': image_file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(image_file_bytes),
    }
    image_file = mock.Mock(**image_file_attr)

    json_file_bytes = open(str(json_file), 'rb').read()
    json_file_attr = {
        'filename': 'annotations.json',
        'read.return_value': json_file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(json_file_bytes),
    }
    json_file = mock.Mock(**json_file_attr)

    request.method = "POST"
    request.files = {"image_file": image_file, "json_file": json_file}

    request.get_data.return_value = None

    response = test_anno_image_input.handle_request(request,
                                                    predict_image_and_json)

    assert response.status_code == 200
    assert '[10, 10, 3], "kaith"' in str(response.response)
Ejemplo n.º 4
0
def test_anno_image_input_http_request_only_json_file(json_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    json_file_bytes = open(str(json_file), 'rb').read()
    json_file_attr = {
        'filename': 'annotations.json',
        'read.return_value': json_file_bytes,
        'mimetype': 'application/json',
        'stream': io.BytesIO(json_file_bytes),
    }
    json_file = mock.Mock(**json_file_attr)

    request.method = "POST"
    request.files = {"json_file": json_file}
    request.headers = {}
    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_only)

    assert "requires an image file" in str(e.value)
Ejemplo n.º 5
0
def test_anno_image_input_http_request_two_image_files(img_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    image_file_bytes = open(str(img_file), 'rb').read()
    image_file_attr = {
        'filename': 'test_img.png',
        'read.return_value': image_file_bytes,
        'mimetype': 'image/png',
        'stream': io.BytesIO(image_file_bytes),
    }
    image_file = mock.Mock(**image_file_attr)

    request.method = "POST"
    request.files = {"image_file": image_file, "image_file_2": image_file}
    request.headers = {}
    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_only)

    assert "received two images" in str(e.value)
Ejemplo n.º 6
0
def test_anno_image_input_custom_accept_extension_not_accepted(img_file):
    test_anno_image_input = AnnotatedImageInput(
        accept_image_formats=[".custom"])
    request = mock.MagicMock(spec=flask.Request)
    file_bytes = open(str(img_file), 'rb').read()
    file_attr = {
        'filename': 'test_img.jpg',
        'read.return_value': file_bytes,
        'mimetype': 'image/jpg',
        'stream': io.BytesIO(file_bytes),
    }
    file = mock.Mock(**file_attr)

    request.method = "POST"
    request.files = {"test_img": file}
    request.headers = {}
    request.get_data.return_value = None

    with pytest.raises(BadInput) as e:
        test_anno_image_input.handle_request(request, predict_image_only)

    assert "Input file not in supported format list" in str(e.value)
Ejemplo n.º 7
0
def test_anno_image_input_http_request_single_image_different_name(img_file):
    test_anno_image_input = AnnotatedImageInput()
    request = mock.MagicMock(spec=flask.Request)
    file_bytes = open(str(img_file), 'rb').read()
    file_attr = {
        'filename': 'test_img.png',
        'read.return_value': file_bytes,
        'mimetype': 'image/png',
        'stream': io.BytesIO(file_bytes),
    }
    file = mock.Mock(**file_attr)

    request.method = "POST"
    request.files = {"a_different_name_used": file}
    request.headers = {}
    request.get_data.return_value = None

    response = test_anno_image_input.handle_request(request,
                                                    predict_image_only)

    assert response.status_code == 200
    assert "[10, 10, 3]" in str(response.response)
Ejemplo n.º 8
0
def test_anno_image_input_octet_stream_json(img_file):
    test_anno_image_input = AnnotatedImageInput(
        accept_image_formats=[".custom"])
    request = mock.MagicMock(spec=flask.Request)
    file_bytes = open(str(img_file), 'rb').read()
    file_attr = {
        'filename': 'test_img.custom',
        'read.return_value': file_bytes,
        'mimetype': 'application/octet-stream',
        'stream': io.BytesIO(file_bytes),
    }
    file = mock.Mock(**file_attr)

    request.method = "POST"
    request.files = {"a_different_name_used": file}
    request.headers = {}
    request.get_data.return_value = None

    response = test_anno_image_input.handle_request(request,
                                                    predict_image_only)

    assert response.status_code == 200
    assert "[10, 10, 3]" in str(response.response)