Newer
Older
# -*- coding: utf-8 -*-
import json
import pytest
from apistar.exceptions import ErrorResponse
from arkindex_worker.cache import (
CachedElement,
CachedEntity,
CachedTranscription,
CachedTranscriptionEntity,
)
from arkindex_worker.models import Transcription
from arkindex_worker.worker.entity import MissingEntityType
from arkindex_worker.worker.transcription import TextOrientation
from . import BASE_API_CALLS
def test_create_entity_wrong_name(mock_elements_worker):
with pytest.raises(
AssertionError, match="name shouldn't be null and should be of type str"
):
mock_elements_worker.create_entity(
name=None,
with pytest.raises(
AssertionError, match="name shouldn't be null and should be of type str"
):
mock_elements_worker.create_entity(
name=1234,
)
def test_create_entity_wrong_type(mock_elements_worker):
with pytest.raises(
AssertionError, match="type shouldn't be null and should be of type str"
):
mock_elements_worker.create_entity(
name="Bob Bob",
type=None,
)
with pytest.raises(
AssertionError, match="type shouldn't be null and should be of type str"
):
mock_elements_worker.create_entity(
name="Bob Bob",
type=1234,
)
def test_create_entity_wrong_corpus(monkeypatch, mock_elements_worker):
# Triggering an error on metas param, not giving corpus should work since
# ARKINDEX_CORPUS_ID environment variable is set on mock_elements_worker
with pytest.raises(AssertionError, match="metas should be of type dict"):
mock_elements_worker.create_entity(
name="Bob Bob",
metas="wrong metas",
)
def test_create_entity_wrong_metas(mock_elements_worker):
with pytest.raises(AssertionError, match="metas should be of type dict"):
mock_elements_worker.create_entity(
name="Bob Bob",
metas="wrong metas",
)
def test_create_entity_wrong_validated(mock_elements_worker):
with pytest.raises(AssertionError, match="validated should be of type bool"):
mock_elements_worker.create_entity(
name="Bob Bob",
validated="wrong validated",
)
def test_create_entity_api_error(responses, mock_elements_worker):
# Set one entity type
mock_elements_worker.entity_types = {"person": "person-entity-type-id"}
responses.add(
responses.POST,
"http://testserver/api/v1/entity/",
status=500,
)
with pytest.raises(ErrorResponse):
mock_elements_worker.create_entity(
name="Bob Bob",
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/entity/"),
("POST", "http://testserver/api/v1/entity/"),
("POST", "http://testserver/api/v1/entity/"),
("POST", "http://testserver/api/v1/entity/"),
("POST", "http://testserver/api/v1/entity/"),
]
def test_create_entity(responses, mock_elements_worker):
# Set one entity type
mock_elements_worker.entity_types = {"person": "person-entity-type-id"}
responses.add(
responses.POST,
"http://testserver/api/v1/entity/",
status=200,
json={"id": "12345678-1234-1234-1234-123456789123"},
)
entity_id = mock_elements_worker.create_entity(
name="Bob Bob",
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/entity/"),
assert json.loads(responses.calls[-1].request.body) == {
"corpus": "11111111-1111-1111-1111-111111111111",
"worker_run_id": "56785678-5678-5678-5678-567856785678",
}
assert entity_id == "12345678-1234-1234-1234-123456789123"
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def test_create_entity_missing_type(responses, mock_elements_worker):
"""
Create entity with an unknown type will fail.
"""
# Call to list entity types
responses.add(
responses.GET,
"http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/entity-types/",
status=200,
json={
"count": 1,
"next": None,
"results": [
{"id": "person-entity-type-id", "name": "person", "color": "00d1b2"}
],
},
)
with pytest.raises(
AssertionError, match="Entity type `new-entity` not found in the corpus."
):
mock_elements_worker.create_entity(
name="Bob Bob",
type="new-entity",
)
assert len(responses.calls) == len(BASE_API_CALLS) + 1
assert [
(call.request.method, call.request.url) for call in responses.calls
] == BASE_API_CALLS + [
(
"GET",
"http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/entity-types/",
),
]
def test_create_entity_with_cache(responses, mock_elements_worker_with_cache):
# Set one entity type
mock_elements_worker_with_cache.entity_types = {"person": "person-entity-type-id"}
responses.add(
responses.POST,
"http://testserver/api/v1/entity/",
status=200,
json={"id": "12345678-1234-1234-1234-123456789123"},
)
entity_id = mock_elements_worker_with_cache.create_entity(
name="Bob Bob",
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/entity/"),
assert json.loads(responses.calls[-1].request.body) == {
"corpus": "11111111-1111-1111-1111-111111111111",
"worker_run_id": "56785678-5678-5678-5678-567856785678",
}
assert entity_id == "12345678-1234-1234-1234-123456789123"
# Check that created entity was properly stored in SQLite cache
assert list(CachedEntity.select()) == [
CachedEntity(
id=UUID("12345678-1234-1234-1234-123456789123"),
type="person",
name="Bob Bob",
validated=False,
worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
def test_create_transcription_entity_wrong_transcription(mock_elements_worker):
with pytest.raises(
AssertionError,
match="transcription shouldn't be null and should be a Transcription",
):
mock_elements_worker.create_transcription_entity(
transcription=None,
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=10,
)
with pytest.raises(
AssertionError,
match="transcription shouldn't be null and should be a Transcription",
):
mock_elements_worker.create_transcription_entity(
transcription=1234,
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=10,
)
def test_create_transcription_entity_wrong_entity(mock_elements_worker):
with pytest.raises(
AssertionError, match="entity shouldn't be null and should be of type str"
):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity=None,
offset=5,
length=10,
)
with pytest.raises(
AssertionError, match="entity shouldn't be null and should be of type str"
):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity=1234,
offset=5,
length=10,
)
def test_create_transcription_entity_wrong_offset(mock_elements_worker):
with pytest.raises(
AssertionError,
match="offset shouldn't be null and should be a positive integer",
):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=None,
length=10,
)
with pytest.raises(
AssertionError,
match="offset shouldn't be null and should be a positive integer",
):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset="not an int",
length=10,
)
with pytest.raises(
AssertionError,
match="offset shouldn't be null and should be a positive integer",
):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=-1,
length=10,
)
def test_create_transcription_entity_wrong_length(mock_elements_worker):
with pytest.raises(
AssertionError,
match="length shouldn't be null and should be a strictly positive integer",
):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=None,
)
with pytest.raises(
AssertionError,
match="length shouldn't be null and should be a strictly positive integer",
):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length="not an int",
)
with pytest.raises(
AssertionError,
match="length shouldn't be null and should be a strictly positive integer",
):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=0,
)
def test_create_transcription_entity_api_error(responses, mock_elements_worker):
responses.add(
responses.POST,
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
status=500,
)
with pytest.raises(ErrorResponse):
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=10,
)
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/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
(
"POST",
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
(
"POST",
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
(
"POST",
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
(
"POST",
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
def test_create_transcription_entity_no_confidence(responses, mock_elements_worker):
responses.add(
responses.POST,
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
status=200,
json={
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
},
)
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=10,
)
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/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
assert json.loads(responses.calls[-1].request.body) == {
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
"worker_run_id": "56785678-5678-5678-5678-567856785678",
}
def test_create_transcription_entity_with_confidence(responses, mock_elements_worker):
responses.add(
responses.POST,
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
status=200,
json={
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
"confidence": 0.33,
},
)
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=10,
confidence=0.33,
)
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/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
]
assert json.loads(responses.calls[-1].request.body) == {
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
"worker_run_id": "56785678-5678-5678-5678-567856785678",
"confidence": 0.33,
}
def test_create_transcription_entity_confidence_none(responses, mock_elements_worker):
responses.add(
responses.POST,
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
status=200,
json={
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
"confidence": None,
},
)
mock_elements_worker.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=10,
confidence=None,
)
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/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
]
assert json.loads(responses.calls[-1].request.body) == {
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
"worker_run_id": "56785678-5678-5678-5678-567856785678",
}
def test_create_transcription_entity_with_cache(
responses, mock_elements_worker_with_cache
):
CachedElement.create(
id=UUID("12341234-1234-1234-1234-123412341234"),
type="page",
)
CachedTranscription.create(
id=UUID("11111111-1111-1111-1111-111111111111"),
element=UUID("12341234-1234-1234-1234-123412341234"),
text="Hello, it's me.",
confidence=0.42,
orientation=TextOrientation.HorizontalLeftToRight,
worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
)
CachedEntity.create(
id=UUID("11111111-1111-1111-1111-111111111111"),
type="person",
name="Bob Bob",
worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
)
responses.add(
responses.POST,
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
status=200,
json={
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
},
)
mock_elements_worker_with_cache.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=10,
)
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/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
assert json.loads(responses.calls[-1].request.body) == {
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
"worker_run_id": "56785678-5678-5678-5678-567856785678",
}
# Check that created transcription entity was properly stored in SQLite cache
assert list(CachedTranscriptionEntity.select()) == [
CachedTranscriptionEntity(
transcription=UUID("11111111-1111-1111-1111-111111111111"),
entity=UUID("11111111-1111-1111-1111-111111111111"),
offset=5,
length=10,
worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
def test_create_transcription_entity_with_confidence_with_cache(
responses, mock_elements_worker_with_cache
):
CachedElement.create(
id=UUID("12341234-1234-1234-1234-123412341234"),
type="page",
)
CachedTranscription.create(
id=UUID("11111111-1111-1111-1111-111111111111"),
element=UUID("12341234-1234-1234-1234-123412341234"),
text="Hello, it's me.",
confidence=0.42,
orientation=TextOrientation.HorizontalLeftToRight,
worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
)
CachedEntity.create(
id=UUID("11111111-1111-1111-1111-111111111111"),
type="person",
name="Bob Bob",
worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
)
responses.add(
responses.POST,
"http://testserver/api/v1/transcription/11111111-1111-1111-1111-111111111111/entity/",
status=200,
json={
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
"confidence": 0.77,
},
)
mock_elements_worker_with_cache.create_transcription_entity(
transcription=Transcription(
{
"id": "11111111-1111-1111-1111-111111111111",
"element": {"id": "myelement"},
}
),
entity="11111111-1111-1111-1111-111111111111",
offset=5,
length=10,
confidence=0.77,
)
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/transcription/11111111-1111-1111-1111-111111111111/entity/",
),
]
assert json.loads(responses.calls[-1].request.body) == {
"entity": "11111111-1111-1111-1111-111111111111",
"offset": 5,
"length": 10,
"worker_run_id": "56785678-5678-5678-5678-567856785678",
"confidence": 0.77,
}
# Check that created transcription entity was properly stored in SQLite cache
assert list(CachedTranscriptionEntity.select()) == [
CachedTranscriptionEntity(
transcription=UUID("11111111-1111-1111-1111-111111111111"),
entity=UUID("11111111-1111-1111-1111-111111111111"),
offset=5,
length=10,
worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
confidence=0.77,
)
]
def test_list_transcription_entities(fake_dummy_worker):
transcription = Transcription({"id": "fake_transcription_id"})
worker_version = "worker_version_id"
fake_dummy_worker.api_client.add_response(
"ListTranscriptionEntities",
id=transcription.id,
worker_version=worker_version,
response={"id": "entity_id"},
)
assert fake_dummy_worker.list_transcription_entities(
transcription, worker_version
) == {"id": "entity_id"}
assert len(fake_dummy_worker.api_client.history) == 1
assert len(fake_dummy_worker.api_client.responses) == 0
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
def test_list_corpus_entities(responses, mock_elements_worker):
corpus_id = "11111111-1111-1111-1111-111111111111"
responses.add(
responses.GET,
f"http://testserver/api/v1/corpus/{corpus_id}/entities/",
json={
"count": 1,
"next": None,
"results": [
{
"id": "fake_entity_id",
}
],
},
)
# list is required to actually do the request
assert list(mock_elements_worker.list_corpus_entities()) == [
{
"id": "fake_entity_id",
}
]
assert len(responses.calls) == len(BASE_API_CALLS) + 1
assert [
(call.request.method, call.request.url) for call in responses.calls
] == BASE_API_CALLS + [
(
"GET",
f"http://testserver/api/v1/corpus/{corpus_id}/entities/",
),
]
@pytest.mark.parametrize(
"wrong_name",
[
1234,
12.5,
],
)
def test_list_corpus_entities_wrong_name(mock_elements_worker, wrong_name):
with pytest.raises(AssertionError, match="name should be of type str"):
mock_elements_worker.list_corpus_entities(name=wrong_name)
@pytest.mark.parametrize(
"wrong_parent",
[{"id": "element_id"}, 12.5, "blabla"],
)
def test_list_corpus_entities_wrong_parent(mock_elements_worker, wrong_parent):
with pytest.raises(AssertionError, match="parent should be of type Element"):
mock_elements_worker.list_corpus_entities(parent=wrong_parent)
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
def test_check_required_entity_types(responses, mock_elements_worker):
# Set one entity type
mock_elements_worker.entity_types = {"person": "person-entity-type-id"}
checked_types = ["person", "new-entity"]
# Call to create new entity type
responses.add(
responses.POST,
"http://testserver/api/v1/entity/types/",
status=200,
match=[
matchers.json_params_matcher(
{
"name": "new-entity",
"corpus": "11111111-1111-1111-1111-111111111111",
}
)
],
json={
"id": "new-entity-id",
"corpus": "11111111-1111-1111-1111-111111111111",
"name": "new-entity",
"color": "ffd1b3",
},
)
mock_elements_worker.check_required_entity_types(
entity_types=checked_types,
)
# Make sure the entity_types entry has been updated
assert mock_elements_worker.entity_types == {
"person": "person-entity-type-id",
"new-entity": "new-entity-id",
}
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/entity/types/",
),
]
def test_check_required_entity_types_no_creation_allowed(
responses, mock_elements_worker
):
# Set one entity type
mock_elements_worker.entity_types = {"person": "person-entity-type-id"}
checked_types = ["person", "new-entity"]
with pytest.raises(
MissingEntityType, match="Entity type `new-entity` was not in the corpus."
):
mock_elements_worker.check_required_entity_types(
entity_types=checked_types, create_missing=False
)
assert len(responses.calls) == len(BASE_API_CALLS)
assert [
(call.request.method, call.request.url) for call in responses.calls
] == BASE_API_CALLS
@pytest.mark.parametrize("transcription", (None, "not a transcription", 1))
def test_create_transcription_entities_wrong_transcription(
mock_elements_worker, transcription
):
with pytest.raises(
AssertionError,
match="transcription shouldn't be null and should be of type Transcription",
):
mock_elements_worker.create_transcription_entities(
transcription=transcription,
entities=[],
)
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
@pytest.mark.parametrize(
"entities, error",
(
(None, "entities shouldn't be null and should be of type list"),
(
"not a list of entities",
"entities shouldn't be null and should be of type list",
),
(1, "entities shouldn't be null and should be of type list"),
(
[
{
"name": "A",
"type_id": "12341234-1234-1234-1234-123412341234",
"offset": 0,
"length": 1,
"confidence": 0.5,
}
]
* 2,
"entities should be unique",
),
),
)
def test_create_transcription_entities_wrong_entities(
mock_elements_worker, entities, error
):
with pytest.raises(AssertionError, match=error):
mock_elements_worker.create_transcription_entities(
transcription=Transcription(id="transcription_id"),
entities=entities,
)
def test_create_transcription_entities_wrong_entities_subtype(mock_elements_worker):
with pytest.raises(
AssertionError, match="Entity at index 0 in entities: Should be of type dict"
):
mock_elements_worker.create_transcription_entities(
transcription=Transcription(id="transcription_id"),
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
entities=["not a dict"],
)
@pytest.mark.parametrize(
"entity, error",
(
(
{
"name": None,
"type_id": "12341234-1234-1234-1234-123412341234",
"offset": 0,
"length": 1,
"confidence": 0.5,
},
"Entity at index 0 in entities: name shouldn't be null and should be of type str",
),
(
{"name": "A", "type_id": None, "offset": 0, "length": 1, "confidence": 0.5},
"Entity at index 0 in entities: type_id shouldn't be null and should be of type str",
),
(
{"name": "A", "type_id": 0, "offset": 0, "length": 1, "confidence": 0.5},
"Entity at index 0 in entities: type_id shouldn't be null and should be of type str",
),
(
{
"name": "A",
"type_id": "12341234-1234-1234-1234-123412341234",
"offset": None,
"length": 1,
"confidence": 0.5,
},
"Entity at index 0 in entities: offset shouldn't be null and should be a positive integer",
),
(
{
"name": "A",
"type_id": "12341234-1234-1234-1234-123412341234",
"offset": -2,
"length": 1,
"confidence": 0.5,
},
"Entity at index 0 in entities: offset shouldn't be null and should be a positive integer",
),
(
{
"name": "A",
"type_id": "12341234-1234-1234-1234-123412341234",
"offset": 0,
"length": None,
"confidence": 0.5,
},
"Entity at index 0 in entities: length shouldn't be null and should be a strictly positive integer",
),
(
{
"name": "A",
"type_id": "12341234-1234-1234-1234-123412341234",
"offset": 0,
"length": 0,
"confidence": 0.5,
},
"Entity at index 0 in entities: length shouldn't be null and should be a strictly positive integer",
),
(
{
"name": "A",
"type_id": "12341234-1234-1234-1234-123412341234",
"offset": 0,
"length": 1,
"confidence": "not None or a float",
},
"Entity at index 0 in entities: confidence should be None or a float in [0..1] range",
),
(
{
"name": "A",
"type_id": "12341234-1234-1234-1234-123412341234",
"offset": 0,
"length": 1,
"confidence": 1.3,
},
"Entity at index 0 in entities: confidence should be None or a float in [0..1] range",
),
),
)
def test_create_transcription_entities_wrong_entity(
mock_elements_worker, entity, error
):
with pytest.raises(AssertionError, match=re.escape(error)):
mock_elements_worker.create_transcription_entities(
transcription=Transcription(id="transcription_id"),