Files
edx-platform/xmodule/modulestore/tests/test_mongo.py
Sagirov Evgeniy c5d1807c81 feat!: remove most Old Mongo functionality (#31134)
This commit leaves behind just enough Old Mongo (DraftModulestore)
functionality to allow read-only access to static assets and the
root CourseBlock. It removes:

* create/update operations
* child/parent traversal
* inheritance related code

It also removes or converts tests for this functionality.

The ability to read from the root CourseBlock was maintained for
backwards compatibility, since top-level course settings are often
stored here, and this is used by various parts of the codebase,
like displaying dashboards and re-building CourseOverview models.

Any attempt to read the contents of a course by getting the
CourseBlock's children will return an empty list (i.e. it will look
empty).

This commit does _not_ delete content on MongoDB or run any sort of
data migration or cleanup.
2023-09-06 10:01:31 -04:00

87 lines
2.9 KiB
Python

"""
Unit tests for the Mongo modulestore
"""
import logging
import ddt
import pytest
# pylint: disable=protected-access
from django.test import TestCase
# pylint: enable=E0611
from opaque_keys.edx.keys import CourseKey
from xblock.exceptions import InvalidScopeError
from xblock.fields import Scope
from xblock.runtime import KeyValueStore
from xmodule.modulestore.mongo import MongoKeyValueStore
log = logging.getLogger(__name__)
@ddt.ddt
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.metadata)
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.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
@ddt.data(
(Scope.content, "foo", "new_data"),
(Scope.settings, "meta", "new_settings"),
)
@ddt.unpack
def test_write(self, scope, key, expected):
self._check_write(KeyValueStore.Key(scope, None, None, key), expected)
def test_write_non_dict_data(self):
self.kvs = MongoKeyValueStore('xml_data', 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')
@ddt.data(
(Scope.content, "foo"),
(Scope.settings, "meta"),
)
@ddt.unpack
def test_delete_key_error(self, scope, expected):
key = KeyValueStore.Key(scope, None, None, expected)
self.kvs.delete(key)
with pytest.raises(KeyError):
self.kvs.get(key)
assert not self.kvs.has(key)
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'))