Skip to content
Snippets Groups Projects
Commit 8dedb440 authored by Manon Blanco's avatar Manon Blanco
Browse files

Allow to create element without zone and on folder

parent 19675ca5
No related branches found
Tags 0.3.2
1 merge request!473Allow to create element without zone and on folder
Pipeline #148486 passed
......@@ -86,8 +86,9 @@ class ElementMixin:
element: Element,
type: str,
name: str,
polygon: list[list[int | float]],
polygon: list[list[int | float]] | None = None,
confidence: float | None = None,
image: str | None = None,
slim_output: bool = True,
) -> str:
"""
......@@ -96,8 +97,10 @@ class ElementMixin:
:param Element element: The parent element.
:param type: Slug of the element type for this child element.
:param name: Name of the child element.
:param polygon: Polygon of the child element.
:param polygon: Optional polygon of the child element.
:param confidence: Optional confidence score, between 0.0 and 1.0.
:param image: Optional image ID of the child element.
:param slim_output: Whether to return the child ID or the full child.
:returns: UUID of the created element.
"""
assert element and isinstance(
......@@ -109,19 +112,29 @@ class ElementMixin:
assert name and isinstance(
name, str
), "name shouldn't be null and should be of type str"
assert polygon and isinstance(
assert polygon is None or isinstance(
polygon, list
), "polygon shouldn't be null and should be of type list"
assert len(polygon) >= 3, "polygon should have at least three points"
assert all(
isinstance(point, list) and len(point) == 2 for point in polygon
), "polygon points should be lists of two items"
assert all(
isinstance(coord, int | float) for point in polygon for coord in point
), "polygon points should be lists of two numbers"
), "polygon should be None or a list"
if polygon is not None:
assert len(polygon) >= 3, "polygon should have at least three points"
assert all(
isinstance(point, list) and len(point) == 2 for point in polygon
), "polygon points should be lists of two items"
assert all(
isinstance(coord, int | float) for point in polygon for coord in point
), "polygon points should be lists of two numbers"
assert confidence is None or (
isinstance(confidence, float) and 0 <= confidence <= 1
), "confidence should be None or a float in [0..1] range"
assert image is None or isinstance(image, str), "image should be None or string"
if image is not None:
# Make sure it's a valid UUID
try:
UUID(image)
except ValueError as e:
raise ValueError("image is not a valid uuid.") from e
if polygon and image is None:
assert element.zone, "An image or a parent with an image is required to create an element with a polygon."
assert isinstance(slim_output, bool), "slim_output should be of type bool"
if self.is_read_only:
......@@ -133,7 +146,7 @@ class ElementMixin:
body={
"type": type,
"name": name,
"image": element.zone.image.id,
"image": image,
"corpus": element.corpus.id,
"polygon": polygon,
"parent": element.id,
......
......@@ -428,19 +428,7 @@ def test_create_sub_element_wrong_name(mock_elements_worker):
def test_create_sub_element_wrong_polygon(mock_elements_worker):
elt = Element({"zone": None})
with pytest.raises(
AssertionError, match="polygon shouldn't be null and should be of type list"
):
mock_elements_worker.create_sub_element(
element=elt,
type="something",
name="0",
polygon=None,
)
with pytest.raises(
AssertionError, match="polygon shouldn't be null and should be of type list"
):
with pytest.raises(AssertionError, match="polygon should be None or a list"):
mock_elements_worker.create_sub_element(
element=elt,
type="something",
......@@ -504,6 +492,42 @@ def test_create_sub_element_wrong_confidence(mock_elements_worker, confidence):
)
@pytest.mark.parametrize(
("image", "error_type", "error_message"),
[
(1, AssertionError, "image should be None or string"),
("not a uuid", ValueError, "image is not a valid uuid."),
],
)
def test_create_sub_element_wrong_image(
mock_elements_worker, image, error_type, error_message
):
with pytest.raises(error_type, match=re.escape(error_message)):
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]],
image=image,
)
def test_create_sub_element_wrong_image_and_polygon(mock_elements_worker):
with pytest.raises(
AssertionError,
match=re.escape(
"An image or a parent with an image is required to create an element with a polygon."
),
):
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]],
image=None,
)
def test_create_sub_element_api_error(responses, mock_elements_worker):
elt = Element(
{
......@@ -580,7 +604,7 @@ def test_create_sub_element(responses, mock_elements_worker, slim_output):
assert json.loads(responses.calls[-1].request.body) == {
"type": "something",
"name": "0",
"image": "22222222-2222-2222-2222-222222222222",
"image": None,
"corpus": "11111111-1111-1111-1111-111111111111",
"polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
"parent": "12341234-1234-1234-1234-123412341234",
......@@ -625,7 +649,7 @@ def test_create_sub_element_confidence(responses, mock_elements_worker):
assert json.loads(responses.calls[-1].request.body) == {
"type": "something",
"name": "0",
"image": "22222222-2222-2222-2222-222222222222",
"image": None,
"corpus": "11111111-1111-1111-1111-111111111111",
"polygon": [[1, 1], [2, 2], [2, 1], [1, 2]],
"parent": "12341234-1234-1234-1234-123412341234",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment