From ee7b43168be2cf4915368489cb16f5b957029ba9 Mon Sep 17 00:00:00 2001 From: minhtuevo Date: Mon, 6 Jan 2025 11:20:17 -0800 Subject: [PATCH 1/3] Removed registry._EXTRA_OPERATORS --- fiftyone/operators/registry.py | 4 ---- tests/unittests/operators/executor_tests.py | 14 +++++--------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/fiftyone/operators/registry.py b/fiftyone/operators/registry.py index 9d68f3d9e9..055fa54c28 100644 --- a/fiftyone/operators/registry.py +++ b/fiftyone/operators/registry.py @@ -68,9 +68,6 @@ def operator_exists(operator_uri, enabled=True): return registry.operator_exists(operator_uri) -_EXTRA_OPERATORS = [] - - class OperatorRegistry(object): """Operator registry. @@ -94,7 +91,6 @@ def list_operators(self, builtin=None, type=None): a list of :class:`fiftyone.operators.Operator` instances """ operators = [] - operators.extend(_EXTRA_OPERATORS) for pctx in self.plugin_contexts: operators.extend(pctx.instances) diff --git a/tests/unittests/operators/executor_tests.py b/tests/unittests/operators/executor_tests.py index f418a994d2..26f4141bb7 100644 --- a/tests/unittests/operators/executor_tests.py +++ b/tests/unittests/operators/executor_tests.py @@ -1,16 +1,13 @@ import pytest -from unittest.mock import MagicMock, patch -from starlette.exceptions import HTTPException +from unittest.mock import patch import fiftyone.operators.types as types from fiftyone.operators.operator import Operator from fiftyone.operators.executor import ( execute_or_delegate_operator, ExecutionResult, - ExecutionContext, ) from fiftyone.operators import OperatorConfig -import fiftyone.operators.registry as registry ECHO_URI = "@voxel51/operators/echo" @@ -30,12 +27,11 @@ def execute(self, ctx): return {"message": ctx.params.get("message", None)} -# Force registration of the operator for testing -registry._EXTRA_OPERATORS.append(EchoOperator(_builtin=True)) - - @pytest.mark.asyncio -async def test_execute_or_delegate_operator(): +@patch("fiftyone.operators.registry.OperatorRegistry.list_operators") +async def test_execute_or_delegate_operator(list_operators): + list_operators.return_value = [EchoOperator(_builtin=True)] + request_params = { "dataset_name": "test_dataset", "operator_uri": ECHO_URI, From a6a0ba8779754f4537781ce0e661be073a9bb2cc Mon Sep 17 00:00:00 2001 From: minhtuevo Date: Mon, 6 Jan 2025 11:41:50 -0800 Subject: [PATCH 2/3] Added list_operators unit tests --- tests/unittests/operators/registry_tests.py | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/unittests/operators/registry_tests.py diff --git a/tests/unittests/operators/registry_tests.py b/tests/unittests/operators/registry_tests.py new file mode 100644 index 0000000000..08f38bf1ba --- /dev/null +++ b/tests/unittests/operators/registry_tests.py @@ -0,0 +1,51 @@ +import unittest +from unittest.mock import MagicMock, patch +from fiftyone.operators.registry import OperatorRegistry +from fiftyone.operators import Panel + + +class TestOperatorRegistry(unittest.TestCase): + @patch('fiftyone.plugins.context.build_plugin_contexts') + def setUp(self, mock_build_plugin_contexts): + # Mocking plugin contexts and operators + self.mock_contexts = [ + MagicMock(instances=[ + MagicMock(_builtin=True, spec=Panel), + MagicMock(_builtin=False, spec=object), + ]), + MagicMock(instances=[ + MagicMock(_builtin=True, spec=object), + MagicMock(_builtin=False, spec=Panel), + ]), + ] + mock_build_plugin_contexts.return_value = self.mock_contexts + + self.registry = OperatorRegistry() + + def test_list_all_operators(self): + operators = self.registry.list_operators() + self.assertEqual(len(operators), 4) + + def test_list_builtin_operators(self): + operators = self.registry.list_operators(builtin=True) + self.assertTrue(all(op._builtin for op in operators)) + self.assertEqual(len(operators), 2) + + def test_list_non_builtin_operators(self): + operators = self.registry.list_operators(builtin=False) + self.assertTrue(all(not op._builtin for op in operators)) + self.assertEqual(len(operators), 2) + + def test_list_panel_type_operators(self): + operators = self.registry.list_operators(type="panel") + self.assertTrue(all(isinstance(op, Panel) for op in operators)) + self.assertEqual(len(operators), 2) + + def test_list_operator_type_operators(self): + operators = self.registry.list_operators(type="operator") + self.assertTrue(all(not isinstance(op, Panel) for op in operators)) + self.assertEqual(len(operators), 2) + + def test_list_invalid_type_raises_error(self): + with self.assertRaises(ValueError): + self.registry.list_operators(type="invalid") From fbead8edba7dd6cc6ceb9068eefaeb0a0cb40b26 Mon Sep 17 00:00:00 2001 From: minhtuevo Date: Mon, 6 Jan 2025 12:02:15 -0800 Subject: [PATCH 3/3] Updated unit test --- tests/unittests/operators/registry_tests.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/unittests/operators/registry_tests.py b/tests/unittests/operators/registry_tests.py index 08f38bf1ba..5326776984 100644 --- a/tests/unittests/operators/registry_tests.py +++ b/tests/unittests/operators/registry_tests.py @@ -47,5 +47,14 @@ def test_list_operator_type_operators(self): self.assertEqual(len(operators), 2) def test_list_invalid_type_raises_error(self): - with self.assertRaises(ValueError): - self.registry.list_operators(type="invalid") + for operator_type in ["invalid", "", 1]: + with self.assertRaises(ValueError): + self.registry.list_operators(type=operator_type) + + def test_list_operators_empty_contexts(self): + with patch( + 'fiftyone.plugins.context.build_plugin_contexts') as mock_build: + mock_build.return_value = [] + registry = OperatorRegistry() + operators = registry.list_operators() + self.assertEqual(len(operators), 0)