Пример #1
0
 def setUp(self):
     super(TestMongoKeyValueStore, self).setUp()  # lint-amnesty, pylint: disable=super-with-arguments
     self.data = {'foo': 'foo_value'}
     self.course_id = CourseKey.from_string('org/course/run')
     self.parent = self.course_id.make_usage_key('parent', 'p')
     self.children = [self.course_id.make_usage_key('child', 'a'), self.course_id.make_usage_key('child', 'b')]
     self.metadata = {'meta': 'meta_val'}
     self.kvs = MongoKeyValueStore(self.data, self.parent, self.children, self.metadata)
Пример #2
0
 def setUp(self):
     self.data = {'foo': 'foo_value'}
     self.location = Location('i4x://org/course/category/name@version')
     self.children = [
         'i4x://org/course/child/a', 'i4x://org/course/child/b'
     ]
     self.metadata = {'meta': 'meta_val'}
     self.kvs = MongoKeyValueStore(self.data, self.children, self.metadata)
Пример #3
0
 def setUp(self):
     super(TestMongoKeyValueStore, self).setUp()
     self.data = {'foo': 'foo_value'}
     self.course_id = SlashSeparatedCourseKey('org', 'course', 'run')
     self.parent = self.course_id.make_usage_key('parent', 'p')
     self.children = [self.course_id.make_usage_key('child', 'a'), self.course_id.make_usage_key('child', 'b')]
     self.metadata = {'meta': 'meta_val'}
     self.kvs = MongoKeyValueStore(self.data, self.parent, self.children, self.metadata)
Пример #4
0
 def setUp(self):
     super().setUp()
     self.data = {'foo': 'foo_value'}
     self.course_id = CourseKey.from_string('org/course/run')
     self.parent = self.course_id.make_usage_key('parent', 'p')
     self.children = [
         self.course_id.make_usage_key('child', 'a'),
         self.course_id.make_usage_key('child', 'b')
     ]
     self.metadata = {'meta': 'meta_val'}
     self.kvs = MongoKeyValueStore(self.data, self.parent, self.children,
                                   self.metadata)
Пример #5
0
 def setUp(self):
     self.data = {'foo': 'foo_value'}
     self.course_id = SlashSeparatedCourseKey('org', 'course', 'run')
     self.parent = self.course_id.make_usage_key('parent', 'p')
     self.children = [self.course_id.make_usage_key('child', 'a'), self.course_id.make_usage_key('child', 'b')]
     self.metadata = {'meta': 'meta_val'}
     self.kvs = MongoKeyValueStore(self.data, self.parent, self.children, self.metadata)
Пример #6
0
 def setUp(self):
     super(TestMongoKeyValueStore, self).setUp()
     self.data = {"foo": "foo_value"}
     self.course_id = SlashSeparatedCourseKey("org", "course", "run")
     self.parent = self.course_id.make_usage_key("parent", "p")
     self.children = [self.course_id.make_usage_key("child", "a"), self.course_id.make_usage_key("child", "b")]
     self.metadata = {"meta": "meta_val"}
     self.kvs = MongoKeyValueStore(self.data, self.parent, self.children, self.metadata)
Пример #7
0
 def setUp(self):
     super(TestMongoKeyValueStore, self).setUp()
     self.data = {'foo': 'foo_value'}
     self.course_id = CourseKey.from_string('org/course/run')
     self.parent = self.course_id.make_usage_key('parent', 'p')
     self.children = [self.course_id.make_usage_key('child', 'a'), self.course_id.make_usage_key('child', 'b')]
     self.metadata = {'meta': 'meta_val'}
     self.kvs = MongoKeyValueStore(self.data, self.parent, self.children, self.metadata)
