test_model_data.py 16.9 KB
Newer Older
1 2 3 4 5 6 7
import factory
import json
from mock import Mock
from django.contrib.auth.models import User

from functools import partial

8 9
from courseware.model_data import LmsKeyValueStore, InvalidWriteError, InvalidScopeError, ModelDataCache
from courseware.models import StudentModule, XModuleContentField, XModuleSettingsField, XModuleStudentInfoField, XModuleStudentPrefsField
10
from xblock.core import Scope, BlockScope
11 12 13 14 15
from xmodule.modulestore import Location

from django.test import TestCase


16 17 18 19 20 21 22
def mock_field(scope, name):
    field = Mock()
    field.scope = scope
    field.name = name
    return field

def mock_descriptor(fields=[], lms_fields=[]):
23 24 25
    descriptor = Mock()
    descriptor.stores_state = True
    descriptor.location = location('def_id')
26 27
    descriptor.module_class.fields = fields
    descriptor.module_class.lms.fields = lms_fields
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    return descriptor

location = partial(Location, 'i4x', 'edX', 'test_course', 'problem')
course_id = 'edX/test_course/test'

content_key = partial(LmsKeyValueStore.Key, Scope.content, None, location('def_id'))
settings_key = partial(LmsKeyValueStore.Key, Scope.settings, None, location('def_id'))
student_state_key = partial(LmsKeyValueStore.Key, Scope.student_state, 'user', location('def_id'))
student_prefs_key = partial(LmsKeyValueStore.Key, Scope.student_preferences, 'user', 'problem')
student_info_key = partial(LmsKeyValueStore.Key, Scope.student_info, 'user', None)


class UserFactory(factory.Factory):
    FACTORY_FOR = User

    username = 'user'


class StudentModuleFactory(factory.Factory):
    FACTORY_FOR = StudentModule

    module_type = 'problem'
    module_state_key = location('def_id').url()
    student = factory.SubFactory(UserFactory)
    course_id = course_id
    state = None


class ContentFactory(factory.Factory):
    FACTORY_FOR = XModuleContentField

    field_name = 'content_field'
    value = json.dumps('content_value')
    definition_id = location('def_id').url()


class SettingsFactory(factory.Factory):
    FACTORY_FOR = XModuleSettingsField

    field_name = 'settings_field'
    value = json.dumps('settings_value')
    usage_id = '%s-%s' % (course_id, location('def_id').url())


class StudentPrefsFactory(factory.Factory):
    FACTORY_FOR = XModuleStudentPrefsField

    field_name = 'student_pref_field'
    value = json.dumps('student_pref_value')
    student = factory.SubFactory(UserFactory)
    module_type = 'problem'


class StudentInfoFactory(factory.Factory):
    FACTORY_FOR = XModuleStudentInfoField

    field_name = 'student_info_field'
    value = json.dumps('student_info_value')
    student = factory.SubFactory(UserFactory)


class TestDescriptorFallback(TestCase):

    def setUp(self):
        self.desc_md = {
            'field_a': 'content',
            'field_b': 'settings',
        }
96
        self.kvs = LmsKeyValueStore(self.desc_md, None)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    def test_get_from_descriptor(self):
        self.assertEquals('content', self.kvs.get(content_key('field_a')))
        self.assertEquals('settings', self.kvs.get(settings_key('field_b')))

    def test_write_to_descriptor(self):
        self.assertRaises(InvalidWriteError, self.kvs.set, content_key('field_a'), 'foo')
        self.assertEquals('content', self.desc_md['field_a'])
        self.assertRaises(InvalidWriteError, self.kvs.set, settings_key('field_b'), 'foo')
        self.assertEquals('settings', self.desc_md['field_b'])

        self.assertRaises(InvalidWriteError, self.kvs.delete, content_key('field_a'))
        self.assertEquals('content', self.desc_md['field_a'])
        self.assertRaises(InvalidWriteError, self.kvs.delete, settings_key('field_b'))
        self.assertEquals('settings', self.desc_md['field_b'])


class TestInvalidScopes(TestCase):
    def setUp(self):
        self.desc_md = {}
117
        self.user = UserFactory.create()
118 119
        self.mdc = ModelDataCache([mock_descriptor([mock_field(Scope.student_state, 'a_field')])], course_id, self.user)
        self.kvs = LmsKeyValueStore(self.desc_md, self.mdc)
120 121

    def test_invalid_scopes(self):
122 123 124
        for scope in (Scope(student=True, block=BlockScope.DEFINITION),
                      Scope(student=False, block=BlockScope.TYPE),
                      Scope(student=False, block=BlockScope.ALL)):
125 126 127 128 129 130 131 132 133
            self.assertRaises(InvalidScopeError, self.kvs.get, LmsKeyValueStore.Key(scope, None, None, 'field'))
            self.assertRaises(InvalidScopeError, self.kvs.set, LmsKeyValueStore.Key(scope, None, None, 'field'), 'value')
            self.assertRaises(InvalidScopeError, self.kvs.delete, LmsKeyValueStore.Key(scope, None, None, 'field'))


class TestStudentModuleStorage(TestCase):

    def setUp(self):
        self.desc_md = {}
134 135
        student_module = StudentModuleFactory(state=json.dumps({'a_field': 'a_value'}))
        self.user = student_module.student
Calen Pennington committed
136
        self.mdc = ModelDataCache([mock_descriptor([mock_field(Scope.student_state, 'a_field')])], course_id, self.user)
137
        self.kvs = LmsKeyValueStore(self.desc_md, self.mdc)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    def test_get_existing_field(self):
        "Test that getting an existing field in an existing StudentModule works"
        self.assertEquals('a_value', self.kvs.get(student_state_key('a_field')))

    def test_get_missing_field(self):
        "Test that getting a missing field from an existing StudentModule raises a KeyError"
        self.assertRaises(KeyError, self.kvs.get, student_state_key('not_a_field'))

    def test_set_existing_field(self):
        "Test that setting an existing student_state field changes the value"
        self.kvs.set(student_state_key('a_field'), 'new_value')
        self.assertEquals(1, StudentModule.objects.all().count())
        self.assertEquals({'a_field': 'new_value'}, json.loads(StudentModule.objects.all()[0].state))

    def test_set_missing_field(self):
        "Test that setting a new student_state field changes the value"
        self.kvs.set(student_state_key('not_a_field'), 'new_value')
        self.assertEquals(1, StudentModule.objects.all().count())
        self.assertEquals({'a_field': 'a_value', 'not_a_field': 'new_value'}, json.loads(StudentModule.objects.all()[0].state))

    def test_delete_existing_field(self):
        "Test that deleting an existing field removes it from the StudentModule"
        self.kvs.delete(student_state_key('a_field'))
        self.assertEquals(1, StudentModule.objects.all().count())
163
        self.assertRaises(KeyError, self.kvs.get, student_state_key('not_a_field'))
164 165 166 167 168 169 170 171 172 173 174 175

    def test_delete_missing_field(self):
        "Test that deleting a missing field from an existing StudentModule raises a KeyError"
        self.assertRaises(KeyError, self.kvs.delete, student_state_key('not_a_field'))
        self.assertEquals(1, StudentModule.objects.all().count())
        self.assertEquals({'a_field': 'a_value'}, json.loads(StudentModule.objects.all()[0].state))


class TestMissingStudentModule(TestCase):
    def setUp(self):
        self.user = UserFactory.create()
        self.desc_md = {}
176 177
        self.mdc = ModelDataCache([mock_descriptor()], course_id, self.user)
        self.kvs = LmsKeyValueStore(self.desc_md, self.mdc)
178 179 180 181 182 183 184

    def test_get_field_from_missing_student_module(self):
        "Test that getting a field from a missing StudentModule raises a KeyError"
        self.assertRaises(KeyError, self.kvs.get, student_state_key('a_field'))

    def test_set_field_in_missing_student_module(self):
        "Test that setting a field in a missing StudentModule creates the student module"
185
        self.assertEquals(0, len(self.mdc.cache))
186 187 188 189
        self.assertEquals(0, StudentModule.objects.all().count())

        self.kvs.set(student_state_key('a_field'), 'a_value')

190
        self.assertEquals(1, len(self.mdc.cache))
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        self.assertEquals(1, StudentModule.objects.all().count())

        student_module = StudentModule.objects.all()[0]
        self.assertEquals({'a_field': 'a_value'}, json.loads(student_module.state))
        self.assertEquals(self.user, student_module.student)
        self.assertEquals(location('def_id').url(), student_module.module_state_key)
        self.assertEquals(course_id, student_module.course_id)

    def test_delete_field_from_missing_student_module(self):
        "Test that deleting a field from a missing StudentModule raises a KeyError"
        self.assertRaises(KeyError, self.kvs.delete, student_state_key('a_field'))


