Skip to content
Snippets Groups Projects
test_elements.py 48.8 KiB
Newer Older
# -*- coding: utf-8 -*-
import json
from argparse import Namespace
from uuid import UUID

import pytest
from apistar.exceptions import ErrorResponse
from arkindex_worker.cache import (
    SQL_VERSION,
    CachedElement,
    CachedImage,
    create_version_table,
    init_cache_db,
)
from arkindex_worker.models import Element
from arkindex_worker.worker import ElementsWorker
from arkindex_worker.worker.element import MissingTypeError
def test_check_required_types_argument_types(mock_elements_worker):
    with pytest.raises(AssertionError) as e:
Nolan's avatar
Nolan committed
        mock_elements_worker.check_required_types()
    assert str(e.value) == "At least one element type slug is required."

    with pytest.raises(AssertionError) as e:
Nolan's avatar
Nolan committed
        mock_elements_worker.check_required_types("lol", 42)
    assert str(e.value) == "Element type slugs must be strings."


Nolan's avatar
Nolan committed
def test_check_required_types(responses, mock_elements_worker):
    corpus_id = "11111111-1111-1111-1111-111111111111"
    responses.add(
        responses.GET,
        f"http://testserver/api/v1/corpus/{corpus_id}/",
        json={
            "id": corpus_id,
            "name": "Some Corpus",
            "types": [{"slug": "folder"}, {"slug": "page"}],
        },
    )
Nolan's avatar
Nolan committed
    mock_elements_worker.setup_api_client()
Nolan's avatar
Nolan committed
    assert mock_elements_worker.check_required_types("page")
    assert mock_elements_worker.check_required_types("page", "folder")

    with pytest.raises(MissingTypeError) as e:
