From 37a5aa25022bc984d222414b67c999c765e608cd Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Tue, 24 Oct 2023 11:57:35 +0100 Subject: [PATCH] Make sure `get_category_with_name()` returns a `Category` or raise `ValueError` --- lib/tool_shed/test/base/populators.py | 9 ++++----- lib/tool_shed/test/base/twilltestcase.py | 6 +++--- .../functional/test_0000_basic_repository_features.py | 7 ++++--- lib/tool_shed/test/functional/test_shed_categories.py | 5 +++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/tool_shed/test/base/populators.py b/lib/tool_shed/test/base/populators.py index 19b6b697b46e..7b444c5efb55 100644 --- a/lib/tool_shed/test/base/populators.py +++ b/lib/tool_shed/test/base/populators.py @@ -271,18 +271,17 @@ def get_category_with_id(self, category_id: str) -> Category: response.raise_for_status() return Category(**response.json()) - def get_category_with_name(self, name: str) -> Optional[Category]: + def get_category_with_name(self, name: str) -> Category: categories = [c for c in self.get_categories() if c.name == name] - return categories[0] if categories else None + if not categories: + raise ValueError(f"No category with name {name} found.") + return categories[0] def repositories_by_category(self, category_id: str) -> RepositoriesByCategory: response = self._api_interactor.get(f"categories/{category_id}/repositories") response.raise_for_status() return RepositoriesByCategory(**response.json()) - def has_category_with_name(self, name: str) -> bool: - return self.get_category_with_name(name) is not None - def get_ordered_installable_revisions(self, owner: str, name: str) -> OrderedInstallableRevisions: request = GetOrderedInstallableRevisionsRequest(owner=owner, name=name) revisions_response = self._api_interactor.get( diff --git a/lib/tool_shed/test/base/twilltestcase.py b/lib/tool_shed/test/base/twilltestcase.py index e8d33a357d7c..757255a9416b 100644 --- a/lib/tool_shed/test/base/twilltestcase.py +++ b/lib/tool_shed/test/base/twilltestcase.py @@ -1035,8 +1035,9 @@ def commit_and_push(self, repository, hgrepo, options, username, password): def create_category(self, **kwd) -> Category: category_name = kwd["name"] - category = self.populator.get_category_with_name(category_name) - if category is None: + try: + category = self.populator.get_category_with_name(category_name) + except ValueError: # not recreating this functionality in the UI I don't think? category = self.populator.new_category(category_name) return category @@ -1591,7 +1592,6 @@ def _install_repository( ) -> None: self.browse_tool_shed(url=self.url) category = self.populator.get_category_with_name(category_name) - assert category self.browse_category(category) self.preview_repository_in_tool_shed(name, owner, strings_displayed=preview_strings_displayed) repository = self._get_repository_by_name_and_owner(name, owner) diff --git a/lib/tool_shed/test/functional/test_0000_basic_repository_features.py b/lib/tool_shed/test/functional/test_0000_basic_repository_features.py index e323187267a6..272a25d7ebfd 100644 --- a/lib/tool_shed/test/functional/test_0000_basic_repository_features.py +++ b/lib/tool_shed/test/functional/test_0000_basic_repository_features.py @@ -370,9 +370,10 @@ def test_0130_verify_handling_of_invalid_characters(self): def test_0135_api_get_repositories_in_category(self): """Load the api endpoint for repositories in a category.""" - categories = [] - categories.append(self.populator.get_category_with_name("Test 0000 Basic Repository Features 1")) - categories.append(self.populator.get_category_with_name("Test 0000 Basic Repository Features 2")) + categories = [ + self.populator.get_category_with_name(name) + for name in ("Test 0000 Basic Repository Features 1", "Test 0000 Basic Repository Features 2") + ] self.get_repositories_category_api(categories) def test_0140_view_invalid_changeset(self): diff --git a/lib/tool_shed/test/functional/test_shed_categories.py b/lib/tool_shed/test/functional/test_shed_categories.py index 3f9dd14fa516..1485617a3bdb 100644 --- a/lib/tool_shed/test/functional/test_shed_categories.py +++ b/lib/tool_shed/test/functional/test_shed_categories.py @@ -1,10 +1,12 @@ +from typing import Dict + from galaxy_test.base.api_util import random_name from ..base.api import ShedApiTestCase class TestShedCategoriesApi(ShedApiTestCase): def test_create_requires_name(self): - body = {} + body: Dict = {} response = self.admin_api_interactor.post("categories", json=body) assert response.status_code == 400 @@ -17,7 +19,6 @@ def test_create_okay(self): assert response.json()["name"] == name category = self.populator.get_category_with_name(name) - assert category is not None assert category.name == name assert category.description == description