class TestPostgresDockerRunner(unittest.TestCase): """ Test the docker runner for the Postgres service """ def setUp(self): self.name = 'livetest-postgres-{}'.format(gen_salt(5)) self.builder = PostgresDockerRunner(name=self.name, ) def tearDown(self): """ teardown the containers """ try: self.builder.teardown() except: pass def test_is_ready(self): """ Check if the instance is ready """ self.builder.start() self.assertTrue(self.builder.running) self.assertTrue(self.builder.ready) self.builder.teardown() self.assertFalse(self.builder.ready) self.assertFalse(self.builder.running)
class TestPostgresDockerRunner(unittest.TestCase): """ Test the docker runner for the Postgres service """ def setUp(self): self.name = 'livetest-postgres-{}'.format(gen_salt(5)) self.builder = PostgresDockerRunner( name=self.name, ) def tearDown(self): """ teardown the containers """ try: self.builder.teardown() except: pass def test_is_ready(self): """ Check if the instance is ready """ self.builder.start() self.assertTrue(self.builder.running) self.assertTrue(self.builder.ready) self.builder.teardown() self.assertFalse(self.builder.ready) self.assertFalse(self.builder.running)
class TestPostgresProvisioner(unittest.TestCase): """ Test the PostgresProvisioner. Use the Docker builder to create the database """ def setUp(self): self.name = 'livetest-postgres-{}'.format(gen_salt(5)) self.builder = PostgresDockerRunner( name=self.name, ) self.builder.start() self.port = self.builder.client.port( self.builder.container['Id'], 5432 )[0]['HostPort'] def tearDown(self): self.builder.teardown() def _make_db_connection(self, db, user=None): """ retruns a sqlalchemy connection object to a database """ if user is None: user = db engine = create_engine( 'postgresql://{user}@localhost:{port}/{db}'.format( user=user, port=self.port, db=db ) ) return engine.connect() def _provision(self, service): """ Run the provision for a given service """ # Modifying a live application's config is the most straightforward # way to connect to a non-default port for this test case PostgresProvisioner(service, container=self.builder)() def test_provisioning_adsws(self): """ after running the provisioner, make sure that the anon user and anon oauth2client exist within the postgres instance """ self._provision('adsws') conn = self._make_db_connection('adsws') res = conn.execute('SELECT * FROM users').fetchall() anon_user = filter( lambda i: i['email'] == 'anonymous@ads', res, )[0] self.assertIsNotNone(anon_user) res = conn.execute('SELECT * FROM oauth2client').fetchall() anon_client = filter( lambda i: i['user_id'] == anon_user['id'], res, ) self.assertIsNotNone(anon_client) def test_provisioning_recommender(self): """ after running the provisioner, make sure that the recommender has the expected tables and that they return at least 1 row """ self._provision('recommender') conn = self._make_db_connection('recommender') coreads = conn.execute('SELECT * FROM coreads').fetchall() clusters = conn.execute('SELECT * FROM clusters').fetchall() clustering = conn.execute('SELECT * FROM clustering').fetchall() self.assertGreater(len(coreads), 0) self.assertGreater(len(clusters), 0) self.assertGreater(len(clustering), 0)