Nolan's avatar
Nolan committed
        assert mock_elements_worker.check_required_types("page", "text_line", "act")
    assert (
        str(e.value)
Nolan's avatar
Nolan committed
        == "Element type(s) act, text_line were not found in the Some Corpus corpus (11111111-1111-1111-1111-111111111111)."
Nolan's avatar
Nolan committed
def test_create_missing_types(responses, mock_elements_worker):
    corpus_id = "11111111-1111-1111-1111-111111111111"

    responses.add(
        responses.GET,
        f"http://testserver/api/v1/corpus/{corpus_id}/",
        json={
            "id": corpus_id,
            "name": "Some Corpus",
            "types": [{"slug": "folder"}, {"slug": "page"}],
        },
    )
    responses.add(
        responses.POST,
        "http://testserver/api/v1/elements/type/",
        match=[
            matchers.json_params_matcher(
                {
                    "slug": "text_line",
                    "display_name": "text_line",
                    "folder": False,
                    "corpus": corpus_id,
                }
            )
        ],
    )
    responses.add(
        responses.POST,
        "http://testserver/api/v1/elements/type/",
        match=[
            matchers.json_params_matcher(
                {
                    "slug": "act",
                    "display_name": "act",
                    "folder": False,
                    "corpus": corpus_id,
                }
            )
        ],
    )
Nolan's avatar
Nolan committed
    mock_elements_worker.setup_api_client()
Nolan's avatar
Nolan committed
    assert mock_elements_worker.check_required_types(
        "page", "text_line", "act", create_missing=True
def test_list_elements_elements_list_arg_wrong_type(
    monkeypatch, tmp_path, mock_elements_worker
):
    elements_path = tmp_path / "elements.json"
    elements_path.write_text("{}")
    monkeypatch.setenv("TASK_ELEMENTS", str(elements_path))
    worker = ElementsWorker()
    worker.configure()

    with pytest.raises(AssertionError) as e:
        worker.list_elements()
    assert str(e.value) == "Elements list must be a list"


def test_list_elements_elements_list_arg_empty_list(
    monkeypatch, tmp_path, mock_elements_worker
):
    elements_path = tmp_path / "elements.json"
    elements_path.write_text("[]")
    monkeypatch.setenv("TASK_ELEMENTS", str(elements_path))
    worker = ElementsWorker()
    worker.configure()

    with pytest.raises(AssertionError) as e:
        worker.list_elements()
    assert str(e.value) == "No elements in elements list"


def test_list_elements_elements_list_arg_missing_id(
    monkeypatch, tmp_path, mock_elements_worker
):
    elements_path = tmp_path / "elements.json"
    with elements_path.open("w") as f:
        json.dump([{"type": "volume"}], f)

    monkeypatch.setenv("TASK_ELEMENTS", str(elements_path))
    worker = ElementsWorker()
    worker.configure()

    elt_list = worker.list_elements()

    assert elt_list == []


def test_list_elements_elements_list_arg(monkeypatch, tmp_path, mock_elements_worker):
    elements_path = tmp_path / "elements.json"
    with elements_path.open("w") as f:
        json.dump(
            [
                {"id": "volumeid", "type": "volume"},
                {"id": "pageid", "type": "page"},
                {"id": "actid", "type": "act"},
                {"id": "surfaceid", "type": "surface"},
            ],
            f,
        )

    monkeypatch.setenv("TASK_ELEMENTS", str(elements_path))
    worker = ElementsWorker()
    worker.configure()

    elt_list = worker.list_elements()

    assert elt_list == ["volumeid", "pageid", "actid", "surfaceid"]


def test_list_elements_element_arg(mocker, mock_elements_worker):
    mocker.patch(
        "arkindex_worker.worker.base.argparse.ArgumentParser.parse_args",
        return_value=Namespace(
Erwan Rouchet's avatar
Erwan Rouchet committed
            element=["volumeid", "pageid"],
            verbose=False,
            elements_list=None,
            database=None,
        ),
    )

    worker = ElementsWorker()
    worker.configure()

    elt_list = worker.list_elements()

    assert elt_list == ["volumeid", "pageid"]


def test_list_elements_both_args_error(mocker, mock_elements_worker, tmp_path):
    elements_path = tmp_path / "elements.json"
    with elements_path.open("w") as f:
        json.dump(
            [
                {"id": "volumeid", "type": "volume"},
                {"id": "pageid", "type": "page"},
                {"id": "actid", "type": "act"},
                {"id": "surfaceid", "type": "surface"},
            ],
            f,
        )
    mocker.patch(
        "arkindex_worker.worker.base.argparse.ArgumentParser.parse_args",
        return_value=Namespace(
            element=["anotherid", "againanotherid"],
            verbose=False,
            elements_list=elements_path.open(),
Erwan Rouchet's avatar
Erwan Rouchet committed
            database=None,
        ),
    )

    worker = ElementsWorker()
    worker.configure()

    with pytest.raises(AssertionError) as e:
        worker.list_elements()
    assert str(e.value) == "elements-list and element CLI args shouldn't be both set"


Erwan Rouchet's avatar
Erwan Rouchet committed
def test_database_arg(mocker, mock_elements_worker, tmp_path):
    database_path = tmp_path / "my_database.sqlite"
    init_cache_db(database_path)
    create_version_table()
Erwan Rouchet's avatar
Erwan Rouchet committed

    mocker.patch(
        "arkindex_worker.worker.base.argparse.ArgumentParser.parse_args",
        return_value=Namespace(
            element=["volumeid", "pageid"],
            verbose=False,
            elements_list=None,
            database=str(database_path),
    worker = ElementsWorker(support_cache=True)
Erwan Rouchet's avatar
Erwan Rouchet committed
    worker.configure()

    assert worker.use_cache is True
    assert worker.cache_path == str(database_path)


def test_database_arg_cache_missing_version_table(
    mocker, mock_elements_worker, tmp_path
):
    database_path = tmp_path / "my_database.sqlite"
    database_path.touch()

    mocker.patch(
        "arkindex_worker.worker.base.argparse.ArgumentParser.parse_args",
        return_value=Namespace(
            element=["volumeid", "pageid"],
            verbose=False,
            elements_list=None,
            database=str(database_path),
            dev=False,
        ),
    )

    worker = ElementsWorker(support_cache=True)
    with pytest.raises(AssertionError) as e:
        worker.configure()
    assert (
        str(e.value)
        == f"The SQLite database {database_path} does not have the correct cache version, it should be {SQL_VERSION}"
    )


def test_load_corpus_classes_api_error(responses, mock_elements_worker):
    responses.add(
        responses.GET,
Nolan's avatar
Nolan committed
        "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
        status=500,
    )

    assert not mock_elements_worker.classes
    with pytest.raises(
        Exception, match="Stopping pagination as data will be incomplete"
    ):
Nolan's avatar
Nolan committed
        mock_elements_worker.load_corpus_classes()
    assert len(responses.calls) == len(BASE_API_CALLS) + 5
    assert [
        (call.request.method, call.request.url) for call in responses.calls
    ] == BASE_API_CALLS + [
        # We do 5 retries
Nolan's avatar
Nolan committed
        (
            "GET",
Nolan's avatar
Nolan committed
            "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
Nolan's avatar
Nolan committed
            "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
Nolan's avatar
Nolan committed
            "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
Nolan's avatar
Nolan committed
            "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
Nolan's avatar
Nolan committed
            "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
    ]
    assert not mock_elements_worker.classes


def test_load_corpus_classes(responses, mock_elements_worker):
    responses.add(
        responses.GET,
Nolan's avatar
Nolan committed
        "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
        status=200,
        json={
            "count": 3,
            "next": None,
            "results": [
                {
                    "id": "0000",
                    "name": "good",
                },
                {
                    "id": "1111",
                    "name": "average",
                },
                {
                    "id": "2222",
                    "name": "bad",
                },
            ],
        },
    )

    assert not mock_elements_worker.classes
Nolan's avatar
Nolan committed
    mock_elements_worker.load_corpus_classes()
    assert len(responses.calls) == len(BASE_API_CALLS) + 1
    assert [
        (call.request.method, call.request.url) for call in responses.calls
    ] == BASE_API_CALLS + [
Nolan's avatar
Nolan committed
        (
            "GET",
Nolan's avatar
Nolan committed
            "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
    ]
    assert mock_elements_worker.classes == {
        "good": "0000",
        "average": "1111",
        "bad": "2222",
    }


def test_create_sub_element_wrong_element(mock_elements_worker):
    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=None,
            type="something",
            name="0",
            polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        )
    assert str(e.value) == "element shouldn't be null and should be of type Element"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element="not element type",
            type="something",
            name="0",
            polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        )
    assert str(e.value) == "element shouldn't be null and should be of type Element"


def test_create_sub_element_wrong_type(mock_elements_worker):
    elt = Element({"zone": None})

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type=None,
            name="0",
            polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        )
    assert str(e.value) == "type shouldn't be null and should be of type str"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type=1234,
            name="0",
            polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        )
    assert str(e.value) == "type shouldn't be null and should be of type str"


def test_create_sub_element_wrong_name(mock_elements_worker):
    elt = Element({"zone": None})

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name=None,
            polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        )
    assert str(e.value) == "name shouldn't be null and should be of type str"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name=1234,
            polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        )
    assert str(e.value) == "name shouldn't be null and should be of type str"


