def test_invalid_scopes(self):
        for scope in (Scope(user=True, block=BlockScope.DEFINITION),
                      Scope(user=False, block=BlockScope.TYPE),
                      Scope(user=False, block=BlockScope.ALL)):
            key = DjangoKeyValueStore.Key(scope, None, None, 'field')

            self.assertRaises(InvalidScopeError, self.kvs.get, key)
            self.assertRaises(InvalidScopeError, self.kvs.set, key, 'value')
            self.assertRaises(InvalidScopeError, self.kvs.delete, key)
            self.assertRaises(InvalidScopeError, self.kvs.has, key)
            self.assertRaises(InvalidScopeError, self.kvs.set_many, {key: 'value'})
Exemple #2
0
def generate_fields(cls):
    """
    A class decorator that generates fields for all field scopes.
    These fields are Dicts, and map usage_ids to values.
    """
    for scope in Scope.scopes():
        setattr(cls, scope.name, Dict(
            help="Values stored in the {} scope".format(scope),
            scope=scope
        ))

    return cls
Exemple #3
0
def generate_fields(cls):
    """
    A class decorator that generates fields for all field scopes.
    These fields are Dicts, and map usage_ids to values.
    """
    for scope in Scope.scopes():
        setattr(
            cls, scope.name,
            Dict(help="Values stored in the {} scope".format(scope),
                 scope=scope))

    return cls
Exemple #4
0
class TestXBlockNoFallback(XBlock):
    """
    Set up a class that contains ModelTypes as fields, but no views or handlers
    """
    content = String(scope=Scope.content, default='c')
    settings = String(scope=Scope.settings, default='s')
    user_state = String(scope=Scope.user_state, default='ss')
    preferences = String(scope=Scope.preferences, default='sp')
    user_info = String(scope=Scope.user_info, default='si')
    by_type = String(scope=Scope(UserScope.NONE, BlockScope.TYPE),
                     default='bt')
    for_all = String(scope=Scope(UserScope.NONE, BlockScope.ALL), default='fa')
    user_def = String(scope=Scope(UserScope.ONE, BlockScope.DEFINITION),
                      default='sd')
    agg_global = String(scope=Scope(UserScope.ALL, BlockScope.ALL),
                        default='ag')
    agg_type = String(scope=Scope(UserScope.ALL, BlockScope.TYPE),
                      default='at')
    agg_def = String(scope=Scope(UserScope.ALL, BlockScope.DEFINITION),
                     default='ad')
    agg_usage = String(scope=Scope.user_state_summary, default='au')

    def handler_without_correct_decoration(self, request, suffix=''):
        """a handler which is missing the @XBlock.handler decoration."""
        pass
Exemple #5
0
    def fallback_view(self, view_name, context=None):               # pylint: disable=W0613
        """
        This view is used by the Acid XBlock to test various features of
        the runtime it is contained in
        """
        block_template = self.resource_string("static/html/acid.html")
        storage_test_template = self.resource_string('static/html/scope_storage_test.html')

        scopes = (
            scope
            for scope in Scope.scopes()
            if (view_name not in self.enabled_fields
                or scope.name in self.enabled_fields[view_name])
        )

        scope_test_contexts = []
        for scope in scopes:
            try:
                scope_test_contexts.append(self.setup_storage(scope.name))
            except Exception as exc:
                logging.warning('Unable to use scope in acid test', exc_info=True)

        frag = Fragment(block_template.format(
            error_class=self.ERROR_CLASS,
            success_class=self.SUCCESS_CLASS,
            failure_class=self.FAILURE_CLASS,
            unknown_class=self.UNKNOWN_CLASS,
            storage_tests='\n'.join(
                storage_test_template.format(
                    alt="alt" if idx % 2 else "",
                    unknown_class=self.UNKNOWN_CLASS,
                    **context
                )
                for idx, context in enumerate(scope_test_contexts)
            ),
        ))

        frag.add_javascript(self.resource_string("static/js/jquery.ajaxq-0.0.1.js"))
        frag.add_javascript(self.resource_string('static/js/acid.js'))
        frag.add_css(self.resource_string("static/css/acid.css"))
        frag.add_css_url('//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css')
        frag.initialize_js('AcidBlock')
        return frag
Exemple #6
0
    def aside_view(self, block, context=None):
        """
        This view is used by the Acid Aside to test various features of
        the runtime it is contained in
        """
        scopes = (scope for scope in Scope.scopes()
                  if scope.name in self.enabled_fields['student_view'])

        scope_test_contexts = []
        for scope in scopes:
            try:
                scope_test_contexts.append(self.setup_storage(scope.name))
            except Exception:
                logging.warning('Unable to use scope in acid test',
                                exc_info=True)

        frag = Fragment(
            self.render_template(
                'html/aside.html.mako',
                usage_id=block.scope_ids.usage_id,
                error_class=self.ERROR_CLASS,
                success_class=self.SUCCESS_CLASS,
                failure_class=self.FAILURE_CLASS,
                unknown_class=self.UNKNOWN_CLASS,
                storage_tests=scope_test_contexts,
                local_resource_url=self.runtime.local_resource_url(
                    self, 'public/test_data.json'),
            ))

        frag.add_javascript(
            self.resource_string("static/js/jquery.ajaxq-0.0.1.js"))
        frag.add_javascript(
            self.resource_string('static/js/acid_update_status.js'))
        frag.add_javascript(self.resource_string('static/js/acid.js'))
        frag.add_css(self.resource_string("static/css/acid.css"))
        frag.add_css_url(
            '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css'
        )
        frag.initialize_js('AcidAsideBlock',
                           {'test_aside': isinstance(block, AcidBlock)})
        return frag
Exemple #7
0
class TestMixin(object):
    """
    Set up namespaces for each scope to use.
    """
    mixin_content = String(scope=Scope.content, default='mixin_c')
    mixin_settings = String(scope=Scope.settings, default='mixin_s')
    mixin_user_state = String(scope=Scope.user_state, default='mixin_ss')
    mixin_preferences = String(scope=Scope.preferences, default='mixin_sp')
    mixin_user_info = String(scope=Scope.user_info, default='mixin_si')
    mixin_by_type = String(scope=Scope(UserScope.NONE, BlockScope.TYPE), default='mixin_bt')
    mixin_for_all = String(scope=Scope(UserScope.NONE, BlockScope.ALL), default='mixin_fa')
    mixin_user_def = String(scope=Scope(UserScope.ONE, BlockScope.DEFINITION), default='mixin_sd')
    mixin_agg_global = String(scope=Scope(UserScope.ALL, BlockScope.ALL), default='mixin_ag')
    mixin_agg_type = String(scope=Scope(UserScope.ALL, BlockScope.TYPE), default='mixin_at')
    mixin_agg_def = String(scope=Scope(UserScope.ALL, BlockScope.DEFINITION), default='mixin_ad')
    mixin_agg_usage = String(scope=Scope.user_state_summary, default='mixin_au')
class TestXBlockNoFallback(XBlock):
    """
    Set up a class that contains ModelTypes as fields, but no views or handlers
    """
    content = String(scope=Scope.content, default='c')
    settings = String(scope=Scope.settings, default='s')
    user_state = String(scope=Scope.user_state, default='ss')
    preferences = String(scope=Scope.preferences, default='sp')
    user_info = String(scope=Scope.user_info, default='si')
    by_type = String(scope=Scope(UserScope.NONE, BlockScope.TYPE), default='bt')
    for_all = String(scope=Scope(UserScope.NONE, BlockScope.ALL), default='fa')
    user_def = String(scope=Scope(UserScope.ONE, BlockScope.DEFINITION), default='sd')
    agg_global = String(scope=Scope(UserScope.ALL, BlockScope.ALL), default='ag')
    agg_type = String(scope=Scope(UserScope.ALL, BlockScope.TYPE), default='at')
    agg_def = String(scope=Scope(UserScope.ALL, BlockScope.DEFINITION), default='ad')
    agg_usage = String(scope=Scope.user_state_summary, default='au')
Exemple #9
0
    def aside_view(self, block, context=None):
        """
        This view is used by the Acid Aside to test various features of
        the runtime it is contained in
        """
        scopes = (
            scope
            for scope in Scope.scopes()
            if scope.name in self.enabled_fields['student_view']
        )

        scope_test_contexts = []
        for scope in scopes:
            try:
                scope_test_contexts.append(self.setup_storage(scope.name))
            except Exception:
                logging.warning('Unable to use scope in acid test', exc_info=True)

        frag = Fragment(self.render_template(
            'html/aside.html.mako',
            usage_id=block.scope_ids.usage_id,
            error_class=self.ERROR_CLASS,
            success_class=self.SUCCESS_CLASS,
            failure_class=self.FAILURE_CLASS,
            unknown_class=self.UNKNOWN_CLASS,
            storage_tests=scope_test_contexts,
            local_resource_url=self.runtime.local_resource_url(self, 'public/test_data.json'),
        ))

        frag.add_javascript(self.resource_string("static/js/jquery.ajaxq-0.0.1.js"))
        frag.add_javascript(self.resource_string('static/js/acid_update_status.js'))
        frag.add_javascript(self.resource_string('static/js/acid.js'))
        frag.add_css(self.resource_string("static/css/acid.css"))
        frag.add_css_url('//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css')
        frag.initialize_js('AcidAsideBlock', {'test_aside': isinstance(block, AcidBlock)})
        return frag
Exemple #10
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(), field_data, [TestMixin])
    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))
        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'))