def test_blocks_to_mark_complete_on_view(self): completable_block_1 = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) usage_id_1 = UsageKey.from_string("i4x://edX/100/a/1").replace(course_key=self.course_key) completable_block_1.scope_ids.usage_id = usage_id_1 completable_block_2 = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) usage_id_2 = UsageKey.from_string("i4x://edX/100/a/2").replace(course_key=self.course_key) completable_block_2.scope_ids.usage_id = usage_id_2 aggregator_block = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) usage_id_3 = UsageKey.from_string("i4x://edX/100/a/3").replace(course_key=self.course_key) aggregator_block.scope_ids.usage_id = usage_id_3 aggregator_block.completion_mode = XBlockCompletionMode.AGGREGATOR self.assertEqual(self.completion_service.blocks_to_mark_complete_on_view([]), []) self.assertEqual( self.completion_service.blocks_to_mark_complete_on_view([aggregator_block]), [] ) self.assertEqual( self.completion_service.blocks_to_mark_complete_on_view( [completable_block_1, completable_block_2, aggregator_block] ), [completable_block_1, completable_block_2] ) BlockCompletion.objects.submit_completion( user=self.user, block_key=completable_block_2.scope_ids.usage_id, completion=1.0 ) self.assertEqual( self.completion_service.blocks_to_mark_complete_on_view( [completable_block_1, completable_block_2, aggregator_block] ), [completable_block_1] ) BlockCompletion.objects.submit_completion( user=self.user, block_key=completable_block_1.scope_ids.usage_id, completion=1.0 ) self.assertEqual( self.completion_service.blocks_to_mark_complete_on_view( [completable_block_1, completable_block_2, aggregator_block] ), [] )
def test_class_tags(): xblock = XBlock(None, None, None) assert xblock._class_tags == set() # pylint: disable=comparison-with-callable class Sub1Block(XBlock): """Toy XBlock""" sub1block = Sub1Block(None, None, None) assert sub1block._class_tags == set() @XBlock.tag("cat dog") class Sub2Block(Sub1Block): """Toy XBlock""" sub2block = Sub2Block(None, None, None) assert sub2block._class_tags == set(["cat", "dog"]) class Sub3Block(Sub2Block): """Toy XBlock""" sub3block = Sub3Block(None, None, None) assert sub3block._class_tags == set(["cat", "dog"]) @XBlock.tag("mixin") class MixinBlock(XBlock): """Toy XBlock""" class Sub4Block(MixinBlock, Sub3Block): """Toy XBlock""" sub4block = Sub4Block(None, None, None) assert sub4block._class_tags == set(["cat", "dog", "mixin"])
def test_blocks_to_mark_complete_on_view(self): completable_block_1 = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) completable_block_1.location = UsageKey.from_string( "i4x://edX/100/a/1").replace(course_key=self.course_key) completable_block_2 = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) completable_block_2.location = UsageKey.from_string( "i4x://edX/100/a/2").replace(course_key=self.course_key) aggregator_block = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) aggregator_block.location = UsageKey.from_string( "i4x://edX/100/a/3").replace(course_key=self.course_key) aggregator_block.completion_mode = XBlockCompletionMode.AGGREGATOR self.assertSetEqual( self.completion_service.blocks_to_mark_complete_on_view({}), set()) self.assertSetEqual( self.completion_service.blocks_to_mark_complete_on_view( {aggregator_block}), set()) self.assertSetEqual( self.completion_service.blocks_to_mark_complete_on_view( {completable_block_1, completable_block_2, aggregator_block}), {completable_block_1, completable_block_2}) BlockCompletion.objects.submit_completion( user=self.user, course_key=self.course_key, block_key=completable_block_2.location, completion=1.0) self.assertSetEqual( self.completion_service.blocks_to_mark_complete_on_view( {completable_block_1, completable_block_2, aggregator_block}), {completable_block_1}) BlockCompletion.objects.submit_completion( user=self.user, course_key=self.course_key, block_key=completable_block_1.location, completion=1.0) self.assertSetEqual( self.completion_service.blocks_to_mark_complete_on_view( {completable_block_1, completable_block_2, aggregator_block}), set())
def test_can_mark_block_complete_on_view(self, mode, has_score, has_custom_completion, can_mark_complete): block = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) block.completion_mode = mode block.has_score = has_score if has_custom_completion: block.has_custom_completion = True self.assertEqual(self.completion_service.can_mark_block_complete_on_view(block), can_mark_complete)
def test_submit_completion(self): completable_block = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) completable_block.location = UsageKey.from_string( "i4x://edX/100/a/1").replace(course_key=self.course_key) service = CompletionService(self.user, self.course_key) service.submit_completion(completable_block.location, 0.75) self.assertEqual( BlockCompletion.objects.get( block_key=completable_block.location).completion, 0.75)
def test_handle_shortcut(): runtime = Mock(spec=['handle']) scope_ids = Mock(spec=[]) request = Mock(spec=[]) block = XBlock(runtime, None, scope_ids) block.handle('handler_name', request) runtime.handle.assert_called_with(block, 'handler_name', request, '') runtime.handle.reset_mock() block.handle('handler_name', request, 'suffix') runtime.handle.assert_called_with(block, 'handler_name', request, 'suffix')
def test_submit_group_completion_by_user_ids(self): third_user = UserFactory.create() completable_block = XBlock(Mock(), scope_ids=Mock(spec=ScopeIds)) completable_block.location = UsageKey.from_string("i4x://edX/100/a/1").replace(course_key=self.course_key) service = CompletionService(self.user, self.course_key) service.submit_group_completion( block_key=completable_block.location, completion=0.25, user_ids=[self.other_user.id, third_user.id], ) completions = list(BlockCompletion.objects.filter(block_key=completable_block.location)) self.assertEqual(len(completions), 2) self.assertTrue(all(bc.completion == 0.25 for bc in completions)) self.assertEqual({bc.user for bc in completions}, {self.other_user, third_user})
def test_services_decorators(): # pylint: disable=E1101 # A default XBlock has requested no services xblock = XBlock(None, None, None) assert_equals(XBlock._services_requested, {}) assert_equals(xblock._services_requested, {}) @XBlock.needs("n") @XBlock.wants("w") class ServiceUsingBlock(XBlock): """XBlock using some services.""" pass service_using_block = ServiceUsingBlock(None, scope_ids=None) assert_equals(ServiceUsingBlock._services_requested, {'n': 'need', 'w': 'want'}) assert_equals(service_using_block._services_requested, {'n': 'need', 'w': 'want'})
def test_services_decorators(): # pylint: disable=E1101 # A default XBlock has requested no services xblock = XBlock(None, None, None) assert XBlock._services_requested == {} assert xblock._services_requested == {} # pylint: disable=comparison-with-callable @XBlock.needs("n") @XBlock.wants("w") class ServiceUsingBlock(XBlock): """XBlock using some services.""" service_using_block = ServiceUsingBlock(None, scope_ids=None) assert ServiceUsingBlock._services_requested == {'n': 'need', 'w': 'want'} assert service_using_block._services_requested == { 'n': 'need', 'w': 'want' }
def test_class_tags(): xblock = XBlock(None, None, None) assert_equals(xblock._class_tags, set()) class Sub1Block(XBlock): """Toy XBlock""" pass sub1block = Sub1Block(None, None, None) assert_equals(sub1block._class_tags, set()) @XBlock.tag("cat dog") class Sub2Block(Sub1Block): """Toy XBlock""" pass sub2block = Sub2Block(None, None, None) assert_equals(sub2block._class_tags, set(["cat", "dog"])) class Sub3Block(Sub2Block): """Toy XBlock""" pass sub3block = Sub3Block(None, None, None) assert_equals(sub3block._class_tags, set(["cat", "dog"])) @XBlock.tag("mixin") class MixinBlock(XBlock): """Toy XBlock""" pass class Sub4Block(MixinBlock, Sub3Block): """Toy XBlock""" pass sub4block = Sub4Block(None, None, None) assert_equals(sub4block._class_tags, set(["cat", "dog", "mixin"]))
def test_assign_field_data(self): field_data = Mock(spec=FieldData) block = XBlock(Mock(spec=Runtime), scope_ids=Mock(spec=ScopeIds)) with self.assertWarns(FieldDataDeprecationWarning): block._field_data = field_data self.assertEqual(field_data, block._field_data)
def test_field_data_paramater(self): field_data = Mock(spec=FieldData) with self.assertWarns(FieldDataDeprecationWarning): block = XBlock(Mock(spec=Runtime), field_data, Mock(spec=ScopeIds)) self.assertEqual(field_data, block._field_data)
""" {{cookiecutter.repo_name}}XBlock main Python class VERSION: {{cookiecutter.version}} {{cookiecutter.description}} xblock-tutorial: http://edx.readthedocs.org/projects/xblock-tutorial/en/latest/concepts/index.html """ import pkg_resources from django.template import Context, Template from xblock.core import XBlock from xblock.fields import Scope, Integer, String, Boolean #List, Set, Float, Dict from xblock.fragment import Fragment class {{cookiecutter.repo_name}}XBlock(XBlock): ''' Icon of the XBlock. Values : [other (default), video, problem] ''' icon_class = "video" ''' Fields #scope * Scope.content : Block definition / No user * Scope.settings : Block usage / No user * Scope.user_state : Block usage / One user * Scope.preferences : Block type / One user * Scope.user_info : All blocks / One user * Scope.user_state_summary : Block usage / All users