def test_create_sub_element_wrong_polygon(mock_elements_worker):
    elt = Element({"zone": None})

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name="0",
            polygon=None,
        )
    assert str(e.value) == "polygon shouldn't be null and should be of type list"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name="O",
            polygon="not a polygon",
        )
    assert str(e.value) == "polygon shouldn't be null and should be of type list"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name="O",
            polygon=[[1, 1], [2, 2]],
        )
    assert str(e.value) == "polygon should have at least three points"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name="O",
            polygon=[[1, 1, 1], [2, 2, 1], [2, 1, 1], [1, 2, 1]],
        )
    assert str(e.value) == "polygon points should be lists of two items"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name="O",
            polygon=[[1], [2], [2], [1]],
        )
    assert str(e.value) == "polygon points should be lists of two items"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name="O",
            polygon=[["not a coord", 1], [2, 2], [2, 1], [1, 2]],
        )
    assert str(e.value) == "polygon points should be lists of two numbers"


@pytest.mark.parametrize("confidence", ["lol", "0.2", -1.0, 1.42, float("inf")])
def test_create_sub_element_wrong_confidence(mock_elements_worker, confidence):
    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_sub_element(
            element=Element({"zone": None}),
            type="something",
            name="blah",
            polygon=[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]],
            confidence=confidence,
        )
    assert str(e.value) == "confidence should be None or a float in [0..1] range"


