Commit d5d7d654 by Nimisha Asthagiri

fixup! Split Test Transformer fixes.

parent 2021ba63
......@@ -10,7 +10,6 @@ from .transformers import (
start_date,
user_partitions,
visibility,
split_test,
library_content,
)
from .user_info import CourseUserInfo
......
......@@ -46,7 +46,8 @@ class SplitTestTransformer(BlockStructureTransformer):
# set group access for each child
for child_location in xblock.children:
child = block_structure.get_xblock(child_location)
child.group_access[partition_for_this_block.id] = [child_to_group[child_location]]
group = child_to_group.get(child_location, None)
child.group_access[partition_for_this_block.id] = [group] if group else []
def transform(self, user_info, block_structure):
"""
......
......@@ -25,6 +25,13 @@ class CourseStructureTestCase(ModuleStoreTestCase):
self.user = UserFactory.create(password=self.password)
self.staff = UserFactory.create(password=self.password, is_staff=True)
def create_block_id(self, block_type, block_ref):
"""
Returns the block id (display name) that is used in the test
course structures for the given block type and block reference string.
"""
return '{}_{}'.format(block_type, block_ref)
def build_xblock(self, block_hierarchy, block_map, parent):
"""
Build an XBlock, add it to block_map, and call build_xblock on the
......@@ -47,7 +54,7 @@ class CourseStructureTestCase(ModuleStoreTestCase):
kwargs['parent'] = parent
xblock = factory.create(
display_name='{} {}'.format(block_type, block_ref),
display_name=self.create_block_id(block_type, block_ref),
publish_item=True,
**kwargs
)
......@@ -63,15 +70,35 @@ class CourseStructureTestCase(ModuleStoreTestCase):
The additional parents are obtained from the '#parents' field
and is expected to be a list of '#ref' values of the parents.
Note: if a '#parents' field is found, the block is removed from
the course block since it is expected to not belong to the root.
If the block is meant to be a direct child of the course as well,
the course should be explicitly listed in '#parents'.
Arguments:
block_hierarchy (BlockStructureDict): Definition of block hierarchy.
block_map (dict[str: XBlock]): Mapping from '#ref' values to their XBlocks.
"""
parents = block_hierarchy.get('#parents', [])
for parent_ref in parents:
parent_block = block_map[parent_ref]
parent_block.children.append(
block_map[block_hierarchy['#ref']].location
)
update_block(parent_block)
if parents:
block_key = block_map[block_hierarchy['#ref']].location
# First remove the block from the course.
# It would be re-added to the course if the course was
# explicitly listed in parents.
course = modulestore().get_item(block_map['course'].location)
course.children.remove(block_key)
block_map['course'] = update_block(course)
# Add this to block to each listed parent.
for parent_ref in parents:
parent_block = modulestore().get_item(block_map[parent_ref].location)
parent_block.children.append(block_key)
block_map[parent_ref] = update_block(parent_block)
# recursively call the children
for child_hierarchy in block_hierarchy.get('#children', []):
self.add_parents(child_hierarchy, block_map)
......@@ -278,3 +305,11 @@ def update_block(block):
Helper method to update the block in the modulestore
"""
return modulestore().update_item(block, 'test_user')
def create_location(org, course, run, block_type, block_id):
"""
Returns the usage key for the given key parameters using the
default modulestore
"""
return modulestore().make_course_key(org, course, run).make_usage_key(block_type, block_id)
\ No newline at end of file
......@@ -2,6 +2,8 @@
Tests for UserPartitionTransformer.
"""
import ddt
from openedx.core.djangoapps.course_groups.partition_scheme import CohortPartitionScheme
from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory, config_course_cohorts
from openedx.core.djangoapps.course_groups.cohorts import add_user_to_cohort
......@@ -10,10 +12,11 @@ from student.tests.factories import CourseEnrollmentFactory
from xmodule.partitions.partitions import Group, UserPartition
from course_blocks.transformers.user_partitions import UserPartitionTransformer
from course_blocks.api import get_course_blocks, clear_course_from_cache
from course_blocks.api import get_course_blocks
from lms.djangoapps.course_blocks.transformers.tests.test_helpers import CourseStructureTestCase
@ddt.ddt
class UserPartitionTransformerTestCase(CourseStructureTestCase):
"""
UserPartitionTransformer Test
......@@ -57,8 +60,6 @@ class UserPartitionTransformerTestCase(CourseStructureTestCase):
group.id,
)
add_user_to_cohort(self.cohorts[0], self.user.username)
self.transformer = UserPartitionTransformer()
def get_course_hierarchy(self):
......@@ -159,11 +160,20 @@ class UserPartitionTransformerTestCase(CourseStructureTestCase):
},
]
def test_user_assigned(self):
@ddt.data(
(None, ('course', 'B', 'O')),
(1, ('course', 'A', 'B', 'C', 'E', 'F', 'G', 'J', 'L', 'M', 'O')),
(2, ('course', 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J', 'M', 'O')),
(3, ('course', 'A', 'B', 'D', 'E', 'I', 'J', 'O')),
(4, ('course', 'B', 'O')),
)
@ddt.unpack
def test_user_assigned(self, group_id, expected_blocks):
"""
Test when user is assigned to group in user partition.
"""
# TODO ddt with testing user in different groups
if group_id:
add_user_to_cohort(self.cohorts[group_id-1], self.user.username)
trans_block_structure = get_course_blocks(
self.user,
......@@ -172,7 +182,7 @@ class UserPartitionTransformerTestCase(CourseStructureTestCase):
)
self.assertSetEqual(
set(trans_block_structure.get_block_keys()),
self.get_block_key_set(self.blocks, 'course', 'A', 'B', 'C', 'E', 'F', 'G', 'J', 'L', 'M', 'O')
self.get_block_key_set(self.blocks, *expected_blocks)
)
def test_staff_user(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment