-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support basic user/password auth with
MySQLEngine
(#17)
- Loading branch information
1 parent
d1ce730
commit d462a3c
Showing
4 changed files
with
150 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
|
||
import sqlalchemy | ||
|
||
from langchain_google_cloud_sql_mysql import MySQLEngine | ||
|
||
project_id = os.environ["PROJECT_ID"] | ||
region = os.environ["REGION"] | ||
instance_id = os.environ["INSTANCE_ID"] | ||
db_name = os.environ["DB_NAME"] | ||
db_user = os.environ["DB_USER"] | ||
db_password = os.environ["DB_PASSWORD"] | ||
|
||
|
||
def test_mysql_engine_with_basic_auth() -> None: | ||
"""Test MySQLEngine works with basic user/password auth.""" | ||
# override MySQLEngine._connector to allow a new Connector to be initiated | ||
MySQLEngine._connector = None | ||
engine = MySQLEngine.from_instance( | ||
project_id=project_id, | ||
region=region, | ||
instance=instance_id, | ||
database=db_name, | ||
user=db_user, | ||
password=db_password, | ||
) | ||
# test connection with query | ||
with engine.connect() as conn: | ||
res = conn.execute(sqlalchemy.text("SELECT 1")).fetchone() | ||
conn.commit() | ||
assert res[0] == 1 # type: ignore | ||
# reset MySQLEngine._connector to allow a new Connector to be initiated | ||
MySQLEngine._connector = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
|
||
from langchain_google_cloud_sql_mysql import MySQLEngine | ||
|
||
|
||
def test_mysql_engine_with_invalid_arg_pattern() -> None: | ||
"""Test MySQLEngine errors when only one of user or password is given. | ||
Both user and password must be specified (basic authentication) | ||
or neither (IAM authentication). | ||
""" | ||
expected_error_msg = "Only one of 'user' or 'password' were specified. Either both should be specified to use basic user/password authentication or neither for IAM DB authentication." | ||
# test password not set | ||
with pytest.raises(ValueError) as exc_info: | ||
MySQLEngine.from_instance( | ||
project_id="my-project", | ||
region="my-region", | ||
instance="my-instance", | ||
database="my-db", | ||
user="my-user", | ||
) | ||
# assert custom error is present | ||
assert exc_info.value.args[0] == expected_error_msg | ||
|
||
# test user not set | ||
with pytest.raises(ValueError) as exc_info: | ||
MySQLEngine.from_instance( | ||
project_id="my-project", | ||
region="my-region", | ||
instance="my-instance", | ||
database="my-db", | ||
password="my-pass", | ||
) | ||
# assert custom error is present | ||
assert exc_info.value.args[0] == expected_error_msg |