Пример #1
0
def test_with_mixins():
    # Testing model metaclass with mixins
    class FieldsMixin(object):
        """Toy class for field testing"""
        field_a = Integer(scope=Scope.settings)

    class BaseClass(object):
        """Toy class for ModelMetaclass testing"""
        __metaclass__ = ModelMetaclass

    class ChildClass(FieldsMixin, BaseClass):
        """Toy class for ModelMetaclass and field testing"""
        pass

    class GrandchildClass(ChildClass):
        """Toy class for ModelMetaclass and field testing"""
        pass

    # `ChildClass` and `GrandchildClass` both obtain the `fields` attribute
    # from the `ModelMetaclass`. Since this is not understood by static analysis,
    # silence this error for the duration of this test.
    # pylint: disable=E1101

    assert hasattr(ChildClass, 'field_a')
    assert_is(ChildClass.field_a, ChildClass.fields['field_a'])

    assert hasattr(GrandchildClass, 'field_a')
    assert_is(GrandchildClass.field_a, GrandchildClass.fields['field_a'])
Пример #2
0
    def student_view(self, _context):
        """Try out some services."""
        # i18n is available, and works.
        def assert_equals_unicode(str1, str2):
            """`str1` equals `str2`, and both are Unicode strings."""
            assert_equals(str1, str2)
            assert isinstance(str1, unicode)
            assert isinstance(str2, unicode)

        i18n = self.runtime.service(self, "i18n")
        assert_equals_unicode(u"Welcome!", i18n.ugettext("Welcome!"))

        assert_equals_unicode(u"Plural", i18n.ungettext("Singular", "Plural", 0))
        assert_equals_unicode(u"Singular", i18n.ungettext("Singular", "Plural", 1))
        assert_equals_unicode(u"Plural", i18n.ungettext("Singular", "Plural", 2))

        when = datetime(2013, 2, 14, 22, 30, 17)
        assert_equals_unicode(u"2013-02-14", i18n.strftime(when, "%Y-%m-%d"))
        assert_equals_unicode(u"Feb 14, 2013", i18n.strftime(when, "SHORT_DATE"))
        assert_equals_unicode(u"Thursday, February 14, 2013", i18n.strftime(when, "LONG_DATE"))
        assert_equals_unicode(u"Feb 14, 2013 at 22:30", i18n.strftime(when, "DATE_TIME"))
        assert_equals_unicode(u"10:30:17 PM", i18n.strftime(when, "TIME"))

        # secret_service is available.
        assert_equals(self.runtime.service(self, "secret_service"), 17)

        # no_such_service is not available, and raises an exception, because we
        # said we needed it.
        with assert_raises_regexp(NoSuchServiceError, "is not available"):
            self.runtime.service(self, "no_such_service")

        # another_not_service is not available, and returns None, because we
        # didn't need it, we only wanted it.
        assert_is(self.runtime.service(self, "another_not_service"), None)
Пример #3
0
    def student_view(self, _context):
        """Try out some services."""
        # i18n is available, and works.
        def assert_equals_unicode(str1, str2):
            """`str1` equals `str2`, and both are Unicode strings."""
            assert_equals(str1, str2)
            assert isinstance(str1, unicode)
            assert isinstance(str2, unicode)

        i18n = self.runtime.service(self, "i18n")
        assert_equals_unicode(u"Welcome!", i18n.ugettext("Welcome!"))

        assert_equals_unicode(u"Plural", i18n.ungettext("Singular", "Plural", 0))
        assert_equals_unicode(u"Singular", i18n.ungettext("Singular", "Plural", 1))
        assert_equals_unicode(u"Plural", i18n.ungettext("Singular", "Plural", 2))

        # secret_service is available.
        assert_equals(self.runtime.service(self, "secret_service"), 17)

        # no_such_service is not available, and raises an exception, because we
        # said we needed it.
        with assert_raises(NoSuchServiceError):
            self.runtime.service(self, "no_such_service")

        # another_not_service is not available, and returns None, because we
        # didn't need it, we only wanted it.
        assert_is(self.runtime.service(self, "another_not_service"), None)
