コード例 #1
0
class TestSettingsService(unittest.TestCase):
    """ Test SettingsService """

    xblock_setting_key1 = 'dummy_block'
    xblock_setting_key2 = 'other_dummy_block'

    def setUp(self):
        """ Setting up tests """
        super().setUp()
        self.settings_service = SettingsService()
        self.xblock_mock = mock.Mock()
        self.xblock_mock.block_settings_key = self.xblock_setting_key1
        self.xblock_mock.unmixed_class = mock.Mock()
        self.xblock_mock.unmixed_class.__name__ = self.xblock_setting_key2

    def test_get_given_none_throws_value_error(self):
        """  Test that given None throws value error """
        with pytest.raises(ValueError):
            self.settings_service.get_settings_bucket(None)

    def test_get_return_default_if_xblock_settings_is_missing(self):
        """ Test that returns default (or None if default not set) if XBLOCK_SETTINGS is not set """
        assert not hasattr(settings, 'XBLOCK_SETTINGS')
        # precondition check
        assert self.settings_service.get_settings_bucket(self.xblock_mock, 'zzz') == 'zzz'

    def test_get_return_empty_dictionary_if_xblock_settings_and_default_is_missing(self):
        """ Test that returns default (or None if default not set) if XBLOCK_SETTINGS is not set """
        assert not hasattr(settings, 'XBLOCK_SETTINGS')
        # precondition check
        assert self.settings_service.get_settings_bucket(self.xblock_mock) == {}

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key2: {'b': 1}})
    def test_get_returns_none_or_default_if_bucket_not_found(self):
        """ Test if settings service returns default if setting not found """
        assert settings.XBLOCK_SETTINGS == {self.xblock_setting_key2: {'b': 1}}
        assert self.settings_service.get_settings_bucket(self.xblock_mock) == {}
        assert self.settings_service.get_settings_bucket(self.xblock_mock, 123) == 123

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key1: 42})
    def test_get_returns_correct_value(self):
        """ Test if settings service returns correct bucket """
        assert settings.XBLOCK_SETTINGS == {self.xblock_setting_key1: 42}
        assert self.settings_service.get_settings_bucket(self.xblock_mock) == 42

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key2: "I'm a setting"})
    def test_get_respects_block_settings_key(self):
        """ Test if settings service respects block_settings_key value """
        assert settings.XBLOCK_SETTINGS == {self.xblock_setting_key2: "I'm a setting"}
        self.xblock_mock.block_settings_key = self.xblock_setting_key2
        assert self.settings_service.get_settings_bucket(self.xblock_mock) == "I'm a setting"

    @override_settings(XBLOCK_SETTINGS={_DummyBlock.__name__: [1, 2, 3]})
    def test_get_uses_class_name_if_block_settings_key_is_not_set(self):
        """ Test if settings service uses class name if block_settings_key attribute does not exist """
        mixologist = Mixologist([])
        block = mixologist.mix(_DummyBlock)
        assert settings.XBLOCK_SETTINGS == {'_DummyBlock': [1, 2, 3]}
        assert self.settings_service.get_settings_bucket(block) == [1, 2, 3]
