From b1c9b2ca0effda8ebe4e31b6a5247c7fe80eb8ab Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Thu, 13 Dec 2012 16:02:23 -0500 Subject: [PATCH] Add more tests of DbModel --- .../lib/xmodule/xmodule/tests/test_runtime.py | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/common/lib/xmodule/xmodule/tests/test_runtime.py b/common/lib/xmodule/xmodule/tests/test_runtime.py index 1bbe3235b8..e71c78e4af 100644 --- a/common/lib/xmodule/xmodule/tests/test_runtime.py +++ b/common/lib/xmodule/xmodule/tests/test_runtime.py @@ -4,6 +4,7 @@ from mock import patch from xmodule.model import * from xmodule.runtime import * + class Metaclass(NamespacesMetaclass, ParentModelMetaclass, ModelMetaclass): pass @@ -53,15 +54,52 @@ class DictKeyValueStore(KeyValueStore): Usage = namedtuple('Usage', 'id, def_id') -def test_empty(): +def check_field(collection, field): + print "Getting %s from %r" % (field.name, collection) + assert_equals(field.default, getattr(collection, field.name)) + new_value = 'new ' + field.name + print "Setting %s to %s on %r" % (field.name, new_value, collection) + setattr(collection, field.name, new_value) + print "Checking %s on %r" % (field.name, collection) + assert_equals(new_value, getattr(collection, field.name)) + print "Deleting %s from %r" % (field.name, collection) + delattr(collection, field.name) + print "Back to defaults for %s in %r" % (field.name, collection) + assert_equals(field.default, getattr(collection, field.name)) + + +def test_namespace_actions(): tester = TestModel(DbModel(DictKeyValueStore(), TestModel, 's0', Usage('u0', 'd0'))) for collection in (tester, tester.test): for field in collection.fields: - print "Getting %s from %r" % (field.name, collection) - assert_equals(field.default, getattr(collection, field.name)) + yield check_field, collection, field + + +def test_db_model_keys(): + key_store = DictKeyValueStore() + tester = TestModel(DbModel(key_store, TestModel, 's0', Usage('u0', 'd0'))) + + for collection in (tester, tester.test): + for field in collection.fields: new_value = 'new ' + field.name - print "Setting %s to %s on %r" % (field.name, new_value, collection) setattr(collection, field.name, new_value) - print "Checking %s on %r" % (field.name, collection) - assert_equals(new_value, getattr(collection, field.name)) + + print key_store.db + assert_equals('new content', key_store.db[KeyValueStore.Key(Scope.content, None, 'd0', 'content')]) + assert_equals('new settings', key_store.db[KeyValueStore.Key(Scope.settings, None, 'u0', 'settings')]) + assert_equals('new student_state', key_store.db[KeyValueStore.Key(Scope.student_state, 's0', 'u0', 'student_state')]) + assert_equals('new student_preferences', key_store.db[KeyValueStore.Key(Scope.student_preferences, 's0', 'TestModel', 'student_preferences')]) + assert_equals('new student_info', key_store.db[KeyValueStore.Key(Scope.student_info, 's0', None, 'student_info')]) + assert_equals('new by_type', key_store.db[KeyValueStore.Key(Scope(False, ModuleScope.TYPE), None, 'TestModel', 'by_type')]) + assert_equals('new for_all', key_store.db[KeyValueStore.Key(Scope(False, ModuleScope.ALL), None, None, 'for_all')]) + assert_equals('new student_def', key_store.db[KeyValueStore.Key(Scope(True, ModuleScope.DEFINITION), 's0', 'd0', 'student_def')]) + + assert_equals('new n_content', key_store.db[KeyValueStore.Key(Scope.content, None, 'd0', 'n_content')]) + assert_equals('new n_settings', key_store.db[KeyValueStore.Key(Scope.settings, None, 'u0', 'n_settings')]) + assert_equals('new n_student_state', key_store.db[KeyValueStore.Key(Scope.student_state, 's0', 'u0', 'n_student_state')]) + assert_equals('new n_student_preferences', key_store.db[KeyValueStore.Key(Scope.student_preferences, 's0', 'TestModel', 'n_student_preferences')]) + assert_equals('new n_student_info', key_store.db[KeyValueStore.Key(Scope.student_info, 's0', None, 'n_student_info')]) + assert_equals('new n_by_type', key_store.db[KeyValueStore.Key(Scope(False, ModuleScope.TYPE), None, 'TestModel', 'n_by_type')]) + assert_equals('new n_for_all', key_store.db[KeyValueStore.Key(Scope(False, ModuleScope.ALL), None, None, 'n_for_all')]) + assert_equals('new n_student_def', key_store.db[KeyValueStore.Key(Scope(True, ModuleScope.DEFINITION), 's0', 'd0', 'n_student_def')])