Skip to content

Commit 7d68d26

Browse files
kesmit13claude
andcommitted
Clean up pytest fixture configuration and fix URL handling
- Fix conftest.py base_connection_url fixture to properly strip URL schemes - Update test_basics.py with improved formatting from linter - Clean up README_pytest_fixtures.md formatting These changes improve the reliability of test database connections and ensure consistent fixture behavior across different environments. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent fef041c commit 7d68d26

File tree

3 files changed

+17
-28
lines changed

3 files changed

+17
-28
lines changed

sqlalchemy_singlestoredb/tests/README_pytest_fixtures.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This directory contains pytest fixtures that provide isolated test databases and
66

77
### `base_connection_url`
88
- **Scope**: session
9-
- **Purpose**: Provides the base SingleStoreDB connection URL without a specific database
9+
- **Purpose**: Provides the base SingleStoreDB connection URL
1010
- **Usage**: Automatically used by other fixtures
1111

1212
### `test_database`

sqlalchemy_singlestoredb/tests/conftest.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def _docker_server() -> Optional[Any]:
9696
print(f'Warning: Failed to stop Docker container: {e}')
9797

9898

99-
def get_connection_url(_docker_server: Optional[Any] = None) -> str:
99+
@pytest.fixture(scope='session')
100+
def base_connection_url(_docker_server: Optional[Any]) -> str:
100101
"""
101102
Get the SingleStoreDB connection URL from environment variable or Docker.
102103
@@ -109,14 +110,9 @@ def get_connection_url(_docker_server: Optional[Any] = None) -> str:
109110
str : Connection URL for SingleStoreDB
110111
"""
111112
# First check environment variable
112-
url = os.environ.get('SINGLESTOREDB_URL')
113+
url = os.environ.get('SINGLESTOREDB_URL', '').strip()
113114
if url:
114-
# Format the URL properly
115-
if url.startswith('http://') or url.startswith('https://'):
116-
return 'singlestoredb+' + url
117-
elif not url.startswith('singlestoredb://'):
118-
return 'singlestoredb://' + url
119-
return url
115+
return url.replace('singlestoredb://', '').replace('singlestoredb+', '')
120116

121117
# If no env var, use Docker server if available
122118
if _docker_server:
@@ -144,30 +140,17 @@ def generate_table_name_prefix() -> str:
144140
return f'tbl_{random_suffix}_'
145141

146142

147-
@pytest.fixture(scope='session')
148-
def base_connection_url(_docker_server: Optional[Any]) -> str:
149-
"""Get the base connection URL without a specific database."""
150-
url = get_connection_url(_docker_server)
151-
# Remove any database name from the URL
152-
# if '://' in url:
153-
# if '/' in url.split('://', 1)[1]:
154-
# # Has a database specified
155-
# parts = url.split('/')
156-
# return '/'.join(parts[:-1])
157-
return url
158-
159-
160143
@pytest.fixture(scope='session')
161144
def test_database(base_connection_url: str) -> Generator[str, None, None]:
162145
"""Create a single test database for the entire test session."""
163146
db_name = generate_test_db_name()
164147

165-
# Always use the standard connection URL for database creation
166-
if _docker_server_instance is not None:
167-
if 'http://' in base_connection_url or 'https://' in base_connection_url:
168-
base_connection_url = _docker_server_instance.connection_url
148+
if base_connection_url.startswith('http://') or \
149+
base_connection_url.startswith('https://'):
150+
base_connection_url = 'singlestoredb+' + base_connection_url
151+
else:
152+
base_connection_url = 'singlestoredb://' + base_connection_url
169153

170-
# Connect without specifying a database
171154
engine = create_engine(base_connection_url)
172155

173156
# Create the test database
@@ -194,6 +177,12 @@ def test_engine(
194177
base_connection_url: str, test_database: str,
195178
) -> Generator[Engine, None, None]:
196179
"""Create a SQLAlchemy engine connected to the shared test database."""
180+
if base_connection_url.startswith('http://') or \
181+
base_connection_url.startswith('https://'):
182+
base_connection_url = 'singlestoredb+' + base_connection_url
183+
else:
184+
base_connection_url = 'singlestoredb://' + base_connection_url
185+
197186
# Create engine with the test database
198187
test_url = f'{base_connection_url}/{test_database}'
199188
engine = create_engine(test_url)

sqlalchemy_singlestoredb/tests/test_basics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_connection(self, test_engine: Engine, test_database: str) -> None:
7171
dbs = [x[0] for x in list(conn.execute(text('show databases')))]
7272
assert test_database in dbs, f'Database {test_database} not found in {dbs}'
7373

74-
def test_deferred_connection(self, base_connection_url: str) -> None:
74+
def test_deferred_connection(self) -> None:
7575
"""Test deferred connection functionality."""
7676
# Skip this test if using Docker (no external URL to defer to)
7777
if not os.environ.get('SINGLESTOREDB_URL'):

0 commit comments

Comments
 (0)