def test_converttype_transform_soft_invalid(self): self.field.setValue('four miles') self.field.setFinalType(DateFieldType()) with self.assertRaises(FieldTypeError): self.field.setTransforms([ConvertTypeTransform('Date')]).run()
def test_converttype_transform_hard(self): self.field.setValue('four miles') self.field.setFinalType(DateFieldType()) self.field.setTransforms([ConvertTypeTransform('Date', hard=True)]).run() self.assertIsNone(self.field.getValue())
def setUp(self): self.fs = FieldSet([ Field('id', IntegerFieldType(), defaultValue=1, key=True), Field('name', StringFieldType(), defaultValue='John doe'), Field('email', StringFieldType(), key=True), Field('birth_date', DateFieldType(), field_final_type=StringFieldType()), Field('favourite_number', FloatFieldType()), Field('created', DateTimeFieldType(), defaultValue='2013-04-04 18:44:32'), Field('updated', DateTimeFieldType(), defaultValue='2013-04-04 18:44:32') ]) self.fm = FieldMap({ 'id': 0, 'name': 1, 'email': 2, 'birth_date': 3, 'favourite_number': 4, 'created': 5, 'updated': 6, 'not_existing_field_name': 6 })
def setUp( self ): self.integer_field = Field( 'integer', IntegerFieldType(), defaultValue = 4 ) self.float_field = Field( 'float', FloatFieldType(), defaultValue = 4.32 ) self.date_field = Field( 'date', DateFieldType(), defaultValue = '2013-02-05' ) self.string_field = Field( 'string', StringFieldType(), defaultValue = 'test' ) self.empty_int_field = Field( 'integer_empty', IntegerFieldType() ) self.empty_str_field = Field( 'string_empty', StringFieldType(), defaultValue = '' )
def test_nullable(self): f1 = metl.field.Field( 'test_name', StringFieldType(), ) f2 = metl.field.Field('test_name', StringFieldType(), nullable=False) f3 = metl.field.Field( 'test_name', StringFieldType(), field_final_type=TextFieldType(), ) f4 = metl.field.Field('test_name', StringFieldType(), field_final_type=TextFieldType(), nullable=False) f5 = metl.field.Field( 'test_name', DateFieldType(), field_final_type=DateTimeFieldType(), ) f6 = metl.field.Field('test_name', DateFieldType(), field_final_type=DateTimeFieldType(), nullable=False) f1.setValue(u' ') f2.setValue(u' ') f3.setValue(u' ') f4.setValue(u' ') f5.setValue(u' ') f6.setValue(u' ') self.assertIsNone(f1.getValue()) self.assertIsNone(f3.getValue()) self.assertIsNone(f5.getValue()) self.assertEqual(f2.getValue(), u'') self.assertEqual(f4.getValue(), u'') self.assertEqual(f6.getValue(), u'')
def test_converttype_transform_hard_with_default(self): self.field.setValue('four miles') self.field.setFinalType(DateFieldType()) self.field.setTransforms([ ConvertTypeTransform('Date', hard=True, defaultValue=datetime.date.today()) ]).run() self.assertEqual(self.field.getValue(), datetime.date.today())
def test_field_type_convert_to_boolean( self ): fs = BooleanFieldType() self.assertTrue( fs.getValue( BooleanFieldType().getValue( True ) ) ) self.assertTrue( fs.getValue( DateFieldType().getValue( datetime.date.today() ) ) ) self.assertTrue( fs.getValue( DateTimeFieldType().getValue( datetime.datetime.now() ) ) ) self.assertTrue( fs.getValue( FloatFieldType().getValue( 4.32112 ) ) ) self.assertTrue( fs.getValue( IntegerFieldType().getValue( 5 ) ) ) self.assertTrue( fs.getValue( StringFieldType().getValue('test') ) ) self.assertTrue( fs.getValue( TextFieldType().getValue('test') ) ) self.assertIsNone( fs.getValue( None ) )
def test_field_type_convert_to_text( self ): fs = TextFieldType() self.assertEqual( fs.getValue( BooleanFieldType().getValue( True ) ), 'True' ) self.assertEqual( fs.getValue( BooleanFieldType().getValue( False ) ), 'False' ) self.assertEqual( fs.getValue( DateFieldType().getValue( datetime.date( 2013, 2, 3 ) ) ), '2013-02-03' ) self.assertEqual( fs.getValue( DateTimeFieldType().getValue( datetime.datetime( 2013, 4, 4, 16, 06, 58, 929515 ) ) ), '2013-04-04 16:06:58.929515' ) self.assertEqual( fs.getValue( FloatFieldType().getValue( 4.232 ) ), '4.232' ) self.assertEqual( fs.getValue( IntegerFieldType().getValue( 5 ) ), '5' ) self.assertEqual( fs.getValue( StringFieldType().getValue('text') ), 'text' ) self.assertEqual( fs.getValue( TextFieldType().getValue('text') ), 'text' ) self.assertIsNone( fs.getValue( None ) )
def test_field_type_convert_to_integer( self ): fs = IntegerFieldType() with self.assertRaises( FieldTypeError ): fs.getValue( DateFieldType().getValue('2013-02-03') ) with self.assertRaises( FieldTypeError ): fs.getValue( DateFieldType().getValue('2013-04-04 16:06:58.929515') ) with self.assertRaises( FieldTypeError ): fs.getValue( StringFieldType().getValue('test') ) with self.assertRaises( FieldTypeError ): fs.getValue( TextFieldType().getValue('test') ) self.assertEqual( fs.getValue( BooleanFieldType().getValue( True ) ), 1 ) self.assertEqual( fs.getValue( BooleanFieldType().getValue( False ) ), 0 ) self.assertEqual( fs.getValue( FloatFieldType().getValue( 4.232 ) ), 4 ) self.assertEqual( fs.getValue( IntegerFieldType().getValue( 5 ) ), 5 ) self.assertEqual( fs.getValue( StringFieldType().getValue('5') ), 5 ) self.assertEqual( fs.getValue( TextFieldType().getValue('5') ), 5 ) self.assertIsNone( fs.getValue( None ) )
def setUp(self): self.startType = DateTimeFieldType() self.endType = DateFieldType() self.defaultValue = datetime.datetime.now() - datetime.timedelta( days=5) self.transValue = self.defaultValue - datetime.timedelta(days=2) self.transforms = [SetTransform(value=self.transValue)] self.field = metl.field.Field('test_name', self.startType, key=True, field_final_type=self.endType, defaultValue=self.defaultValue)
def test_field_type_convert_to_datetime( self ): fs = DateTimeFieldType() with self.assertRaises( FieldTypeError ): fs.getValue( BooleanFieldType().getValue( True ) ) with self.assertRaises( FieldTypeError ): fs.getValue( FloatFieldType().getValue( 4.32112 ) ) with self.assertRaises( FieldTypeError ): fs.getValue( IntegerFieldType().getValue( 5 ) ) with self.assertRaises( FieldTypeError ): fs.getValue( StringFieldType().getValue('test') ) with self.assertRaises( FieldTypeError ): fs.getValue( TextFieldType().getValue('test') ) self.assertEqual( fs.getValue( DateFieldType().getValue('2013-04-04 16:06:58.929515') ), datetime.datetime( 2013, 4, 4 ) ) self.assertEqual( fs.getValue( DateTimeFieldType().getValue( datetime.datetime( 2013, 4, 4, 16, 06, 58, 929515 ) ) ), datetime.datetime( 2013, 4, 4, 16, 06, 58, 929515 ) ) self.assertEqual( fs.getValue( StringFieldType().getValue('2013-04-04 16:06:58.929515') ), datetime.datetime( 2013, 4, 4, 16, 06, 58, 929515 ) ) self.assertEqual( fs.getValue( TextFieldType().getValue('2013-04-04 16:06:58.929515') ), datetime.datetime( 2013, 4, 4, 16, 06, 58, 929515 ) ) self.assertIsNone( fs.getValue( None ) )
def test_is_in_source_condition( self ): false_source_records = [ FieldSet([ Field( 'integer', IntegerFieldType(), defaultValue = 1 ), Field( 'float', FloatFieldType(), defaultValue = 4.2 ), Field( 'date', DateFieldType(), defaultValue = '2012-02-04' ) ]), FieldSet([ Field( 'integer', IntegerFieldType(), defaultValue = 2 ), Field( 'float', FloatFieldType(), defaultValue = 4.3 ), Field( 'date', DateFieldType(), defaultValue = '2012-02-05' ) ]), FieldSet([ Field( 'integer', IntegerFieldType(), defaultValue = 3 ), Field( 'float', FloatFieldType(), defaultValue = 4.4 ), Field( 'date', DateFieldType(), defaultValue = '2012-02-06' ) ]) ] self.assertFalse( IsInSourceCondition( false_source_records, 'integer' ).getResult( self.integer_field ) ) self.assertFalse( IsInSourceCondition( false_source_records, 'float' ).getResult( self.float_field ) ) self.assertFalse( IsInSourceCondition( false_source_records, 'date' ).getResult( self.date_field ) ) true_source_records = [ FieldSet([ Field( 'integer', IntegerFieldType(), defaultValue = 3 ), Field( 'float', FloatFieldType(), defaultValue = 4.31 ), Field( 'date', DateFieldType(), defaultValue = '2012-02-04' ) ]), FieldSet([ Field( 'integer', IntegerFieldType(), defaultValue = 4 ), Field( 'float', FloatFieldType(), defaultValue = 4.32 ), Field( 'date', DateFieldType(), defaultValue = '2013-02-05' ) ]), FieldSet([ Field( 'integer', IntegerFieldType(), defaultValue = 5 ), Field( 'float', FloatFieldType(), defaultValue = 4.33 ), Field( 'date', DateFieldType(), defaultValue = '2012-02-06' ) ]) ] self.assertTrue( IsInSourceCondition( true_source_records, 'integer' ).getResult( self.integer_field ) ) self.assertTrue( IsInSourceCondition( true_source_records, 'float' ).getResult( self.float_field ) ) self.assertTrue( IsInSourceCondition( true_source_records, 'date' ).getResult( self.date_field ) )
def test_converttype_transform_soft(self): self.field.setValue('2012-02-12') self.field.setFinalType(DateFieldType()) self.field.setTransforms([ConvertTypeTransform('Date')]).run() self.assertEqual(self.field.getValue(), datetime.date(2012, 2, 12))
def test_date_field_type( self ): ft = DateFieldType() with self.assertRaises( FieldTypeError ): ft.getValue( 'invalid' ) with self.assertRaises( FieldTypeError ): ft.getValue( 321312312 ) with self.assertRaises( FieldTypeError ): ft.getValue( '2013-02-31' ) self.assertEqual( ft.getValue('2013-02-03'), datetime.date( 2013, 2, 3 ) ) self.assertEqual( ft.getValue('2013/02/03'), datetime.date( 2013, 2, 3 ) ) self.assertEqual( ft.getValue('2013.02.03'), datetime.date( 2013, 2, 3 ) ) self.assertEqual( ft.getValue('02/03/2013'), datetime.date( 2013, 2, 3 ) ) self.assertEqual( ft.getValue('Dec 26, 2012 10:01:26 AM'), datetime.date( 2012, 12, 26 ) ) self.assertEqual( ft.getValue('2012.12.26 10.01.26'), datetime.date( 2012, 12, 26 ) ) self.assertEqual( ft.getValue( datetime.date( 2013, 2, 3 ) ), datetime.date( 2013, 2, 3 ) ) self.assertEqual( ft.getValue( datetime.datetime.now().replace( year = 2013, month = 2, day = 3 ) ), datetime.date( 2013, 2, 3 ) ) self.assertIsNone( ft.getValue( None ) )
def test_field_type_convert_to_date( self ): fs = DateFieldType() with self.assertRaises( FieldTypeError ): fs.getValue( BooleanFieldType().getValue( True ) ) with self.assertRaises( FieldTypeError ): fs.getValue( FloatFieldType().getValue( 4.32112 ) ) with self.assertRaises( FieldTypeError ): fs.getValue( IntegerFieldType().getValue( 5 ) ) with self.assertRaises( FieldTypeError ): fs.getValue( StringFieldType().getValue('test') ) with self.assertRaises( FieldTypeError ): fs.getValue( TextFieldType().getValue('test') ) self.assertEqual( fs.getValue( DateFieldType().getValue( datetime.date.today() ) ), datetime.date.today() ) self.assertEqual( fs.getValue( DateTimeFieldType().getValue( datetime.datetime.now() ) ), datetime.date.today() ) self.assertEqual( fs.getValue( StringFieldType().getValue('2013-02-03') ), datetime.date( 2013, 2, 3 ) ) self.assertEqual( fs.getValue( TextFieldType().getValue('2013-02-03') ), datetime.date( 2013, 2, 3 ) ) self.assertIsNone( fs.getValue( None ) )
def test_convertable(self): self.assertTrue(self.field.isConvertable(DateFieldType())) self.assertTrue(self.field.isConvertable(StringFieldType()))