def test_float(self): t = Float() self.assertIs(t.to_python(None), None) self.assertRaises(ValueError, lambda: t.to_python('test')) self.assertEqual(t.to_python(123), 123) self.assertEqual(t.to_python('123'), 123) self.assertEqual(t.from_python('test'), 'test') self.assertRaises(ValidationError, lambda: t.from_python('test', validate=True)) self.assertAlmostEqual(t.from_python('128', validate=True), 128.0) self.assertAlmostEqual(t.from_python(128, validate=True), 128.0)
def test_from_python__is_none(self): # self.assertIsNone(String().from_python(None)) self.assertIsNone(Byte().from_python(None)) self.assertIsNone(Short().from_python(None)) self.assertIsNone(Integer().from_python(None)) self.assertIsNone(Long().from_python(None)) self.assertIsNone(Float().from_python(None)) self.assertIsNone(Double().from_python(None)) # self.assertIsNone(Date().from_python(None)) # self.assertIsNone(Boolean().from_python(None)) # self.assertIsNone(Binary().from_python(None)) self.assertIsNone(Ip().from_python(None)) self.assertIsNone(Object(DynamicDocument).from_python(None)) # self.assertIsNone(Nested().to_python(None)) # self.assertIsNone(List(String).from_python(None)) self.assertIsNone(GeoPoint().from_python(None)) self.assertIsNone(Completion().from_python(None))
def to_float(value, type=None): type = type or Float() v = type.to_python_single(value) if math.isnan(v) or math.isinf(v): raise ValueError('NaN or Inf is not supported') return v