def test_ensure_defaults_nonexistent_alias(self): msg = "The connection 'nonexistent' doesn't exist." conns = ConnectionHandler({ DEFAULT_DB_ALIAS: {'ENGINE': 'django.db.backends.dummy'}, }) with self.assertRaisesMessage(ConnectionDoesNotExist, msg): conns.ensure_defaults('nonexistent')
def get_num_connections() -> int: """ Connect to the "postgres" database to see pg_stat_activity data. We need raw psycopg2 cursor access here, because the test-runner creates a separate test database otherwise. """ handler = ConnectionHandler({"default": settings.POSTGRES_CONN_PARAMS}) handler.ensure_defaults("default") connection = handler["default"] app_user = settings.DATABASES["default"]["USER"] # seems like the connections need some time to close on the backend? time.sleep(1) try: with connection.cursor() as cursor: # debug - show pg_stat_activity records cursor.execute(SHOW_CONNECTIONS, [app_user]) rows = cursor.fetchall() headers = [x.name for x in cursor.description] tabulated = tabulate(rows, headers=headers) print("Open connections:") print(tabulated) # filter on the test query unexpected = [row for row in rows if row[-1] == TEST_QUERY] count = len(unexpected) finally: connection.close() return count