class TestSettingsStorage(TestCase):

    def setUp(self):
        settings = SettingsFactory.create()
        self.user = UserFactory.create()
        self.desc_md = {}
210
        self.mdc = ModelDataCache([mock_descriptor([mock_field(Scope.settings, 'settings_field')])], course_id, self.user)
211
        self.kvs = LmsKeyValueStore(self.desc_md, self.mdc)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

    def test_get_existing_field(self):
        "Test that getting an existing field in an existing SettingsField works"
        self.assertEquals('settings_value', self.kvs.get(settings_key('settings_field')))

    def test_get_missing_field(self):
        "Test that getting a missing field from an existing SettingsField raises a KeyError"
        self.assertRaises(KeyError, self.kvs.get, settings_key('not_settings_field'))

    def test_set_existing_field(self):
        "Test that setting an existing field changes the value"
        self.kvs.set(settings_key('settings_field'), 'new_value')
        self.assertEquals(1, XModuleSettingsField.objects.all().count())
        self.assertEquals('new_value', json.loads(XModuleSettingsField.objects.all()[0].value))

    def test_set_missing_field(self):
        "Test that setting a new field changes the value"
        self.kvs.set(settings_key('not_settings_field'), 'new_value')
        self.assertEquals(2, XModuleSettingsField.objects.all().count())
        self.assertEquals('settings_value', json.loads(XModuleSettingsField.objects.get(field_name='settings_field').value))
        self.assertEquals('new_value', json.loads(XModuleSettingsField.objects.get(field_name='not_settings_field').value))

    def test_delete_existing_field(self):
        "Test that deleting an existing field removes it"
        self.kvs.delete(settings_key('settings_field'))
        self.assertEquals(0, XModuleSettingsField.objects.all().count())

    def test_delete_missing_field(self):
        "Test that deleting a missing field from an existing SettingsField raises a KeyError"
        self.assertRaises(KeyError, self.kvs.delete, settings_key('not_settings_field'))
        self.assertEquals(1, XModuleSettingsField.objects.all().count())


class TestContentStorage(TestCase):

    def setUp(self):
        content = ContentFactory.create()
        self.user = UserFactory.create()
        self.desc_md = {}
251
        self.mdc = ModelDataCache([mock_descriptor([mock_field(Scope.content, 'content_field')])], course_id, self.user)
252
        self.kvs = LmsKeyValueStore(self.desc_md, self.mdc)
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

    def test_get_existing_field(self):
        "Test that getting an existing field in an existing ContentField works"
        self.assertEquals('content_value', self.kvs.get(content_key('content_field')))

    def test_get_missing_field(self):
        "Test that getting a missing field from an existing ContentField raises a KeyError"
        self.assertRaises(KeyError, self.kvs.get, content_key('not_content_field'))

    def test_set_existing_field(self):
        "Test that setting an existing field changes the value"
        self.kvs.set(content_key('content_field'), 'new_value')
        self.assertEquals(1, XModuleContentField.objects.all().count())
        self.assertEquals('new_value', json.loads(XModuleContentField.objects.all()[0].value))

    def test_set_missing_field(self):
        "Test that setting a new field changes the value"
        self.kvs.set(content_key('not_content_field'), 'new_value')
        self.assertEquals(2, XModuleContentField.objects.all().count())
        self.assertEquals('content_value', json.loads(XModuleContentField.objects.get(field_name='content_field').value))
        self.assertEquals('new_value', json.loads(XModuleContentField.objects.get(field_name='not_content_field').value))

    def test_delete_existing_field(self):
        "Test that deleting an existing field removes it"
        self.kvs.delete(content_key('content_field'))
        self.assertEquals(0, XModuleContentField.objects.all().count())

    def test_delete_missing_field(self):
        "Test that deleting a missing field from an existing ContentField raises a KeyError"
        self.assertRaises(KeyError, self.kvs.delete, content_key('not_content_field'))
        self.assertEquals(1, XModuleContentField.objects.all().count())


class TestStudentPrefsStorage(TestCase):

    def setUp(self):
        student_pref = StudentPrefsFactory.create()
        self.user = student_pref.student
        self.desc_md = {}
