Skip to content

Commit

Permalink
feat: adds get_object_collections API and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pomegranited committed Aug 28, 2024
1 parent 1fda3e5 commit a2cd234
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions openedx_learning/apps/authoring/collections/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"get_collection",
"get_collections",
"get_learning_package_collections",
"get_object_collections",
"remove_from_collections",
"update_collection",
]
Expand Down Expand Up @@ -160,6 +161,16 @@ def remove_from_collections(
return total_deleted


def get_object_collections(object_id: int) -> QuerySet[Collection]:
"""
Get all collections associated with a given PublishableEntity.
Only enabled collections are returned.
"""
entity = PublishableEntity.objects.get(pk=object_id)
return entity.collections.filter(enabled=True).order_by("pk")


def get_learning_package_collections(learning_package_id: int) -> QuerySet[Collection]:
"""
Get all collections for a given learning package
Expand Down
24 changes: 24 additions & 0 deletions tests/openedx_learning/apps/authoring/collections/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class CollectionContentsTestCase(CollectionTestCase):
collection0: Collection
collection1: Collection
collection2: Collection
disabled_collection: Collection

@classmethod
def setUpTestData(cls) -> None:
Expand Down Expand Up @@ -268,6 +269,17 @@ def setUpTestData(cls) -> None:
cls.draft_entity.id,
]),
)
cls.disabled_collection = collection_api.create_collection(
cls.learning_package.id,
title="Disabled Collection",
created_by=None,
description="This disabled collection contains 1 object",
contents_qset=PublishableEntity.objects.filter(id__in=[
cls.published_entity.id,
]),
)
cls.disabled_collection.enabled = False
cls.disabled_collection.save()

def test_create_collection_contents(self):
"""
Expand Down Expand Up @@ -371,6 +383,18 @@ def test_remove_from_collections(self):
self.collection2.refresh_from_db()
assert self.collection2.modified == modified_time

def test_get_object_collections(self):
"""
Tests fetching the enabled collections which contain a given object.
"""
collections = collection_api.get_object_collections(
self.published_entity.id,
)
assert list(collections) == [
self.collection1,
self.collection2,
]


class UpdateCollectionTestCase(CollectionTestCase):
"""
Expand Down

0 comments on commit a2cd234

Please sign in to comment.