Skip to content

Commit 045b614

Browse files
feat: raise exception if database doesnt exist (#96)
Closes #78 I wanted to write a test to assert the exception gets raised and had a hard time fitting it into the test framework because by the time I receive the target in the test methods its already instantiated with the sample config with the valid database so I cant override it in an individual test. So I'd need to create a suite and call get_target_test_class just to run a single test, instead I just ran the components as a standalone test. --------- Co-authored-by: Edgar R. M. <edgar@meltano.com>
1 parent 2d7a610 commit 045b614

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

target_snowflake/connector.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def create_engine(self) -> Engine:
134134
Returns:
135135
A new SQLAlchemy Engine.
136136
"""
137-
return sqlalchemy.create_engine(
137+
engine = sqlalchemy.create_engine(
138138
self.sqlalchemy_url,
139139
connect_args={
140140
"session_parameters": {
@@ -143,6 +143,12 @@ def create_engine(self) -> Engine:
143143
},
144144
echo=False,
145145
)
146+
connection = engine.connect()
147+
db_names = [db[1] for db in connection.execute(text("SHOW DATABASES;")).fetchall()]
148+
if self.config["database"] not in db_names:
149+
raise Exception(f"Database '{self.config['database']}' does not exist or the user/role doesn't have access to it.")
150+
return engine
151+
146152

147153
def prepare_column(
148154
self,

tests/test_target_snowflake.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,14 @@ class TestTargetSnowflake(BaseSnowflakeTargetTests, StandardTargetTests): # typ
8989

9090
class TestTargetSnowflakeBatch(BaseSnowflakeTargetTests, BatchTargetTests): # type: ignore[misc, valid-type] # noqa: E501
9191
"""Batch Target Tests."""
92+
93+
def test_invalid_database():
94+
INVALID_TEST_CONFIG = copy.deepcopy(SAMPLE_CONFIG)
95+
INVALID_TEST_CONFIG["database"] = "FOO_BAR_DOESNT_EXIST"
96+
runner = TargetTestRunner(
97+
TargetSnowflake,
98+
config=INVALID_TEST_CONFIG,
99+
input_filepath="tests/target_test_streams/existing_table.singer"
100+
)
101+
with pytest.raises(Exception):
102+
runner.sync_all()

0 commit comments

Comments
 (0)