def test_create_sub_element_api_error(responses, mock_elements_worker):
    elt = Element(
        {
            "id": "12341234-1234-1234-1234-123412341234",
            "corpus": {"id": "11111111-1111-1111-1111-111111111111"},
            "zone": {"image": {"id": "22222222-2222-2222-2222-222222222222"}},
        }
    )
    responses.add(
        responses.POST,
        "http://testserver/api/v1/elements/create/",
        status=500,
    )

    with pytest.raises(ErrorResponse):
        mock_elements_worker.create_sub_element(
            element=elt,
            type="something",
            name="0",
            polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        )

    assert len(responses.calls) == len(BASE_API_CALLS) + 5
    assert [
        (call.request.method, call.request.url) for call in responses.calls
    ] == BASE_API_CALLS + [
        # We retry 5 times the API call
        ("POST", "http://testserver/api/v1/elements/create/"),
        ("POST", "http://testserver/api/v1/elements/create/"),
        ("POST", "http://testserver/api/v1/elements/create/"),
        ("POST", "http://testserver/api/v1/elements/create/"),
        ("POST", "http://testserver/api/v1/elements/create/"),
@pytest.mark.parametrize("slim_output", [True, False])
def test_create_sub_element(responses, mock_elements_worker, slim_output):
    elt = Element(
        {
            "id": "12341234-1234-1234-1234-123412341234",
            "corpus": {"id": "11111111-1111-1111-1111-111111111111"},
            "zone": {"image": {"id": "22222222-2222-2222-2222-222222222222"}},
        }
    )
    child_elt = {
        "id": "12345678-1234-1234-1234-123456789123",
        "corpus": {"id": "11111111-1111-1111-1111-111111111111"},
        "zone": {"image": {"id": "22222222-2222-2222-2222-222222222222"}},
    }
    responses.add(
        responses.POST,
        "http://testserver/api/v1/elements/create/",
    element_creation_response = mock_elements_worker.create_sub_element(
        element=elt,
        type="something",
        name="0",
        polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        slim_output=slim_output,
    assert len(responses.calls) == len(BASE_API_CALLS) + 1
    assert [
        (call.request.method, call.request.url) for call in responses.calls
    ] == BASE_API_CALLS + [
            "http://testserver/api/v1/elements/create/",
    assert json.loads(responses.calls[-1].request.body) == {
        "type": "something",
        "name": "0",
        "image": "22222222-2222-2222-2222-222222222222",
        "corpus": "11111111-1111-1111-1111-111111111111",
        "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
        "parent": "12341234-1234-1234-1234-123412341234",
        "worker_run_id": "56785678-5678-5678-5678-567856785678",
    if slim_output:
        assert element_creation_response == "12345678-1234-1234-1234-123456789123"
    else:
        assert Element(element_creation_response) == Element(child_elt)


def test_create_sub_element_confidence(responses, mock_elements_worker):
    elt = Element(
        {
            "id": "12341234-1234-1234-1234-123412341234",
            "corpus": {"id": "11111111-1111-1111-1111-111111111111"},
            "zone": {"image": {"id": "22222222-2222-2222-2222-222222222222"}},
        }
    )
    responses.add(
        responses.POST,
        "http://testserver/api/v1/elements/create/",
        status=200,
        json={"id": "12345678-1234-1234-1234-123456789123"},
    )

    sub_element_id = mock_elements_worker.create_sub_element(
        element=elt,
        type="something",
        name="0",
        polygon=[[1, 1], [2, 2], [2, 1], [1, 2]],
        confidence=0.42,
    )

    assert len(responses.calls) == len(BASE_API_CALLS) + 1
    assert [
        (call.request.method, call.request.url) for call in responses.calls
    ] == BASE_API_CALLS + [
        ("POST", "http://testserver/api/v1/elements/create/"),
    ]
    assert json.loads(responses.calls[-1].request.body) == {
        "type": "something",
        "name": "0",
        "image": "22222222-2222-2222-2222-222222222222",
        "corpus": "11111111-1111-1111-1111-111111111111",
        "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
        "parent": "12341234-1234-1234-1234-123412341234",
        "worker_run_id": "56785678-5678-5678-5678-567856785678",
    }
    assert sub_element_id == "12345678-1234-1234-1234-123456789123"


def test_create_elements_wrong_parent(mock_elements_worker):
Erwan Rouchet's avatar
Erwan Rouchet committed
    with pytest.raises(TypeError) as e:
        mock_elements_worker.create_elements(
            parent=None,
            elements=[],
        )
Erwan Rouchet's avatar
Erwan Rouchet committed
    assert (
        str(e.value) == "Parent element should be an Element or CachedElement instance"
    )
Erwan Rouchet's avatar
Erwan Rouchet committed
    with pytest.raises(TypeError) as e:
        mock_elements_worker.create_elements(
            parent="not element type",
            elements=[],
        )
Erwan Rouchet's avatar
Erwan Rouchet committed
    assert (
        str(e.value) == "Parent element should be an Element or CachedElement instance"
    )
Erwan Rouchet's avatar
Erwan Rouchet committed
def test_create_elements_no_zone(mock_elements_worker):
    elt = Element({"zone": None})
Erwan Rouchet's avatar
Erwan Rouchet committed
    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=None,
        )
    assert str(e.value) == "create_elements cannot be used on parents without zones"

    elt = CachedElement(
        id="11111111-1111-1111-1111-1111111111", name="blah", type="blah"
    )
    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=None,
        )
    assert str(e.value) == "create_elements cannot be used on parents without images"


def test_create_elements_wrong_elements(mock_elements_worker):
    elt = Element({"zone": {"image": {"id": "image_id"}}})

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=None,
        )
    assert str(e.value) == "elements shouldn't be null and should be of type list"

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements="not a list",
        )
    assert str(e.value) == "elements shouldn't be null and should be of type list"


def test_create_elements_wrong_elements_instance(mock_elements_worker):
Erwan Rouchet's avatar
Erwan Rouchet committed
    elt = Element({"zone": {"image": {"id": "image_id"}}})

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=["not a dict"],
        )
    assert str(e.value) == "Element at index 0 in elements: Should be of type dict"


