Skip to content

Commit

Permalink
feat: Add tags count to taxonomy serializer (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV authored Nov 30, 2023
1 parent 75895cf commit 06f4f54
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion openedx_learning/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Open edX Learning ("Learning Core").
"""
__version__ = "0.3.6"
__version__ = "0.3.7"
6 changes: 6 additions & 0 deletions openedx_tagging/core/tagging/rest_api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class TaxonomySerializer(serializers.ModelSerializer):
"""
Serializer for the Taxonomy model.
"""
tags_count = serializers.SerializerMethodField()

class Meta:
model = Taxonomy
fields = [
Expand All @@ -45,6 +47,7 @@ class Meta:
"allow_free_text",
"system_defined",
"visible_to_authors",
"tags_count",
]

def to_representation(self, instance):
Expand All @@ -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
"""
Expand Down
43 changes: 39 additions & 4 deletions tests/openedx_tagging/core/tagging/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 06f4f54

Please sign in to comment.