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

Parametrize `test_classifications`

parent 673b5cc1
No related branches found
No related tags found
No related merge requests found
Pipeline #159904 passed
...@@ -190,54 +190,145 @@ def test_retrieve_ml_class_not_in_cache(responses, mock_elements_worker): ...@@ -190,54 +190,145 @@ def test_retrieve_ml_class_not_in_cache(responses, mock_elements_worker):
] ]
def test_create_classification_wrong_element(mock_elements_worker): @pytest.mark.parametrize(
with pytest.raises( ("data", "error_message"),
AssertionError, [
match="element shouldn't be null and should be an Element or CachedElement", # Wrong element
): (
mock_elements_worker.create_classification( {
element=None, "element": None,
ml_class="a_class", "ml_class": "a_class",
confidence=0.42, "confidence": 0.42,
high_confidence=True, "high_confidence": True,
) },
"element shouldn't be null and should be an Element or CachedElement",
with pytest.raises( ),
AssertionError, (
match="element shouldn't be null and should be an Element or CachedElement", {
): "element": "not element type",
mock_elements_worker.create_classification( "ml_class": "a_class",
element="not element type", "confidence": 0.42,
ml_class="a_class", "high_confidence": True,
confidence=0.42, },
high_confidence=True, "element shouldn't be null and should be an Element or CachedElement",
) ),
# Wrong ml_class
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"ml_class": None,
"confidence": 0.42,
"high_confidence": True,
},
"ml_class shouldn't be null and should be of type str",
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"ml_class": 1234,
"confidence": 0.42,
"high_confidence": True,
},
"ml_class shouldn't be null and should be of type str",
),
# Wrong confidence
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"ml_class": "a_class",
"confidence": None,
"high_confidence": True,
},
"confidence shouldn't be null and should be a float in [0..1] range",
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"ml_class": "a_class",
"confidence": "wrong confidence",
"high_confidence": True,
},
"confidence shouldn't be null and should be a float in [0..1] range",
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"ml_class": "a_class",
"confidence": 0,
"high_confidence": True,
},
"confidence shouldn't be null and should be a float in [0..1] range",
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"ml_class": "a_class",
"confidence": 2.00,
"high_confidence": True,
},
"confidence shouldn't be null and should be a float in [0..1] range",
),
# Wrong high_confidence
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"ml_class": "a_class",
"confidence": 0.42,
"high_confidence": None,
},
"high_confidence shouldn't be null and should be of type bool",
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"ml_class": "a_class",
"confidence": 0.42,
"high_confidence": "wrong high_confidence",
},
"high_confidence shouldn't be null and should be of type bool",
),
],
)
def test_create_classification_wrong_data(data, error_message, mock_elements_worker):
mock_elements_worker.classes = {"a_class": "0000"}
with pytest.raises(AssertionError, match=re.escape(error_message)):
mock_elements_worker.create_classification(**data)
def test_create_classification_wrong_ml_class(mock_elements_worker, responses): def test_create_classification_api_error(responses, mock_elements_worker):
mock_elements_worker.classes = {"a_class": "0000"}
elt = Element({"id": "12341234-1234-1234-1234-123412341234"}) elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
responses.add(
responses.POST,
"http://testserver/api/v1/classifications/",
status=500,
)
with pytest.raises( with pytest.raises(ErrorResponse):
AssertionError, match="ml_class shouldn't be null and should be of type str"
):
mock_elements_worker.create_classification( mock_elements_worker.create_classification(
element=elt, element=elt,
ml_class=None, ml_class="a_class",
confidence=0.42, confidence=0.42,
high_confidence=True, high_confidence=True,
) )
with pytest.raises( assert len(responses.calls) == len(BASE_API_CALLS) + 5
AssertionError, match="ml_class shouldn't be null and should be of type str" assert [
): (call.request.method, call.request.url) for call in responses.calls
mock_elements_worker.create_classification( ] == BASE_API_CALLS + [
element=elt, # We retry 5 times the API call
ml_class=1234, ("POST", "http://testserver/api/v1/classifications/"),
confidence=0.42, ("POST", "http://testserver/api/v1/classifications/"),
high_confidence=True, ("POST", "http://testserver/api/v1/classifications/"),
) ("POST", "http://testserver/api/v1/classifications/"),
("POST", "http://testserver/api/v1/classifications/"),
]
# Automatically create a missing class ! def test_create_classification_create_ml_class(mock_elements_worker, responses):
elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
# Automatically create a missing class!
responses.add( responses.add(
responses.POST, responses.POST,
"http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/", "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
...@@ -283,119 +374,6 @@ def test_create_classification_wrong_ml_class(mock_elements_worker, responses): ...@@ -283,119 +374,6 @@ def test_create_classification_wrong_ml_class(mock_elements_worker, responses):
] ]
def test_create_classification_wrong_confidence(mock_elements_worker):
mock_elements_worker.classes = {"a_class": "0000"}
elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
with pytest.raises(
AssertionError,
match=re.escape(
"confidence shouldn't be null and should be a float in [0..1] range"
),
):
mock_elements_worker.create_classification(
element=elt,
ml_class="a_class",
confidence=None,
high_confidence=True,
)
with pytest.raises(
AssertionError,
match=re.escape(
"confidence shouldn't be null and should be a float in [0..1] range"
),
):
mock_elements_worker.create_classification(
element=elt,
ml_class="a_class",
confidence="wrong confidence",
high_confidence=True,
)
with pytest.raises(
AssertionError,
match=re.escape(
"confidence shouldn't be null and should be a float in [0..1] range"
),
):
mock_elements_worker.create_classification(
element=elt,
ml_class="a_class",
confidence=0,
high_confidence=True,
)
with pytest.raises(
AssertionError,
match=re.escape(
"confidence shouldn't be null and should be a float in [0..1] range"
),
):
mock_elements_worker.create_classification(
element=elt,
ml_class="a_class",
confidence=2.00,
high_confidence=True,
)
def test_create_classification_wrong_high_confidence(mock_elements_worker):
mock_elements_worker.classes = {"a_class": "0000"}
elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
with pytest.raises(
AssertionError,
match="high_confidence shouldn't be null and should be of type bool",
):
mock_elements_worker.create_classification(
element=elt,
ml_class="a_class",
confidence=0.42,
high_confidence=None,
)
with pytest.raises(
AssertionError,
match="high_confidence shouldn't be null and should be of type bool",
):
mock_elements_worker.create_classification(
element=elt,
ml_class="a_class",
confidence=0.42,
high_confidence="wrong high_confidence",
)
def test_create_classification_api_error(responses, mock_elements_worker):
mock_elements_worker.classes = {"a_class": "0000"}
elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
responses.add(
responses.POST,
"http://testserver/api/v1/classifications/",
status=500,
)
with pytest.raises(ErrorResponse):
mock_elements_worker.create_classification(
element=elt,
ml_class="a_class",
confidence=0.42,
high_confidence=True,
)
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/classifications/"),
("POST", "http://testserver/api/v1/classifications/"),
("POST", "http://testserver/api/v1/classifications/"),
("POST", "http://testserver/api/v1/classifications/"),
("POST", "http://testserver/api/v1/classifications/"),
]
def test_create_classification(responses, mock_elements_worker): def test_create_classification(responses, mock_elements_worker):
mock_elements_worker.classes = {"a_class": "0000"} mock_elements_worker.classes = {"a_class": "0000"}
elt = Element({"id": "12341234-1234-1234-1234-123412341234"}) elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
...@@ -519,278 +497,263 @@ def test_create_classification_duplicate_worker_run(responses, mock_elements_wor ...@@ -519,278 +497,263 @@ def test_create_classification_duplicate_worker_run(responses, mock_elements_wor
} }
def test_create_classifications_wrong_element(mock_elements_worker): @pytest.mark.parametrize(
with pytest.raises( ("data", "error_message", "error_type"),
AssertionError, [
match="element shouldn't be null and should be an Element or CachedElement", # Wrong element
): (
mock_elements_worker.create_classifications( {
element=None, "element": None,
classifications=[ "classifications": [
{ {
"ml_class_id": "uuid1", "ml_class_id": "uuid1",
"confidence": 0.75, "confidence": 0.75,
"high_confidence": False, "high_confidence": False,
}, },
{ {
"ml_class_id": "uuid2", "ml_class_id": "uuid2",
"confidence": 0.25, "confidence": 0.25,
"high_confidence": False, "high_confidence": False,
}, },
], ],
) },
"element shouldn't be null and should be an Element or CachedElement",
with pytest.raises( AssertionError,
AssertionError,
match="element shouldn't be null and should be an Element or CachedElement",
):
mock_elements_worker.create_classifications(
element="not element type",
classifications=[
{
"ml_class_id": "uuid1",
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": "uuid2",
"confidence": 0.25,
"high_confidence": False,
},
],
)
def test_create_classifications_wrong_classifications(mock_elements_worker):
elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
with pytest.raises(
AssertionError,
match="classifications shouldn't be null and should be of type list",
):
mock_elements_worker.create_classifications(
element=elt,
classifications=None,
)
with pytest.raises(
AssertionError,
match="classifications shouldn't be null and should be of type list",
):
mock_elements_worker.create_classifications(
element=elt,
classifications=1234,
)
with pytest.raises(
AssertionError,
match="Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
):
mock_elements_worker.create_classifications(
element=elt,
classifications=[
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": 0.25,
"high_confidence": False,
},
],
)
with pytest.raises(
AssertionError,
match="Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
):
mock_elements_worker.create_classifications(
element=elt,
classifications=[
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": None,
"confidence": 0.25,
"high_confidence": False,
},
],
)
with pytest.raises(
AssertionError,
match="Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
):
mock_elements_worker.create_classifications(
element=elt,
classifications=[
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": 1234,
"confidence": 0.25,
"high_confidence": False,
},
],
)
with pytest.raises(
ValueError,
match="Classification at index 1 in classifications: ml_class_id is not a valid uuid.",
):
mock_elements_worker.create_classifications(
element=elt,
classifications=[
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": "not_an_uuid",
"confidence": 0.25,
"high_confidence": False,
},
],
)
with pytest.raises(
AssertionError,
match=re.escape(
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
), ),
): (
mock_elements_worker.create_classifications( {
element=elt, "element": "not element type",
classifications=[ "classifications": [
{ {
"ml_class_id": str(uuid4()), "ml_class_id": "uuid1",
"confidence": 0.75, "confidence": 0.75,
"high_confidence": False, "high_confidence": False,
}, },
{ {
"ml_class_id": str(uuid4()), "ml_class_id": "uuid2",
"high_confidence": False, "confidence": 0.25,
}, "high_confidence": False,
], },
) ],
},
with pytest.raises( "element shouldn't be null and should be an Element or CachedElement",
AssertionError, AssertionError,
match=re.escape(
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
), ),
): # Wrong classifications
mock_elements_worker.create_classifications( (
element=elt, {
classifications=[ "element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
{ "classifications": None,
"ml_class_id": str(uuid4()), },
"confidence": 0.75, "classifications shouldn't be null and should be of type list",
"high_confidence": False, AssertionError,
},
{
"ml_class_id": str(uuid4()),
"confidence": None,
"high_confidence": False,
},
],
)
with pytest.raises(
AssertionError,
match=re.escape(
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
), ),
): (
mock_elements_worker.create_classifications( {
element=elt, "element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
classifications=[ "classifications": 1234,
{ },
"ml_class_id": str(uuid4()), "classifications shouldn't be null and should be of type list",
"confidence": 0.75, AssertionError,
"high_confidence": False,
},
{
"ml_class_id": str(uuid4()),
"confidence": "wrong confidence",
"high_confidence": False,
},
],
)
with pytest.raises(
AssertionError,
match=re.escape(
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
), ),
): # Wrong classifications > ml_class_id
mock_elements_worker.create_classifications( (
element=elt, {
classifications=[ "element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
{ "classifications": [
"ml_class_id": str(uuid4()), {
"confidence": 0.75, "ml_class_id": str(uuid4()),
"high_confidence": False, "confidence": 0.75,
}, "high_confidence": False,
{ },
"ml_class_id": str(uuid4()), {
"confidence": 0, "ml_class_id": 0.25,
"high_confidence": False, "high_confidence": False,
}, },
], ],
) },
"Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
with pytest.raises( AssertionError,
AssertionError,
match=re.escape(
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
), ),
): (
mock_elements_worker.create_classifications( {
element=elt, "element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
classifications=[ "classifications": [
{ {
"ml_class_id": str(uuid4()), "ml_class_id": str(uuid4()),
"confidence": 0.75, "confidence": 0.75,
"high_confidence": False, "high_confidence": False,
}, },
{ {
"ml_class_id": str(uuid4()), "ml_class_id": None,
"confidence": 2.00, "confidence": 0.25,
"high_confidence": False, "high_confidence": False,
}, },
], ],
) },
"Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
with pytest.raises( AssertionError,
AssertionError,
match=re.escape(
"Classification at index 1 in classifications: high_confidence should be of type bool"
), ),
): (
mock_elements_worker.create_classifications( {
element=elt, "element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
classifications=[ "classifications": [
{ {
"ml_class_id": str(uuid4()), "ml_class_id": str(uuid4()),
"confidence": 0.75, "confidence": 0.75,
"high_confidence": False, "high_confidence": False,
}, },
{ {
"ml_class_id": str(uuid4()), "ml_class_id": 1234,
"confidence": 0.25, "confidence": 0.25,
"high_confidence": "wrong high_confidence", "high_confidence": False,
}, },
], ],
) },
"Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
AssertionError,
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"classifications": [
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": "not_an_uuid",
"confidence": 0.25,
"high_confidence": False,
},
],
},
"Classification at index 1 in classifications: ml_class_id is not a valid uuid.",
ValueError,
),
# Wrong classifications > confidence
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"classifications": [
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": str(uuid4()),
"high_confidence": False,
},
],
},
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range",
AssertionError,
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"classifications": [
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": str(uuid4()),
"confidence": None,
"high_confidence": False,
},
],
},
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range",
AssertionError,
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"classifications": [
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": str(uuid4()),
"confidence": "wrong confidence",
"high_confidence": False,
},
],
},
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range",
AssertionError,
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"classifications": [
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": str(uuid4()),
"confidence": 0,
"high_confidence": False,
},
],
},
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range",
AssertionError,
),
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"classifications": [
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": str(uuid4()),
"confidence": 2.00,
"high_confidence": False,
},
],
},
"Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range",
AssertionError,
),
# Wrong classifications > high_confidence
(
{
"element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
"classifications": [
{
"ml_class_id": str(uuid4()),
"confidence": 0.75,
"high_confidence": False,
},
{
"ml_class_id": str(uuid4()),
"confidence": 0.25,
"high_confidence": "wrong high_confidence",
},
],
},
"Classification at index 1 in classifications: high_confidence should be of type bool",
AssertionError,
),
],
)
def test_create_classifications_wrong_data(
data, error_message, error_type, mock_elements_worker
):
with pytest.raises(error_type, match=re.escape(error_message)):
mock_elements_worker.create_classifications(**data)
def test_create_classifications_api_error(responses, mock_elements_worker): def test_create_classifications_api_error(responses, mock_elements_worker):
......
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