-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathtest_project.py
More file actions
1220 lines (1058 loc) · 47.3 KB
/
Copy pathtest_project.py
File metadata and controls
1220 lines (1058 loc) · 47.3 KB
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
147
148
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
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
771
772
773
774
775
776
777
778
779
780
781
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
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
895
896
897
898
899
900
901
902
903
904
905
906
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
997
998
999
1000
import json
import os
from unittest.mock import patch
import requests
import responses
from responses.matchers import json_params_matcher
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, ordered
class TestProject(RoboflowTest):
def _create_test_dataset(self, images=None):
"""
Create a test dataset with specified images or a default image
Args:
images: List of image dictionaries. If None, a default image will be used.
Returns:
Dictionary representing a parsed dataset
"""
if images is None:
images = [{"file": "image1.jpg", "split": "train", "annotationfile": {"file": "image1.xml"}}]
return {"location": "/test/location/", "images": images}
def _setup_upload_dataset_mocks(
self,
test_dataset=None,
image_return=None,
annotation_return=None,
project_created=False,
save_annotation_side_effect=None,
upload_image_side_effect=None,
):
"""
Set up common mocks for upload_dataset tests
Args:
test_dataset: The dataset to return from parsefolder. If None, creates a default dataset
image_return: Return value for upload_image. Default is successful upload
annotation_return: Return value for save_annotation. Default is successful annotation
project_created: Whether to simulate a newly created project
save_annotation_side_effect: Side effect function for save_annotation
upload_image_side_effect: Side effect function for upload_image
Returns:
Dictionary of mock objects with start and stop methods
"""
if test_dataset is None:
test_dataset = self._create_test_dataset()
if image_return is None:
image_return = ({"id": "test-id", "success": True}, 0.1, 0)
if annotation_return is None:
annotation_return = ({"success": True}, 0.1, 0)
# Create the mock objects
mocks = {
"parser": patch("roboflow.util.folderparser.parsefolder", return_value=test_dataset),
"upload": patch("roboflow.core.project.Project.upload_image", side_effect=upload_image_side_effect)
if upload_image_side_effect
else patch("roboflow.core.project.Project.upload_image", return_value=image_return),
"save_annotation": patch(
"roboflow.core.project.Project.save_annotation", side_effect=save_annotation_side_effect
)
if save_annotation_side_effect
else patch("roboflow.core.project.Project.save_annotation", return_value=annotation_return),
"get_project": patch(
"roboflow.core.workspace.Workspace._get_or_create_project", return_value=(self.project, project_created)
),
}
return mocks
def test_check_valid_image_with_accepted_formats(self):
images_to_test = [
"rabbit.JPG",
"rabbit2.jpg",
"hand-rabbit.PNG",
"woodland-rabbit.png",
"file_example_TIFF_1MB.tiff",
"sky-rabbit.heic",
"whatsnew.avif",
]
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",
]
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}&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}&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}&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_upload_single_file_returns_result(self):
"""upload() should return a list with the single_upload result dict for a single file (#254)."""
image_id = "test-upload-id"
responses.add(
responses.POST,
f"{API_URL}/dataset/{PROJECT_NAME}/upload?api_key={ROBOFLOW_API_KEY}&batch={DEFAULT_BATCH_NAME}",
json={"success": True, "id": image_id},
status=200,
)
result = self.project.upload("tests/images/rabbit.JPG")
self.assertIsInstance(result, list)
self.assertEqual(len(result), 1)
entry = result[0]
self.assertIsInstance(entry, dict)
self.assertEqual(entry["image"]["id"], image_id)
self.assertIn("upload_time", entry)
self.assertIn("upload_retry_attempts", entry)
def test_upload_directory_returns_list_of_results(self):
"""upload() should return a list of single_upload results for a directory (#254)."""
test_dir = "tests/images"
# Determine how many valid images are in the directory so we can mock
# exactly that many upload responses.
valid_images = [f for f in os.listdir(test_dir) if self.project.check_valid_image(os.path.join(test_dir, f))]
for i, _ in enumerate(valid_images):
responses.add(
responses.POST,
f"{API_URL}/dataset/{PROJECT_NAME}/upload?api_key={ROBOFLOW_API_KEY}&batch={DEFAULT_BATCH_NAME}",
json={"success": True, "id": f"img-{i}"},
status=200,
)
result = self.project.upload(test_dir)
self.assertIsInstance(result, list)
self.assertEqual(len(result), len(valid_images))
for i, entry in enumerate(result):
self.assertIsInstance(entry, dict)
self.assertEqual(entry["image"]["id"], f"img-{i}")
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))
def test_create_annotation_job_success(self):
job_name = "Test Job"
batch_id = "test-batch-123"
num_images = 10
labeler_email = "labeler@example.com"
reviewer_email = "reviewer@example.com"
expected_response = {
"success": True,
"job": {
"id": "job-123",
"name": job_name,
"batch": batch_id,
"num_images": num_images,
"labeler": labeler_email,
"reviewer": reviewer_email,
"status": "created",
"created": 1616161616,
},
}
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/jobs?api_key={ROBOFLOW_API_KEY}"
responses.add(
responses.POST,
expected_url,
json=expected_response,
status=200,
match=[
json_params_matcher(
{
"name": job_name,
"batch": batch_id,
"num_images": num_images,
"labelerEmail": labeler_email,
"reviewerEmail": reviewer_email,
}
)
],
)
result = self.project.create_annotation_job(
name=job_name,
batch_id=batch_id,
num_images=num_images,
labeler_email=labeler_email,
reviewer_email=reviewer_email,
)
self.assertEqual(result, expected_response)
self.assertTrue(result["success"])
self.assertEqual(result["job"]["id"], "job-123")
self.assertEqual(result["job"]["name"], job_name)
def test_create_annotation_job_error(self):
job_name = "Test Job"
batch_id = "invalid-batch"
num_images = 10
labeler_email = "labeler@example.com"
reviewer_email = "reviewer@example.com"
error_response = {"error": "Batch not found"}
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/jobs?api_key={ROBOFLOW_API_KEY}"
responses.add(responses.POST, expected_url, json=error_response, status=404)
with self.assertRaises(RuntimeError) as context:
self.project.create_annotation_job(
name=job_name,
batch_id=batch_id,
num_images=num_images,
labeler_email=labeler_email,
reviewer_email=reviewer_email,
)
self.assertEqual(str(context.exception), "Batch not found")
@ordered
@responses.activate
def test_project_upload_dataset(self):
"""Test upload_dataset functionality with various scenarios"""
test_scenarios = [
{
"name": "string_annotationdesc",
"dataset": [{"file": "test_image.jpg", "split": "train", "annotationfile": "string_annotation.txt"}],
"params": {"num_workers": 1},
"assertions": {},
},
{
"name": "success_basic",
"dataset": [
{"file": "image1.jpg", "split": "train", "annotationfile": {"file": "image1.xml"}},
{"file": "image2.jpg", "split": "valid", "annotationfile": {"file": "image2.xml"}},
],
"params": {},
"assertions": {"parser": [("/test/dataset",)], "upload": {"count": 2}, "save_annotation": {"count": 2}},
"image_return": ({"id": "test-id-1", "success": True}, 0.1, 0),
},
{
"name": "custom_parameters",
"dataset": None,
"params": {
"num_workers": 2,
"project_license": "CC BY 4.0",
"project_type": "classification",
"batch_name": "test-batch",
"num_retries": 3,
},
"assertions": {"upload": {"count": 1, "kwargs": {"batch_name": "test-batch", "num_retry_uploads": 3}}},
},
{
"name": "explicit_split_overrides_parsed_directory_splits",
"dataset": [
{"file": "image1.jpg", "split": "train"},
{"file": "image2.jpg", "split": "test"},
],
"params": {"split": "valid", "num_workers": 1},
"assertions": {"upload": {"count": 2, "kwargs": {"split": "valid"}}},
},
{
"name": "project_creation",
"dataset": None,
"params": {"project_name": "new-project"},
"assertions": {},
"project_created": True,
},
{
"name": "with_labelmap",
"dataset": [
{
"file": "image1.jpg",
"split": "train",
"annotationfile": {"file": "image1.xml", "labelmap": "path/to/labelmap.json"},
}
],
"params": {},
"assertions": {"save_annotation": {"count": 1}, "load_labelmap": {"count": 1}},
"extra_mocks": [
(
"load_labelmap",
"roboflow.util.image_utils.load_labelmap",
{"return_value": {"old_label": "new_label"}},
)
],
},
{
"name": "concurrent_uploads",
"dataset": [{"file": f"image{i}.jpg", "split": "train"} for i in range(10)],
"params": {"num_workers": 5},
"assertions": {"thread_pool": {"count": 1, "kwargs": {"max_workers": 5}}},
"extra_mocks": [("thread_pool", "concurrent.futures.ThreadPoolExecutor", {})],
},
{"name": "empty_dataset", "dataset": [], "params": {}, "assertions": {"upload": {"count": 0}}},
{
"name": "raw_text_annotation",
"dataset": [
{
"file": "image1.jpg",
"split": "train",
"annotationfile": {"rawText": "annotation content here", "format": "json"},
}
],
"params": {},
"assertions": {"save_annotation": {"count": 1}},
},
{
"name": "with_predictions_flag_true",
"dataset": [
{"file": "pred1.jpg", "split": "train", "annotationfile": {"file": "pred1.xml"}},
{"file": "pred2.jpg", "split": "valid", "annotationfile": {"file": "pred2.xml"}},
],
"params": {"is_prediction": True},
"assertions": {
"upload": {"count": 2},
"save_annotation": {"count": 2, "kwargs": {"is_prediction": True}},
},
},
{
"name": "with_predictions_flag_false",
"dataset": [
{"file": "gt1.jpg", "split": "train", "annotationfile": {"file": "gt1.xml"}},
],
"params": {"is_prediction": False},
"assertions": {
"upload": {"count": 1},
"save_annotation": {"count": 1, "kwargs": {"is_prediction": False}},
},
},
{
"name": "predictions_with_batch",
"dataset": [
{"file": "batch_pred.jpg", "split": "train", "annotationfile": {"file": "batch_pred.xml"}},
],
"params": {
"is_prediction": True,
"batch_name": "prediction-batch",
"num_retries": 2,
},
"assertions": {
"upload": {
"count": 1,
"kwargs": {
"batch_name": "prediction-batch",
"num_retry_uploads": 2,
},
},
"save_annotation": {
"count": 1,
"kwargs": {
"is_prediction": True,
"job_name": "prediction-batch",
"num_retry_uploads": 2,
},
},
},
},
]
error_cases = [
{
"name": "image_upload_error",
"side_effect": {
"upload_image_side_effect": lambda *args, **kwargs: (_ for _ in ()).throw(
ImageUploadError("Failed to upload image")
)
},
"params": {"num_workers": 1},
},
{
"name": "annotation_upload_error",
"side_effect": {
"save_annotation_side_effect": lambda *args, **kwargs: (_ for _ in ()).throw(
AnnotationSaveError("Failed to save annotation")
)
},
"params": {"num_workers": 1},
},
]
for scenario in test_scenarios:
test_dataset = (
self._create_test_dataset(scenario.get("dataset")) if scenario.get("dataset") is not None else None
)
extra_mocks = {}
if "extra_mocks" in scenario:
for mock_name, target, config in scenario.get("extra_mocks", []):
extra_mocks[mock_name] = patch(target, **config)
mocks = self._setup_upload_dataset_mocks(
test_dataset=test_dataset,
image_return=scenario.get("image_return"),
project_created=scenario.get("project_created", False),
)
mock_objects = {}
for name, mock in mocks.items():
mock_objects[name] = mock.start()
for name, mock in extra_mocks.items():
mock_objects[name] = mock.start()
try:
params = {"dataset_path": "/test/dataset", "project_name": PROJECT_NAME}
params.update(scenario.get("params", {}))
self.workspace.upload_dataset(**params)
for mock_name, assertion in scenario.get("assertions", {}).items():
if isinstance(assertion, list):
mock_obj = mock_objects.get(mock_name)
call_args_list = [args for args, _ in mock_obj.call_args_list]
for expected_args in assertion:
self.assertIn(expected_args, call_args_list)
elif isinstance(assertion, dict):
mock_obj = mock_objects.get(mock_name)
if "count" in assertion:
self.assertEqual(mock_obj.call_count, assertion["count"])
if "kwargs" in assertion and mock_obj.call_count > 0:
_, kwargs = mock_obj.call_args
for key, value in assertion["kwargs"].items():
self.assertEqual(kwargs.get(key), value)
finally:
for mock in list(mocks.values()) + list(extra_mocks.values()):
mock.stop()
for case in error_cases:
mocks = self._setup_upload_dataset_mocks(**case.get("side_effect", {}))
for mock in mocks.values():
mock.start()
try:
params = {"dataset_path": "/test/dataset", "project_name": PROJECT_NAME}
params.update(case.get("params", {}))
self.workspace.upload_dataset(**params)
finally:
for mock in mocks.values():
mock.stop()
def test_get_batches_success(self):
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/batches?api_key={ROBOFLOW_API_KEY}"
mock_response = {
"batches": [
{
"name": "Uploaded on 11/22/22 at 1:39 pm",
"numJobs": 2,
"images": 115,
"uploaded": {"_seconds": 1669146024, "_nanoseconds": 818000000},
"id": "batch-1",
},
{
"numJobs": 0,
"images": 11,
"uploaded": {"_seconds": 1669236873, "_nanoseconds": 47000000},
"name": "Upload via API",
"id": "batch-2",
},
]
}
responses.add(responses.GET, expected_url, json=mock_response, status=200)
batches = self.project.get_batches()
self.assertIsInstance(batches, dict)
self.assertIn("batches", batches)
self.assertEqual(len(batches["batches"]), 2)
self.assertEqual(batches["batches"][0]["id"], "batch-1")
self.assertEqual(batches["batches"][0]["name"], "Uploaded on 11/22/22 at 1:39 pm")
self.assertEqual(batches["batches"][0]["images"], 115)
self.assertEqual(batches["batches"][0]["numJobs"], 2)
self.assertEqual(batches["batches"][1]["id"], "batch-2")
self.assertEqual(batches["batches"][1]["name"], "Upload via API")
def test_get_batches_error(self):
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/batches?api_key={ROBOFLOW_API_KEY}"
error_response = {"error": "Cannot retrieve batches"}
responses.add(responses.GET, expected_url, json=error_response, status=404)
with self.assertRaises(RuntimeError) as context:
self.project.get_batches()
self.assertEqual(str(context.exception), "Cannot retrieve batches")
def test_get_batch_success(self):
batch_id = "batch-123"
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/batches/{batch_id}?api_key={ROBOFLOW_API_KEY}"
mock_response = {
"batch": {
"name": "Uploaded on 11/22/22 at 1:39 pm",
"numJobs": 2,
"images": 115,
"uploaded": {"_seconds": 1669146024, "_nanoseconds": 818000000},
"id": batch_id,
}
}
responses.add(responses.GET, expected_url, json=mock_response, status=200)
batch = self.project.get_batch(batch_id)
self.assertIsInstance(batch, dict)
self.assertIn("batch", batch)
self.assertEqual(batch["batch"]["id"], batch_id)
self.assertEqual(batch["batch"]["name"], "Uploaded on 11/22/22 at 1:39 pm")
self.assertEqual(batch["batch"]["images"], 115)
self.assertEqual(batch["batch"]["numJobs"], 2)
self.assertIn("uploaded", batch["batch"])
def test_get_batch_error(self):
batch_id = "nonexistent-batch"
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/batches/{batch_id}?api_key={ROBOFLOW_API_KEY}"
error_response = {"error": "Batch not found"}
responses.add(responses.GET, expected_url, json=error_response, status=404)
with self.assertRaises(RuntimeError) as context:
self.project.get_batch(batch_id)
self.assertEqual(str(context.exception), "Batch not found")
def test_delete_images_success(self):
image_ids = ["image1.jpg", "image2.jpg"]
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images?api_key={ROBOFLOW_API_KEY}"
responses.add(
responses.DELETE,
expected_url,
status=204,
match=[
json_params_matcher(
{
"images": image_ids,
}
)
],
)
self.project.delete_images(image_ids=image_ids)
def test_delete_images_error(self):
image_ids = ["image1.jpg", "image2.jpg"]
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/images?api_key={ROBOFLOW_API_KEY}"
error_response = {"error": "Failed to delete images"}
responses.add(
responses.DELETE,
expected_url,
json=error_response,
status=400,
match=[
json_params_matcher(
{
"images": image_ids,
}
)
],
)
with self.assertRaises(RuntimeError) as context:
self.project.delete_images(image_ids=image_ids)
self.assertEqual(str(context.exception), "Failed to delete images")
def test_update_image_metadata_delegates_with_workspace_slug(self):
with patch("roboflow.adapters.rfapi.update_image_metadata") as mock_update:
mock_update.return_value = {"success": True}
result = self.project.update_image_metadata(
"img-1",
metadata={"camera_id": "cam001"},
add_tags=["reviewed"],
)
self.assertEqual(result, {"success": True})
mock_update.assert_called_once_with(
api_key=ROBOFLOW_API_KEY,
workspace_url=WORKSPACE_NAME,
image_id="img-1",
metadata={"camera_id": "cam001"},
remove_metadata=None,
add_tags=["reviewed"],
remove_tags=None,
)
def test_classification_dataset_upload(self):
from roboflow.util import folderparser
classification_folder = "tests/datasets/corrosion-singlelabel-classification"
# Parse with classification flag to get inferred annotations
parsed_dataset = folderparser.parsefolder(classification_folder, is_classification=True)
# Create a mock project with classification type
self.project.type = "classification"
annotation_calls = []
def capture_annotation_calls(annotation_path, **kwargs):
annotation_calls.append({"annotation_path": annotation_path, "image_id": kwargs.get("image_id")})
return ({"success": True}, 0.1, 0)
mocks = {
"parser": patch("roboflow.util.folderparser.parsefolder", return_value=parsed_dataset),
"upload": patch(
"roboflow.core.project.Project.upload_image",
return_value=({"id": "test-id", "success": True}, 0.1, 0),
),
"save_annotation": patch(
"roboflow.core.project.Project.save_annotation", side_effect=capture_annotation_calls
),
"get_project": patch(
"roboflow.core.workspace.Workspace._get_or_create_project", return_value=(self.project, False)
),
}
mock_objects = {}
for name, mock in mocks.items():
mock_objects[name] = mock.start()
try:
self.workspace.upload_dataset(dataset_path=classification_folder, project_name=PROJECT_NAME, num_workers=1)
self.assertEqual(mock_objects["upload"].call_count, 10)
self.assertEqual(len(annotation_calls), 10)
corrosion_count = sum(1 for call in annotation_calls if call["annotation_path"] == "Corrosion")
no_corrosion_count = sum(1 for call in annotation_calls if call["annotation_path"] == "no-corrosion")
self.assertEqual(corrosion_count, 5)
self.assertEqual(no_corrosion_count, 5)
for call in annotation_calls:
self.assertIn(call["annotation_path"], ["Corrosion", "no-corrosion"])
finally:
for mock in mocks.values():
mock.stop()
def test_classification_edge_cases(self):
edge_case_dataset = [
# These should not get annotations
{"file": "root_img.jpg", "split": "train", "dirname": "/"},
{"file": "dot_img.jpg", "split": "train", "dirname": "/."},
# These should get annotations from folder structure
{
"file": "nested.jpg",
"split": "train",
"dirname": "/train/defects/rust/severe",
"annotationfile": {"type": "classification_folder", "classification_label": "severe"},
},
{
"file": "normal.jpg",
"split": "train",
"dirname": "/train/good",
"annotationfile": {"type": "classification_folder", "classification_label": "good"},
},
]
self.project.type = "classification"
annotation_calls = []
def capture_annotation_calls(annotation_path, **kwargs):
annotation_calls.append(annotation_path)
return ({"success": True}, 0.1, 0)
test_dataset = self._create_test_dataset(edge_case_dataset)
mocks = self._setup_upload_dataset_mocks(
test_dataset=test_dataset, save_annotation_side_effect=capture_annotation_calls
)
for mock in mocks.values():
mock.start()
try:
self.workspace.upload_dataset(dataset_path="/test/dataset", project_name=PROJECT_NAME, num_workers=1)
self.assertEqual(len(annotation_calls), 2)
self.assertIn("severe", annotation_calls)
self.assertIn("good", annotation_calls)
finally:
for mock in mocks.values():
mock.stop()
def test_multilabel_classification_dataset_upload(self):
from roboflow.util import folderparser
multilabel_folder = "tests/datasets/skinproblem-multilabel-classification"
parsed_dataset = folderparser.parsefolder(multilabel_folder, is_classification=True)
self.project.type = "classification"
self.project.multilabel = True
annotation_calls = []
def capture_annotation_calls(annotation_path, **kwargs):
annotation_calls.append(annotation_path)
return ({"success": True}, 0.1, 0)
mocks = {
"parser": patch("roboflow.util.folderparser.parsefolder", return_value=parsed_dataset),
"upload": patch(
"roboflow.core.project.Project.upload_image",
return_value=({"id": "test-id", "success": True}, 0.1, 0),
),
"save_annotation": patch(
"roboflow.core.project.Project.save_annotation", side_effect=capture_annotation_calls
),
"get_project": patch(
"roboflow.core.workspace.Workspace._get_or_create_project", return_value=(self.project, False)
),
}
for mock in mocks.values():
mock.start()
try:
self.workspace.upload_dataset(dataset_path=multilabel_folder, project_name=PROJECT_NAME, num_workers=1)
self.assertEqual(len(annotation_calls), len(parsed_dataset["images"]))
for call in annotation_calls:
labels = json.loads(call)
self.assertIsInstance(labels, list)
self.assertGreater(len(labels), 0)
finally:
for mock in mocks.values():
mock.stop()
def test_search_with_annotation_job_params(self):
"""Test that annotation_job and annotation_job_id parameters are properly included in search requests"""
# Test 1: Search with annotation_job=True
expected_url = f"{API_URL}/{WORKSPACE_NAME}/{PROJECT_NAME}/search?api_key={ROBOFLOW_API_KEY}"
mock_response = {
"results": [
{"id": "image1", "name": "test1.jpg", "created": 1616161616, "labels": ["person"]},
{"id": "image2", "name": "test2.jpg", "created": 1616161617, "labels": ["car"]},
]
}
responses.add(
responses.POST,
expected_url,
json=mock_response,
status=200,
match=[
json_params_matcher(
{
"offset": 0,
"limit": 100,
"batch": False,
"annotation_job": True,
"fields": ["id", "created", "name", "labels"],
}
)
],
)
results = self.project.search(annotation_job=True)
self.assertEqual(len(results), 2)
self.assertEqual(results[0]["id"], "image1")
# Test 2: Search with annotation_job_id
test_job_id = "job_123456"
responses.add(
responses.POST,
expected_url,
json=mock_response,
status=200,
match=[
json_params_matcher(
{
"offset": 0,
"limit": 100,
"batch": False,
"annotation_job_id": test_job_id,
"fields": ["id", "created", "name", "labels"],
}
)
],
)
results = self.project.search(annotation_job_id=test_job_id)
self.assertEqual(len(results), 2)
# Test 3: Search with both parameters
responses.add(
responses.POST,
expected_url,
json=mock_response,
status=200,
match=[
json_params_matcher(
{
"offset": 0,
"limit": 50,
"batch": False,
"annotation_job": False,
"annotation_job_id": test_job_id,
"prompt": "dog",
"fields": ["id", "created", "name", "labels"],
}
)
],
)
results = self.project.search(prompt="dog", annotation_job=False, annotation_job_id=test_job_id, limit=50)
self.assertEqual(len(results), 2)
# Test 4: Verify parameters are not included when None
responses.add(
responses.POST,
expected_url,
json=mock_response,
status=200,
match=[
json_params_matcher(
{
"offset": 0,
"limit": 100,
"batch": False,
"fields": ["id", "created", "name", "labels"],
# annotation_job and annotation_job_id should NOT be in the payload
}
)
],
)
# This should pass because json_params_matcher only checks that the
# specified keys match, it doesn't fail if additional keys are missing
results = self.project.search()
self.assertEqual(len(results), 2)
class TestZipUpload(RoboflowTest):
def _rfapi_mocks(self, get_status_side_effect=None, get_status_return=None):
import_target = "roboflow.core.workspace.rfapi"
init_mock = patch(
f"{import_target}.init_zip_upload",
return_value={"signedUrl": "https://signed.example/upload", "taskId": "task-123"},
)
put_mock = patch(f"{import_target}.upload_zip_to_signed_url", return_value=None)
if get_status_side_effect is not None:
status_mock = patch(f"{import_target}.get_zip_upload_status", side_effect=get_status_side_effect)
else:
status_mock = patch(
f"{import_target}.get_zip_upload_status",
return_value=get_status_return or {"status": "completed", "result": {"ok": True}},
)
project_mock = patch(
"roboflow.core.workspace.Workspace._get_or_create_project",
return_value=(self.project, False),
)
return {"init": init_mock, "put": put_mock, "status": status_mock, "project": project_mock}
def test_zip_path_passthrough(self):
import tempfile
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as fh:
fh.write(b"fake zip")
zip_path = fh.name
mocks = self._rfapi_mocks()
zip_dir_mock = patch("roboflow.core.workspace._zip_directory")
started = {name: m.start() for name, m in mocks.items()}
started["zip_dir"] = zip_dir_mock.start()
try:
result = self.workspace.upload_dataset(dataset_path=zip_path, project_name=PROJECT_NAME)
self.assertEqual(result, {"status": "completed", "result": {"ok": True}})
started["init"].assert_called_once()
started["put"].assert_called_once()
started["zip_dir"].assert_not_called()
put_args, _ = started["put"].call_args
self.assertEqual(put_args[0], "https://signed.example/upload")
self.assertEqual(put_args[1], zip_path)
finally:
for m in list(mocks.values()) + [zip_dir_mock]:
m.stop()
import os as _os
if _os.path.exists(zip_path):
_os.unlink(zip_path)
def test_directory_with_use_zip_upload_zips_and_cleans_up(self):
import os as _os
import tempfile
# Pre-create a temp zip path to be returned by _zip_directory
fd, fake_zip = tempfile.mkstemp(suffix=".zip", prefix="roboflow-upload-")
_os.close(fd)