Skip to content

Commit

Permalink
fix: clear plugin context before performing search by id (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunato committed Apr 5, 2023
1 parent cfa4cfc commit 324bc80
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions eodag/api/core.py
Expand Up @@ -1180,6 +1180,7 @@ def _search_by_id(self, uid, provider=None, **kwargs):
)
logger.debug("Using plugin class for search: %s", plugin.__class__.__name__)
auth = self._plugins_manager.get_auth_plugin(plugin.provider)
plugin.clear()
results, _ = self._do_search(plugin, auth=auth, id=uid, **kwargs)
if len(results) == 1:
if not results[0].product_type:
Expand Down
44 changes: 40 additions & 4 deletions tests/units/test_core.py
Expand Up @@ -1380,14 +1380,50 @@ def test__prepare_search_unknown_product_type(self, mock_fetch_product_types_lis
self.dag._prepare_search(product_type="foo")
mock_fetch_product_types_list.assert_called_once_with(self.dag)

@mock.patch("eodag.plugins.manager.PluginManager.get_search_plugins", autospec=True)
def test__search_by_id(self, mock_get_search_plugins):
"""_search_by_id must filter search plugins using given kwargs"""
self.dag._search_by_id(uid="foo", productType="bar", provider="baz")
@mock.patch(
"eodag.api.core.EODataAccessGateway._do_search",
autospec=True,
return_value=([mock.Mock()], 1),
)
@mock.patch("eodag.plugins.manager.PluginManager.get_auth_plugin", autospec=True)
@mock.patch(
"eodag.plugins.manager.PluginManager.get_search_plugins",
autospec=True,
return_value=[mock.Mock()],
)
def test__search_by_id(
self, mock_get_search_plugins, mock_get_auth_plugin, mock__do_search
):
"""_search_by_id must filter search plugins using given kwargs, clear plugin and perform search"""

found = self.dag._search_by_id(uid="foo", productType="bar", provider="baz")

# get_search_plugins
mock_get_search_plugins.assert_called_once_with(
self.dag._plugins_manager, product_type="bar", provider="baz"
)

# search plugin clear
mock_get_search_plugins.return_value[0].clear.assert_called_once()

# _do_search returns 1 product
mock__do_search.assert_called_once_with(
self.dag,
mock_get_search_plugins.return_value[0],
auth=mock_get_auth_plugin.return_value,
id="foo",
productType="bar",
)
self.assertEqual(found, mock__do_search.return_value)

mock__do_search.reset_mock()
# return None if more than 1 product is found
mock__do_search.return_value = ([mock.Mock(), mock.Mock()], 2)
with self.assertLogs(level="INFO") as cm:
found = self.dag._search_by_id(uid="foo", productType="bar", provider="baz")
self.assertEqual(found, (SearchResult([]), 0))
self.assertIn("Several products found for this id", str(cm.output))

@mock.patch("eodag.plugins.search.qssearch.QueryStringSearch", autospec=True)
def test__do_search_support_itemsperpage_higher_than_maximum(self, search_plugin):
"""_do_search must create a count query by default"""
Expand Down

0 comments on commit 324bc80

Please sign in to comment.