|
| 1 | +"""Tests for slim install compatibility. |
| 2 | +
|
| 3 | +Verifies that the package can be imported and lightweight features work |
| 4 | +even when heavy dependencies (PIL, opencv, numpy, matplotlib) are missing. |
| 5 | +
|
| 6 | +In a full install, these tests verify the guards don't break normal behavior. |
| 7 | +In a slim install, they verify graceful degradation. |
| 8 | +""" |
| 9 | + |
| 10 | +import unittest |
| 11 | + |
| 12 | + |
| 13 | +class TestSlimImport(unittest.TestCase): |
| 14 | + """Verify that importing the package always succeeds.""" |
| 15 | + |
| 16 | + def test_import_roboflow(self): |
| 17 | + import roboflow |
| 18 | + |
| 19 | + self.assertIsNotNone(roboflow.__version__) |
| 20 | + |
| 21 | + def test_import_vision_events_adapter(self): |
| 22 | + from roboflow.adapters import vision_events_api |
| 23 | + |
| 24 | + self.assertTrue(callable(vision_events_api.write_event)) |
| 25 | + self.assertTrue(callable(vision_events_api.write_batch)) |
| 26 | + self.assertTrue(callable(vision_events_api.query)) |
| 27 | + self.assertTrue(callable(vision_events_api.list_use_cases)) |
| 28 | + self.assertTrue(callable(vision_events_api.get_custom_metadata_schema)) |
| 29 | + self.assertTrue(callable(vision_events_api.upload_image)) |
| 30 | + |
| 31 | + def test_import_config(self): |
| 32 | + from roboflow.config import API_URL |
| 33 | + |
| 34 | + self.assertIsInstance(API_URL, str) |
| 35 | + |
| 36 | + def test_import_rfapi(self): |
| 37 | + from roboflow.adapters.rfapi import RoboflowError |
| 38 | + |
| 39 | + self.assertTrue(issubclass(RoboflowError, Exception)) |
| 40 | + |
| 41 | + def test_import_cli(self): |
| 42 | + from roboflow.cli import app |
| 43 | + |
| 44 | + self.assertIsNotNone(app) |
| 45 | + |
| 46 | + |
| 47 | +class TestSlimGracefulDegradation(unittest.TestCase): |
| 48 | + """Verify that heavy features fail with clear errors when deps are missing. |
| 49 | +
|
| 50 | + These tests only exercise the error path when PIL/opencv are absent. |
| 51 | + In a full install they verify the guard exists but doesn't fire. |
| 52 | + """ |
| 53 | + |
| 54 | + def test_workspace_and_project_attributes_exist(self): |
| 55 | + """Workspace and Project are either real classes or None sentinels.""" |
| 56 | + import roboflow |
| 57 | + |
| 58 | + # In full install these are classes; in slim they're None |
| 59 | + ws = roboflow.Workspace |
| 60 | + proj = roboflow.Project |
| 61 | + self.assertTrue(ws is None or callable(ws)) |
| 62 | + self.assertTrue(proj is None or callable(proj)) |
| 63 | + |
| 64 | + def test_roboflow_workspace_guard(self): |
| 65 | + """If Workspace is None (slim), calling workspace() raises ImportError.""" |
| 66 | + import roboflow |
| 67 | + |
| 68 | + if roboflow.Workspace is not None: |
| 69 | + self.skipTest("Full install, Workspace is available") |
| 70 | + |
| 71 | + rf = roboflow.Roboflow.__new__(roboflow.Roboflow) |
| 72 | + rf.api_key = "test" |
| 73 | + rf.current_workspace = "test" |
| 74 | + rf.model_format = "yolov8" |
| 75 | + |
| 76 | + with self.assertRaises(ImportError) as ctx: |
| 77 | + rf.workspace() |
| 78 | + self.assertIn("pip install roboflow", str(ctx.exception)) |
| 79 | + |
| 80 | + def test_roboflow_project_guard(self): |
| 81 | + """If Project is None (slim), calling project() raises ImportError.""" |
| 82 | + import roboflow |
| 83 | + |
| 84 | + if roboflow.Project is not None: |
| 85 | + self.skipTest("Full install, Project is available") |
| 86 | + |
| 87 | + rf = roboflow.Roboflow.__new__(roboflow.Roboflow) |
| 88 | + rf.api_key = "test" |
| 89 | + rf.current_workspace = "test" |
| 90 | + |
| 91 | + with self.assertRaises(ImportError) as ctx: |
| 92 | + rf.project("test-project") |
| 93 | + self.assertIn("pip install roboflow", str(ctx.exception)) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + unittest.main() |
0 commit comments