Exemple #1
0
def test_xblock_no_student_view():
    # Try to get a response. Will try to render via WorkbenchRuntime.render;
    # since no view is provided in the XBlock, will return a Fragment that
    # indicates there is no view available.
    client = Client()
    response = client.get("/view/xblockwithoutstudentview/")
    assert_true('No such view' in response.content)
def test_xblock_no_student_view():
    # Try to get a response. Will try to render via WorkbenchRuntime.render;
    # since no view is provided in the XBlock, will return a Fragment that
    # indicates there is no view available.
    client = Client()
    response = client.get("/view/xblockwithoutstudentview/")
    assert_true("No such view" in response.content)
Exemple #3
0
def test_xblock_with_handler():
    # Tests an XBlock that provides a handler, and has some simple
    # student state
    client = Client()

    # Initially, the data is the default.
    response = client.get("/view/testit/")
    response_content = response.content.decode('utf-8')

    expected = u"The data: %r." % u"def"
    assert_true(expected in response_content)

    parsed = response_content.split(':::')
    assert_equals(len(parsed), 3)
    handler_url = parsed[1]

    # Now change the data.
    response = client.post(handler_url, "{}", "text/json")
    the_data = json.loads(response.content)['the_data']
    assert_equals(the_data, "defx")

    # Change it again.
    response = client.post(handler_url, "{}", "text/json")
    the_data = json.loads(response.content)['the_data']
    assert_equals(the_data, "defxx")
    def test_validate_incorrect_inputs(self, ValidationMessage):
        xblock = self.make_xblock()

        data = Mock(href='http://youtube.com/watch?v=something')
        validation = Mock()
        validation.add = Mock()

        xblock.validate_field_data(validation, data)
        assert_true(validation.add.called)
Exemple #5
0
    def test_set_after_get_doesnt_save(self):
        with patch.object(self.field_data, 'set_many') as patched_set_many:

            self.set(self.get())
            self.block.save()
            assert_false(patched_set_many.called)

            self.set(self.new_value)
            self.block.save()
            assert_true(patched_set_many.called)
Exemple #6
0
    def test_set_after_get_doesnt_save(self):
        with patch.object(self.field_data, 'set_many') as patched_set_many:

            self.set(self.get())
            self.block.save()
            assert_false(patched_set_many.called)

            self.set(self.new_value)
            self.block.save()
            assert_true(patched_set_many.called)
Exemple #7
0
def test_db_model_keys():
    # Tests that updates to fields are properly recorded in the KeyValueStore,
    # and that the keys have been constructed correctly
    key_store = DictKeyValueStore()
    db_model = DbModel(key_store)
    runtime = Runtime(Mock(), db_model, [TestMixin])
    tester = runtime.construct_xblock_from_class(TestXBlock, ScopeIds('s0', 'TestXBlock', 'd0', 'u0'))

    assert_false(db_model.has(tester, 'not a field'))

    for field in tester.fields.values():
        new_value = 'new ' + field.name
        assert_false(db_model.has(tester, field.name))
        setattr(tester, field.name, new_value)

    # Write out the values
    tester.save()

    # Make sure everything saved correctly
    for field in tester.fields.values():
        assert_true(db_model.has(tester, field.name))

    def get_key_value(scope, user_id, block_scope_id, field_name):
        """Gets the value, from `key_store`, of a Key with the given values."""
        new_key = KeyValueStore.Key(scope, user_id, block_scope_id, field_name)
        return key_store.db_dict[new_key]

    # Examine each value in the database and ensure that keys were constructed correctly
    assert_equals('new content', get_key_value(Scope.content, None, 'd0', 'content'))
    assert_equals('new settings', get_key_value(Scope.settings, None, 'u0', 'settings'))
    assert_equals('new user_state', get_key_value(Scope.user_state, 's0', 'u0', 'user_state'))
    assert_equals('new preferences', get_key_value(Scope.preferences, 's0', 'TestXBlock', 'preferences'))
    assert_equals('new user_info', get_key_value(Scope.user_info, 's0', None, 'user_info'))
    assert_equals('new by_type', get_key_value(Scope(UserScope.NONE, BlockScope.TYPE), None, 'TestXBlock', 'by_type'))
    assert_equals('new for_all', get_key_value(Scope(UserScope.NONE, BlockScope.ALL), None, None, 'for_all'))
    assert_equals('new user_def', get_key_value(Scope(UserScope.ONE, BlockScope.DEFINITION), 's0', 'd0', 'user_def'))
    assert_equals('new agg_global', get_key_value(Scope(UserScope.ALL, BlockScope.ALL), None, None, 'agg_global'))
    assert_equals('new agg_type', get_key_value(Scope(UserScope.ALL, BlockScope.TYPE), None, 'TestXBlock', 'agg_type'))
    assert_equals('new agg_def', get_key_value(Scope(UserScope.ALL, BlockScope.DEFINITION), None, 'd0', 'agg_def'))
    assert_equals('new agg_usage', get_key_value(Scope.user_state_summary, None, 'u0', 'agg_usage'))
    assert_equals('new mixin_content', get_key_value(Scope.content, None, 'd0', 'mixin_content'))
    assert_equals('new mixin_settings', get_key_value(Scope.settings, None, 'u0', 'mixin_settings'))
    assert_equals('new mixin_user_state', get_key_value(Scope.user_state, 's0', 'u0', 'mixin_user_state'))
    assert_equals('new mixin_preferences', get_key_value(Scope.preferences, 's0', 'TestXBlock', 'mixin_preferences'))
    assert_equals('new mixin_user_info', get_key_value(Scope.user_info, 's0', None, 'mixin_user_info'))
    assert_equals('new mixin_by_type', get_key_value(Scope(UserScope.NONE, BlockScope.TYPE), None, 'TestXBlock', 'mixin_by_type'))
    assert_equals('new mixin_for_all', get_key_value(Scope(UserScope.NONE, BlockScope.ALL), None, None, 'mixin_for_all'))
    assert_equals('new mixin_user_def', get_key_value(Scope(UserScope.ONE, BlockScope.DEFINITION), 's0', 'd0', 'mixin_user_def'))
    assert_equals('new mixin_agg_global', get_key_value(Scope(UserScope.ALL, BlockScope.ALL), None, None, 'mixin_agg_global'))
    assert_equals('new mixin_agg_type', get_key_value(Scope(UserScope.ALL, BlockScope.TYPE), None, 'TestXBlock', 'mixin_agg_type'))
    assert_equals('new mixin_agg_def', get_key_value(Scope(UserScope.ALL, BlockScope.DEFINITION), None, 'd0', 'mixin_agg_def'))
    assert_equals('new mixin_agg_usage', get_key_value(Scope.user_state_summary, None, 'u0', 'mixin_agg_usage'))
Exemple #8
0
def test_db_model_keys():
    # Tests that updates to fields are properly recorded in the KeyValueStore,
    # and that the keys have been constructed correctly
    key_store = DictKeyValueStore()
    db_model = DbModel(key_store)
    runtime = Runtime(Mock(), db_model, [TestMixin])
    tester = runtime.construct_xblock_from_class(TestXBlock, ScopeIds('s0', 'TestXBlock', 'd0', 'u0'))

    assert_false(db_model.has(tester, 'not a field'))

    for field in tester.fields.values():
        new_value = 'new ' + field.name
        assert_false(db_model.has(tester, field.name))
        setattr(tester, field.name, new_value)

    # Write out the values
    tester.save()

    # Make sure everything saved correctly
    for field in tester.fields.values():
        assert_true(db_model.has(tester, field.name))

    def get_key_value(scope, user_id, block_scope_id, field_name):
        """Gets the value, from `key_store`, of a Key with the given values."""
        new_key = KeyValueStore.Key(scope, user_id, block_scope_id, field_name)
        return key_store.db_dict[new_key]

    # Examine each value in the database and ensure that keys were constructed correctly
    assert_equals('new content', get_key_value(Scope.content, None, 'd0', 'content'))
    assert_equals('new settings', get_key_value(Scope.settings, None, 'u0', 'settings'))
    assert_equals('new user_state', get_key_value(Scope.user_state, 's0', 'u0', 'user_state'))
    assert_equals('new preferences', get_key_value(Scope.preferences, 's0', 'TestXBlock', 'preferences'))
    assert_equals('new user_info', get_key_value(Scope.user_info, 's0', None, 'user_info'))
    assert_equals('new by_type', get_key_value(Scope(UserScope.NONE, BlockScope.TYPE), None, 'TestXBlock', 'by_type'))
    assert_equals('new for_all', get_key_value(Scope(UserScope.NONE, BlockScope.ALL), None, None, 'for_all'))
    assert_equals('new user_def', get_key_value(Scope(UserScope.ONE, BlockScope.DEFINITION), 's0', 'd0', 'user_def'))
    assert_equals('new agg_global', get_key_value(Scope(UserScope.ALL, BlockScope.ALL), None, None, 'agg_global'))
    assert_equals('new agg_type', get_key_value(Scope(UserScope.ALL, BlockScope.TYPE), None, 'TestXBlock', 'agg_type'))
    assert_equals('new agg_def', get_key_value(Scope(UserScope.ALL, BlockScope.DEFINITION), None, 'd0', 'agg_def'))
    assert_equals('new agg_usage', get_key_value(Scope.user_state_summary, None, 'u0', 'agg_usage'))
    assert_equals('new mixin_content', get_key_value(Scope.content, None, 'd0', 'mixin_content'))
    assert_equals('new mixin_settings', get_key_value(Scope.settings, None, 'u0', 'mixin_settings'))
    assert_equals('new mixin_user_state', get_key_value(Scope.user_state, 's0', 'u0', 'mixin_user_state'))
    assert_equals('new mixin_preferences', get_key_value(Scope.preferences, 's0', 'TestXBlock', 'mixin_preferences'))
    assert_equals('new mixin_user_info', get_key_value(Scope.user_info, 's0', None, 'mixin_user_info'))
    assert_equals('new mixin_by_type', get_key_value(Scope(UserScope.NONE, BlockScope.TYPE), None, 'TestXBlock', 'mixin_by_type'))
    assert_equals('new mixin_for_all', get_key_value(Scope(UserScope.NONE, BlockScope.ALL), None, None, 'mixin_for_all'))
    assert_equals('new mixin_user_def', get_key_value(Scope(UserScope.ONE, BlockScope.DEFINITION), 's0', 'd0', 'mixin_user_def'))
    assert_equals('new mixin_agg_global', get_key_value(Scope(UserScope.ALL, BlockScope.ALL), None, None, 'mixin_agg_global'))
    assert_equals('new mixin_agg_type', get_key_value(Scope(UserScope.ALL, BlockScope.TYPE), None, 'TestXBlock', 'mixin_agg_type'))
    assert_equals('new mixin_agg_def', get_key_value(Scope(UserScope.ALL, BlockScope.DEFINITION), None, 'd0', 'mixin_agg_def'))
    assert_equals('new mixin_agg_usage', get_key_value(Scope.user_state_summary, None, 'u0', 'mixin_agg_usage'))
def test_ugettext_calls():
    """
    Test ugettext calls in xblock.
    """
    runtime = TestRuntime()
    block = XBlockWithServices(runtime, scope_ids=Mock(spec=[]))
    assert_equals(block.ugettext('test'), u'test')
    assert_true(isinstance(block.ugettext('test'), unicode))

    # NoSuchServiceError exception should raise if i18n is none/empty.
    runtime = TestRuntime(services={'i18n': None})
    block = XBlockWithServices(runtime, scope_ids=Mock(spec=[]))
    with assert_raises(NoSuchServiceError):
        block.ugettext('test')
Exemple #10
0
def test_kv_store():
    # Simple test to makes sure we can get things in and out
    kvs = WorkbenchDjangoKeyValueStore()
    key = KeyValueStore.Key(scope=Scope.content,
                            user_id="rusty",
                            block_scope_id="my_scenario.my_block.d0",
                            field_name="age")

    assert_false(kvs.has(key))
    kvs.set(key, 7)
    assert_true(kvs.has(key))
    assert_equals(kvs.get(key), 7)
    kvs.delete(key)
    assert_false(kvs.has(key))
Exemple #11
0
def test_ugettext_calls():
    """
    Test ugettext calls in xblock.
    """
    runtime = TestRuntime()
    block = XBlockWithServices(runtime, scope_ids=Mock(spec=[]))
    assert_equals(block.ugettext('test'), 'test')
    assert_true(isinstance(block.ugettext('test'), six.text_type))

    # NoSuchServiceError exception should raise if i18n is none/empty.
    runtime = TestRuntime(services={
        'i18n': None
    })
    block = XBlockWithServices(runtime, scope_ids=Mock(spec=[]))
    with assert_raises(NoSuchServiceError):
        block.ugettext('test')
Exemple #12
0
def test_kv_store():
    # Simple test to makes sure we can get things in and out
    kvs = WorkbenchDjangoKeyValueStore()
    key = KeyValueStore.Key(
        scope=Scope.content,
        user_id="rusty",
        block_scope_id="my_scenario.my_block.d0",
        field_name="age"
    )

    assert_false(kvs.has(key))
    kvs.set(key, 7)
    assert_true(kvs.has(key))
    assert_equals(kvs.get(key), 7)
    kvs.delete(key)
    assert_false(kvs.has(key))
def test_xblock_with_handler():
    # Tests an XBlock that provides a handler, and has some simple
    # student state
    client = Client()

    # Initially, the data is the default.
    response = client.get("/view/testit/")
    assert_true("The data: 'def'." in response.content)
    parsed = response.content.split(":::")
    assert_equals(len(parsed), 3)
    handler_url = parsed[1]

    # Now change the data.
    response = client.post(handler_url, "{}", "text/json")
    the_data = json.loads(response.content)["the_data"]
    assert_equals(the_data, "defx")

    # Change it again.
    response = client.post(handler_url, "{}", "text/json")
    the_data = json.loads(response.content)["the_data"]
    assert_equals(the_data, "defxx")
Exemple #14
0
def test_setting_the_same_value_marks_field_as_dirty():
    """
    Check that setting field to the same value does not mark mutable fields as dirty.
    This might be an unexpected behavior though
    """
    class FieldTester(XBlock):
        """Test block for set - get test."""
        non_mutable = String(scope=Scope.settings)
        list_field = List(scope=Scope.settings)
        dict_field = Dict(scope=Scope.settings)

    runtime = TestRuntime(services={'field-data': DictFieldData({})})
    field_tester = FieldTester(runtime, scope_ids=Mock(spec=ScopeIds))

    # precondition checks
    assert_equals(len(field_tester._dirty_fields), 0)
    assert_false(field_tester.fields['list_field'].is_set_on(field_tester))
    assert_false(field_tester.fields['dict_field'].is_set_on(field_tester))
    assert_false(field_tester.fields['non_mutable'].is_set_on(field_tester))

    field_tester.non_mutable = field_tester.non_mutable
    field_tester.list_field = field_tester.list_field
    field_tester.dict_field = field_tester.dict_field

    assert_in(field_tester.fields['non_mutable'], field_tester._dirty_fields)
    assert_in(field_tester.fields['list_field'], field_tester._dirty_fields)
    assert_in(field_tester.fields['dict_field'], field_tester._dirty_fields)

    assert_true(field_tester.fields['non_mutable'].is_set_on(field_tester))
    assert_true(field_tester.fields['list_field'].is_set_on(field_tester))
    assert_true(field_tester.fields['dict_field'].is_set_on(field_tester))
