Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed registry._EXTRA_OPERATORS #5347

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions fiftyone/operators/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ def operator_exists(operator_uri, enabled=True):
return registry.operator_exists(operator_uri)


_EXTRA_OPERATORS = []


class OperatorRegistry(object):
"""Operator registry.

Expand All @@ -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)

Expand Down
14 changes: 5 additions & 9 deletions tests/unittests/operators/executor_tests.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! I just used this in teams but as unittest.mock.patch().

Is there a way to use the real list_operators? Thats a big part of whats being tested here. Although _EXTRA_OPERATORS isn't a better solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it! We can achieve the same effect by mocking out fopc.build_plugin_contexts but I think it's cleaner to mock out list_operators.

I can add a unit test for list_operators too so that it is also covered by tests.

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,
Expand Down
60 changes: 60 additions & 0 deletions tests/unittests/operators/registry_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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):
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)
Loading