def test_coerce_float(): """Test coerce_float.""" validator = ParsecValidator() # The good for value, result in [ ('3', 3.0), ('9.80', 9.80), ('3.141592654', 3.141592654), ('"3.141592654"', 3.141592654), ("'3.141592654'", 3.141592654), ('-3', -3.0), ('-3.1', -3.1), ('0', 0.0), ('-0', -0.0), ('0.0', 0.0), ('1e20', 1.0e20), ('6.02e23', 6.02e23), ('-1.6021765e-19', -1.6021765e-19), ('6.62607004e-34', 6.62607004e-34) ]: assert pytest.approx( validator.coerce_float(value, ['whatever']), result) assert validator.coerce_int('', ['whatever']) is None # not a number # The bad for value in [ 'None', ' Who cares? ', 'True', '[]', '[3.14]', '3.14, 2.72' ]: with pytest.raises(IllegalValueError): validator.coerce_float(value, ['whatever'])
def test_coerce_none_fails(): with pytest.raises(AttributeError): ParsecValidator.coerce_boolean(None, []) with pytest.raises(AttributeError): ParsecValidator.coerce_float(None, []) with pytest.raises(AttributeError): ParsecValidator.coerce_int(None, [])
def test_coerce_none_fails(self): with self.assertRaises(AttributeError): ParsecValidator.coerce_boolean(None, []) with self.assertRaises(AttributeError): ParsecValidator.coerce_float(None, []) with self.assertRaises(AttributeError): ParsecValidator.coerce_int(None, [])
def test_coerce_none_fails(self): with self.assertRaises(AttributeError): ParsecValidator.coerce_boolean(None, []) with self.assertRaises(AttributeError): ParsecValidator.coerce_float(None, []) with self.assertRaises(AttributeError): ParsecValidator.coerce_int(None, [])
def test_coerce_float(self): """Test coerce_float.""" validator = ParsecValidator() # The good for value, result in [ ('3', 3.0), ('9.80', 9.80), ('3.141592654', 3.141592654), ('"3.141592654"', 3.141592654), ("'3.141592654'", 3.141592654), ('-3', -3.0), ('-3.1', -3.1), ('0', 0.0), ('-0', -0.0), ('0.0', 0.0), ('1e20', 1.0e20), ('6.02e23', 6.02e23), ('-1.6021765e-19', -1.6021765e-19), ('6.62607004e-34', 6.62607004e-34) ]: self.assertAlmostEqual( validator.coerce_float(value, ['whatever']), result) self.assertEqual( validator.coerce_int('', ['whatever']), None) # not a number # The bad for value in [ 'None', ' Who cares? ', 'True', '[]', '[3.14]', '3.14, 2.72' ]: self.assertRaises( IllegalValueError, validator.coerce_float, value, ['whatever'])
def test_coerce_float(self): """Test coerce_float.""" validator = ParsecValidator() # The good for value, result in [ ('3', 3.0), ('9.80', 9.80), ('3.141592654', 3.141592654), ('"3.141592654"', 3.141592654), ("'3.141592654'", 3.141592654), ('-3', -3.0), ('-3.1', -3.1), ('0', 0.0), ('-0', -0.0), ('0.0', 0.0), ('1e20', 1.0e20), ('6.02e23', 6.02e23), ('-1.6021765e-19', -1.6021765e-19), ('6.62607004e-34', 6.62607004e-34) ]: self.assertAlmostEqual( validator.coerce_float(value, ['whatever']), result) self.assertEqual( validator.coerce_int('', ['whatever']), None) # not a number # The bad for value in [ 'None', ' Who cares? ', 'True', '[]', '[3.14]', '3.14, 2.72' ]: self.assertRaises( IllegalValueError, validator.coerce_float, value, ['whatever'])
def test_coerce_float__bad(value: str): with pytest.raises(IllegalValueError): ParsecValidator.coerce_float(value, ['whatever'])
def test_coerce_float__empty(): # not a number assert ParsecValidator.coerce_float('', ['whatever']) is None
def test_coerce_float(value: str, expected: float): """Test coerce_float.""" assert ( ParsecValidator.coerce_float(value, ['whatever']) == approx(expected) )