コード例 #2
0
class TestSettingsService(TestCase):
    """ Test SettingsService """
    shard = 1

    xblock_setting_key1 = 'dummy_block'
    xblock_setting_key2 = 'other_dummy_block'

    def setUp(self):
        """ Setting up tests """
        super(TestSettingsService, self).setUp()
        self.settings_service = SettingsService()
        self.xblock_mock = mock.Mock()
        self.xblock_mock.block_settings_key = self.xblock_setting_key1
        self.xblock_mock.unmixed_class = mock.Mock()
        self.xblock_mock.unmixed_class.__name__ = self.xblock_setting_key2

    def test_get_given_none_throws_value_error(self):
        """  Test that given None throws value error """
        with self.assertRaises(ValueError):
            self.settings_service.get_settings_bucket(None)

    def test_get_return_default_if_xblock_settings_is_missing(self):
        """ Test that returns default (or None if default not set) if XBLOCK_SETTINGS is not set """
        self.assertFalse(hasattr(settings, 'XBLOCK_SETTINGS'))  # precondition check
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock, 'zzz'), 'zzz')

    def test_get_return_empty_dictionary_if_xblock_settings_and_default_is_missing(self):
        """ Test that returns default (or None if default not set) if XBLOCK_SETTINGS is not set """
        self.assertFalse(hasattr(settings, 'XBLOCK_SETTINGS'))  # precondition check
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock), {})

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key2: {'b': 1}})
    def test_get_returns_none_or_default_if_bucket_not_found(self):
        """ Test if settings service returns default if setting not found """
        self.assertEqual(settings.XBLOCK_SETTINGS, {self.xblock_setting_key2: {'b': 1}})
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock), {})
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock, 123), 123)

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key1: 42})
    def test_get_returns_correct_value(self):
        """ Test if settings service returns correct bucket """
        self.assertEqual(settings.XBLOCK_SETTINGS, {self.xblock_setting_key1: 42})
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock), 42)

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key2: "I'm a setting"})
    def test_get_respects_block_settings_key(self):
        """ Test if settings service respects block_settings_key value """
        self.assertEqual(settings.XBLOCK_SETTINGS, {self.xblock_setting_key2: "I'm a setting"})
        self.xblock_mock.block_settings_key = self.xblock_setting_key2
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock), "I'm a setting")

    @override_settings(XBLOCK_SETTINGS={_DummyBlock.__name__: [1, 2, 3]})
    def test_get_uses_class_name_if_block_settings_key_is_not_set(self):
        """ Test if settings service uses class name if block_settings_key attribute does not exist """
        mixologist = Mixologist([])
        block = mixologist.mix(_DummyBlock)
        self.assertEqual(settings.XBLOCK_SETTINGS, {"_DummyBlock": [1, 2, 3]})
        self.assertEqual(self.settings_service.get_settings_bucket(block), [1, 2, 3])
コード例 #3
0
class TestSettingsService(TestCase):
    """ Test SettingsService """

    xblock_setting_key1 = 'dummy_block'
    xblock_setting_key2 = 'other_dummy_block'

    def setUp(self):
        """ Setting up tests """
        super(TestSettingsService, self).setUp()
        self.settings_service = SettingsService()
        self.xblock_mock = mock.Mock()
        self.xblock_mock.block_settings_key = self.xblock_setting_key1
        self.xblock_mock.unmixed_class = mock.Mock()
        self.xblock_mock.unmixed_class.__name__ = self.xblock_setting_key2

    def test_get_given_none_throws_value_error(self):
        """  Test that given None throws value error """
        with self.assertRaises(ValueError):
            self.settings_service.get_settings_bucket(None)

    def test_get_return_default_if_xblock_settings_is_missing(self):
        """ Test that returns default (or None if default not set) if XBLOCK_SETTINGS is not set """
        self.assertFalse(hasattr(settings, 'XBLOCK_SETTINGS'))  # precondition check
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock, 'zzz'), 'zzz')

    def test_get_return_empty_dictionary_if_xblock_settings_and_default_is_missing(self):
        """ Test that returns default (or None if default not set) if XBLOCK_SETTINGS is not set """
        self.assertFalse(hasattr(settings, 'XBLOCK_SETTINGS'))  # precondition check
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock), {})

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key2: {'b': 1}})
    def test_get_returns_none_or_default_if_bucket_not_found(self):
        """ Test if settings service returns default if setting not found """
        self.assertEqual(getattr(settings, 'XBLOCK_SETTINGS'), {self.xblock_setting_key2: {'b': 1}})
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock), {})
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock, 123), 123)

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key1: 42})
    def test_get_returns_correct_value(self):
        """ Test if settings service returns correct bucket """
        self.assertEqual(getattr(settings, 'XBLOCK_SETTINGS'), {self.xblock_setting_key1: 42})
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock), 42)

    @override_settings(XBLOCK_SETTINGS={xblock_setting_key2: "I'm a setting"})
    def test_get_respects_block_settings_key(self):
        """ Test if settings service respects block_settings_key value """
        self.assertEqual(getattr(settings, 'XBLOCK_SETTINGS'), {self.xblock_setting_key2: "I'm a setting"})
        self.xblock_mock.block_settings_key = self.xblock_setting_key2
        self.assertEqual(self.settings_service.get_settings_bucket(self.xblock_mock), "I'm a setting")

    @override_settings(XBLOCK_SETTINGS={_DummyBlock.__name__: [1, 2, 3]})
    def test_get_uses_class_name_if_block_settings_key_is_not_set(self):
        """ Test if settings service uses class name if block_settings_key attribute does not exist """
        mixologist = Mixologist([])
        block = mixologist.mix(_DummyBlock)
        self.assertEqual(getattr(settings, 'XBLOCK_SETTINGS'), {"_DummyBlock": [1, 2, 3]})
        self.assertEqual(self.settings_service.get_settings_bucket(block), [1, 2, 3])