From bfb5c2047c6cfd2e4f8b5c5dd0769e11494b3e6e Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Tue, 17 Oct 2023 08:33:35 +0200 Subject: [PATCH] Add category object (#499) * adding Category object * adding category listing for Weblate instance * adding category listing for a project Closes #494 --- wlc/__init__.py | 17 +++++++++++++++++ wlc/test_base.py | 3 +++ wlc/test_data/api/categories | 21 +++++++++++++++++++++ wlc/test_data/api/categories-1 | 7 +++++++ wlc/test_data/api/projects-hello | 1 + wlc/test_data/api/projects-hello-categories | 21 +++++++++++++++++++++ wlc/test_wlc.py | 19 ++++++++++++++++++- 7 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 wlc/test_data/api/categories create mode 100644 wlc/test_data/api/categories-1 create mode 100644 wlc/test_data/api/projects-hello-categories diff --git a/wlc/__init__.py b/wlc/__init__.py index fbd12348..3a6bf0e5 100644 --- a/wlc/__init__.py +++ b/wlc/__init__.py @@ -300,6 +300,10 @@ def list_languages(self): """List languages in the instance.""" return self.list_factory("languages/", Language) + def list_categories(self, path="categories/"): + """List categories in the instance.""" + return self.list_factory(path, Category) + def add_source_string( self, project, component, msgid, msgstr, source_language=None ): @@ -588,6 +592,11 @@ def changes(self): self.ensure_loaded("changes_list_url") return self.weblate.list_changes(self._attribs["changes_list_url"]) + def categories(self): + """List categories in the project.""" + self.ensure_loaded("categories_url") + return self.weblate.list_categories(self._attribs["categories_url"]) + def delete(self): self.weblate.raw_request("delete", self._url) @@ -595,6 +604,13 @@ def create_component(self, **kwargs): return self.weblate.create_component(self.slug, **kwargs) +class Category(LazyObject): + """Category object.""" + + PARAMS: ClassVar[Tuple[str, ...]] = ("category", "name", "project", "slug", "url") + MAPPINGS: ClassVar[Dict[str, Any]] = {"project": Project} + + class Component(LazyObject, RepoObjectMixin): """Component object.""" @@ -624,6 +640,7 @@ class Component(LazyObject, RepoObjectMixin): OPTIONALS: ClassVar[Set[str]] = {"source_language", "is_glossary", "category"} ID: ClassVar[str] = "slug" MAPPINGS: ClassVar[Dict[str, Any]] = { + "category": Category, "project": Project, "source_language": Language, } diff --git a/wlc/test_base.py b/wlc/test_base.py index b9c3a517..5cde79e1 100644 --- a/wlc/test_base.py +++ b/wlc/test_base.py @@ -160,6 +160,8 @@ def register_error( def register_uris(): """Register URIs for responses.""" paths = ( + "categories", + "categories/1", "changes", "components", "components/hello/android", @@ -177,6 +179,7 @@ def register_uris(): "projects/empty", "projects/empty/components", "projects/hello", + "projects/hello/categories", "projects/hello/changes", "projects/hello/components", "projects/hello/languages", diff --git a/wlc/test_data/api/categories b/wlc/test_data/api/categories new file mode 100644 index 00000000..b6d03030 --- /dev/null +++ b/wlc/test_data/api/categories @@ -0,0 +1,21 @@ +{ + "count": 2, + "next": null, + "previous": null, + "results": [ + { + "category": null, + "name": "3.12", + "project": "http://127.0.0.1:8000/api/projects/hello/", + "slug": "latest", + "url": "http://127.0.0.1:8000/api/categories/1/" + }, + { + "category": null, + "name": "3.11", + "project": "http://127.0.0.1:8000/api/projects/hello/", + "slug": "3-11", + "url": "http://127.0.0.1:8000/api/categories/2/" + } + ] +} diff --git a/wlc/test_data/api/categories-1 b/wlc/test_data/api/categories-1 new file mode 100644 index 00000000..9a6a39b5 --- /dev/null +++ b/wlc/test_data/api/categories-1 @@ -0,0 +1,7 @@ +{ + "category": null, + "name": "Hi", + "project": "http://127.0.0.1:8000/api/projects/hello/", + "slug": "hi", + "url": "http://127.0.0.1:8000/api/categories/1/" +} diff --git a/wlc/test_data/api/projects-hello b/wlc/test_data/api/projects-hello index c183ab4c..0b8eeb5e 100644 --- a/wlc/test_data/api/projects-hello +++ b/wlc/test_data/api/projects-hello @@ -1,4 +1,5 @@ { + "categories_url": "http://127.0.0.1:8000/api/projects/hello/categories/", "components_list_url": "http://127.0.0.1:8000/api/projects/hello/components/", "name": "Hello", "repository_url": "http://127.0.0.1:8000/api/projects/hello/repository/", diff --git a/wlc/test_data/api/projects-hello-categories b/wlc/test_data/api/projects-hello-categories new file mode 100644 index 00000000..08f311b7 --- /dev/null +++ b/wlc/test_data/api/projects-hello-categories @@ -0,0 +1,21 @@ +{ + "count": 2, + "next": null, + "previous": null, + "results": [ + { + "category": null, + "name": "hi", + "project": "http://127.0.0.1:8000/api/projects/hello/", + "slug": "hi", + "url": "http://127.0.0.1:8000/api/categories/1/" + }, + { + "category": null, + "name": "hiya", + "project": "http://127.0.0.1:8000/api/projects/hello/", + "slug": "hiya", + "url": "http://127.0.0.1:8000/api/categories/2/" + } + ] +} diff --git a/wlc/test_wlc.py b/wlc/test_wlc.py index d3223d6a..b53150b9 100644 --- a/wlc/test_wlc.py +++ b/wlc/test_wlc.py @@ -11,6 +11,7 @@ from wlc import ( API_URL, + Category, Change, Component, Project, @@ -112,6 +113,10 @@ def test_translations(self): """Test listing translations.""" self.assertEqual(len(list(Weblate().list_translations())), 50) + def test_categories(self): + """Test listing translations.""" + self.assertEqual(len(list(Weblate().list_categories())), 2) + def test_authentication(self): """Test authentication against server.""" with self.assertRaisesRegex(WeblateException, "permission"): @@ -404,6 +409,10 @@ def test_statistics(self): stats = obj.statistics() self.assertEqual(stats["name"], "Hello") + def test_categories(self): + obj = self.get() + self.assertEqual(2, len(list(obj.categories()))) + def test_create_component(self): """Component creation test.""" obj = self.get() @@ -443,7 +452,6 @@ def check_object(self, obj): """Perform verification whether object is valid.""" self.assertEqual(obj.name, "Weblate") self.assertEqual(obj.priority, 100) - self.assertEqual(obj.category, "http://127.0.0.1:8000/api/categories/1/") self.assertEqual(obj.agreement, "") def check_list(self, obj): @@ -662,6 +670,15 @@ def test_units_delete(self): self.assertIn("--deleted--", resp.decode()) +class CategoryTest(APITest): + def test(self): + obj = Category(Weblate(), "http://127.0.0.1:8000/api/categories/1/") + self.assertIsInstance(obj, Category) + self.assertIsNone(obj.category) + self.assertEqual(obj.name, "Hi") + self.assertEqual(obj.slug, "hi") + + # Delete the reference, so that the abstract class is not discovered # when running tests del ObjectTest