@@ -74,15 +74,22 @@ def setUpTestData(cls) -> None:
7474 description = "Description of Collection 2" ,
7575 )
7676 cls .collection3 = api .create_collection (
77- cls .learning_package_2 .id ,
77+ cls .learning_package .id ,
7878 key = "COL3" ,
7979 created_by = None ,
8080 title = "Collection 3" ,
8181 description = "Description of Collection 3" ,
8282 )
83+ cls .another_library_collection = api .create_collection (
84+ cls .learning_package_2 .id ,
85+ key = "COL4" ,
86+ created_by = None ,
87+ title = "Collection 4" ,
88+ description = "Description of Collection 4" ,
89+ )
8390 cls .disabled_collection = api .create_collection (
8491 cls .learning_package .id ,
85- key = "COL4 " ,
92+ key = "another_library " ,
8693 created_by = None ,
8794 title = "Disabled Collection" ,
8895 description = "Description of Disabled Collection" ,
@@ -113,7 +120,7 @@ def test_get_collection_wrong_learning_package(self):
113120 Test getting a collection that doesn't exist in the requested learning package.
114121 """
115122 with self .assertRaises (ObjectDoesNotExist ):
116- api .get_collection (self .learning_package .pk , self .collection3 .key )
123+ api .get_collection (self .learning_package .pk , self .another_library_collection .key )
117124
118125 def test_get_collections (self ):
119126 """
@@ -123,6 +130,7 @@ def test_get_collections(self):
123130 assert list (collections ) == [
124131 self .collection1 ,
125132 self .collection2 ,
133+ self .collection3 ,
126134 ]
127135
128136 def test_get_invalid_collections (self ):
@@ -140,6 +148,7 @@ def test_get_all_collections(self):
140148 self .assertQuerySetEqual (collections , [
141149 self .collection1 ,
142150 self .collection2 ,
151+ self .collection3 ,
143152 self .disabled_collection ,
144153 ], ordered = True )
145154
@@ -151,6 +160,7 @@ def test_get_all_enabled_collections(self):
151160 self .assertQuerySetEqual (collections , [
152161 self .collection1 ,
153162 self .collection2 ,
163+ self .collection3 ,
154164 ], ordered = True )
155165
156166 def test_get_all_disabled_collections (self ):
@@ -301,6 +311,7 @@ def test_create_collection_entities(self):
301311 self .draft_component .publishable_entity ,
302312 ]
303313 assert not list (self .collection3 .entities .all ())
314+ assert not list (self .another_library_collection .entities .all ())
304315
305316 def test_add_to_collection (self ):
306317 """
@@ -352,13 +363,13 @@ def test_add_to_collection_wrong_learning_package(self):
352363 with self .assertRaises (ValidationError ):
353364 api .add_to_collection (
354365 self .learning_package_2 .id ,
355- self .collection3 .key ,
366+ self .another_library_collection .key ,
356367 PublishableEntity .objects .filter (id__in = [
357368 self .published_component .pk ,
358369 ]),
359370 )
360371
361- assert not list (self .collection3 .entities .all ())
372+ assert not list (self .another_library_collection .entities .all ())
362373
363374 def test_remove_from_collection (self ):
364375 """
@@ -405,6 +416,10 @@ def test_get_collection_components(self):
405416 self .learning_package .id ,
406417 self .collection3 .key ,
407418 ))
419+ assert not list (api .get_collection_components (
420+ self .learning_package .id ,
421+ self .another_library_collection .key ,
422+ ))
408423
409424
410425class UpdateCollectionTestCase (CollectionTestCase ):
@@ -573,3 +588,96 @@ def test_restore(self):
573588 self .published_component .publishable_entity ,
574589 self .draft_component .publishable_entity ,
575590 ]
591+
592+
593+ class SetCollectionsTestCase (CollectionEntitiesTestCase ):
594+ def test_set_collections (self ):
595+ """
596+ Test setting collections in a component
597+ """
598+ modified_time = datetime (2024 , 8 , 8 , tzinfo = timezone .utc )
599+ with freeze_time (modified_time ):
600+ api .set_collections (
601+ self .learning_package .id ,
602+ self .draft_component .publishable_entity ,
603+ collection_qset = Collection .objects .filter (id__in = [
604+ self .collection1 .pk ,
605+ self .collection2 .pk ,
606+ ]),
607+ created_by = self .user .id ,
608+ )
609+ assert list (self .collection1 .entities .all ()) == [
610+ self .published_component .publishable_entity ,
611+ self .draft_component .publishable_entity ,
612+ ]
613+ assert list (self .collection2 .entities .all ()) == [
614+ self .published_component .publishable_entity ,
615+ self .draft_component .publishable_entity ,
616+ ]
617+
618+ for collection_entity in CollectionPublishableEntity .objects .filter (
619+ entity = self .draft_component .publishable_entity
620+ ):
621+ if collection_entity .collection == self .collection1 :
622+ # The collection1 was newly associated, so it has a created_by
623+ assert collection_entity .created_by == self .user
624+ else :
625+ # The collection2 was already associated, with no created_by
626+ assert collection_entity .created_by == None
627+
628+ # The collection1 was newly associated, so the modified time is set
629+ assert Collection .objects .get (id = self .collection1 .pk ).modified == modified_time
630+ # The collection2 was already associated, so the modified time is unchanged
631+ assert Collection .objects .get (id = self .collection2 .pk ).modified != modified_time
632+
633+ # Set collections again, but this time remove collection1 and add collection3
634+ # Expected result: collection2 & collection3 associated to component and collection1 is excluded.
635+ new_modified_time = datetime (2024 , 8 , 8 , tzinfo = timezone .utc )
636+ with freeze_time (new_modified_time ):
637+ api .set_collections (
638+ self .learning_package .id ,
639+ self .draft_component .publishable_entity ,
640+ collection_qset = Collection .objects .filter (id__in = [
641+ self .collection3 .pk ,
642+ self .collection2 .pk ,
643+ ]),
644+ created_by = self .user .id ,
645+ )
646+ assert list (self .collection1 .entities .all ()) == [
647+ self .published_component .publishable_entity ,
648+ ]
649+ assert list (self .collection2 .entities .all ()) == [
650+ self .published_component .publishable_entity ,
651+ self .draft_component .publishable_entity ,
652+ ]
653+ assert list (self .collection3 .entities .all ()) == [
654+ self .draft_component .publishable_entity ,
655+ ]
656+ # update modified time of all three collections as they were all updated
657+ assert Collection .objects .get (id = self .collection1 .pk ).modified == new_modified_time
658+ # collection2 was unchanged, so it should have the same modified time as before
659+ assert Collection .objects .get (id = self .collection2 .pk ).modified != new_modified_time
660+ assert Collection .objects .get (id = self .collection3 .pk ).modified == new_modified_time
661+
662+ def test_set_collection_wrong_learning_package (self ):
663+ """
664+ We cannot set collections with a different learning package than the component.
665+ """
666+ learning_package_3 = api .create_learning_package (
667+ key = "ComponentTestCase-test-key-3" ,
668+ title = "Components Test Case Learning Package-3" ,
669+ )
670+
671+ with self .assertRaises (ValidationError ):
672+ api .set_collections (
673+ learning_package_3 .id ,
674+ self .draft_component .publishable_entity ,
675+ collection_qset = Collection .objects .filter (id__in = [
676+ self .collection1 .pk ,
677+ ]),
678+ created_by = self .user .id ,
679+ )
680+
681+ assert list (self .collection1 .entities .all ()) == [
682+ self .published_component .publishable_entity ,
683+ ]
0 commit comments