Пример #4
0
def test_ambiguous_plugins():
    # We can load ok blocks even if there are bad blocks.
    cls = XBlock.load_class("good_block")
    assert_is(cls, UnambiguousBlock)

    # Trying to load bad blocks raises an exception.
    expected_msg = (
        "Ambiguous entry points for bad_block: "
        "xblock.test.test_plugin.AmbiguousBlock1, "
        "xblock.test.test_plugin.AmbiguousBlock2"
    )
    with assert_raises_regexp(AmbiguousPluginError, expected_msg):
        XBlock.load_class("bad_block")

    # We can use our own function as the select function.
    class MyOwnException(Exception):
        """We'll raise this from `boom`."""
        pass

    def boom(identifier, entry_points):
        """A select function to prove user-defined functions are called."""
        assert len(entry_points) == 2
        assert identifier == "bad_block"
        raise MyOwnException("This is boom")

    with assert_raises_regexp(MyOwnException, "This is boom"):
        XBlock.load_class("bad_block", select=boom)
Пример #5
0
def test_nosuch_plugin():
    # We can provide a default class to return for missing plugins.
    cls = XBlock.load_class("nosuch_block", default=UnambiguousBlock)
    assert_is(cls, UnambiguousBlock)

    # If we don't provide a default class, an exception is raised.
    with assert_raises_regexp(PluginMissingError, "nosuch_block"):
        XBlock.load_class("nosuch_block")
Пример #6
0
    def test_set_preserves_identity(self):
        first_get = self.get()
        assert_is_not(self.new_value, first_get)
        self.set(self.new_value)
        second_get = self.get()

        assert_is(self.new_value, second_get)
        assert_is_not(first_get, second_get)
Пример #7
0
    def test_set_with_save_preserves_identity(self):
        first_get = self.get()
        self.set(self.new_value)
        self.block.save()
        second_get = self.get()

        assert_is(self.new_value, second_get)
        assert_is_not(first_get, second_get)
Пример #8
0
    def test_only_generate_classes_once(self):
        assert_is(
            self.mixologist.mix(FieldTester),
            self.mixologist.mix(FieldTester),
        )

        assert_is_not(
            self.mixologist.mix(FieldTester),
            self.mixologist.mix(TestXBlock),
        )
Пример #9
0
    def student_view(self, context):
        """Try the services."""
        # First, call the super class, its assertions should still pass.
        super(SubXBlockWithServices, self).student_view(context)

        # no_such_service_sub is not available, and raises an exception,
        # because we said we needed it.
        with assert_raises_regexp(NoSuchServiceError, "is not available"):
            self.runtime.service(self, "no_such_service_sub")

        # another_not_service_sub is not available, and returns None,
        # because we didn't need it, we only wanted it.
        assert_is(self.runtime.service(self, "another_not_service_sub"), None)
Пример #10
0
def test_model_metaclass():
    class ModelMetaclassTester(object):
        """Toy class for ModelMetaclass testing"""
        __metaclass__ = ModelMetaclass

        field_a = Integer(scope=Scope.settings)
        field_b = Integer(scope=Scope.content)

        def __init__(self, field_data):
            self._field_data = field_data

    class ChildClass(ModelMetaclassTester):
        """Toy class for ModelMetaclass testing"""
        pass

    # `ModelMetaclassTester` and `ChildClass` both obtain the `fields` attribute
    # from the `ModelMetaclass`. Since this is not understood by static analysis,
    # silence this error for the duration of this test.
    # pylint: disable=E1101
    assert hasattr(ModelMetaclassTester, 'field_a')
    assert hasattr(ModelMetaclassTester, 'field_b')

    assert_is(ModelMetaclassTester.field_a, ModelMetaclassTester.fields['field_a'])
    assert_is(ModelMetaclassTester.field_b, ModelMetaclassTester.fields['field_b'])

    assert hasattr(ChildClass, 'field_a')
    assert hasattr(ChildClass, 'field_b')

    assert_is(ChildClass.field_a, ChildClass.fields['field_a'])
    assert_is(ChildClass.field_b, ChildClass.fields['field_b'])