Пример #8
0
class TestMongoKeyValueStore(unittest.TestCase):
    """
    Tests for MongoKeyValueStore.
    """

    def setUp(self):
        super(TestMongoKeyValueStore, self).setUp()
        self.data = {"foo": "foo_value"}
        self.course_id = SlashSeparatedCourseKey("org", "course", "run")
        self.parent = self.course_id.make_usage_key("parent", "p")
        self.children = [self.course_id.make_usage_key("child", "a"), self.course_id.make_usage_key("child", "b")]
        self.metadata = {"meta": "meta_val"}
        self.kvs = MongoKeyValueStore(self.data, self.parent, self.children, self.metadata)

    def test_read(self):
        assert_equals(self.data["foo"], self.kvs.get(KeyValueStore.Key(Scope.content, None, None, "foo")))
        assert_equals(self.parent, self.kvs.get(KeyValueStore.Key(Scope.parent, None, None, "parent")))
        assert_equals(self.children, self.kvs.get(KeyValueStore.Key(Scope.children, None, None, "children")))
        assert_equals(self.metadata["meta"], self.kvs.get(KeyValueStore.Key(Scope.settings, None, None, "meta")))

    def test_read_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            key = KeyValueStore.Key(scope, None, None, "foo")
            with assert_raises(InvalidScopeError):
                self.kvs.get(key)
            assert_false(self.kvs.has(key))

    def test_read_non_dict_data(self):
        self.kvs = MongoKeyValueStore("xml_data", self.parent, self.children, self.metadata)
        assert_equals("xml_data", self.kvs.get(KeyValueStore.Key(Scope.content, None, None, "data")))

    def _check_write(self, key, value):
        self.kvs.set(key, value)
        assert_equals(value, self.kvs.get(key))

    def test_write(self):
        yield (self._check_write, KeyValueStore.Key(Scope.content, None, None, "foo"), "new_data")
        yield (self._check_write, KeyValueStore.Key(Scope.children, None, None, "children"), [])
        yield (self._check_write, KeyValueStore.Key(Scope.settings, None, None, "meta"), "new_settings")
        # write Scope.parent raises InvalidScope, which is covered in test_write_invalid_scope

    def test_write_non_dict_data(self):
        self.kvs = MongoKeyValueStore("xml_data", self.parent, self.children, self.metadata)
        self._check_write(KeyValueStore.Key(Scope.content, None, None, "data"), "new_data")

    def test_write_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.set(KeyValueStore.Key(scope, None, None, "foo"), "new_value")

    def _check_delete_default(self, key, default_value):
        self.kvs.delete(key)
        assert_equals(default_value, self.kvs.get(key))
        assert self.kvs.has(key)

    def _check_delete_key_error(self, key):
        self.kvs.delete(key)
        with assert_raises(KeyError):
            self.kvs.get(key)
        assert_false(self.kvs.has(key))

    def test_delete(self):
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.content, None, None, "foo"))
        yield (self._check_delete_default, KeyValueStore.Key(Scope.children, None, None, "children"), [])
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.settings, None, None, "meta"))

    def test_delete_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.delete(KeyValueStore.Key(scope, None, None, "foo"))
Пример #9
0
class TestMongoKeyValueStore(object):
    """
    Tests for MongoKeyValueStore.
    """
    def setUp(self):
        self.data = {'foo': 'foo_value'}
        self.location = Location('i4x://org/course/category/name@version')
        self.children = [
            'i4x://org/course/child/a', 'i4x://org/course/child/b'
        ]
        self.metadata = {'meta': 'meta_val'}
        self.kvs = MongoKeyValueStore(self.data, self.children, self.metadata)

    def _check_read(self, key, expected_value):
        """
        Asserts the get and has methods.
        """
        assert_equals(expected_value, self.kvs.get(key))
        assert self.kvs.has(key)

    def test_read(self):
        assert_equals(
            self.data['foo'],
            self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'foo')))
        assert_equals(
            self.children,
            self.kvs.get(
                KeyValueStore.Key(Scope.children, None, None, 'children')))
        assert_equals(
            self.metadata['meta'],
            self.kvs.get(KeyValueStore.Key(Scope.settings, None, None,
                                           'meta')))
        assert_equals(
            None,
            self.kvs.get(KeyValueStore.Key(Scope.parent, None, None,
                                           'parent')))

    def test_read_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            key = KeyValueStore.Key(scope, None, None, 'foo')
            with assert_raises(InvalidScopeError):
                self.kvs.get(key)
            assert_false(self.kvs.has(key))

    def test_read_non_dict_data(self):
        self.kvs._data = 'xml_data'
        assert_equals(
            'xml_data',
            self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'data')))

    def _check_write(self, key, value):
        self.kvs.set(key, value)
        assert_equals(value, self.kvs.get(key))

    def test_write(self):
        yield (self._check_write,
               KeyValueStore.Key(Scope.content, None, None, 'foo'), 'new_data')
        yield (self._check_write,
               KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_write,
               KeyValueStore.Key(Scope.settings, None, None,
                                 'meta'), 'new_settings')

    def test_write_non_dict_data(self):
        self.kvs._data = 'xml_data'
        self._check_write(KeyValueStore.Key(Scope.content, None, None, 'data'),
                          'new_data')

    def test_write_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state,
                      Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.set(KeyValueStore.Key(scope, None, None, 'foo'),
                             'new_value')

    def _check_delete_default(self, key, default_value):
        self.kvs.delete(key)
        assert_equals(default_value, self.kvs.get(key))
        assert self.kvs.has(key)

    def _check_delete_key_error(self, key):
        self.kvs.delete(key)
        with assert_raises(KeyError):
            self.kvs.get(key)
        assert_false(self.kvs.has(key))

    def test_delete(self):
        yield (self._check_delete_key_error,
               KeyValueStore.Key(Scope.content, None, None, 'foo'))
        yield (self._check_delete_default,
               KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_delete_key_error,
               KeyValueStore.Key(Scope.settings, None, None, 'meta'))

    def test_delete_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state,
                      Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.delete(KeyValueStore.Key(scope, None, None, 'foo'))
