class ConnectionTestCase(object): """ A base class to make testing connections a little less verbose/more standard. This automatically sets up a connection object for the service (``self.conn``) & runs a test to ensure the connection has all the right API methods. Usage:: from tests.integration.base import ConnectionTestCase from tests import unittest class MyConnTest(ConnectionTestCase, unittest.TestCase): service_name = 's3' ops = [ 'abort_multipart_upload', 'complete_multipart_upload', 'copy_object', 'create_bucket', 'create_multipart_upload', # ... ] # You can add your own test methods here... """ service_name = None ops = [] def setUp(self): super(ConnectionTestCase, self).setUp() self.session = Session() self.conn_class = self.session.get_connection(self.service_name) self.conn = self.conn_class() def test_op_methods(self): if not self.service_name: return if not len(self.ops): self.fail("There are no expected Connection methods supplied.") for op_name in self.ops: self.assertTrue( hasattr(self.conn, op_name), msg="{0} is missing.".format(op_name) ) self.assertTrue( callable(getattr(self.conn, op_name)), msg="{0} is not callable.".format(op_name) )
class SessionTestCase(unittest.TestCase): def setUp(self): super(SessionTestCase, self).setUp() self.session = Session() def test_get_core_service(self): client = self.session.get_core_service('sqs') self.assertTrue(isinstance(client, BotocoreService)) def test_get_connection_exists(self): self.assertEqual(len(self.session.cache), 0) # Put in a sentinel. self.session.cache.set_connection('test', FakeConnection) self.assertEqual(len(self.session.cache), 1) client = self.session.get_connection('test') self.assertTrue(client is FakeConnection) def test_get_connection_does_not_exist(self): self.assertEqual(len(self.session.cache), 0) client = self.session.get_connection('sqs') self.assertEqual(client.__name__, 'SqsConnection') self.assertEqual(len(self.session.cache), 1) def test_get_resource_exists(self): self.assertEqual(len(self.session.cache), 0) # Put in a sentinel. self.session.cache.set_resource('test', 'Test', FakeConnection) self.assertEqual(len(self.session.cache), 1) # Ugly, but needed due to our faking connections above. Test = self.session.get_resource('test', 'Test', base_class=object) self.assertTrue(Test is FakeConnection) def test_get_resource_does_not_exist(self): self.assertEqual(len(self.session.cache), 0) Queue = self.session.get_resource('sqs', 'Queue') self.assertEqual(Queue.__name__, 'Queue') self.assertEqual(len(self.session.cache), 1) def test_get_collection_exists(self): self.assertEqual(len(self.session.cache), 0) # Put in a sentinel. self.session.cache.set_collection('test', 'Test', FakeConnection) self.assertEqual(len(self.session.cache), 1) # Ugly, but needed due to our faking connections above. TestCollection = self.session.get_collection( 'test', 'Test', base_class=object ) self.assertTrue(TestCollection is FakeConnection) def test_get_collection_does_not_exist(self): self.assertEqual(len(self.session.cache), 0) QueueCollection = self.session.get_collection('sqs', 'QueueCollection') self.assertEqual(QueueCollection.__name__, 'QueueCollection') self.assertEqual(len(self.session.cache), 1) def test_connect_to_region(self): client = self.session.connect_to('sqs', region_name='us-west-2') self.assertEqual(client.__class__.__name__, 'SqsConnection') self.assertEqual(client.region_name, 'us-west-2')