292 293 294 295 296
        self.mdc = ModelDataCache([mock_descriptor([
            mock_field(Scope.student_preferences, 'student_pref_field'),
            mock_field(Scope.student_preferences, 'not_student_pref_field'),
        ])], course_id, self.user)
        self.kvs = LmsKeyValueStore(self.desc_md, self.mdc)
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

    def test_get_existing_field(self):
        "Test that getting an existing field in an existing StudentPrefsField works"
        self.assertEquals('student_pref_value', self.kvs.get(student_prefs_key('student_pref_field')))

    def test_get_missing_field(self):
        "Test that getting a missing field from an existing StudentPrefsField raises a KeyError"
        self.assertRaises(KeyError, self.kvs.get, student_prefs_key('not_student_pref_field'))

    def test_set_existing_field(self):
        "Test that setting an existing field changes the value"
        self.kvs.set(student_prefs_key('student_pref_field'), 'new_value')
        self.assertEquals(1, XModuleStudentPrefsField.objects.all().count())
        self.assertEquals('new_value', json.loads(XModuleStudentPrefsField.objects.all()[0].value))

    def test_set_missing_field(self):
        "Test that setting a new field changes the value"
        self.kvs.set(student_prefs_key('not_student_pref_field'), 'new_value')
        self.assertEquals(2, XModuleStudentPrefsField.objects.all().count())
        self.assertEquals('student_pref_value', json.loads(XModuleStudentPrefsField.objects.get(field_name='student_pref_field').value))
        self.assertEquals('new_value', json.loads(XModuleStudentPrefsField.objects.get(field_name='not_student_pref_field').value))

    def test_delete_existing_field(self):
        "Test that deleting an existing field removes it"
        self.kvs.delete(student_prefs_key('student_pref_field'))
        self.assertEquals(0, XModuleStudentPrefsField.objects.all().count())

    def test_delete_missing_field(self):
        "Test that deleting a missing field from an existing StudentPrefsField raises a KeyError"
        self.assertRaises(KeyError, self.kvs.delete, student_prefs_key('not_student_pref_field'))
        self.assertEquals(1, XModuleStudentPrefsField.objects.all().count())


class TestStudentInfoStorage(TestCase):

    def setUp(self):
        student_info = StudentInfoFactory.create()
        self.user = student_info.student
        self.desc_md = {}
336 337 338 339 340
        self.mdc = ModelDataCache([mock_descriptor([
            mock_field(Scope.student_info, 'student_info_field'),
            mock_field(Scope.student_info, 'not_student_info_field'),
        ])], course_id, self.user)
        self.kvs = LmsKeyValueStore(self.desc_md, self.mdc)
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

    def test_get_existing_field(self):
        "Test that getting an existing field in an existing StudentInfoField works"
        self.assertEquals('student_info_value', self.kvs.get(student_info_key('student_info_field')))

    def test_get_missing_field(self):
        "Test that getting a missing field from an existing StudentInfoField raises a KeyError"
        self.assertRaises(KeyError, self.kvs.get, student_info_key('not_student_info_field'))

    def test_set_existing_field(self):
        "Test that setting an existing field changes the value"
        self.kvs.set(student_info_key('student_info_field'), 'new_value')
        self.assertEquals(1, XModuleStudentInfoField.objects.all().count())
        self.assertEquals('new_value', json.loads(XModuleStudentInfoField.objects.all()[0].value))

    def test_set_missing_field(self):
        "Test that setting a new field changes the value"
        self.kvs.set(student_info_key('not_student_info_field'), 'new_value')
        self.assertEquals(2, XModuleStudentInfoField.objects.all().count())
        self.assertEquals('student_info_value', json.loads(XModuleStudentInfoField.objects.get(field_name='student_info_field').value))
        self.assertEquals('new_value', json.loads(XModuleStudentInfoField.objects.get(field_name='not_student_info_field').value))

    def test_delete_existing_field(self):
        "Test that deleting an existing field removes it"
        self.kvs.delete(student_info_key('student_info_field'))
        self.assertEquals(0, XModuleStudentInfoField.objects.all().count())

    def test_delete_missing_field(self):
        "Test that deleting a missing field from an existing StudentInfoField raises a KeyError"
        self.assertRaises(KeyError, self.kvs.delete, student_info_key('not_student_info_field'))
        self.assertEquals(1, XModuleStudentInfoField.objects.all().count())