Пример #10
0
 def test_write_non_dict_data(self):
     self.kvs = MongoKeyValueStore('xml_data', self.children, self.metadata)
     self._check_write(KeyValueStore.Key(Scope.content, None, None, 'data'), 'new_data')
Пример #11
0
 def test_read_non_dict_data(self):
     self.kvs = MongoKeyValueStore('xml_data', self.children, self.metadata)
     assert_equals('xml_data', self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'data')))
Пример #12
0
class TestMongoKeyValueStore(object):
    """
    Tests for MongoKeyValueStore.
    """

    def setUp(self):
        self.data = {'foo': 'foo_value'}
        self.course_id = SlashSeparatedCourseKey('org', 'course', 'run')
        self.children = [self.course_id.make_usage_key('child', 'a'), self.course_id.make_usage_key('child', 'b')]
        self.metadata = {'meta': 'meta_val'}
        self.kvs = MongoKeyValueStore(self.data, self.children, self.metadata)

    def test_read(self):
        assert_equals(self.data['foo'], self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'foo')))
        assert_equals(self.children, self.kvs.get(KeyValueStore.Key(Scope.children, None, None, 'children')))
        assert_equals(self.metadata['meta'], self.kvs.get(KeyValueStore.Key(Scope.settings, None, None, 'meta')))
        assert_equals(None, self.kvs.get(KeyValueStore.Key(Scope.parent, None, None, 'parent')))

    def test_read_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            key = KeyValueStore.Key(scope, None, None, 'foo')
            with assert_raises(InvalidScopeError):
                self.kvs.get(key)
            assert_false(self.kvs.has(key))

    def test_read_non_dict_data(self):
        self.kvs = MongoKeyValueStore('xml_data', self.children, self.metadata)
        assert_equals('xml_data', self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'data')))

    def _check_write(self, key, value):
        self.kvs.set(key, value)
        assert_equals(value, self.kvs.get(key))

    def test_write(self):
        yield (self._check_write, KeyValueStore.Key(Scope.content, None, None, 'foo'), 'new_data')
        yield (self._check_write, KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_write, KeyValueStore.Key(Scope.settings, None, None, 'meta'), 'new_settings')

    def test_write_non_dict_data(self):
        self.kvs = MongoKeyValueStore('xml_data', self.children, self.metadata)
        self._check_write(KeyValueStore.Key(Scope.content, None, None, 'data'), 'new_data')

    def test_write_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.set(KeyValueStore.Key(scope, None, None, 'foo'), 'new_value')

    def _check_delete_default(self, key, default_value):
        self.kvs.delete(key)
        assert_equals(default_value, self.kvs.get(key))
        assert self.kvs.has(key)

    def _check_delete_key_error(self, key):
        self.kvs.delete(key)
        with assert_raises(KeyError):
            self.kvs.get(key)
        assert_false(self.kvs.has(key))

    def test_delete(self):
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.content, None, None, 'foo'))
        yield (self._check_delete_default, KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.settings, None, None, 'meta'))

    def test_delete_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.delete(KeyValueStore.Key(scope, None, None, 'foo'))
