-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtest_project.py
146 lines (120 loc) · 5.45 KB
/
test_project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import requests
import responses
from roboflow import API_URL
from roboflow.adapters.rfapi import AnnotationSaveError, ImageUploadError
from roboflow.config import DEFAULT_BATCH_NAME
from tests import PROJECT_NAME, ROBOFLOW_API_KEY, WORKSPACE_NAME, RoboflowTest
class TestProject(RoboflowTest):
def test_check_valid_image_with_accepted_formats(self):
images_to_test = [
"rabbit.JPG",
"rabbit2.jpg",
"hand-rabbit.PNG",
"woodland-rabbit.png",
]
for image in images_to_test:
self.assertTrue(self.project.check_valid_image(f"tests/images/{image}"))
def test_check_valid_image_with_unaccepted_formats(self):
images_to_test = [
"sky-rabbit.gif",
"sky-rabbit.heic",
]
for image in images_to_test:
self.assertFalse(self.project.check_valid_image(f"tests/images/{image}"))
def test_upload_raises_upload_image_error(self):
responses.add(
responses.POST,
f"{API_URL}/dataset/{PROJECT_NAME}/upload?api_key={ROBOFLOW_API_KEY}" f"&batch={DEFAULT_BATCH_NAME}",
json={
"error": {
"message": "Invalid image.",
"type": "InvalidImageException",
"hint": "This image was already annotated; to overwrite the annotation, pass overwrite=true...",
}
},
status=400,
)
with self.assertRaises(ImageUploadError) as error:
self.project.upload(
"tests/images/rabbit.JPG",
annotation_path="tests/annotations/valid_annotation.json",
)
self.assertEqual(str(error.exception), "Invalid image.")
def test_upload_raises_upload_annotation_error(self):
image_id = "hbALkCFdNr9rssgOUXug"
image_name = "invalid_annotation.json"
# Image upload
responses.add(
responses.POST,
f"{API_URL}/dataset/{PROJECT_NAME}/upload?api_key={ROBOFLOW_API_KEY}" f"&batch={DEFAULT_BATCH_NAME}",
json={"success": True, "id": image_id},
status=200,
)
# Annotation
responses.add(
responses.POST,
f"{API_URL}/dataset/{PROJECT_NAME}/annotate/{image_id}?api_key={ROBOFLOW_API_KEY}" f"&name={image_name}",
json={
"error": {
"message": "Image was already annotated.",
"type": "InvalidImageException",
"hint": "This image was already annotated; to overwrite the annotation, pass overwrite=true...",
}
},
status=400,
)
with self.assertRaises(AnnotationSaveError) as error:
self.project.upload(
"tests/images/rabbit.JPG",
annotation_path=f"tests/annotations/{image_name}",
)
self.assertEqual(str(error.exception), "Image was already annotated.")
def test_image_success(self):
image_id = "test-image-id"
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images/{image_id}?api_key={ROBOFLOW_API_KEY}"
mock_response = {
"image": {
"id": image_id,
"name": "test_image.jpg",
"annotation": {
"key": "some-key",
"width": 640,
"height": 480,
"boxes": [{"label": "person", "x": 100, "y": 150, "width": 50, "height": 80}],
},
"labels": ["person"],
"split": "train",
"tags": ["tag1", "tag2"],
"created": 1616161616,
"urls": {
"original": "https://example.com/image.jpg",
"thumb": "https://example.com/thumb.jpg",
"annotation": "https://example.com/annotation.json",
},
"embedding": [0.1, 0.2, 0.3],
}
}
responses.add(responses.GET, expected_url, json=mock_response, status=200)
image_details = self.project.image(image_id)
self.assertIsInstance(image_details, dict)
self.assertEqual(image_details["id"], image_id)
self.assertEqual(image_details["name"], "test_image.jpg")
self.assertIn("annotation", image_details)
self.assertIn("labels", image_details)
self.assertEqual(image_details["split"], "train")
def test_image_not_found(self):
image_id = "nonexistent-image-id"
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images/{image_id}?api_key={ROBOFLOW_API_KEY}"
mock_response = {"error": "Image not found."}
responses.add(responses.GET, expected_url, json=mock_response, status=404)
with self.assertRaises(RuntimeError) as context:
self.project.image(image_id)
self.assertIn("HTTP error occurred while fetching image details", str(context.exception))
def test_image_invalid_json_response(self):
image_id = "invalid-json-image-id"
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images/{image_id}?api_key={ROBOFLOW_API_KEY}"
invalid_json = "Invalid JSON response"
responses.add(responses.GET, expected_url, body=invalid_json, status=200)
with self.assertRaises(requests.exceptions.JSONDecodeError) as context:
self.project.image(image_id)
self.assertIn("Expecting value", str(context.exception))