def test_register_invalid_path(self): """We raise an exception if a component is registered with a non-existent path. """ test_path = "/a/test/component/directory" registry = ComponentRegistry.instance() with self.assertRaises(StreamlitAPIException) as ctx: registry.register_component(CustomComponent("test_component", test_path)) self.assertIn("No such component directory", ctx.exception)
def test_register_component_no_path(self): """It's not an error to register a component without a path.""" registry = ComponentRegistry.instance() # Return None when the component hasn't been registered self.assertIsNone(registry.get_component_path("test_component")) # And also return None when the component doesn't have a path registry.register_component( CustomComponent("test_component", url="http://not.a.url")) self.assertIsNone(registry.get_component_path("test_component"))
def test_register_duplicate_path(self): """It's not an error to re-register a component. (This can happen during development). """ test_path_1 = "/a/test/component/directory" test_path_2 = "/another/test/component/directory" def isdir(path): return path in (test_path_1, test_path_2) registry = ComponentRegistry.instance() with mock.patch( "streamlit.components.v1.components.os.path.isdir", side_effect=isdir ): registry.register_component(CustomComponent("test_component", test_path_1)) registry.register_component(CustomComponent("test_component", test_path_1)) self.assertEqual(test_path_1, registry.get_component_path("test_component")) registry.register_component(CustomComponent("test_component", test_path_2)) self.assertEqual(test_path_2, registry.get_component_path("test_component"))
def test_register_component_with_path(self): """Registering a component should associate it with its path.""" test_path = "/a/test/component/directory" def isdir(path): return path == test_path registry = ComponentRegistry.instance() with mock.patch("streamlit.components.v1.components.os.path.isdir", side_effect=isdir): registry.register_component( CustomComponent("test_component", path=test_path)) self.assertEqual(test_path, registry.get_component_path("test_component"))