|
| 1 | +import json |
| 2 | +import unittest |
| 3 | + |
| 4 | +import responses |
| 5 | + |
| 6 | +from roboflow.adapters.rfapi import RoboflowError |
| 7 | +from roboflow.config import API_URL |
| 8 | + |
| 9 | + |
| 10 | +class TestWorkspaceSearch(unittest.TestCase): |
| 11 | + API_KEY = "test_key" |
| 12 | + WORKSPACE = "test-ws" |
| 13 | + SEARCH_URL = f"{API_URL}/{WORKSPACE}/search/v1?api_key={API_KEY}" |
| 14 | + |
| 15 | + def _make_workspace(self): |
| 16 | + from roboflow.core.workspace import Workspace |
| 17 | + |
| 18 | + info = { |
| 19 | + "workspace": { |
| 20 | + "name": "Test", |
| 21 | + "url": self.WORKSPACE, |
| 22 | + "projects": [], |
| 23 | + "members": [], |
| 24 | + } |
| 25 | + } |
| 26 | + return Workspace(info, api_key=self.API_KEY, default_workspace=self.WORKSPACE, model_format="yolov8") |
| 27 | + |
| 28 | + # --- search() tests --- |
| 29 | + |
| 30 | + @responses.activate |
| 31 | + def test_search_basic(self): |
| 32 | + body = { |
| 33 | + "results": [{"filename": "a.jpg"}, {"filename": "b.jpg"}], |
| 34 | + "total": 2, |
| 35 | + "continuationToken": None, |
| 36 | + } |
| 37 | + responses.add(responses.POST, self.SEARCH_URL, json=body, status=200) |
| 38 | + |
| 39 | + ws = self._make_workspace() |
| 40 | + result = ws.search("tag:review") |
| 41 | + |
| 42 | + self.assertEqual(result["total"], 2) |
| 43 | + self.assertEqual(len(result["results"]), 2) |
| 44 | + self.assertIsNone(result["continuationToken"]) |
| 45 | + |
| 46 | + # Verify request payload |
| 47 | + sent = json.loads(responses.calls[0].request.body) |
| 48 | + self.assertEqual(sent["query"], "tag:review") |
| 49 | + self.assertEqual(sent["pageSize"], 50) |
| 50 | + self.assertEqual(sent["fields"], ["tags", "projects", "filename"]) |
| 51 | + self.assertNotIn("continuationToken", sent) |
| 52 | + |
| 53 | + @responses.activate |
| 54 | + def test_search_with_continuation_token(self): |
| 55 | + body = {"results": [{"filename": "c.jpg"}], "total": 3, "continuationToken": None} |
| 56 | + responses.add(responses.POST, self.SEARCH_URL, json=body, status=200) |
| 57 | + |
| 58 | + ws = self._make_workspace() |
| 59 | + ws.search("*", continuation_token="tok_abc") |
| 60 | + |
| 61 | + sent = json.loads(responses.calls[0].request.body) |
| 62 | + self.assertEqual(sent["continuationToken"], "tok_abc") |
| 63 | + |
| 64 | + @responses.activate |
| 65 | + def test_search_custom_fields(self): |
| 66 | + body = {"results": [], "total": 0, "continuationToken": None} |
| 67 | + responses.add(responses.POST, self.SEARCH_URL, json=body, status=200) |
| 68 | + |
| 69 | + ws = self._make_workspace() |
| 70 | + ws.search("*", fields=["filename", "embedding"]) |
| 71 | + |
| 72 | + sent = json.loads(responses.calls[0].request.body) |
| 73 | + self.assertEqual(sent["fields"], ["filename", "embedding"]) |
| 74 | + |
| 75 | + @responses.activate |
| 76 | + def test_search_api_error(self): |
| 77 | + responses.add(responses.POST, self.SEARCH_URL, json={"error": "unauthorized"}, status=401) |
| 78 | + |
| 79 | + ws = self._make_workspace() |
| 80 | + with self.assertRaises(RoboflowError): |
| 81 | + ws.search("tag:review") |
| 82 | + |
| 83 | + # --- search_all() tests --- |
| 84 | + |
| 85 | + @responses.activate |
| 86 | + def test_search_all_single_page(self): |
| 87 | + body = { |
| 88 | + "results": [{"filename": "a.jpg"}, {"filename": "b.jpg"}], |
| 89 | + "total": 2, |
| 90 | + "continuationToken": None, |
| 91 | + } |
| 92 | + responses.add(responses.POST, self.SEARCH_URL, json=body, status=200) |
| 93 | + |
| 94 | + ws = self._make_workspace() |
| 95 | + pages = list(ws.search_all("*")) |
| 96 | + |
| 97 | + self.assertEqual(len(pages), 1) |
| 98 | + self.assertEqual(len(pages[0]), 2) |
| 99 | + |
| 100 | + @responses.activate |
| 101 | + def test_search_all_multiple_pages(self): |
| 102 | + page1 = { |
| 103 | + "results": [{"filename": "a.jpg"}], |
| 104 | + "total": 2, |
| 105 | + "continuationToken": "tok_page2", |
| 106 | + } |
| 107 | + page2 = { |
| 108 | + "results": [{"filename": "b.jpg"}], |
| 109 | + "total": 2, |
| 110 | + "continuationToken": None, |
| 111 | + } |
| 112 | + responses.add(responses.POST, self.SEARCH_URL, json=page1, status=200) |
| 113 | + responses.add(responses.POST, self.SEARCH_URL, json=page2, status=200) |
| 114 | + |
| 115 | + ws = self._make_workspace() |
| 116 | + pages = list(ws.search_all("*", page_size=1)) |
| 117 | + |
| 118 | + self.assertEqual(len(pages), 2) |
| 119 | + self.assertEqual(pages[0][0]["filename"], "a.jpg") |
| 120 | + self.assertEqual(pages[1][0]["filename"], "b.jpg") |
| 121 | + |
| 122 | + # Verify second request used the continuation token |
| 123 | + sent2 = json.loads(responses.calls[1].request.body) |
| 124 | + self.assertEqual(sent2["continuationToken"], "tok_page2") |
| 125 | + |
| 126 | + @responses.activate |
| 127 | + def test_search_all_empty_results(self): |
| 128 | + body = {"results": [], "total": 0, "continuationToken": None} |
| 129 | + responses.add(responses.POST, self.SEARCH_URL, json=body, status=200) |
| 130 | + |
| 131 | + ws = self._make_workspace() |
| 132 | + pages = list(ws.search_all("*")) |
| 133 | + |
| 134 | + self.assertEqual(len(pages), 0) |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + unittest.main() |
0 commit comments