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 create_field(self): f = Field(name="f1", datatype=Decimal, validation_method='val_meth', calculation_method='calc_meth', domain_mapping='mapper') return f
def test_bind_method_raises_error_if_method_is_missing_from_model(self): f = Field(name="f1", datatype=str, validation_method='method_is_missing') m = StubModel() self.assertRaisesRegex( InvalidModelError, "method_is_missing is not defined in StubModel", BoundField, f, m)
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_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_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_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_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 get_fields(cls): if cls._FIELDS is None: cls._FIELDS = [] with open(os.path.join(os.path.dirname(__file__), 'fields.csv')) as f: reader = csv.DictReader(f) for row in reader: cls._set_datatype(row) cls._convert_empty_string_to_none(row) cls._FIELDS.append(Field(**row)) return cls._FIELDS
def test_field_construction(self): f = Field(name="f1", datatype=str, validation_method='vm', calculation_method='cm', domain_mapping='dm') self.assertEquals("f1", f.name) self.assertEquals(str, f.datatype) self.assertEquals("vm", f.validation_method) self.assertEquals("cm", f.calculation_method) self.assertEquals("dm", f.domain_mapping)
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_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_bind_domain_mapping_direct_model_must_have_domain_object(self): f = Field(name="f1", datatype=Decimal, domain_mapping="do.f1") self.assertRaisesRegex( InvalidModelError, 'StubModel does not support get_domain_object method', BoundField, f, StubModel())
def test_bind_domain_mapping_method_when_invalid_mapping(self): f = Field(name="f1", datatype=Decimal, domain_mapping="a.b.c") self.assertRaisesRegex(InvalidFieldDefinitionlError, 'Invalid domain_mapping a.b.c for field f1', BoundField, f, StubModel())
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_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")
def test_map_field_maps_to_models(self): field = Field(name = 'quantity', datatype = Decimal) actual = BulkUploadFormat.map_one_field("Quantity", field, models = [FirstModel, SecondModel]) self.assertEquals("X", actual["First"]) self.assertEquals("", actual["Second"])
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())