[python-experimental] Raise builtin AttributeError instead of custom ApiKeyError (#6229)

* Use AttributeError instead of ApiKeyError because that's what the hasattr builtin function uses

* Use AttributeError instead of ApiKeyError because that's what the hasattr builtin function uses

* fix unit tests

* create ApiAttributeError exception

* fix formatting

* run samples scripts
This commit is contained in:
Sebastien Rosset
2020-05-10 19:02:41 -07:00
committed by GitHub
parent 44b51a136f
commit fa37a696a8
24 changed files with 237 additions and 55 deletions

View File

@@ -51,19 +51,35 @@ class TestFruit(unittest.TestCase):
)
# setting a value that doesn't exist raises an exception
# with a key
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
fruit['invalid_variable'] = 'some value'
# Assert that we can call the builtin hasattr() function.
# hasattr should return False for non-existent attribute.
# Internally hasattr catches the AttributeError exception.
self.assertFalse(hasattr(fruit, 'invalid_variable'))
# Assert that we can call the builtin hasattr() function.
# hasattr should return True for existent attribute.
self.assertTrue(hasattr(fruit, 'color'))
# with setattr
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
setattr(fruit, 'invalid_variable', 'some value')
# getting a value that doesn't exist raises an exception
# with a key
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
invalid_variable = fruit['cultivar']
# with getattr
with self.assertRaises(petstore_api.ApiKeyError):
invalid_variable = getattr(fruit, 'cultivar', 'some value')
# Per Python doc, if the named attribute does not exist,
# default is returned if provided.
self.assertEquals(getattr(fruit, 'cultivar', 'some value'), 'some value')
# Per Python doc, if the named attribute does not exist,
# default is returned if provided, otherwise AttributeError is raised.
with self.assertRaises(AttributeError):
getattr(fruit, 'cultivar')
# make sure that the ModelComposed class properties are correct
# model._composed_schemas stores the anyOf/allOf/oneOf info

View File

@@ -46,19 +46,21 @@ class TestFruitReq(unittest.TestCase):
)
# setting a value that doesn't exist raises an exception
# with a key
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
fruit['invalid_variable'] = 'some value'
# with setattr
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
setattr(fruit, 'invalid_variable', 'some value')
# getting a value that doesn't exist raises an exception
# with a key
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
invalid_variable = fruit['cultivar']
# with getattr
with self.assertRaises(petstore_api.ApiKeyError):
invalid_variable = getattr(fruit, 'cultivar', 'some value')
self.assertEquals(getattr(fruit, 'cultivar', 'some value'), 'some value')
with self.assertRaises(AttributeError):
getattr(fruit, 'cultivar')
# make sure that the ModelComposed class properties are correct
# model._composed_schemas stores the anyOf/allOf/oneOf info

View File

@@ -51,19 +51,21 @@ class TestGmFruit(unittest.TestCase):
)
# setting a value that doesn't exist raises an exception
# with a key
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
fruit['invalid_variable'] = 'some value'
# with setattr
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
setattr(fruit, 'invalid_variable', 'some value')
# getting a value that doesn't exist raises an exception
# with a key
with self.assertRaises(petstore_api.ApiKeyError):
with self.assertRaises(AttributeError):
invalid_variable = fruit['cultivar']
# with getattr
with self.assertRaises(petstore_api.ApiKeyError):
invalid_variable = getattr(fruit, 'cultivar', 'some value')
self.assertTrue(getattr(fruit, 'cultivar', 'some value'), 'some value')
with self.assertRaises(AttributeError):
invalid_variable = getattr(fruit, 'cultivar')
# make sure that the ModelComposed class properties are correct
# model._composed_schemas stores the anyOf/allOf/oneOf info