def test_retrieve_all_non_empty(database, annotations_repository): empty_annotation = AnnotationsFactory.build(annotations=[]) annotations = AnnotationsFactory.build_batch(4) database[COLLECTION].insert_many( AnnotationsSchema(many=True).dump([*annotations, empty_annotation]) ) assert annotations_repository.retrieve_all_non_empty() == annotations
def test_update(database, annotations_repository): annotations = AnnotationsFactory.build() fragment_number = annotations.fragment_number updated = AnnotationsFactory.build(fragment_number=fragment_number) annotations_repository.create_or_update(annotations) annotations_repository.create_or_update(updated) assert database[COLLECTION].find_one( {"fragmentNumber": str(fragment_number)}, {"_id": False} ) == AnnotationsSchema().dump(updated)
def test_signs_get( client, annotations_repository, photo_repository, when, fragment_repository, text_with_labels, ): fragment = TransliteratedFragmentFactory.build( number=MuseumNumber.of("K.2"), text=text_with_labels) fragment_repository.create(fragment) annotation_data = AnnotationDataFactory.build(sign_name="signName", path=[2, 0, 0]) annotation = AnnotationFactory.build(data=annotation_data) annotations_repository.create_or_update( AnnotationsFactory.build(fragment_number="K.2", annotations=[annotation])) result = client.simulate_get("/signs/signName/images") assert len(result.json) > 0 result_json = result.json[0] assert result_json["fragmentNumber"] == str(fragment.number) assert isinstance(result_json["image"], str) assert result_json["script"] == fragment.script assert result_json["label"] == "i Stone wig Stone wig 2" assert result.status == falcon.HTTP_OK
def test_query_by_museum_number(database, annotations_repository): annotations = AnnotationsFactory.build() fragment_number = annotations.fragment_number database[COLLECTION].insert_one(AnnotationsSchema().dump(annotations)) assert annotations_repository.query_by_museum_number(fragment_number) == annotations
def test_update_invalid_number(client): annotations = AnnotationsFactory.build() body = AnnotationsSchema().dumps(annotations) url = "/fragments/invalid/annotations" post_result = client.simulate_post(url, body=body) assert post_result.status == falcon.HTTP_UNPROCESSABLE_ENTITY
def test_update_not_allowed(guest_client): annotations = AnnotationsFactory.build() body = AnnotationsSchema().dumps(annotations) url = "/fragments/not match/annotations" result = guest_client.simulate_post(url, body=body) assert result.status == falcon.HTTP_FORBIDDEN
def test_cropped_images_from_sign( annotations_repository, photo_repository, fragment_repository, when, text_with_labels, ): image_extractor = AnnotationImageExtractor(fragment_repository, annotations_repository, photo_repository) single_annotation = AnnotationFactory.build( data=AnnotationDataFactory.build(path=[2, 0, 0])) annotation = AnnotationsFactory.build(annotations=[single_annotation]) sign = "test-sign" fragment = TransliteratedFragmentFactory.build(text=text_with_labels) (when(annotations_repository).find_by_sign(sign).thenReturn([annotation])) (when(fragment_repository).query_by_museum_number( annotation.fragment_number).thenReturn(fragment)) (when(photo_repository).query_by_file_name( f"{annotation.fragment_number}.jpg").thenReturn( create_test_photo("K.2"))) result = image_extractor.cropped_images_from_sign(sign) assert len(result) > 0 first_cropped_annotation = result[0] assert isinstance(first_cropped_annotation, CroppedAnnotation) assert first_cropped_annotation.script == fragment.script assert first_cropped_annotation.label == "i Stone wig Stone wig 2"
def test_write_fragment_numbers(tmp_path): annotations = AnnotationsFactory.build_batch(5) dir = tmp_path / "annotations" dir.mkdir() file_name = dir / "Annotation_numbers.txt" retrieve_annotations.write_fragment_numbers(annotations, file_name) result = "\n".join([str(annotation.fragment_number) for annotation in annotations]) assert file_name.read_text() == f"Total of 5 Annotations\n{result}\n"
def test_find(annotations_repository, photo_repository, changelog, when): annotations = AnnotationsFactory.build() when(annotations_repository).query_by_museum_number( annotations.fragment_number ).thenReturn(annotations) service = AnnotationsService( EblAiClient(""), annotations_repository, photo_repository, changelog ) assert service.find(annotations.fragment_number) == annotations
def test_find_by_sign(database, annotations_repository): annotations = AnnotationsFactory.build_batch(5) sign_query = annotations[0].annotations[0].data.sign_name database[COLLECTION].insert_many(AnnotationsSchema(many=True).dump(annotations)) results = annotations_repository.find_by_sign(sign_query) assert len(results) >= 1 for result in results: for annotation in result.annotations: assert annotation.data.sign_name == sign_query
def test_update(annotations_repository, photo_repository, when, user, changelog): fragment_number = MuseumNumber("K", "1") annotations = AnnotationsFactory.build(fragment_number=fragment_number) updated_annotations = AnnotationsFactory.build(fragment_number=fragment_number) when(annotations_repository).query_by_museum_number(fragment_number).thenReturn( annotations ) when(annotations_repository).create_or_update(updated_annotations).thenReturn() when(changelog).create( "annotations", user.profile, {"_id": str(fragment_number), **SCHEMA.dump(annotations)}, {"_id": str(fragment_number), **SCHEMA.dump(updated_annotations)}, ).thenReturn() service = AnnotationsService( EblAiClient(""), annotations_repository, photo_repository, changelog ) assert service.update(updated_annotations, user) == updated_annotations
def test_create_annotations(photo_repository, when, photo): annotation = AnnotationsFactory.build() image = mock({"save": lambda _: None, "size": (640, 480, 3)}) when(photo_repository).query_by_file_name( f"{annotation.fragment_number}.jpg" ).thenReturn(photo) when(Image).open(...).thenReturn(image) when(retrieve_annotations).write_annotations(...).thenReturn(None) create_annotations([annotation], "", "", photo_repository) verify(image).save(f"{annotation.fragment_number}.jpg")
def test_update(client): annotations = AnnotationsFactory.build() fragment_number = annotations.fragment_number body = AnnotationsSchema().dumps(annotations) url = f"/fragments/{fragment_number}/annotations" post_result = client.simulate_post(url, body=body) expected_json = AnnotationsSchema().dump(annotations) assert post_result.status == falcon.HTTP_OK assert post_result.json == expected_json get_result = client.simulate_get( f"/fragments/{fragment_number}/annotations", params={"generateAnnotations": False}, ) assert get_result.json == expected_json
def test_prepare_annotations(): annotations_1 = AnnotationFactory.build() annotations_2 = AnnotationFactory.build( data=AnnotationDataFactory.build(type=AnnotationValueType.RULING_DOLLAR_LINE) ) annotations_3 = AnnotationFactory.build( data=AnnotationDataFactory.build(sign_name="") ) annotation = AnnotationsFactory.build( annotations=[annotations_1, annotations_2, annotations_3] ) bounding_boxes, signs = retrieve_annotations.prepare_annotations( annotation, 100, 100 ) assert len(bounding_boxes) == 2 assert len(signs) == 2 assert signs[0] == annotations_1.data.sign_name assert signs[1] == annotations_3.data.value
def test_retrieve_all(database, annotations_repository): annotations = AnnotationsFactory.build_batch(5) database[COLLECTION].insert_many(AnnotationsSchema(many=True).dump(annotations)) assert annotations_repository.retrieve_all_non_empty() == annotations