Exemple #1
0
 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, [])
Exemple #2
0
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, [])
Exemple #3
0
 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, [])
Exemple #4
0
def test_coerce_int():
    """Test coerce_int."""
    validator = ParsecValidator()
    # The good
    for value, result in [('0', 0), ('3', 3), ('-3', -3), ('-0', -0),
                          ('653456', 653456),
                          ('-8362583645365', -8362583645365)]:
        pytest.approx(validator.coerce_int(value, ['whatever']), result)
    assert validator.coerce_int('', ['whatever']) is None  # not a number
    # The bad
    for value in ['None', ' Who cares? ', 'True', '4.8', '[]', '[3]', '60*60']:
        with pytest.raises(IllegalValueError):
            validator.coerce_int(value, ['whatever'])
Exemple #5
0
 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'])
Exemple #6
0
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'])
Exemple #7
0
 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'])
Exemple #8
0
 def test_coerce_int(self):
     """Test coerce_int."""
     validator = ParsecValidator()
     # The good
     for value, result in [('0', 0), ('3', 3), ('-3', -3), ('-0', -0),
                           ('653456', 653456),
                           ('-8362583645365', -8362583645365)]:
         self.assertAlmostEqual(validator.coerce_int(value, ['whatever']),
                                result)
     self.assertEqual(validator.coerce_int('', ['whatever']),
                      None)  # not a number
     # The bad
     for value in [
             'None', ' Who cares? ', 'True', '4.8', '[]', '[3]', '60*60'
     ]:
         self.assertRaises(IllegalValueError, validator.coerce_int, value,
                           ['whatever'])
Exemple #9
0
 def test_coerce_int(self):
     """Test coerce_int."""
     validator = ParsecValidator()
     # The good
     for value, result in [
         ('0', 0),
         ('3', 3),
         ('-3', -3),
         ('-0', -0),
         ('653456', 653456),
         ('-8362583645365', -8362583645365)
     ]:
         self.assertAlmostEqual(
             validator.coerce_int(value, ['whatever']), result)
     self.assertEqual(
         validator.coerce_int('', ['whatever']), None)  # not a number
     # The bad
     for value in [
         'None', ' Who cares? ', 'True', '4.8', '[]', '[3]', '60*60'
     ]:
         self.assertRaises(
             IllegalValueError,
             validator.coerce_int, value, ['whatever'])
Exemple #10
0
def test_coerce_int__bad(value: str):
    with pytest.raises(IllegalValueError):
        ParsecValidator.coerce_int(value, ['whatever'])
Exemple #11
0
def test_coerce_int__empty():
    assert ParsecValidator.coerce_int('', ['whatever']) is None  # not a number
Exemple #12
0
def test_coerce_int(value: str, expected: int):
    """Test coerce_int."""
    assert ParsecValidator.coerce_int(value, ['whatever']) == expected