1111
1212import os
1313import threading
14+ import datetime
15+ from contextlib import contextmanager
16+ from unittest import mock
17+
18+ import asyncpg
19+ import pytest
20+ import pytest_asyncio
21+ from asyncpg import connect , Connection
1422
23+ from sentry_sdk import capture_message , start_transaction
24+ from sentry_sdk .integrations .asyncpg import AsyncPGIntegration
25+ from sentry_sdk .consts import SPANDATA
26+ from sentry_sdk .tracing_utils import record_sql_queries
27+ from tests .conftest import ApproxDict
1528
1629PG_HOST = os .getenv ("SENTRY_PYTHON_TEST_POSTGRES_HOST" , "localhost" )
1730PG_PORT = int (os .getenv ("SENTRY_PYTHON_TEST_POSTGRES_PORT" , "5432" ))
@@ -28,22 +41,6 @@ def _get_db_name():
2841
2942PG_NAME = _get_db_name ()
3043
31- import datetime
32- from contextlib import contextmanager
33- from unittest import mock
34-
35- import asyncpg
36- import pytest
37- import pytest_asyncio
38- from asyncpg import connect , Connection
39-
40- from sentry_sdk import capture_message , start_transaction
41- from sentry_sdk .integrations .asyncpg import AsyncPGIntegration
42- from sentry_sdk .consts import SPANDATA
43- from sentry_sdk .tracing_utils import record_sql_queries
44- from tests .conftest import ApproxDict
45-
46-
4744PG_CONNECTION_URI = "postgresql://{}:{}@{}/{}" .format (
4845 PG_USER , PG_PASSWORD , PG_HOST , PG_NAME
4946)
@@ -65,29 +62,6 @@ def _get_db_name():
6562
6663@pytest_asyncio .fixture (autouse = True )
6764async def _clean_pg ():
68- # Connect to the default postgres database to create our test database
69- default_conn_uri = "postgresql://{}:{}@{}/{}" .format (
70- PG_USER , PG_PASSWORD , PG_HOST , PG_NAME_BASE
71- )
72-
73- # Create the test database if it doesn't exist
74- try :
75- default_conn = await connect (default_conn_uri )
76- try :
77- # Check if database exists, create if not
78- result = await default_conn .fetchval (
79- "SELECT 1 FROM pg_database WHERE datname = $1" , PG_NAME
80- )
81- if not result :
82- await default_conn .execute (f'CREATE DATABASE "{ PG_NAME } "' )
83- finally :
84- await default_conn .close ()
85- except Exception :
86- # If we can't connect to default postgres db, assume our test db already exists
87- # or that we're connecting to the same database (PG_NAME == PG_NAME_BASE)
88- pass
89-
90- # Now connect to our test database and set up the table
9165 conn = await connect (PG_CONNECTION_URI )
9266 await conn .execute ("DROP TABLE IF EXISTS users" )
9367 await conn .execute (
0 commit comments