def test_set_value_does_not_set_user_entered_flag(self): f = self.create_field() bf = BoundField(f, StubModel()) bf.set_value(Decimal("100.1"), user_entered=False) self.assertEquals(Decimal("100.1"), bf.value) self.assertEquals(True, bf.has_value) self.assertEquals(False, bf.has_user_entered_value)
def test_set_value_does_not_set_user_entered_flag(self): f = self.create_field() bf = BoundField(f , StubModel()) bf.set_value(Decimal("100.1"),user_entered=False) self.assertEquals(Decimal("100.1"), bf.value) self.assertEquals(True, bf.has_value) self.assertEquals(False, bf.has_user_entered_value)
def test_set_value_converts_datatype_and_set_value_flag(self): f = self.create_field() bf = BoundField(f , StubModel()) bf.set_value("100.1") self.assertEquals(Decimal("100.1"), bf.value) self.assertEquals(True, bf.has_value) self.assertEquals(True, bf.has_user_entered_value)
def test_set_value_converts_datatype_and_set_value_flag(self): f = self.create_field() bf = BoundField(f, StubModel()) bf.set_value("100.1") self.assertEquals(Decimal("100.1"), bf.value) self.assertEquals(True, bf.has_value) self.assertEquals(True, bf.has_user_entered_value)
def test_set_value_unsets_value_on_none(self): f = self.create_field() bf = BoundField(f , StubModel()) bf.set_value(Decimal("100.1")) bf.set_value(None) self.assertEquals( None, bf.value) self.assertEquals( False, bf.has_value) self.assertEquals( False, bf.has_user_entered_value)
def test_recalc(self): f = self.create_field() bf = BoundField(f , StubModel()) self.assertEquals(True, bf.recalc() ) self.assertEquals(Decimal("123"), bf.value) bf.set_value("101") self.assertEquals(False, bf.recalc() ) self.assertEquals(Decimal("101"), bf.value)
def test_map_to_and_from_domain_when_no_mapper(self): f = Field(name="f1", datatype=Decimal) bf = BoundField(f, StubModel()) bf.set_value("123") bf.map_to_domain() bf.set_value("456") bf.map_from_domain() self.assertEqual(Decimal("456"), bf.value)
def test_map_object_value_to_when_none(self): so = StubObj() bf = BoundField(Field("field2", str), None) bf.set_value(None) model = VanillaModel() model._map_object_value(so, bf, bf.TO, finder) self.assertEquals(so.field2, None)
def test_map_object_value_basic_to(self): so = StubObj() bf = BoundField(Field("field1", str), None) bf.set_value('some value') model = VanillaModel() model._map_object_value(so, bf, bf.TO, finder) self.assertEquals(so.field1, 'Found some value')
def test_map_to_and_from_domain_when_no_mapper(self): f = Field(name="f1", datatype=Decimal ) bf = BoundField(f , StubModel()) bf.set_value("123") bf.map_to_domain() bf.set_value("456") bf.map_from_domain() self.assertEqual(Decimal("456"), bf.value)
def test_set_value_produces_readable_errors_when_conversion_failed_date( self): bf = BoundField(Field(name="f1", datatype=datetime.date), StubModel()) self.assertRaisesRegex( DataTypeConversionError, "Invalid value >2012-13-12< needs to be YYYY-MM-DD format", bf.set_value, "2012-13-12")
def test_bimd_method_checks_argument_count(self): f = self.create_field() m = StubModel() bf = BoundField(f, m) self.assertRaisesRegex( InvalidModelError, "Wrong number of arguments to calc_meth method. Expected 3 got 1", bf._bind_method, 'calculation_method', m, 3)
def test_map_to_and_from_domain_using_model_proxy(self): f = self.create_field() m = StubModel() bf = BoundField(f, m) bf.set_value("123") bf.map_to_domain() self.assertEqual(Decimal("123"), m.domain_value) m.domain_value = Decimal('456') bf.map_from_domain() self.assertEqual(Decimal("456"), bf.value)
def test_map_object_value_basic_from(self): so = StubObj() bf = BoundField(Field("field1", str), None) model = VanillaModel() actual = model._map_object_value(so, bf, bf.FROM, finder) expect = "a name" self.assertEquals(expect, actual)
def test_map_object_value_from_when_none(self): so = StubObj() bf = BoundField(Field("field1", str), None) so.field1 = None model = VanillaModel() actual = model._map_object_value(so, bf, bf.FROM, finder) expect = None self.assertEquals(expect, actual)
def test_can_create(self): f = self.create_field() m = StubModel() bf = BoundField(f, m) self.assertEquals(m.calc_meth, bf.calculation_method) self.assertEquals(m.val_meth, bf.validation_method) self.assertEquals(m.mapper, bf.domain_mapping_method) self.assertEquals(f, bf.definition) self.assertEquals('f1', bf.name) self.assertEquals(None, bf.value) self.assertEquals(False, bf.has_value) self.assertEquals(False, bf.has_user_entered_value)
def test_map_to_and_from_domain_using_model_proxy(self): f = self.create_field() m = StubModel() bf = BoundField( f , m) bf.set_value("123") bf.map_to_domain() self.assertEqual(Decimal("123"), m.domain_value) m.domain_value = Decimal('456') bf.map_from_domain() self.assertEqual(Decimal("456"), bf.value)
def test_set_value_unsets_value_on_none(self): f = self.create_field() bf = BoundField(f, StubModel()) bf.set_value(Decimal("100.1")) bf.set_value(None) self.assertEquals(None, bf.value) self.assertEquals(False, bf.has_value) self.assertEquals(False, bf.has_user_entered_value)
def test_map_to_and_from_domain_using_direct_syntax(self): class DomainObject: f1 = 'aaa' do = DomainObject() f = Field(name="f1", datatype=Decimal, domain_mapping="do.f1") m = StubModel() m.get_domain_object = lambda name: do bf = BoundField(f, m) bf.set_value("123") bf.map_to_domain() self.assertEqual(Decimal("123"), do.f1) do.f1 = Decimal("456") bf.map_from_domain() self.assertEqual(Decimal("456"), bf.value)
def test_recalc(self): f = self.create_field() bf = BoundField(f, StubModel()) self.assertEquals(True, bf.recalc()) self.assertEquals(Decimal("123"), bf.value) bf.set_value("101") self.assertEquals(False, bf.recalc()) self.assertEquals(Decimal("101"), bf.value)
def test_map_to_and_from_domain_using_direct_syntax(self): class DomainObject: f1 = 'aaa' do = DomainObject() f = Field(name="f1", datatype=Decimal, domain_mapping="do.f1" ) m = StubModel() m.get_domain_object = lambda name: do bf = BoundField( f , m) bf.set_value("123") bf.map_to_domain() self.assertEqual(Decimal("123"), do.f1) do.f1 = Decimal("456") bf.map_from_domain() self.assertEqual(Decimal("456"), bf.value)
def bind_fields(self): for field_name in self.FIELD_DEPENDS: setattr(self, field_name, BoundField(FieldFactory.get_field(field_name), self))
def test_set_date_fields_as_string(self): f = Field(name="f1", datatype=datetime.date ) bf = BoundField(f , StubModel()) bf.set_value("2015-01-02") self.assertEquals(date(2015,1,2), bf.value)
def test_validate(self): f = self.create_field() bf = BoundField(f, StubModel()) self.assertEquals('Validation Error', bf.validate())
def test_validate_when_val_meth_is_not_set(self): f = Field(name="f1", datatype=Decimal) bf = BoundField(f, StubModel()) self.assertEquals(None, bf.validate())
def test_validate_when_val_meth_is_not_set(self): f = Field(name="f1", datatype=Decimal ) bf = BoundField(f , StubModel()) self.assertEquals( None, bf.validate() )
def test_validate(self): f = self.create_field() bf = BoundField(f , StubModel()) self.assertEquals( 'Validation Error', bf.validate() )
def test_recalc_when_calc_method_is_none(self): f = Field(name="f1", datatype=Decimal ) bf = BoundField(f , StubModel()) self.assertEquals(False, bf.recalc() ) self.assertEquals(None, bf.value)
def test_bind_domain_mapping_method_when_no_mapping(self): f = Field(name="f1", datatype=Decimal) bf = BoundField(f, StubModel()) self.assertEquals(None, bf.domain_mapping_method)
def test_recalc_when_calc_method_is_none(self): f = Field(name="f1", datatype=Decimal) bf = BoundField(f, StubModel()) self.assertEquals(False, bf.recalc()) self.assertEquals(None, bf.value)
def test_set_date_fields_as_string(self): f = Field(name="f1", datatype=datetime.date) bf = BoundField(f, StubModel()) bf.set_value("2015-01-02") self.assertEquals(date(2015, 1, 2), bf.value)
def test_set_value_produces_readable_errors_when_conversion_failed_decimal( self): bf = BoundField(Field(name="f1", datatype=Decimal), StubModel()) self.assertRaisesRegex(DataTypeConversionError, "Invalid value >abv< needs to be Numeric", bf.set_value, "abv")