Пример #11
0
    def test_multiply_mixed(self):
        mixalot = Mixologist([ThirdMixin, FirstMixin])

        pre_mixed = mixalot.mix(self.mixologist.mix(FieldTester))
        post_mixed = self.mixologist.mix(mixalot.mix(FieldTester))

        assert_is(pre_mixed.fields['field'], FirstMixin.field)
        assert_is(post_mixed.fields['field'], ThirdMixin.field)

        assert_is(FieldTester, pre_mixed.unmixed_class)
        assert_is(FieldTester, post_mixed.unmixed_class)

        assert_equals(4, len(pre_mixed.__bases__))  # 1 for the original class + 3 mixin classes
        assert_equals(4, len(post_mixed.__bases__))
Пример #12
0
 def test_unmixed_class(self):
     assert_is(FieldTester, self.mixologist.mix(FieldTester).unmixed_class)
Пример #13
0
    def test_get_preserves_identity(self):
        first_get = self.get()
        second_get = self.get()

        assert_is(first_get, second_get)
Пример #14
0
 def test_duplicate_mixins(self):
     singly_mixed = self.mixologist.mix(FieldTester)
     doubly_mixed = self.mixologist.mix(singly_mixed)
     assert_is(singly_mixed, doubly_mixed)
     assert_is(FieldTester, singly_mixed.unmixed_class)
Пример #15
0
 def test_mixed_fields(self):
     mixed = self.mixologist.mix(FieldTester)
     assert_is(mixed.fields['field'], FirstMixin.field)
     assert_is(mixed.fields['field_a'], FieldTester.field_a)
Пример #16
0
 def test_mixin_fields(self):
     assert_is(FirstMixin.fields['field'], FirstMixin.field)
Пример #17
0
 def test_unmixed_class(self):
     assert_is(FieldTester, self.mixologist.mix(FieldTester).unmixed_class)
Пример #18
0
 def test_mixin_order(self):
     assert_is(1, self.mixologist.mix(FieldTester).number)
     assert_is(1, self.mixologist.mix(FieldTester).fields['field'].default)
Пример #19
0
    def test_get_with_save_preserves_identity(self):
        first_get = self.get()
        self.block.save()
        second_get = self.get()

        assert_is(first_get, second_get)
Пример #20
0
 def test_mixin_fields(self):
     assert_is(FirstMixin.fields['field'], FirstMixin.field)  # pylint: disable=unsubscriptable-object
Пример #21
0
    def test_get_preserves_identity(self):
        first_get = self.get()
        second_get = self.get()

        assert_is(first_get, second_get)
Пример #22
0
 def test_mixin_order(self):
     assert_is(1, self.mixologist.mix(FieldTester).number)
     assert_is(1, self.mixologist.mix(FieldTester).fields['field'].default)
Пример #23
0
 def test_duplicate_mixins(self):
     singly_mixed = self.mixologist.mix(FieldTester)
     doubly_mixed = self.mixologist.mix(singly_mixed)
     assert_is(singly_mixed, doubly_mixed)
     assert_is(FieldTester, singly_mixed.unmixed_class)
Пример #24
0
    def test_get_with_save_preserves_identity(self):
        first_get = self.get()
        self.block.save()
        second_get = self.get()

        assert_is(first_get, second_get)
Пример #25
0
 def test_mixin_fields(self):
     assert_is(FirstMixin.fields['field'], FirstMixin.field)
Пример #26
0
 def test_mixed_fields(self):
     mixed = self.mixologist.mix(FieldTester)
     assert_is(mixed.fields['field'], FirstMixin.field)
     assert_is(mixed.fields['field_a'], FieldTester.field_a)
Пример #27
0
 def test_mixin_fields(self):
     assert_is(FirstMixin.fields['field'], FirstMixin.field)  # pylint: disable=unsubscriptable-object