Пример #13
0
 def test_write_non_dict_data(self):
     self.kvs = MongoKeyValueStore('xml_data', self.children, self.metadata)
     self._check_write(KeyValueStore.Key(Scope.content, None, None, 'data'),
                       'new_data')
Пример #14
0
class TestMongoKeyValueStore(object):
    """
    Tests for MongoKeyValueStore.
    """
    def setUp(self):
        self.data = {'foo': 'foo_value'}
        self.course_id = SlashSeparatedCourseKey('org', 'course', 'run')
        self.children = [
            self.course_id.make_usage_key('child', 'a'),
            self.course_id.make_usage_key('child', 'b')
        ]
        self.metadata = {'meta': 'meta_val'}
        self.kvs = MongoKeyValueStore(self.data, self.children, self.metadata)

    def test_read(self):
        assert_equals(
            self.data['foo'],
            self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'foo')))
        assert_equals(
            self.children,
            self.kvs.get(
                KeyValueStore.Key(Scope.children, None, None, 'children')))
        assert_equals(
            self.metadata['meta'],
            self.kvs.get(KeyValueStore.Key(Scope.settings, None, None,
                                           'meta')))
        assert_equals(
            None,
            self.kvs.get(KeyValueStore.Key(Scope.parent, None, None,
                                           'parent')))

    def test_read_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            key = KeyValueStore.Key(scope, None, None, 'foo')
            with assert_raises(InvalidScopeError):
                self.kvs.get(key)
            assert_false(self.kvs.has(key))

    def test_read_non_dict_data(self):
        self.kvs = MongoKeyValueStore('xml_data', self.children, self.metadata)
        assert_equals(
            'xml_data',
            self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'data')))

    def _check_write(self, key, value):
        self.kvs.set(key, value)
        assert_equals(value, self.kvs.get(key))

    def test_write(self):
        yield (self._check_write,
               KeyValueStore.Key(Scope.content, None, None, 'foo'), 'new_data')
        yield (self._check_write,
               KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_write,
               KeyValueStore.Key(Scope.settings, None, None,
                                 'meta'), 'new_settings')

    def test_write_non_dict_data(self):
        self.kvs = MongoKeyValueStore('xml_data', self.children, self.metadata)
        self._check_write(KeyValueStore.Key(Scope.content, None, None, 'data'),
                          'new_data')

    def test_write_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state,
                      Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.set(KeyValueStore.Key(scope, None, None, 'foo'),
                             'new_value')

    def _check_delete_default(self, key, default_value):
        self.kvs.delete(key)
        assert_equals(default_value, self.kvs.get(key))
        assert self.kvs.has(key)

    def _check_delete_key_error(self, key):
        self.kvs.delete(key)
        with assert_raises(KeyError):
            self.kvs.get(key)
        assert_false(self.kvs.has(key))

    def test_delete(self):
        yield (self._check_delete_key_error,
               KeyValueStore.Key(Scope.content, None, None, 'foo'))
        yield (self._check_delete_default,
               KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_delete_key_error,
               KeyValueStore.Key(Scope.settings, None, None, 'meta'))

    def test_delete_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state,
                      Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.delete(KeyValueStore.Key(scope, None, None, 'foo'))
Пример #15
0
 def setUp(self):
     self.data = {"foo": "foo_value"}
     self.location = Location("i4x://org/course/category/name@version")
     self.children = ["i4x://org/course/child/a", "i4x://org/course/child/b"]
     self.metadata = {"meta": "meta_val"}
     self.kvs = MongoKeyValueStore(self.data, self.children, self.metadata)
