Replace try...except block with assertRaises
So the test logic looks tidy and clean. Change-Id: I8415fbe1d4c0ce59c5c9f173240fcd6eb15b0c63
This commit is contained in:
parent
9891638801
commit
0034edfc56
|
@ -77,13 +77,9 @@ class ParameterTest(testtools.TestCase):
|
||||||
'AllowedValues': ['foo'],
|
'AllowedValues': ['foo'],
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'Default': 'bar'}
|
'Default': 'bar'}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, 'foo')
|
self.new_parameter, 'p', schema, 'foo')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_no_echo_true(self):
|
def test_no_echo_true(self):
|
||||||
p = self.new_parameter('anechoic',
|
p = self.new_parameter('anechoic',
|
||||||
|
@ -131,25 +127,17 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'String',
|
schema = {'Type': 'String',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'MinLength': '4'}
|
'MinLength': '4'}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, 'foo')
|
self.new_parameter, 'p', schema, 'foo')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_string_overflow(self):
|
def test_string_overflow(self):
|
||||||
schema = {'Type': 'String',
|
schema = {'Type': 'String',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'MaxLength': '2'}
|
'MaxLength': '2'}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, 'foo')
|
self.new_parameter, 'p', schema, 'foo')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_string_pattern_good(self):
|
def test_string_pattern_good(self):
|
||||||
schema = {'Type': 'String',
|
schema = {'Type': 'String',
|
||||||
|
@ -161,25 +149,17 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'String',
|
schema = {'Type': 'String',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'AllowedPattern': '[a-z]*'}
|
'AllowedPattern': '[a-z]*'}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, '1foo')
|
self.new_parameter, 'p', schema, '1foo')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_string_pattern_bad_suffix(self):
|
def test_string_pattern_bad_suffix(self):
|
||||||
schema = {'Type': 'String',
|
schema = {'Type': 'String',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'AllowedPattern': '[a-z]*'}
|
'AllowedPattern': '[a-z]*'}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, 'foo1')
|
self.new_parameter, 'p', schema, 'foo1')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_string_value_list_good(self):
|
def test_string_value_list_good(self):
|
||||||
schema = {'Type': 'String',
|
schema = {'Type': 'String',
|
||||||
|
@ -191,13 +171,9 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'String',
|
schema = {'Type': 'String',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'AllowedValues': ['foo', 'bar', 'baz']}
|
'AllowedValues': ['foo', 'bar', 'baz']}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, 'blarg')
|
self.new_parameter, 'p', schema, 'blarg')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_number_int_good(self):
|
def test_number_int_good(self):
|
||||||
schema = {'Type': 'Number',
|
schema = {'Type': 'Number',
|
||||||
|
@ -217,25 +193,17 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'Number',
|
schema = {'Type': 'Number',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'MinValue': '4'}
|
'MinValue': '4'}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, '3')
|
self.new_parameter, 'p', schema, '3')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_number_high(self):
|
def test_number_high(self):
|
||||||
schema = {'Type': 'Number',
|
schema = {'Type': 'Number',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'MaxValue': '2'}
|
'MaxValue': '2'}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, '3')
|
self.new_parameter, 'p', schema, '3')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_number_value_list_good(self):
|
def test_number_value_list_good(self):
|
||||||
schema = {'Type': 'Number',
|
schema = {'Type': 'Number',
|
||||||
|
@ -247,13 +215,9 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'Number',
|
schema = {'Type': 'Number',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'AllowedValues': ['1', '3', '5']}
|
'AllowedValues': ['1', '3', '5']}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, '2')
|
self.new_parameter, 'p', schema, '2')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_list_value_list_good(self):
|
def test_list_value_list_good(self):
|
||||||
schema = {'Type': 'CommaDelimitedList',
|
schema = {'Type': 'CommaDelimitedList',
|
||||||
|
@ -271,13 +235,9 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'CommaDelimitedList',
|
schema = {'Type': 'CommaDelimitedList',
|
||||||
'ConstraintDescription': 'wibble',
|
'ConstraintDescription': 'wibble',
|
||||||
'AllowedValues': ['foo', 'bar', 'baz']}
|
'AllowedValues': ['foo', 'bar', 'baz']}
|
||||||
try:
|
err = self.assertRaises(ValueError, self.new_parameter,
|
||||||
self.new_parameter('p', schema, 'foo,baz,blarg')
|
'p', schema, 'foo,baz,blarg')
|
||||||
except ValueError as ve:
|
self.assertIn('wibble', str(err))
|
||||||
msg = str(ve)
|
|
||||||
self.assertNotEqual(msg.find('wibble'), -1)
|
|
||||||
else:
|
|
||||||
self.fail('ValueError not raised')
|
|
||||||
|
|
||||||
def test_map_value(self):
|
def test_map_value(self):
|
||||||
'''Happy path for value thats already a map.'''
|
'''Happy path for value thats already a map.'''
|
||||||
|
@ -292,12 +252,9 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'Json',
|
schema = {'Type': 'Json',
|
||||||
'ConstraintDescription': 'wibble'}
|
'ConstraintDescription': 'wibble'}
|
||||||
val = {"foo": "bar", "not_json": len}
|
val = {"foo": "bar", "not_json": len}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, val)
|
self.new_parameter, 'p', schema, val)
|
||||||
except ValueError as verr:
|
self.assertIn('Value must be valid JSON', str(err))
|
||||||
self.assertIn('Value must be valid JSON', str(verr))
|
|
||||||
else:
|
|
||||||
self.fail("Value error not raised")
|
|
||||||
|
|
||||||
def test_map_value_parse(self):
|
def test_map_value_parse(self):
|
||||||
'''Happy path for value that's a string.'''
|
'''Happy path for value that's a string.'''
|
||||||
|
@ -313,12 +270,9 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'Json',
|
schema = {'Type': 'Json',
|
||||||
'ConstraintDescription': 'wibble'}
|
'ConstraintDescription': 'wibble'}
|
||||||
val = "I am not a map"
|
val = "I am not a map"
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, val)
|
self.new_parameter, 'p', schema, val)
|
||||||
except ValueError as verr:
|
self.assertIn('Value must be valid JSON', str(err))
|
||||||
self.assertIn('Value must be valid JSON', str(verr))
|
|
||||||
else:
|
|
||||||
self.fail("Value error not raised")
|
|
||||||
|
|
||||||
def test_map_values_good(self):
|
def test_map_values_good(self):
|
||||||
'''Happy path for map keys.'''
|
'''Happy path for map keys.'''
|
||||||
|
@ -335,36 +289,27 @@ class ParameterTest(testtools.TestCase):
|
||||||
schema = {'Type': 'Json',
|
schema = {'Type': 'Json',
|
||||||
'AllowedValues': ["foo", "bar", "baz"]}
|
'AllowedValues': ["foo", "bar", "baz"]}
|
||||||
val = {"foo": "bar", "items": [1, 2, 3]}
|
val = {"foo": "bar", "items": [1, 2, 3]}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, val)
|
self.new_parameter, 'p', schema, val)
|
||||||
except ValueError as verr:
|
self.assertIn("items", str(err))
|
||||||
self.assertIn("items", str(verr))
|
|
||||||
else:
|
|
||||||
self.fail("Value error not raised")
|
|
||||||
|
|
||||||
def test_map_underrun(self):
|
def test_map_underrun(self):
|
||||||
'''Test map length under MIN_LEN.'''
|
'''Test map length under MIN_LEN.'''
|
||||||
schema = {'Type': 'Json',
|
schema = {'Type': 'Json',
|
||||||
'MinLength': 3}
|
'MinLength': 3}
|
||||||
val = {"foo": "bar", "items": [1, 2, 3]}
|
val = {"foo": "bar", "items": [1, 2, 3]}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, val)
|
self.new_parameter, 'p', schema, val)
|
||||||
except ValueError as verr:
|
self.assertIn('underflows', str(err))
|
||||||
self.assertIn('underflows', str(verr))
|
|
||||||
else:
|
|
||||||
self.fail("Value error not raised")
|
|
||||||
|
|
||||||
def test_map_overrun(self):
|
def test_map_overrun(self):
|
||||||
'''Test map length over MAX_LEN.'''
|
'''Test map length over MAX_LEN.'''
|
||||||
schema = {'Type': 'Json',
|
schema = {'Type': 'Json',
|
||||||
'MaxLength': 1}
|
'MaxLength': 1}
|
||||||
val = {"foo": "bar", "items": [1, 2, 3]}
|
val = {"foo": "bar", "items": [1, 2, 3]}
|
||||||
try:
|
err = self.assertRaises(ValueError,
|
||||||
self.new_parameter('p', schema, val)
|
self.new_parameter, 'p', schema, val)
|
||||||
except ValueError as verr:
|
self.assertIn('overflows', str(err))
|
||||||
self.assertIn('overflows', str(verr))
|
|
||||||
else:
|
|
||||||
self.fail("Value error not raised")
|
|
||||||
|
|
||||||
def test_missing_param(self):
|
def test_missing_param(self):
|
||||||
'''Test missing user parameter.'''
|
'''Test missing user parameter.'''
|
||||||
|
|
Loading…
Reference in New Issue