Exemple #15
0
def test_db_model_keys():
    # Tests that updates to fields are properly recorded in the KeyValueStore,
    # and that the keys have been constructed correctly
    key_store = DictKeyValueStore()
    field_data = KvsFieldData(key_store)
    runtime = TestRuntime(Mock(), mixins=[TestMixin], services={"field-data": field_data})
    tester = runtime.construct_xblock_from_class(TestXBlock, ScopeIds("s0", "TestXBlock", "d0", "u0"))

    assert_false(field_data.has(tester, "not a field"))

    for field in tester.fields.values():
        new_value = "new " + field.name
        assert_false(field_data.has(tester, field.name))
        if isinstance(field, List):
            new_value = [new_value]
        setattr(tester, field.name, new_value)

    # Write out the values
    tester.save()

    # Make sure everything saved correctly
    for field in tester.fields.values():
        assert_true(field_data.has(tester, field.name))

    def get_key_value(scope, user_id, block_scope_id, field_name):
        """Gets the value, from `key_store`, of a Key with the given values."""
        new_key = KeyValueStore.Key(scope, user_id, block_scope_id, field_name)
        return key_store.db_dict[new_key]

    # Examine each value in the database and ensure that keys were constructed correctly
    assert_equals("new content", get_key_value(Scope.content, None, "d0", "content"))
    assert_equals("new settings", get_key_value(Scope.settings, None, "u0", "settings"))
    assert_equals("new user_state", get_key_value(Scope.user_state, "s0", "u0", "user_state"))
    assert_equals("new preferences", get_key_value(Scope.preferences, "s0", "TestXBlock", "preferences"))
    assert_equals("new user_info", get_key_value(Scope.user_info, "s0", None, "user_info"))
    assert_equals("new by_type", get_key_value(Scope(UserScope.NONE, BlockScope.TYPE), None, "TestXBlock", "by_type"))
    assert_equals("new for_all", get_key_value(Scope(UserScope.NONE, BlockScope.ALL), None, None, "for_all"))
    assert_equals("new user_def", get_key_value(Scope(UserScope.ONE, BlockScope.DEFINITION), "s0", "d0", "user_def"))
    assert_equals("new agg_global", get_key_value(Scope(UserScope.ALL, BlockScope.ALL), None, None, "agg_global"))
    assert_equals("new agg_type", get_key_value(Scope(UserScope.ALL, BlockScope.TYPE), None, "TestXBlock", "agg_type"))
    assert_equals("new agg_def", get_key_value(Scope(UserScope.ALL, BlockScope.DEFINITION), None, "d0", "agg_def"))
    assert_equals("new agg_usage", get_key_value(Scope.user_state_summary, None, "u0", "agg_usage"))
    assert_equals("new mixin_content", get_key_value(Scope.content, None, "d0", "mixin_content"))
    assert_equals("new mixin_settings", get_key_value(Scope.settings, None, "u0", "mixin_settings"))
    assert_equals("new mixin_user_state", get_key_value(Scope.user_state, "s0", "u0", "mixin_user_state"))
    assert_equals("new mixin_preferences", get_key_value(Scope.preferences, "s0", "TestXBlock", "mixin_preferences"))
    assert_equals("new mixin_user_info", get_key_value(Scope.user_info, "s0", None, "mixin_user_info"))
    assert_equals(
        "new mixin_by_type", get_key_value(Scope(UserScope.NONE, BlockScope.TYPE), None, "TestXBlock", "mixin_by_type")
    )
    assert_equals(
        "new mixin_for_all", get_key_value(Scope(UserScope.NONE, BlockScope.ALL), None, None, "mixin_for_all")
    )
    assert_equals(
        "new mixin_user_def", get_key_value(Scope(UserScope.ONE, BlockScope.DEFINITION), "s0", "d0", "mixin_user_def")
    )
    assert_equals(
        "new mixin_agg_global", get_key_value(Scope(UserScope.ALL, BlockScope.ALL), None, None, "mixin_agg_global")
    )
    assert_equals(
        "new mixin_agg_type", get_key_value(Scope(UserScope.ALL, BlockScope.TYPE), None, "TestXBlock", "mixin_agg_type")
    )
    assert_equals(
        "new mixin_agg_def", get_key_value(Scope(UserScope.ALL, BlockScope.DEFINITION), None, "d0", "mixin_agg_def")
    )
    assert_equals("new mixin_agg_usage", get_key_value(Scope.user_state_summary, None, "u0", "mixin_agg_usage"))
 def test_delete_with_save_writes(self):
     self.delete()
     self.block.save()
     assert_false(self.field_data.has(self.block, 'field'))
     assert_true(self.is_default())
Exemple #17
0
 def test_delete_with_save_writes(self):
     self.delete()
     self.block.save()
     assert_false(self.field_data.has(self.block, 'field'))
     assert_true(self.is_default())