Пример #16
0
class TestMongoKeyValueStore(object):
    """
    Tests for MongoKeyValueStore.
    """

    def setUp(self):
        self.data = {"foo": "foo_value"}
        self.location = Location("i4x://org/course/category/name@version")
        self.children = ["i4x://org/course/child/a", "i4x://org/course/child/b"]
        self.metadata = {"meta": "meta_val"}
        self.kvs = MongoKeyValueStore(self.data, self.children, self.metadata)

    def test_read(self):
        assert_equals(self.data["foo"], self.kvs.get(KeyValueStore.Key(Scope.content, None, None, "foo")))
        assert_equals(self.children, self.kvs.get(KeyValueStore.Key(Scope.children, None, None, "children")))
        assert_equals(self.metadata["meta"], self.kvs.get(KeyValueStore.Key(Scope.settings, None, None, "meta")))
        assert_equals(None, self.kvs.get(KeyValueStore.Key(Scope.parent, None, None, "parent")))

    def test_read_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            key = KeyValueStore.Key(scope, None, None, "foo")
            with assert_raises(InvalidScopeError):
                self.kvs.get(key)
            assert_false(self.kvs.has(key))

    def test_read_non_dict_data(self):
        self.kvs = MongoKeyValueStore("xml_data", self.children, self.metadata)
        assert_equals("xml_data", self.kvs.get(KeyValueStore.Key(Scope.content, None, None, "data")))

    def _check_write(self, key, value):
        self.kvs.set(key, value)
        assert_equals(value, self.kvs.get(key))

    def test_write(self):
        yield (self._check_write, KeyValueStore.Key(Scope.content, None, None, "foo"), "new_data")
        yield (self._check_write, KeyValueStore.Key(Scope.children, None, None, "children"), [])
        yield (self._check_write, KeyValueStore.Key(Scope.settings, None, None, "meta"), "new_settings")

    def test_write_non_dict_data(self):
        self.kvs = MongoKeyValueStore("xml_data", self.children, self.metadata)
        self._check_write(KeyValueStore.Key(Scope.content, None, None, "data"), "new_data")

    def test_write_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.set(KeyValueStore.Key(scope, None, None, "foo"), "new_value")

    def _check_delete_default(self, key, default_value):
        self.kvs.delete(key)
        assert_equals(default_value, self.kvs.get(key))
        assert self.kvs.has(key)

    def _check_delete_key_error(self, key):
        self.kvs.delete(key)
        with assert_raises(KeyError):
            self.kvs.get(key)
        assert_false(self.kvs.has(key))

    def test_delete(self):
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.content, None, None, "foo"))
        yield (self._check_delete_default, KeyValueStore.Key(Scope.children, None, None, "children"), [])
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.settings, None, None, "meta"))

    def test_delete_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.delete(KeyValueStore.Key(scope, None, None, "foo"))
Пример #17
0
 def test_write_non_dict_data(self):
     self.kvs = MongoKeyValueStore("xml_data", self.parent, self.children, self.metadata)
     self._check_write(KeyValueStore.Key(Scope.content, None, None, "data"), "new_data")
Пример #18
0
class TestMongoKeyValueStore(TestCase):
    """
    Tests for MongoKeyValueStore.
    """
    def setUp(self):
        super().setUp()
        self.data = {'foo': 'foo_value'}
        self.course_id = CourseKey.from_string('org/course/run')
        self.parent = self.course_id.make_usage_key('parent', 'p')
        self.children = [
            self.course_id.make_usage_key('child', 'a'),
            self.course_id.make_usage_key('child', 'b')
        ]
        self.metadata = {'meta': 'meta_val'}
        self.kvs = MongoKeyValueStore(self.data, self.parent, self.children,
                                      self.metadata)

    def test_read(self):
        assert self.kvs.get(KeyValueStore.Key(Scope.content, None, None,
                                              'foo')) == self.data['foo']
        assert self.kvs.get(
            KeyValueStore.Key(Scope.parent, None, None,
                              'parent')) == self.parent
        assert self.kvs.get(
            KeyValueStore.Key(Scope.children, None, None,
                              'children')) == self.children
        assert self.kvs.get(
            KeyValueStore.Key(Scope.settings, None, None,
                              'meta')) == self.metadata['meta']

    def test_read_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            key = KeyValueStore.Key(scope, None, None, 'foo')
            with pytest.raises(InvalidScopeError):
                self.kvs.get(key)
            assert not self.kvs.has(key)

    def test_read_non_dict_data(self):
        self.kvs = MongoKeyValueStore('xml_data', self.parent, self.children,
                                      self.metadata)
        assert self.kvs.get(
            KeyValueStore.Key(Scope.content, None, None, 'data')) == 'xml_data'

    def _check_write(self, key, value):
        self.kvs.set(key, value)
        assert self.kvs.get(key) == value

    def test_write(self):
        yield (self._check_write,
               KeyValueStore.Key(Scope.content, None, None, 'foo'), 'new_data')
        yield (self._check_write,
               KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_write,
               KeyValueStore.Key(Scope.children, None, None, 'parent'), None)
        yield (self._check_write,
               KeyValueStore.Key(Scope.settings, None, None,
                                 'meta'), 'new_settings')

    def test_write_non_dict_data(self):
        self.kvs = MongoKeyValueStore('xml_data', self.parent, self.children,
                                      self.metadata)
        self._check_write(KeyValueStore.Key(Scope.content, None, None, 'data'),
                          'new_data')

    def test_write_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            with pytest.raises(InvalidScopeError):
                self.kvs.set(KeyValueStore.Key(scope, None, None, 'foo'),
                             'new_value')

    def _check_delete_default(self, key, default_value):
        self.kvs.delete(key)
        assert self.kvs.get(key) == default_value
        assert self.kvs.has(key)

    def _check_delete_key_error(self, key):
        self.kvs.delete(key)
        with pytest.raises(KeyError):
            self.kvs.get(key)
        assert not self.kvs.has(key)

    def test_delete(self):
        yield (self._check_delete_key_error,
               KeyValueStore.Key(Scope.content, None, None, 'foo'))
        yield (self._check_delete_default,
               KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_delete_key_error,
               KeyValueStore.Key(Scope.settings, None, None, 'meta'))

    def test_delete_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state,
                      Scope.parent):
            with pytest.raises(InvalidScopeError):
                self.kvs.delete(KeyValueStore.Key(scope, None, None, 'foo'))
