def test_int_no_data_loss(self): """ Int fields refuse to drop floating point data """ field = Field(data_type=int, coerce=True) with self.assertRaises(ValueError): field.coerce(4.5) with self.assertRaises(ValueError): field.coerce(Decimal('4.5'))
def test_coerce_boto_s3key(self): """ Coerce boto key to S3 key """ field = Field(data_type=S3Type('mybucket')) boto_key = boto.s3.key.Key() boto_key.key = 'my/path' ret = field.coerce(boto_key) self.assertTrue(isinstance(ret, Key)) self.assertEqual(ret.key, 'my/path')
def test_coerce_number_set_fail(self): """ Coerce to number set fails """ field = Field(data_type=set_(int)) with self.assertRaises(TypeError): field.coerce([2, '4'])
def test_coerce_basic_set_fail(self): """ Coercing to untyped set fails """ field = Field(data_type=set) with self.assertRaises(TypeError): field.coerce(['a', 'b'])
def test_coerce_date_fail(self): """ Coercing to date fails """ field = Field(data_type=date, coerce=True) with self.assertRaises(TypeError): field.coerce(12345)
def test_coerce_bool(self): """ Coerce to bool """ field = Field(data_type=bool, coerce=True) ret = field.coerce(2) self.assertTrue(isinstance(ret, bool))
def test_coerce_list(self): """ Coerce to list """ field = Field(data_type=list, coerce=True) ret = field.coerce(set([1, 2])) self.assertTrue(isinstance(ret, list))
def test_coerce_dict(self): """ Coerce to dict """ field = Field(data_type=dict, coerce=True) ret = field.coerce([(1, 2)]) self.assertTrue(isinstance(ret, dict))
def test_coerce_str_fail(self): """ Coerce to bytes fails if coerce=False """ field = Field(data_type=six.binary_type) with self.assertRaises(TypeError): field.coerce(5)
def test_always_coerce_unicode_str(self): """ Always coerce unicode to bytes """ field = Field(data_type=six.binary_type) ret = field.coerce(six.u('val')) self.assertTrue(isinstance(ret, six.binary_type))
def test_coerce_str(self): """ Coerce to bytes """ field = Field(data_type=six.binary_type, coerce=True) ret = field.coerce(5) self.assertTrue(isinstance(ret, six.binary_type))
def test_coerce_unicode_fail(self): """ Coerce to unicode fails if coerce=False """ field = Field(data_type=six.text_type) with self.assertRaises(TypeError): field.coerce(5)
def test_coerce_unicode(self): """ Coerce to unicode """ field = Field(data_type=six.text_type, coerce=True) ret = field.coerce(5) self.assertTrue(isinstance(ret, six.text_type))
def test_always_coerce_str_unicode(self): """ Always coerce bytes to unicode """ field = Field(data_type=six.text_type) ret = field.coerce(b'val') self.assertTrue(isinstance(ret, six.text_type))
def test_coerce_decimal_fail(self): """ Coerce to Decimal fails if coerce=False """ field = Field(data_type=Decimal) with self.assertRaises(TypeError): field.coerce(5.5)
def test_int_coerce(self): """ Int fields can coerce floats """ field = Field(data_type=int, coerce=True) ret = field.coerce(4.0) self.assertEquals(ret, 4) self.assertTrue(isinstance(ret, int))
def test_coerce_set(self): """ Coerce to set """ field = Field(data_type=set, coerce=True) ret = field.coerce([1, 2]) self.assertTrue(isinstance(ret, set))
def test_int_coerce_fail(self): """ Coerce to int fails if coerce=False """ field = Field(data_type=int) with self.assertRaises(TypeError): field.coerce(4.0)
def test_coerce_dict_fail(self): """ Coerce to dict fails if coerce=False """ field = Field(data_type=dict) with self.assertRaises(TypeError): field.coerce([(1, 2)])
def test_int_coerce_long(self): """ Int fields can transparently handle longs """ field = Field(data_type=int) val = 100 ret = field.coerce(val) self.assertEqual(ret, val)
def test_coerce_list_fail(self): """ Coerce to list fails if coerce=False """ field = Field(data_type=list) with self.assertRaises(TypeError): field.coerce(set([1, 2]))
def test_coerce_float(self): """ Coerce to float """ field = Field(data_type=float, coerce=True) ret = field.coerce('4.3') self.assertTrue(isinstance(ret, float))
def test_coerce_bool_fail(self): """ Coerce to bool fails if coerce=False """ field = Field(data_type=bool) with self.assertRaises(TypeError): field.coerce(2)
def test_always_coerce_int_float(self): """ Always coerce ints to float """ field = Field(data_type=float) ret = field.coerce(5) self.assertTrue(isinstance(ret, float))
def test_coerce_basic_set(self): """ Coerce to an untyped set """ field = Field(data_type=set, coerce=True) ret = field.coerce(['a', 'b']) self.assertEqual(ret, set(['a', 'b']))
def test_coerce_float_fail(self): """ Coerce to float fails if coerce=False """ field = Field(data_type=float) with self.assertRaises(TypeError): field.coerce('4.3')
def test_coerce_number_set(self): """ Coerce to number set """ field = Field(data_type=set_(int), coerce=True) ret = field.coerce([2, '4']) self.assertEqual(ret, set([2, 4]))
def test_coerce_decimal(self): """ Coerce to Decimal """ field = Field(data_type=Decimal, coerce=True) ret = field.coerce(5.5) self.assertTrue(isinstance(ret, Decimal))
def test_coerce_binary_set(self): """ Coerce to binary set """ field = Field(data_type=set_(six.binary_type), coerce=True) ret = field.coerce([six.u('hello')]) self.assertEqual(ret, set([b'hello']))
def test_coerce_s3key(self): """ Coerce string to S3 key """ field = Field(data_type=S3Type('mybucket')) ret = field.coerce('my/path') self.assertTrue(isinstance(ret, Key)) self.assertEqual(ret.key, 'my/path')