Skip to content

Commit 90d0e53

Browse files
fix: get_containers_with_entity() was returning duplicate results (#304)
1 parent 0bb1b61 commit 90d0e53

File tree

2 files changed

+10
-11
lines changed
  • openedx_learning/apps/authoring/publishing
  • tests/openedx_learning/apps/authoring/units

2 files changed

+10
-11
lines changed

openedx_learning/apps/authoring/publishing/api.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,23 +1370,16 @@ def get_containers_with_entity(
13701370
ignore_pinned: if true, ignore any pinned references to the entity.
13711371
"""
13721372
if ignore_pinned:
1373-
# TODO: Do we need to run distinct() on this? Will fix in https://github.com/openedx/openedx-learning/issues/303
13741373
qs = Container.objects.filter(
13751374
# Note: these two conditions must be in the same filter() call, or the query won't be correct.
13761375
publishable_entity__draft__version__containerversion__entity_list__entitylistrow__entity_id=publishable_entity_pk, # pylint: disable=line-too-long # noqa: E501
13771376
publishable_entity__draft__version__containerversion__entity_list__entitylistrow__entity_version_id=None, # pylint: disable=line-too-long # noqa: E501
1378-
).order_by("pk") # Ordering is mostly for consistent test cases.
1377+
)
13791378
else:
13801379
qs = Container.objects.filter(
13811380
publishable_entity__draft__version__containerversion__entity_list__entitylistrow__entity_id=publishable_entity_pk, # pylint: disable=line-too-long # noqa: E501
1382-
).order_by("pk") # Ordering is mostly for consistent test cases.
1383-
# Could alternately do this query in two steps. Not sure which is more efficient; depends on how the DB plans it.
1384-
# # Find all the EntityLists that contain the given entity:
1385-
# lists = EntityList.objects.filter(entitylistrow__entity_id=publishable_entity_pk).values_list('pk', flat=True)
1386-
# qs = Container.objects.filter(
1387-
# publishable_entity__draft__version__containerversion__entity_list__in=lists
1388-
# )
1389-
return qs
1381+
)
1382+
return qs.order_by("pk").distinct() # Ordering is mostly for consistent test cases.
13901383

13911384

13921385
def get_container_children_count(

tests/openedx_learning/apps/authoring/units/test_api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,11 @@ def test_units_containing(self):
968968
# Units 5/6 don't contain it
969969
_unit5_no = self.create_unit_with_components([self.component_2_v1, self.component_2], key="u5")
970970
_unit6_no = self.create_unit_with_components([], key="u6")
971+
# To test unique results, unit 7 ✅ contains several copies of component 1. Also tests matching against
972+
# components that aren't in the first position.
973+
unit7_several = self.create_unit_with_components([
974+
self.component_2, self.component_1, self.component_1_v1, self.component_1,
975+
], key="u7")
971976

972977
# No need to publish anything as the get_containers_with_entity() API only considers drafts (for now).
973978

@@ -980,6 +985,7 @@ def test_units_containing(self):
980985
unit1_1pinned,
981986
unit2_1pinned_v2,
982987
unit4_unpinned,
988+
unit7_several, # This should only appear once, not several times.
983989
]
984990

985991
# Test retrieving only "unpinned", for cases like potential deletion of a component, where we wouldn't care
@@ -990,7 +996,7 @@ def test_units_containing(self):
990996
c.unit for c in
991997
authoring_api.get_containers_with_entity(self.component_1.pk, ignore_pinned=True).select_related("unit")
992998
]
993-
assert result2 == [unit4_unpinned]
999+
assert result2 == [unit4_unpinned, unit7_several]
9941000

9951001
def test_add_remove_container_children(self):
9961002
"""

0 commit comments

Comments
 (0)