From 06f4f5476f2dc70ee4664960d8f6988b3f83e505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Ch=C3=A1vez?= Date: Thu, 30 Nov 2023 17:10:25 -0500 Subject: [PATCH] feat: Add tags count to taxonomy serializer (#125) --- openedx_learning/__init__.py | 2 +- .../core/tagging/rest_api/v1/serializers.py | 6 +++ .../core/tagging/test_views.py | 43 +++++++++++++++++-- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/openedx_learning/__init__.py b/openedx_learning/__init__.py index d458a9e2..73a0541a 100644 --- a/openedx_learning/__init__.py +++ b/openedx_learning/__init__.py @@ -1,4 +1,4 @@ """ Open edX Learning ("Learning Core"). """ -__version__ = "0.3.6" +__version__ = "0.3.7" diff --git a/openedx_tagging/core/tagging/rest_api/v1/serializers.py b/openedx_tagging/core/tagging/rest_api/v1/serializers.py index ca95c91c..211f3d53 100644 --- a/openedx_tagging/core/tagging/rest_api/v1/serializers.py +++ b/openedx_tagging/core/tagging/rest_api/v1/serializers.py @@ -34,6 +34,8 @@ class TaxonomySerializer(serializers.ModelSerializer): """ Serializer for the Taxonomy model. """ + tags_count = serializers.SerializerMethodField() + class Meta: model = Taxonomy fields = [ @@ -45,6 +47,7 @@ class Meta: "allow_free_text", "system_defined", "visible_to_authors", + "tags_count", ] def to_representation(self, instance): @@ -54,6 +57,9 @@ def to_representation(self, instance): instance = instance.cast() return super().to_representation(instance) + def get_tags_count(self, instance): + return instance.tag_set.count() + class ObjectTagListQueryParamsSerializer(serializers.Serializer): # pylint: disable=abstract-method """ diff --git a/tests/openedx_tagging/core/tagging/test_views.py b/tests/openedx_tagging/core/tagging/test_views.py index f3f88208..00151f30 100644 --- a/tests/openedx_tagging/core/tagging/test_views.py +++ b/tests/openedx_tagging/core/tagging/test_views.py @@ -126,12 +126,20 @@ def test_list_taxonomy_queryparams(self, enabled, expected_status: int, expected assert len(response.data["results"]) == expected_count @ddt.data( - (None, status.HTTP_401_UNAUTHORIZED), - ("user", status.HTTP_200_OK), - ("staff", status.HTTP_200_OK), + (None, status.HTTP_401_UNAUTHORIZED, 0), + ("user", status.HTTP_200_OK, 10), + ("staff", status.HTTP_200_OK, 20), ) @ddt.unpack - def test_list_taxonomy(self, user_attr: str | None, expected_status: int): + def test_list_taxonomy(self, user_attr: str | None, expected_status: int, tags_count: int): + taxonomy = api.create_taxonomy(name="Taxonomy enabled 1", enabled=True) + for i in range(tags_count): + tag = Tag( + taxonomy=taxonomy, + value=f"Tag {i}", + ) + tag.save() + url = TAXONOMY_LIST_URL if user_attr: @@ -141,6 +149,33 @@ def test_list_taxonomy(self, user_attr: str | None, expected_status: int): response = self.client.get(url) assert response.status_code == expected_status + # Check results + if tags_count: + assert response.data["results"] == [ + { + "id": -1, + "name": "Languages", + "description": "Languages that are enabled on this system.", + "enabled": True, + "allow_multiple": False, + "allow_free_text": False, + "system_defined": True, + "visible_to_authors": True, + "tags_count": 0, + }, + { + "id": taxonomy.id, + "name": "Taxonomy enabled 1", + "description": "", + "enabled": True, + "allow_multiple": True, + "allow_free_text": False, + "system_defined": False, + "visible_to_authors": True, + "tags_count": tags_count, + }, + ] + def test_list_taxonomy_pagination(self) -> None: url = TAXONOMY_LIST_URL api.create_taxonomy(name="T1", enabled=True)