Skip to content

Commit

Permalink
Add category object (#499)
Browse files Browse the repository at this point in the history
* adding Category object
* adding category listing for Weblate instance
* adding category listing for a project

Closes #494
  • Loading branch information
m-aciek authored Oct 17, 2023
1 parent 91a5d9b commit bfb5c20
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
17 changes: 17 additions & 0 deletions wlc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down Expand Up @@ -588,13 +592,25 @@ 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)

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."""

Expand Down Expand Up @@ -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,
}
Expand Down
3 changes: 3 additions & 0 deletions wlc/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def register_error(
def register_uris():
"""Register URIs for responses."""
paths = (
"categories",
"categories/1",
"changes",
"components",
"components/hello/android",
Expand All @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions wlc/test_data/api/categories
Original file line number Diff line number Diff line change
@@ -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/"
}
]
}
7 changes: 7 additions & 0 deletions wlc/test_data/api/categories-1
Original file line number Diff line number Diff line change
@@ -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/"
}
1 change: 1 addition & 0 deletions wlc/test_data/api/projects-hello
Original file line number Diff line number Diff line change
@@ -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/",
Expand Down
21 changes: 21 additions & 0 deletions wlc/test_data/api/projects-hello-categories
Original file line number Diff line number Diff line change
@@ -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/"
}
]
}
19 changes: 18 additions & 1 deletion wlc/test_wlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from wlc import (
API_URL,
Category,
Change,
Component,
Project,
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bfb5c20

Please sign in to comment.