|
| 1 | +# |
| 2 | +# This was commented out, as it is almost ready to go, however we do not yet have an updated |
| 3 | +# implementation of project.upload_annotations that uses AnnotationImports rather than |
| 4 | +# BulkImportRequest. Once we have that, we should be calling this new implementation |
| 5 | +# rather than the old one. |
| 6 | +# |
| 7 | +# |
| 8 | +# |
| 9 | +# |
| 10 | +# |
| 11 | +# |
| 12 | +# import uuid |
| 13 | +# import ndjson |
| 14 | +# import pytest |
| 15 | +# import requests |
| 16 | + |
| 17 | +# from labelbox.exceptions import MALValidationError, UuidError |
| 18 | +# from labelbox.schema.enums import AnnotationImportState |
| 19 | +# from labelbox.schema.annotation_import import MALPredictionImport |
| 20 | + |
| 21 | +# """ |
| 22 | +# - Here we only want to check that the uploads are calling the validation |
| 23 | +# - Then with unit tests we can check the types of errors raised |
| 24 | + |
| 25 | +# """ |
| 26 | + |
| 27 | + |
| 28 | +# def test_create_from_url(configured_project): |
| 29 | +# name = str(uuid.uuid4()) |
| 30 | +# url = "https://storage.googleapis.com/labelbox-public-bucket/predictions_test_v2.ndjson" |
| 31 | + |
| 32 | +# annotation_import = configured_project.upload_annotations( |
| 33 | +# name=name, annotations=predictions) |
| 34 | + |
| 35 | +# assert annotation_import.project() == configured_project |
| 36 | +# assert annotation_import.name == name |
| 37 | +# assert annotation_import.input_file_url == url |
| 38 | +# assert annotation_import.error_file_url is None |
| 39 | +# assert annotation_import.status_file_url is None |
| 40 | +# assert annotation_import.state == AnnotationImportState.RUNNING |
| 41 | + |
| 42 | + |
| 43 | +# def test_validate_file(client, configured_project): |
| 44 | +# name = str(uuid.uuid4()) |
| 45 | +# url = "https://storage.googleapis.com/labelbox-public-bucket/predictions_test_v2.ndjson" |
| 46 | +# with pytest.raises(MALValidationError): |
| 47 | +# configured_project.upload_annotations(name=name, |
| 48 | +# annotations=url, |
| 49 | +# validate=True) |
| 50 | +# #Schema ids shouldn't match |
| 51 | + |
| 52 | + |
| 53 | +# def test_create_from_objects(configured_project, predictions): |
| 54 | +# name = str(uuid.uuid4()) |
| 55 | + |
| 56 | +# annotation_import = configured_project.upload_annotations( |
| 57 | +# name=name, annotations=predictions) |
| 58 | + |
| 59 | +# assert annotation_import.project() == configured_project |
| 60 | +# assert annotation_import.name == name |
| 61 | +# assert annotation_import.error_file_url is None |
| 62 | +# assert annotation_import.status_file_url is None |
| 63 | +# assert annotation_import.state == AnnotationImportState.RUNNING |
| 64 | +# assert_file_content(annotation_import.input_file_url, predictions) |
| 65 | + |
| 66 | + |
| 67 | +# def test_create_from_local_file(tmp_path, predictions, configured_project): |
| 68 | +# name = str(uuid.uuid4()) |
| 69 | +# file_name = f"{name}.ndjson" |
| 70 | +# file_path = tmp_path / file_name |
| 71 | +# with file_path.open("w") as f: |
| 72 | +# ndjson.dump(predictions, f) |
| 73 | + |
| 74 | +# annotation_import = configured_project.upload_annotations( |
| 75 | +# name=name, annotations=str(file_path), validate=False) |
| 76 | + |
| 77 | +# assert annotation_import.project() == configured_project |
| 78 | +# assert annotation_import.name == name |
| 79 | +# assert annotation_import.error_file_url is None |
| 80 | +# assert annotation_import.status_file_url is None |
| 81 | +# assert annotation_import.state == AnnotationImportState.RUNNING |
| 82 | +# assert_file_content(annotation_import.input_file_url, predictions) |
| 83 | + |
| 84 | + |
| 85 | +# def test_get(client, configured_project): |
| 86 | +# name = str(uuid.uuid4()) |
| 87 | +# url = "https://storage.googleapis.com/labelbox-public-bucket/predictions_test_v2.ndjson" |
| 88 | +# configured_project.upload_annotations(name=name, |
| 89 | +# annotations=url, |
| 90 | +# validate=False) |
| 91 | + |
| 92 | +# annotation_import = MALPredictionImport.from_name( |
| 93 | +# client, project_id=configured_project.uid, name=name) |
| 94 | +# print("asdf:", annotation_import) |
| 95 | + |
| 96 | +# assert annotation_import.project() == configured_project |
| 97 | +# assert annotation_import.name == name |
| 98 | +# assert annotation_import.input_file_url == url |
| 99 | +# assert annotation_import.error_file_url is None |
| 100 | +# assert annotation_import.status_file_url is None |
| 101 | +# assert annotation_import.state == AnnotationImportState.RUNNING |
| 102 | + |
| 103 | + |
| 104 | +# def test_validate_ndjson(tmp_path, configured_project): |
| 105 | +# file_name = f"broken.ndjson" |
| 106 | +# file_path = tmp_path / file_name |
| 107 | +# with file_path.open("w") as f: |
| 108 | +# f.write("test") |
| 109 | + |
| 110 | +# with pytest.raises(ValueError): |
| 111 | +# configured_project.upload_annotations(name="name", |
| 112 | +# annotations=str(file_path)) |
| 113 | + |
| 114 | + |
| 115 | +# def test_validate_ndjson_uuid(tmp_path, configured_project, predictions): |
| 116 | +# file_name = f"repeat_uuid.ndjson" |
| 117 | +# file_path = tmp_path / file_name |
| 118 | +# repeat_uuid = predictions.copy() |
| 119 | +# uid = str(uuid.uuid4()) |
| 120 | +# repeat_uuid[0]['uuid'] = uid |
| 121 | +# repeat_uuid[1]['uuid'] = uid |
| 122 | + |
| 123 | +# with file_path.open("w") as f: |
| 124 | +# ndjson.dump(repeat_uuid, f) |
| 125 | + |
| 126 | +# with pytest.raises(UuidError): |
| 127 | +# configured_project.upload_annotations(name="name", |
| 128 | +# annotations=str(file_path)) |
| 129 | + |
| 130 | +# with pytest.raises(UuidError): |
| 131 | +# configured_project.upload_annotations(name="name", |
| 132 | +# annotations=repeat_uuid) |
| 133 | + |
| 134 | + |
| 135 | +# @pytest.mark.slow |
| 136 | +# def test_wait_till_done(rectangle_inference, configured_project): |
| 137 | +# name = str(uuid.uuid4()) |
| 138 | +# url = configured_project.client.upload_data(content=ndjson.dumps( |
| 139 | +# [rectangle_inference]), |
| 140 | +# sign=True) |
| 141 | +# annotation_import = configured_project.upload_annotations(name=name, |
| 142 | +# annotations=url, |
| 143 | +# validate=False) |
| 144 | + |
| 145 | +# assert len(annotation_import.inputs) == 1 |
| 146 | +# annotation_import.wait_until_done() |
| 147 | +# assert annotation_import.state == AnnotationImportState.FINISHED |
| 148 | + |
| 149 | +# # Check that the status files are being returned as expected |
| 150 | +# assert len(annotation_import.errors) == 0 |
| 151 | +# assert len(annotation_import.inputs) == 1 |
| 152 | +# assert annotation_import.inputs[0]['uuid'] == rectangle_inference['uuid'] |
| 153 | +# assert len(annotation_import.statuses) == 1 |
| 154 | +# assert annotation_import.statuses[0]['status'] == 'SUCCESS' |
| 155 | +# assert annotation_import.statuses[0]['uuid'] == rectangle_inference[ |
| 156 | +# 'uuid'] |
| 157 | + |
| 158 | + |
| 159 | +# def assert_file_content(url: str, predictions): |
| 160 | +# response = requests.get(url) |
| 161 | +# assert response.text == ndjson.dumps(predictions) |
0 commit comments