def test_create_elements_wrong_elements_name(mock_elements_worker):
Erwan Rouchet's avatar
Erwan Rouchet committed
    elt = Element({"zone": {"image": {"id": "image_id"}}})

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": None,
                    "type": "something",
                    "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: name shouldn't be null and should be of type str"
    )

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": 1234,
                    "type": "something",
                    "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: name shouldn't be null and should be of type str"
    )


def test_create_elements_wrong_elements_type(mock_elements_worker):
Erwan Rouchet's avatar
Erwan Rouchet committed
    elt = Element({"zone": {"image": {"id": "image_id"}}})

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": None,
                    "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: type shouldn't be null and should be of type str"
    )

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": 1234,
                    "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: type shouldn't be null and should be of type str"
    )


def test_create_elements_wrong_elements_polygon(mock_elements_worker):
Erwan Rouchet's avatar
Erwan Rouchet committed
    elt = Element({"zone": {"image": {"id": "image_id"}}})

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": "something",
                    "polygon": None,
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: polygon shouldn't be null and should be of type list"
    )

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": "something",
                    "polygon": "not a polygon",
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: polygon shouldn't be null and should be of type list"
    )

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": "something",
                    "polygon": [[1, 1], [2, 2]],
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: polygon should have at least three points"
    )

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": "something",
                    "polygon": [[1, 1, 1], [2, 2, 1], [2, 1, 1], [1, 2, 1]],
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: polygon points should be lists of two items"
    )

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": "something",
                    "polygon": [[1], [2], [2], [1]],
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: polygon points should be lists of two items"
    )

    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": "something",
                    "polygon": [["not a coord", 1], [2, 2], [2, 1], [1, 2]],
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: polygon points should be lists of two numbers"
    )


