def test_query_on_some_bad(self): if self.is_banning_disabled(): return True provider = ClientProvider('some_bad') adapter = DatabaseAdapter(provider, None) adapter.raw_query(sql="SELECT 1") self.assertIsNotNone(provider.next_client())
def query_schema(self): db_adapter = DatabaseAdapter( self.db(role='replica', db_name=self.db_names.get('replica')), self.model_metadata(include_schema=False)) return db_adapter.raw_query( sql=db_adapter.db_client.lexicon.table_schema_query( self._table_name()), where={ 'table_name': self._table_name() }).to_dict(orient='records')
def test_query_on_all_bad(self): if self.is_banning_disabled(): return True provider = ClientProvider('all_bad') adapter = DatabaseAdapter(provider, None) with self.assertRaises(NoAvailableConnectionsError): adapter.raw_query(sql="SELECT 1") self.assertIsNone(provider.next_client()) # all connections are banned time.sleep(adapter.ban_time) # the ban is lifted after ban_time seconds elapse self.assertIsNotNone(provider.next_client())
def test_query_on_some_bad(self): if self.is_banning_disabled(): return True provider = ClientProvider('some_bad') adapter = DatabaseAdapter(provider, None) adapter.raw_query(sql="SELECT 1") found_query_event = False for event in self.subscriber.published_events: if event.name == "query": found_query_event = True self.assertTrue(found_query_event)
def test_retries(self): if self.is_banning_disabled(): return True query_report = None with QueryTracer(): provider = ClientProvider('all_bad') adapter = DatabaseAdapter(provider, None) with self.assertRaises(NoAvailableConnectionsError): adapter.raw_query(sql="SELECT 1") query_report = QueryTracer.get_report() expected_query_attempts = provider.connection_count() * adapter.max_retries expected_connection_bans = provider.connection_count() self.assertEqual(len(query_report["query_list"]), expected_query_attempts) self.assertEqual(len(query_report["ban_list"]), expected_connection_bans)
def db_adapter(self, role='replica', db_name=None): if not hasattr(self, '_db_metadata'): self._db_metadata = {} db_name = db_name or self.db_names.get(role) key = '%s_%s' % (db_name, role) if key not in self._db_metadata: self._db_metadata[key] = self.model_metadata() return DatabaseAdapter(self.db(role=role, db_name=db_name), self._db_metadata[key])
def test_query_on_all_bad(self): if self.is_banning_disabled(): return True provider = ClientProvider('all_bad') adapter = DatabaseAdapter(provider, None) with self.assertRaises(NoAvailableConnectionsError): adapter.raw_query(sql="SELECT 1") found_no_available_connections_raised_event = False connection_banned_event_count = 0 for event in self.subscriber.published_events: if event.name == "no_available_connections_raised": found_no_available_connections_raised_event = True self.assertIsNotNone(event.error) if event.name == "connection_banned": connection_banned_event_count += 1 self.assertIsNotNone(event.error) self.assertTrue( connection_banned_event_count == provider.connection_count()) self.assertTrue(found_no_available_connections_raised_event)
def query(sql=None, filename=None, extract=None, db=None, **kwargs): if db is None: raise argparse.ArgumentError('You must provide a database name') kwargs['stack'] = stack_marker(inspect.stack()) filename = filename or extract if filename and not filename.startswith('/'): filename = os.path.join(os.environ['PWD'], filename) if 'where' not in kwargs and 'params' in kwargs: kwargs['where'] = kwargs['params'] return DatabaseAdapter(ClientProvider(db), None).raw_query(sql=sql, filename=filename, **kwargs)