def test_db_model_keys(): # Tests that updates to fields are properly recorded in the KeyValueStore, # and that the keys have been constructed correctly key_store = DictKeyValueStore() db_model = DbModel(key_store) runtime = Runtime(Mock(), db_model, [TestMixin]) tester = runtime.construct_xblock_from_class(TestXBlock, ScopeIds('s0', 'TestXBlock', 'd0', 'u0')) assert_false(db_model.has(tester, 'not a field')) for field in tester.fields.values(): new_value = 'new ' + field.name assert_false(db_model.has(tester, field.name)) setattr(tester, field.name, new_value) # Write out the values tester.save() # Make sure everything saved correctly for field in tester.fields.values(): assert_true(db_model.has(tester, field.name)) def get_key_value(scope, user_id, block_scope_id, field_name): """Gets the value, from `key_store`, of a Key with the given values.""" new_key = KeyValueStore.Key(scope, user_id, block_scope_id, field_name) return key_store.db_dict[new_key] # Examine each value in the database and ensure that keys were constructed correctly assert_equals('new content', get_key_value(Scope.content, None, 'd0', 'content')) assert_equals('new settings', get_key_value(Scope.settings, None, 'u0', 'settings')) assert_equals('new user_state', get_key_value(Scope.user_state, 's0', 'u0', 'user_state')) assert_equals('new preferences', get_key_value(Scope.preferences, 's0', 'TestXBlock', 'preferences')) assert_equals('new user_info', get_key_value(Scope.user_info, 's0', None, 'user_info')) assert_equals('new by_type', get_key_value(Scope(UserScope.NONE, BlockScope.TYPE), None, 'TestXBlock', 'by_type')) assert_equals('new for_all', get_key_value(Scope(UserScope.NONE, BlockScope.ALL), None, None, 'for_all')) assert_equals('new user_def', get_key_value(Scope(UserScope.ONE, BlockScope.DEFINITION), 's0', 'd0', 'user_def')) assert_equals('new agg_global', get_key_value(Scope(UserScope.ALL, BlockScope.ALL), None, None, 'agg_global')) assert_equals('new agg_type', get_key_value(Scope(UserScope.ALL, BlockScope.TYPE), None, 'TestXBlock', 'agg_type')) assert_equals('new agg_def', get_key_value(Scope(UserScope.ALL, BlockScope.DEFINITION), None, 'd0', 'agg_def')) assert_equals('new agg_usage', get_key_value(Scope.user_state_summary, None, 'u0', 'agg_usage')) assert_equals('new mixin_content', get_key_value(Scope.content, None, 'd0', 'mixin_content')) assert_equals('new mixin_settings', get_key_value(Scope.settings, None, 'u0', 'mixin_settings')) assert_equals('new mixin_user_state', get_key_value(Scope.user_state, 's0', 'u0', 'mixin_user_state')) assert_equals('new mixin_preferences', get_key_value(Scope.preferences, 's0', 'TestXBlock', 'mixin_preferences')) assert_equals('new mixin_user_info', get_key_value(Scope.user_info, 's0', None, 'mixin_user_info')) assert_equals('new mixin_by_type', get_key_value(Scope(UserScope.NONE, BlockScope.TYPE), None, 'TestXBlock', 'mixin_by_type')) assert_equals('new mixin_for_all', get_key_value(Scope(UserScope.NONE, BlockScope.ALL), None, None, 'mixin_for_all')) assert_equals('new mixin_user_def', get_key_value(Scope(UserScope.ONE, BlockScope.DEFINITION), 's0', 'd0', 'mixin_user_def')) assert_equals('new mixin_agg_global', get_key_value(Scope(UserScope.ALL, BlockScope.ALL), None, None, 'mixin_agg_global')) assert_equals('new mixin_agg_type', get_key_value(Scope(UserScope.ALL, BlockScope.TYPE), None, 'TestXBlock', 'mixin_agg_type')) assert_equals('new mixin_agg_def', get_key_value(Scope(UserScope.ALL, BlockScope.DEFINITION), None, 'd0', 'mixin_agg_def')) assert_equals('new mixin_agg_usage', get_key_value(Scope.user_state_summary, None, 'u0', 'mixin_agg_usage'))
def test_default_fn(): key_store = SerialDefaultKVS() db_model = DbModel(key_store) tester = TestIntegerXblock(Mock(), db_model, Mock()) tester2 = TestIntegerXblock(Mock(), db_model, Mock()) # ensure value is not in tester before any actions assert_false(db_model.has(tester, 'counter')) # ensure value is same over successive calls for same DbModel first_call = tester.counter assert_equals(first_call, 1) assert_equals(first_call, tester.counter) # ensure the value is not saved in the object assert_false(db_model.has(tester, 'counter')) # ensure save does not save the computed default back to the object tester.save() assert_false(db_model.has(tester, 'counter')) # ensure second object gets another value second_call = tester2.counter assert_equals(second_call, 2)