11"""
22Basic tests for the units API.
33"""
4+
45from unittest .mock import patch
56
67import ddt # type: ignore[import]
1819
1920@ddt .ddt
2021class UnitTestCase (ComponentTestCase ):
21- """ Test cases for Units (containers of components) """
22+ """Test cases for Units (containers of components)"""
2223
2324 def setUp (self ) -> None :
2425 super ().setUp ()
@@ -31,10 +32,10 @@ def setUp(self) -> None:
3132 title = "Querying Counting Problem (2)" ,
3233 )
3334
34- def create_component (self , * , title : str = "Test Component" , key : str = "component:1" ) -> tuple [
35- authoring_models . Component , authoring_models . ComponentVersion
36- ]:
37- """ Helper method to quickly create a component """
35+ def create_component (
36+ self , * , title : str = "Test Component" , key : str = "component:1"
37+ ) -> tuple [ authoring_models . Component , authoring_models . ComponentVersion ]:
38+ """Helper method to quickly create a component"""
3839 return content_api .create_component_and_version (
3940 self .learning_package .id ,
4041 component_type = self .problem_type ,
@@ -51,7 +52,7 @@ def create_unit_with_components(
5152 title = "Unit" ,
5253 key = "unit:key" ,
5354 ) -> authoring_models .Unit :
54- """ Helper method to quickly create a unit with some components """
55+ """Helper method to quickly create a unit with some components"""
5556 unit , _unit_v1 = content_api .create_unit_and_version (
5657 learning_package_id = self .learning_package .id ,
5758 key = key ,
@@ -232,7 +233,10 @@ def test_add_corrupted_component(self):
232233 self .component_1_v1 .publishable_entity_version .refresh_from_db () # No error
233234 self .component_1_v1 .publishable_entity_version .entity .refresh_from_db () # No error
234235 # Now add this corrupted component, pinned to v1:
235- with pytest .raises (ValidationError , match = 'The entity "xblock.v1:problem:Query Counting" cannot be added to a "unit" container.' ):
236+ with pytest .raises (
237+ ValidationError ,
238+ match = 'The entity "xblock.v1:problem:Query Counting" cannot be added to a "unit" container.' ,
239+ ):
236240 self .create_unit_with_components ([self .component_1_v1 ])
237241
238242 def test_create_empty_unit_and_version (self ):
@@ -524,10 +528,14 @@ def test_create_two_units_with_same_components(self):
524528
525529 # Check that the contents are as expected:
526530 assert [row .component_version for row in content_api .get_components_in_unit (unit1 , published = False )] == [
527- self .component_2_v1 , self .component_2_v1 , self .component_1_v1 ,
531+ self .component_2_v1 ,
532+ self .component_2_v1 ,
533+ self .component_1_v1 ,
528534 ]
529535 assert [row .component_version for row in content_api .get_components_in_unit (unit2 , published = False )] == [
530- self .component_1_v1 , self .component_2_v1 , self .component_1_v1 ,
536+ self .component_1_v1 ,
537+ self .component_2_v1 ,
538+ self .component_1_v1 ,
531539 ]
532540
533541 # Modify component 1
@@ -699,7 +707,7 @@ def test_cannot_add_soft_deleted_component(self, publish_first):
699707 self .create_unit_with_components ([component ])
700708
701709 def test_removing_component (self ):
702- """ Test removing a component from a unit (but not deleting it) """
710+ """Test removing a component from a unit (but not deleting it)"""
703711 unit = self .create_unit_with_components ([self .component_1 , self .component_2 ])
704712 content_api .publish_all_drafts (self .learning_package .id )
705713
@@ -739,7 +747,7 @@ def test_removing_component(self):
739747 ]
740748
741749 def test_soft_deleting_component (self ):
742- """ Test soft deleting a component that's in a unit (but not removing it) """
750+ """Test soft deleting a component that's in a unit (but not removing it)"""
743751 unit = self .create_unit_with_components ([self .component_1 , self .component_2 ])
744752 content_api .publish_all_drafts (self .learning_package .id )
745753
@@ -770,7 +778,7 @@ def test_soft_deleting_component(self):
770778 ]
771779
772780 def test_soft_deleting_and_removing_component (self ):
773- """ Test soft deleting a component that's in a unit AND removing it """
781+ """Test soft deleting a component that's in a unit AND removing it"""
774782 unit = self .create_unit_with_components ([self .component_1 , self .component_2 ])
775783 content_api .publish_all_drafts (self .learning_package .id )
776784
@@ -806,7 +814,7 @@ def test_soft_deleting_and_removing_component(self):
806814 ]
807815
808816 def test_soft_deleting_pinned_component (self ):
809- """ Test soft deleting a pinned 📌 component that's in a unit """
817+ """Test soft deleting a pinned 📌 component that's in a unit"""
810818 unit = self .create_unit_with_components ([self .component_1_v1 , self .component_2_v1 ])
811819 content_api .publish_all_drafts (self .learning_package .id )
812820
@@ -951,17 +959,28 @@ def test_units_containing(self):
951959 # Unit 3 doesn't contain it
952960 _unit3_no = self .create_unit_with_components ([self .component_2 ], key = "u3" )
953961 # Unit 4 ✅ has component 1, unpinned
954- unit4_unpinned = self .create_unit_with_components ([
955- self .component_1 , self .component_2 , self .component_2_v1 ,
956- ], key = "u4" )
962+ unit4_unpinned = self .create_unit_with_components (
963+ [
964+ self .component_1 ,
965+ self .component_2 ,
966+ self .component_2_v1 ,
967+ ],
968+ key = "u4" ,
969+ )
957970 # Units 5/6 don't contain it
958971 _unit5_no = self .create_unit_with_components ([self .component_2_v1 , self .component_2 ], key = "u5" )
959972 _unit6_no = self .create_unit_with_components ([], key = "u6" )
960973 # To test unique results, unit 7 ✅ contains several copies of component 1. Also tests matching against
961974 # components that aren't in the first position.
962- unit7_several = self .create_unit_with_components ([
963- self .component_2 , self .component_1 , self .component_1_v1 , self .component_1 ,
964- ], key = "u7" )
975+ unit7_several = self .create_unit_with_components (
976+ [
977+ self .component_2 ,
978+ self .component_1 ,
979+ self .component_1_v1 ,
980+ self .component_1 ,
981+ ],
982+ key = "u7" ,
983+ )
965984
966985 # No need to publish anything as the get_containers_with_entity() API only considers drafts (for now).
967986
@@ -986,11 +1005,13 @@ def test_get_components_in_unit_queries(self):
9861005 Test the query count of get_components_in_unit()
9871006 This also tests the generic method get_entities_in_container()
9881007 """
989- unit = self .create_unit_with_components ([
990- self .component_1 ,
991- self .component_2 ,
992- self .component_2_v1 ,
993- ])
1008+ unit = self .create_unit_with_components (
1009+ [
1010+ self .component_1 ,
1011+ self .component_2 ,
1012+ self .component_2_v1 ,
1013+ ]
1014+ )
9941015 with self .assertNumQueries (3 ):
9951016 result = content_api .get_components_in_unit (unit , published = False )
9961017 assert result == [
0 commit comments