@pytest.mark.parametrize("confidence", ["lol", "0.2", -1.0, 1.42, float("inf")])
def test_create_elements_wrong_elements_confidence(mock_elements_worker, confidence):
    with pytest.raises(AssertionError) as e:
        mock_elements_worker.create_elements(
            parent=Element({"zone": {"image": {"id": "image_id"}}}),
            elements=[
                {
                    "name": "a",
                    "type": "something",
                    "polygon": [[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]],
                    "confidence": confidence,
                }
            ],
        )
    assert (
        str(e.value)
        == "Element at index 0 in elements: confidence should be None or a float in [0..1] range"
    )


def test_create_elements_api_error(responses, mock_elements_worker):
Erwan Rouchet's avatar
Erwan Rouchet committed
    elt = Element(
        {
            "id": "12341234-1234-1234-1234-123412341234",
            "zone": {
                "image": {
                    "id": "c0fec0fe-c0fe-c0fe-c0fe-c0fec0fec0fe",
                    "width": 42,
                    "height": 42,
                    "url": "http://aaaa",
                }
            },
        }
    )
    responses.add(
        responses.POST,
        "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/children/bulk/",
        status=500,
    )

    with pytest.raises(ErrorResponse):
        mock_elements_worker.create_elements(
            parent=elt,
            elements=[
                {
                    "name": "0",
                    "type": "something",
                    "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
                }
            ],
        )

    assert len(responses.calls) == len(BASE_API_CALLS) + 5
    assert [
        (call.request.method, call.request.url) for call in responses.calls
    ] == BASE_API_CALLS + [
        # We retry 5 times the API call
        (
            "POST",
            "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/children/bulk/",
        ),
        (
            "POST",
            "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/children/bulk/",
        ),
        (
            "POST",
            "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/children/bulk/",
        ),
        (
            "POST",
            "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/children/bulk/",
        ),
        (
            "POST",
            "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/children/bulk/",
        ),
Erwan Rouchet's avatar
Erwan Rouchet committed
def test_create_elements_cached_element(responses, mock_elements_worker_with_cache):
    image = CachedImage.create(
        id=UUID("c0fec0fe-c0fe-c0fe-c0fe-c0fec0fec0fe"),
        width=42,
        height=42,
        url="http://aaaa",
    )
    elt = CachedElement.create(
        id=UUID("12341234-1234-1234-1234-123412341234"),
        type="parent",
        image_id=image.id,
        polygon="[[0, 0], [0, 1000], [1000, 1000], [1000, 0], [0, 0]]",
    )
    responses.add(
        responses.POST,
        "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/children/bulk/",
        status=200,
        json=[{"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08"}],
    )

    created_ids = mock_elements_worker_with_cache.create_elements(
        parent=elt,
        elements=[
            {
                "name": "0",
                "type": "something",
                "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
            }
        ],
    )

    assert len(responses.calls) == len(BASE_API_CALLS) + 1
    assert [
        (call.request.method, call.request.url) for call in responses.calls
    ] == BASE_API_CALLS + [
        (
            "POST",
            "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/children/bulk/",
        ),
    assert json.loads(responses.calls[-1].request.body) == {
Erwan Rouchet's avatar
Erwan Rouchet committed
        "elements": [
            {
                "name": "0",
                "type": "something",
                "polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
            }
        ],
        "worker_run_id": "56785678-5678-5678-5678-567856785678",
Erwan Rouchet's avatar
Erwan Rouchet committed
    }
    assert created_ids == [{"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08"}]

    # Check that created elements were properly stored in SQLite cache
    assert list(CachedElement.select().order_by(CachedElement.id)) == [
        elt,