def test_to_raw_value__on_all_defaults__should_return_the_same_string( self): obj = Field('my_name') res = obj.to_raw_value('value1') assert res == 'value1'
def test_to_raw_value__if_type_is_enum__should_return_its_value_string_representation( self): obj = Field('my_name', EnumStub) res = obj.to_raw_value(EnumStub.val2) assert res == '456'
def test_to_raw_value__if_type_set__should_return_string_representation( self, datatype, value): obj = Field('my_name', datatype) res = obj.to_raw_value(value) assert type(res) == str and res == str(value)
def test_to_field_value__if_type_set__should_return_value_of_this_type( self, datatype, value, expect): obj = Field('my_name', datatype) res = obj.to_field_value(value) assert res == expect and type(res) == type(expect)
def test_to_raw_value__if_value_has_another_type__should_raise_error( self, datatype, value): get_cb = Mock() set_cb = Mock(return_value=555) validation_cb = Mock(return_value=False) obj = Field('my_name', datatype, get_cb, set_cb, validation_cb) with pytest.raises(TypeError): obj.to_raw_value(value)
def test_to_raw_value__if_set_cb_set__should_use_its_value_and_cast_to_string( self, datatype, value, set_cb, expect): get_cb = Mock() obj = Field('my_name', datatype, get_cb, set_cb) res = obj.to_raw_value(value) assert res == expect and type(res) == type(expect) get_cb.assert_not_called()
def test_to_field_value__if_get_cb_returns_value_not_of_datatype__should_also_cast_to_datatype( self, datatype, value, get_cb, expect): set_cb = Mock() validation_cb = Mock() obj = Field('my_name', datatype, get_cb, set_cb, validation_cb) res = obj.to_field_value(value) assert res == expect and type(res) == type(expect) set_cb.assert_not_called() validation_cb.assert_not_called()
def test_to_field_value__if_get_cb_returns_none__should_return_none( self, datatype, value): set_cb = Mock() validation_cb = Mock() obj = Field('my_name', datatype, lambda _: None, set_cb, validation_cb) res = obj.to_field_value(value) assert res is None set_cb.assert_not_called() validation_cb.assert_not_called()
def test_to_raw_value__if_set_cb_and_validation_cb_passed__should_return_string_of_get_cb_value( self, datatype, value, set_cb, expect): get_cb = Mock() validation_cb = Mock(return_value=True) obj = Field('my_name', datatype, get_cb, set_cb, validation_cb) res = obj.to_raw_value(value) assert res == expect and type(res) == type(expect) get_cb.assert_not_called() validation_cb.assert_called_once_with(value)
def test_init__should_set_default_properties(self): obj = Field('my_name') assert obj._raw_name == 'my_name' assert obj._field_datatype == str assert obj._get_cb is None assert obj._set_cb is None assert obj._validation_cb is None
def test_init__should_set_properties(self): field_datatype, get_cb, set_cb, validation_cb = Mock(), Mock(), Mock( ), Mock() obj = Field('my_name', field_datatype, get_cb, set_cb, validation_cb) assert obj._raw_name == 'my_name' assert obj._field_datatype is field_datatype assert obj._get_cb is get_cb assert obj._set_cb is set_cb assert obj._validation_cb is validation_cb
def test_hash__should_be_hashable(self): obj = Field('my_name') assert hash(obj) == hash('my_name')
class ModelStub2(Model): table_name = 'test' field1 = Field('field')
def test_field_datatype_prop__should_return_field_datatype(self): obj = Field('my_name', int) assert obj.field_datatype == int
def test_raw_name_prop__should_return_raw_name(self): obj = Field('my_name') assert obj.raw_name == 'my_name'
class MyModel(Model): table_name = 'test' field1 = Field('FieldOne') field2 = Field('FieldTwo', int)
class MyModel(Model): table_name = 'test' field1 = Field('FieldOne')
class ModelStub2(Model): table_name = 'table2' field1 = Field('Field1') incremented_field = Field('IncField')
class ModelStub(Model): table_name = 'table1' incremented_field = Field('IncField', int, lambda x: int(x) + 1, lambda x: x - 1, lambda x: x > 0) append_foo_field = Field('FooField', str, lambda x: x + 'Foo', lambda x: x[:-3], lambda x: len(x) > 0)
class MyModel2(Model): table_name = 'test' field3 = Field('FieldThree') field4 = Field('FieldFour')