Пример #19
0
class TestMongoKeyValueStore(object):

    def setUp(self):
        self.data = {'foo': 'foo_value'}
        self.location = Location('i4x://org/course/category/name@version')
        self.children = ['i4x://org/course/child/a', 'i4x://org/course/child/b']
        self.metadata = {'meta': 'meta_val'}
        self.kvs = MongoKeyValueStore(self.data, self.children, self.metadata, self.location)

    def _check_read(self, key, expected_value):
        assert_equals(expected_value, self.kvs.get(key))
        assert self.kvs.has(key)

    def test_read(self):
        assert_equals(self.data['foo'], self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'foo')))
        assert_equals(self.location, self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'location')))
        assert_equals(self.children, self.kvs.get(KeyValueStore.Key(Scope.children, None, None, 'children')))
        assert_equals(self.metadata['meta'], self.kvs.get(KeyValueStore.Key(Scope.settings, None, None, 'meta')))
        assert_equals(None, self.kvs.get(KeyValueStore.Key(Scope.parent, None, None, 'parent')))

    def test_read_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            key = KeyValueStore.Key(scope, None, None, 'foo')
            with assert_raises(InvalidScopeError):
                self.kvs.get(key)
            assert_false(self.kvs.has(key))

    def test_read_non_dict_data(self):
        self.kvs._data = 'xml_data'
        assert_equals('xml_data', self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'data')))

    def _check_write(self, key, value):
        self.kvs.set(key, value)
        assert_equals(value, self.kvs.get(key))

    def test_write(self):
        yield (self._check_write, KeyValueStore.Key(Scope.content, None, None, 'foo'), 'new_data')
        yield (self._check_write, KeyValueStore.Key(Scope.content, None, None, 'location'), Location('i4x://org/course/category/name@new_version'))
        yield (self._check_write, KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_write, KeyValueStore.Key(Scope.settings, None, None, 'meta'), 'new_settings')

    def test_write_non_dict_data(self):
        self.kvs._data = 'xml_data'
        self._check_write(KeyValueStore.Key(Scope.content, None, None, 'data'), 'new_data')

    def test_write_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.set(KeyValueStore.Key(scope, None, None, 'foo'), 'new_value')

    def _check_delete_default(self, key, default_value):
        self.kvs.delete(key)
        assert_equals(default_value, self.kvs.get(key))
        assert self.kvs.has(key)

    def _check_delete_key_error(self, key):
        self.kvs.delete(key)
        with assert_raises(KeyError):
            self.kvs.get(key)
        assert_false(self.kvs.has(key))

    def test_delete(self):
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.content, None, None, 'foo'))
        yield (self._check_delete_default, KeyValueStore.Key(Scope.content, None, None, 'location'), Location(None))
        yield (self._check_delete_default, KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.settings, None, None, 'meta'))

    def test_delete_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with assert_raises(InvalidScopeError):
                self.kvs.delete(KeyValueStore.Key(scope, None, None, 'foo'))
