|
| 1 | +import requests |
1 | 2 | import responses
|
2 | 3 |
|
3 | 4 | from roboflow import API_URL
|
4 | 5 | from roboflow.adapters.rfapi import AnnotationSaveError, ImageUploadError
|
5 | 6 | from roboflow.config import DEFAULT_BATCH_NAME
|
6 |
| -from tests import PROJECT_NAME, ROBOFLOW_API_KEY, RoboflowTest |
| 7 | +from tests import PROJECT_NAME, ROBOFLOW_API_KEY, WORKSPACE_NAME, RoboflowTest |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class TestProject(RoboflowTest):
|
@@ -82,3 +83,64 @@ def test_upload_raises_upload_annotation_error(self):
|
82 | 83 | )
|
83 | 84 |
|
84 | 85 | self.assertEqual(str(error.exception), "Image was already annotated.")
|
| 86 | + |
| 87 | + def test_image_success(self): |
| 88 | + image_id = "test-image-id" |
| 89 | + expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images/{image_id}?api_key={ROBOFLOW_API_KEY}" |
| 90 | + mock_response = { |
| 91 | + "image": { |
| 92 | + "id": image_id, |
| 93 | + "name": "test_image.jpg", |
| 94 | + "annotation": { |
| 95 | + "key": "some-key", |
| 96 | + "width": 640, |
| 97 | + "height": 480, |
| 98 | + "boxes": [{"label": "person", "x": 100, "y": 150, "width": 50, "height": 80}], |
| 99 | + }, |
| 100 | + "labels": ["person"], |
| 101 | + "split": "train", |
| 102 | + "tags": ["tag1", "tag2"], |
| 103 | + "created": 1616161616, |
| 104 | + "urls": { |
| 105 | + "original": "https://example.com/image.jpg", |
| 106 | + "thumb": "https://example.com/thumb.jpg", |
| 107 | + "annotation": "https://example.com/annotation.json", |
| 108 | + }, |
| 109 | + "embedding": [0.1, 0.2, 0.3], |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + responses.add(responses.GET, expected_url, json=mock_response, status=200) |
| 114 | + |
| 115 | + image_details = self.project.image(image_id) |
| 116 | + |
| 117 | + self.assertIsInstance(image_details, dict) |
| 118 | + self.assertEqual(image_details["id"], image_id) |
| 119 | + self.assertEqual(image_details["name"], "test_image.jpg") |
| 120 | + self.assertIn("annotation", image_details) |
| 121 | + self.assertIn("labels", image_details) |
| 122 | + self.assertEqual(image_details["split"], "train") |
| 123 | + |
| 124 | + def test_image_not_found(self): |
| 125 | + image_id = "nonexistent-image-id" |
| 126 | + expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images/{image_id}?api_key={ROBOFLOW_API_KEY}" |
| 127 | + mock_response = {"error": "Image not found."} |
| 128 | + |
| 129 | + responses.add(responses.GET, expected_url, json=mock_response, status=404) |
| 130 | + |
| 131 | + with self.assertRaises(RuntimeError) as context: |
| 132 | + self.project.image(image_id) |
| 133 | + |
| 134 | + self.assertIn("HTTP error occurred while fetching image details", str(context.exception)) |
| 135 | + |
| 136 | + def test_image_invalid_json_response(self): |
| 137 | + image_id = "invalid-json-image-id" |
| 138 | + expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images/{image_id}?api_key={ROBOFLOW_API_KEY}" |
| 139 | + invalid_json = "Invalid JSON response" |
| 140 | + |
| 141 | + responses.add(responses.GET, expected_url, body=invalid_json, status=200) |
| 142 | + |
| 143 | + with self.assertRaises(requests.exceptions.JSONDecodeError) as context: |
| 144 | + self.project.image(image_id) |
| 145 | + |
| 146 | + self.assertIn("Expecting value", str(context.exception)) |
0 commit comments