def test_change_mutable_default(): """ Ensure that mutating the default value for a field causes the changes to be saved, and doesn't corrupt other instances """ class MutableTester(XBlock): """Test class with mutable fields.""" list_field = List() field_data_a = DictFieldData({}) mutable_test_a = MutableTester(TestRuntime(services={'field-data': field_data_a}), scope_ids=Mock(spec=ScopeIds)) field_data_b = DictFieldData({}) mutable_test_b = MutableTester(TestRuntime(services={'field-data': field_data_b}), scope_ids=Mock(spec=ScopeIds)) # Saving without changing the default value shouldn't write to field_data mutable_test_a.list_field # pylint: disable=W0104 mutable_test_a.save() with assert_raises(KeyError): field_data_a.get(mutable_test_a, 'list_field') mutable_test_a.list_field.append(1) mutable_test_a.save() assert_equals([1], field_data_a.get(mutable_test_a, 'list_field')) with assert_raises(KeyError): field_data_b.get(mutable_test_b, 'list_field')
def test_dict_field_access(): # Check that dicts are correctly saved when not directly set class FieldTester(XBlock): """Test XBlock for field access testing""" field_a = Dict(scope=Scope.settings) field_b = Dict(scope=Scope.content, default={'a': 1, 'b': 2, 'c': 3}) field_c = Dict(scope=Scope.content, default={'a': 4, 'b': 5, 'c': 6}) field_d = Dict(scope=Scope.settings) field_data = DictFieldData({ 'field_a': {'a': 200}, 'field_b': {'a': 11, 'b': 12, 'c': 13} }) field_tester = FieldTester( TestRuntime(services={'field-data': field_data}), None, Mock() ) # Check initial values have been set properly assert_equals({'a': 200}, field_tester.field_a) assert_equals({'a': 11, 'b': 12, 'c': 13}, field_tester.field_b) assert_equals({'a': 4, 'b': 5, 'c': 6}, field_tester.field_c) assert_equals({}, field_tester.field_d) # Update the fields field_tester.field_a['a'] = 250 field_tester.field_b['d'] = 14 field_tester.field_c['a'] = 0 field_tester.field_d['new'] = 'value' # The fields should be update in the cache, but /not/ in the underlying kvstore. assert_equals({'a': 250}, field_tester.field_a) assert_equals({'a': 11, 'b': 12, 'c': 13, 'd': 14}, field_tester.field_b) assert_equals({'a': 0, 'b': 5, 'c': 6}, field_tester.field_c) assert_equals({'new': 'value'}, field_tester.field_d) # Examine model data directly # Caveat: there's not a clean way to copy the originally provided values for `field_a` and `field_b` # when we instantiate the XBlock. So, the values for those two in both `field_data` and `_field_data_cache` # point at the same object. Thus, `field_a` and `field_b` actually have the correct values in # `field_data` right now. `field_c` does not, because it has never been written to the `field_data`. assert_false(field_data.has(field_tester, 'field_c')) assert_false(field_data.has(field_tester, 'field_d')) field_tester.save() # verify that the fields have been updated correctly assert_equals({'a': 250}, field_tester.field_a) assert_equals({'a': 11, 'b': 12, 'c': 13, 'd': 14}, field_tester.field_b) assert_equals({'a': 0, 'b': 5, 'c': 6}, field_tester.field_c) assert_equals({'new': 'value'}, field_tester.field_d) # Now, the fields should be updated in the underlying kvstore assert_equals({'a': 250}, field_data.get(field_tester, 'field_a')) assert_equals({'a': 11, 'b': 12, 'c': 13, 'd': 14}, field_data.get(field_tester, 'field_b')) assert_equals({'a': 0, 'b': 5, 'c': 6}, field_data.get(field_tester, 'field_c')) assert_equals({'new': 'value'}, field_data.get(field_tester, 'field_d'))
def test_list_field_access(): # Check that lists are correctly saved when not directly set class FieldTester(XBlock): """Test XBlock for field access testing""" field_a = List(scope=Scope.settings) field_b = List(scope=Scope.content, default=[1, 2, 3]) field_c = List(scope=Scope.content, default=[4, 5, 6]) field_d = List(scope=Scope.settings) field_data = DictFieldData({'field_a': [200], 'field_b': [11, 12, 13]}) field_tester = FieldTester( TestRuntime(services={'field-data': field_data}), scope_ids=Mock(spec=ScopeIds)) # Check initial values have been set properly assert_equals([200], field_tester.field_a) assert_equals([11, 12, 13], field_tester.field_b) assert_equals([4, 5, 6], field_tester.field_c) assert_equals([], field_tester.field_d) # Update the fields field_tester.field_a.append(1) field_tester.field_b.append(14) field_tester.field_c.append(7) field_tester.field_d.append(1) # The fields should be update in the cache, but /not/ in the underlying kvstore. assert_equals([200, 1], field_tester.field_a) assert_equals([11, 12, 13, 14], field_tester.field_b) assert_equals([4, 5, 6, 7], field_tester.field_c) assert_equals([1], field_tester.field_d) # Examine model data directly # Caveat: there's not a clean way to copy the originally provided values for `field_a` and `field_b` # when we instantiate the XBlock. So, the values for those two in both `field_data` and `_field_data_cache` # point at the same object. Thus, `field_a` and `field_b` actually have the correct values in # `field_data` right now. `field_c` does not, because it has never been written to the `field_data`. assert_false(field_data.has(field_tester, 'field_c')) assert_false(field_data.has(field_tester, 'field_d')) # save the XBlock field_tester.save() # verify that the fields have been updated correctly assert_equals([200, 1], field_tester.field_a) assert_equals([11, 12, 13, 14], field_tester.field_b) assert_equals([4, 5, 6, 7], field_tester.field_c) assert_equals([1], field_tester.field_d) # Now, the fields should be updated in the underlying kvstore assert_equals([200, 1], field_data.get(field_tester, 'field_a')) assert_equals([11, 12, 13, 14], field_data.get(field_tester, 'field_b')) assert_equals([4, 5, 6, 7], field_data.get(field_tester, 'field_c')) assert_equals([1], field_data.get(field_tester, 'field_d'))
def test_list_field_access(): # Check that lists are correctly saved when not directly set class FieldTester(XBlock): """Test XBlock for field access testing""" field_a = List(scope=Scope.settings) field_b = List(scope=Scope.content, default=[1, 2, 3]) field_c = List(scope=Scope.content, default=[4, 5, 6]) field_d = List(scope=Scope.settings) field_data = DictFieldData({"field_a": [200], "field_b": [11, 12, 13]}) field_tester = FieldTester(TestRuntime(services={"field-data": field_data}), scope_ids=Mock(spec=ScopeIds)) # Check initial values have been set properly assert_equals([200], field_tester.field_a) assert_equals([11, 12, 13], field_tester.field_b) assert_equals([4, 5, 6], field_tester.field_c) assert_equals([], field_tester.field_d) # Update the fields field_tester.field_a.append(1) field_tester.field_b.append(14) field_tester.field_c.append(7) field_tester.field_d.append(1) # The fields should be update in the cache, but /not/ in the underlying kvstore. assert_equals([200, 1], field_tester.field_a) assert_equals([11, 12, 13, 14], field_tester.field_b) assert_equals([4, 5, 6, 7], field_tester.field_c) assert_equals([1], field_tester.field_d) # Examine model data directly # Caveat: there's not a clean way to copy the originally provided values for `field_a` and `field_b` # when we instantiate the XBlock. So, the values for those two in both `field_data` and `_field_data_cache` # point at the same object. Thus, `field_a` and `field_b` actually have the correct values in # `field_data` right now. `field_c` does not, because it has never been written to the `field_data`. assert_false(field_data.has(field_tester, "field_c")) assert_false(field_data.has(field_tester, "field_d")) # save the XBlock field_tester.save() # verify that the fields have been updated correctly assert_equals([200, 1], field_tester.field_a) assert_equals([11, 12, 13, 14], field_tester.field_b) assert_equals([4, 5, 6, 7], field_tester.field_c) assert_equals([1], field_tester.field_d) # Now, the fields should be updated in the underlying kvstore assert_equals([200, 1], field_data.get(field_tester, "field_a")) assert_equals([11, 12, 13, 14], field_data.get(field_tester, "field_b")) assert_equals([4, 5, 6, 7], field_data.get(field_tester, "field_c")) assert_equals([1], field_data.get(field_tester, "field_d"))
def test_field_serialization(): # Some Fields can define their own serialization mechanisms. # This test ensures that we are using them properly. class CustomField(Field): """ Specifiy a custom field that defines its own serialization """ def from_json(self, value): return value['value'] def to_json(self, value): return {'value': value} class FieldTester(XBlock): """Test XBlock for field serialization testing""" field = CustomField() field_data = DictFieldData({ 'field': {'value': 4} }) field_tester = FieldTester( TestRuntime(services={'field-data': field_data}), None, Mock(), ) assert_equals(4, field_tester.field) field_tester.field = 5 field_tester.save() assert_equals({'value': 5}, field_data.get(field_tester, 'field'))
def test_field_access(): class FieldTester(XBlock): """Test XBlock for field access testing""" field_a = Integer(scope=Scope.settings) field_b = Integer(scope=Scope.content, default=10) field_c = Integer(scope=Scope.user_state, default=42) float_a = Float(scope=Scope.settings, default=5.8) float_b = Float(scope=Scope.settings) field_data = DictFieldData({'field_a': 5, 'float_a': 6.1, 'field_x': 15}) field_tester = FieldTester( TestRuntime(services={'field-data': field_data}), scope_ids=Mock()) # Verify that the fields have been set assert_equals(5, field_tester.field_a) assert_equals(10, field_tester.field_b) assert_equals(42, field_tester.field_c) assert_equals(6.1, field_tester.float_a) assert_equals(None, field_tester.float_b) assert not hasattr(field_tester, 'field_x') # Set two of the fields. field_tester.field_a = 20 field_tester.float_a = 20.5 # field_a should be updated in the cache, but /not/ in the underlying db. assert_equals(20, field_tester.field_a) assert_equals(20.5, field_tester.float_a) assert_equals(5, field_data.get(field_tester, 'field_a')) assert_equals(6.1, field_data.get(field_tester, 'float_a')) # save the XBlock field_tester.save() # verify that the fields have been updated correctly assert_equals(20, field_tester.field_a) assert_equals(20.5, field_tester.float_a) # Now, field_a should be updated in the underlying db assert_equals(20, field_data.get(field_tester, 'field_a')) assert_equals(20.5, field_data.get(field_tester, 'float_a')) assert_equals(10, field_tester.field_b) assert_equals(42, field_tester.field_c) assert_equals(None, field_tester.float_b) # Deletes happen immediately (do not require a save) del field_tester.field_a del field_tester.float_a # After delete, we should find default values in the cache assert_equals(None, field_tester.field_a) assert_equals(5.8, field_tester.float_a) # But the fields should not actually be present in the underlying kvstore with assert_raises(KeyError): field_data.get(field_tester, 'field_a') assert_false(field_data.has(field_tester, 'field_a')) with assert_raises(KeyError): field_data.get(field_tester, 'float_a') assert_false(field_data.has(field_tester, 'float_a'))
def test_field_access(): class FieldTester(XBlock): """Test XBlock for field access testing""" field_a = Integer(scope=Scope.settings) field_b = Integer(scope=Scope.content, default=10) field_c = Integer(scope=Scope.user_state, default=42) float_a = Float(scope=Scope.settings, default=5.8) float_b = Float(scope=Scope.settings) field_data = DictFieldData({"field_a": 5, "float_a": 6.1, "field_x": 15}) field_tester = FieldTester(TestRuntime(services={"field-data": field_data}), scope_ids=Mock()) # Verify that the fields have been set assert_equals(5, field_tester.field_a) assert_equals(10, field_tester.field_b) assert_equals(42, field_tester.field_c) assert_equals(6.1, field_tester.float_a) assert_equals(None, field_tester.float_b) assert not hasattr(field_tester, "field_x") # Set two of the fields. field_tester.field_a = 20 field_tester.float_a = 20.5 # field_a should be updated in the cache, but /not/ in the underlying db. assert_equals(20, field_tester.field_a) assert_equals(20.5, field_tester.float_a) assert_equals(5, field_data.get(field_tester, "field_a")) assert_equals(6.1, field_data.get(field_tester, "float_a")) # save the XBlock field_tester.save() # verify that the fields have been updated correctly assert_equals(20, field_tester.field_a) assert_equals(20.5, field_tester.float_a) # Now, field_a should be updated in the underlying db assert_equals(20, field_data.get(field_tester, "field_a")) assert_equals(20.5, field_data.get(field_tester, "float_a")) assert_equals(10, field_tester.field_b) assert_equals(42, field_tester.field_c) assert_equals(None, field_tester.float_b) # Deletes happen immediately (do not require a save) del field_tester.field_a del field_tester.float_a # After delete, we should find default values in the cache assert_equals(None, field_tester.field_a) assert_equals(5.8, field_tester.float_a) # But the fields should not actually be present in the underlying kvstore with assert_raises(KeyError): field_data.get(field_tester, "field_a") assert_false(field_data.has(field_tester, "field_a")) with assert_raises(KeyError): field_data.get(field_tester, "float_a") assert_false(field_data.has(field_tester, "float_a"))
def test_field_access(): class FieldTester(XBlock): """Test XBlock for field access testing""" field_a = Integer(scope=Scope.settings) field_b = Integer(scope=Scope.content, default=10) field_c = Integer(scope=Scope.user_state, default=42) float_a = Float(scope=Scope.settings, default=5.8) float_b = Float(scope=Scope.settings) field_data = DictFieldData({'field_a': 5, 'float_a': 6.1, 'field_x': 15}) field_tester = FieldTester(TestRuntime(services={'field-data': field_data}), scope_ids=Mock()) # Verify that the fields have been set assert field_tester.field_a == 5 assert field_tester.field_b == 10 assert field_tester.field_c == 42 assert field_tester.float_a == 6.1 assert field_tester.float_b is None assert not hasattr(field_tester, 'field_x') # Set two of the fields. field_tester.field_a = 20 field_tester.float_a = 20.5 # field_a should be updated in the cache, but /not/ in the underlying db. assert field_tester.field_a == 20 assert field_tester.float_a == 20.5 assert field_data.get(field_tester, 'field_a') == 5 assert field_data.get(field_tester, 'float_a') == 6.1 # save the XBlock field_tester.save() # verify that the fields have been updated correctly assert field_tester.field_a == 20 assert field_tester.float_a == 20.5 # Now, field_a should be updated in the underlying db assert field_data.get(field_tester, 'field_a') == 20 assert field_data.get(field_tester, 'float_a') == 20.5 assert field_tester.field_b == 10 assert field_tester.field_c == 42 assert field_tester.float_b is None # Deletes happen immediately (do not require a save) del field_tester.field_a del field_tester.float_a # After delete, we should find default values in the cache assert field_tester.field_a is None assert field_tester.float_a == 5.8 # But the fields should not actually be present in the underlying kvstore with pytest.raises(KeyError): field_data.get(field_tester, 'field_a') assert not field_data.has(field_tester, 'field_a') with pytest.raises(KeyError): field_data.get(field_tester, 'float_a') assert not field_data.has(field_tester, 'float_a')