Пример #20
0
 def test_read_non_dict_data(self):
     self.kvs = MongoKeyValueStore('xml_data', self.children, self.metadata)
     assert_equals(
         'xml_data',
         self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'data')))
Пример #21
0
 def setUp(self):
     self.data = {'foo': 'foo_value'}
     self.location = Location('i4x://org/course/category/name@version')
     self.children = ['i4x://org/course/child/a', 'i4x://org/course/child/b']
     self.metadata = {'meta': 'meta_val'}
     self.kvs = MongoKeyValueStore(self.data, self.children, self.metadata, self.location)
Пример #22
0
class TestMongoKeyValueStore(TestCase):
    """
    Tests for MongoKeyValueStore.
    """

    def setUp(self):
        super(TestMongoKeyValueStore, self).setUp()
        self.data = {'foo': 'foo_value'}
        self.course_id = CourseKey.from_string('org/course/run')
        self.parent = self.course_id.make_usage_key('parent', 'p')
        self.children = [self.course_id.make_usage_key('child', 'a'), self.course_id.make_usage_key('child', 'b')]
        self.metadata = {'meta': 'meta_val'}
        self.kvs = MongoKeyValueStore(self.data, self.parent, self.children, self.metadata)

    def test_read(self):
        assert self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'foo')) == self.data['foo']
        assert self.kvs.get(KeyValueStore.Key(Scope.parent, None, None, 'parent')) == self.parent
        assert self.kvs.get(KeyValueStore.Key(Scope.children, None, None, 'children')) == self.children
        assert self.kvs.get(KeyValueStore.Key(Scope.settings, None, None, 'meta')) == self.metadata['meta']

    def test_read_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            key = KeyValueStore.Key(scope, None, None, 'foo')
            with pytest.raises(InvalidScopeError):
                self.kvs.get(key)
            assert not self.kvs.has(key)

    def test_read_non_dict_data(self):
        self.kvs = MongoKeyValueStore('xml_data', self.parent, self.children, self.metadata)
        assert self.kvs.get(KeyValueStore.Key(Scope.content, None, None, 'data')) == 'xml_data'

    def _check_write(self, key, value):
        self.kvs.set(key, value)
        assert self.kvs.get(key) == value

    def test_write(self):
        yield (self._check_write, KeyValueStore.Key(Scope.content, None, None, 'foo'), 'new_data')
        yield (self._check_write, KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_write, KeyValueStore.Key(Scope.children, None, None, 'parent'), None)
        yield (self._check_write, KeyValueStore.Key(Scope.settings, None, None, 'meta'), 'new_settings')

    def test_write_non_dict_data(self):
        self.kvs = MongoKeyValueStore('xml_data', self.parent, self.children, self.metadata)
        self._check_write(KeyValueStore.Key(Scope.content, None, None, 'data'), 'new_data')

    def test_write_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state):
            with pytest.raises(InvalidScopeError):
                self.kvs.set(KeyValueStore.Key(scope, None, None, 'foo'), 'new_value')

    def _check_delete_default(self, key, default_value):
        self.kvs.delete(key)
        assert self.kvs.get(key) == default_value
        assert self.kvs.has(key)

    def _check_delete_key_error(self, key):
        self.kvs.delete(key)
        with pytest.raises(KeyError):
            self.kvs.get(key)
        assert not self.kvs.has(key)

    def test_delete(self):
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.content, None, None, 'foo'))
        yield (self._check_delete_default, KeyValueStore.Key(Scope.children, None, None, 'children'), [])
        yield (self._check_delete_key_error, KeyValueStore.Key(Scope.settings, None, None, 'meta'))

    def test_delete_invalid_scope(self):
        for scope in (Scope.preferences, Scope.user_info, Scope.user_state, Scope.parent):
            with pytest.raises(InvalidScopeError):
                self.kvs.delete(KeyValueStore.Key(scope, None, None, 'foo'))