Skip to content

Commit 42498dd

Browse files
WIP
1 parent fc1a758 commit 42498dd

4 files changed

Lines changed: 50 additions & 76 deletions

File tree

src/openedx_content/applets/units/admin.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/openedx_content/applets/units/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
This module provides functions to manage units.
44
"""
5+
56
from dataclasses import dataclass
67
from functools import partial
78

@@ -23,12 +24,14 @@
2324
create_unit = partial(publishing_api.create_container, container_type=Unit)
2425
create_unit_and_version = partial(publishing_api.create_container_and_version, container_type=Unit)
2526

27+
2628
@dataclass(frozen=True)
2729
class UnitListEntry:
2830
"""
2931
[ 🛑 UNSTABLE ]
3032
Data about a single entity in a container, e.g. a component in a unit.
3133
"""
34+
3235
component_version: ComponentVersion
3336
pinned: bool = False
3437

src/openedx_content/applets/units/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ class Unit(ContainerTypeImplementation):
2626
@classmethod
2727
def validate_entity(cls, entity: PublishableEntity) -> None:
2828
"""Check if the given entity is allowed as a child of a Unit"""
29-
try:
30-
getattr(entity, "component")
31-
except PublishableEntity.component.RelatedObjectDoesNotExist:
32-
raise
29+
# Units only allow Components as children, so the entity must be 1:1 with Component:
30+
getattr(entity, "component") # Could raise PublishableEntity.component.RelatedObjectDoesNotExist
3331

3432
# validate settings
3533

tests/openedx_content/applets/units/test_api.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Basic tests for the units API.
33
"""
4+
45
from unittest.mock import patch
56

67
import ddt # type: ignore[import]
@@ -18,7 +19,7 @@
1819

1920
@ddt.ddt
2021
class 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

Comments
 (0)