-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_setup.py
More file actions
31 lines (25 loc) · 1.05 KB
/
test_setup.py
File metadata and controls
31 lines (25 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pytest
from sqlalchemy import create_engine, Engine
from contextlib import contextmanager
from sqlalchemy.orm import DeclarativeBase, Session
class TestSetup:
@pytest.fixture(autouse=True)
def get_details(self, connection_details):
self.arguments = connection_details.copy()
def db_engine(self) -> Engine:
HOST = self.arguments["host"]
HTTP_PATH = self.arguments["http_path"]
ACCESS_TOKEN = self.arguments["access_token"]
CATALOG = self.arguments["catalog"]
SCHEMA = self.arguments["schema"]
connect_args = {"user_agent_entry": "SQLAlchemy e2e Tests"}
conn_string = f"databricks://token:{ACCESS_TOKEN}@{HOST}?http_path={HTTP_PATH}&catalog={CATALOG}&schema={SCHEMA}"
return create_engine(conn_string, connect_args=connect_args)
@contextmanager
def table_context(self, table: DeclarativeBase):
engine = self.db_engine()
table.metadata.create_all(engine)
try:
yield engine
finally:
table.metadata.drop_all(engine)