-
Notifications
You must be signed in to change notification settings - Fork 18
chore: support ids in Documents and update insert SQL query to upsert #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f51761
chore: support ids in Documents and update insert SQL query to upsert
dishaprakash 9eccb3e
chore: Add Langchain vectorstore standard suite tests (#272)
dishaprakash 453d25b
Newline
dishaprakash e67f442
remove Newline
dishaprakash 5444544
Review changes
dishaprakash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,159 @@ | ||
| # Copyright 2025 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 uuid | ||
|
|
||
| import pytest | ||
| import pytest_asyncio | ||
| from langchain_tests.integration_tests import VectorStoreIntegrationTests | ||
| from langchain_tests.integration_tests.vectorstores import EMBEDDING_SIZE | ||
| from sqlalchemy import text | ||
|
|
||
| from langchain_google_cloud_sql_pg import Column, PostgresEngine, PostgresVectorStore | ||
|
|
||
| DEFAULT_TABLE = "test_table_standard_test_suite" + str(uuid.uuid4()) | ||
| DEFAULT_TABLE_SYNC = "test_table_sync_standard_test_suite" + str(uuid.uuid4()) | ||
|
|
||
|
|
||
| def get_env_var(key: str, desc: str) -> str: | ||
| v = os.environ.get(key) | ||
| if v is None: | ||
| raise ValueError(f"Must set env var {key} to: {desc}") | ||
| return v | ||
|
|
||
|
|
||
| async def aexecute( | ||
| engine: PostgresEngine, | ||
| query: str, | ||
| ) -> None: | ||
| async def run(engine, query): | ||
| async with engine._pool.connect() as conn: | ||
| await conn.execute(text(query)) | ||
| await conn.commit() | ||
|
|
||
| await engine._run_as_async(run(engine, query)) | ||
|
|
||
|
|
||
| @pytest.mark.filterwarnings("ignore") | ||
| @pytest.mark.asyncio | ||
| class TestStandardSuiteSync(VectorStoreIntegrationTests): | ||
| @pytest.fixture(scope="module") | ||
| def db_project(self) -> str: | ||
| return get_env_var("PROJECT_ID", "project id for google cloud") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def db_region(self) -> str: | ||
| return get_env_var("REGION", "region for Cloud SQL instance") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def db_instance(self) -> str: | ||
| return get_env_var("INSTANCE_ID", "instance for Cloud SQL") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def db_name(self) -> str: | ||
| return get_env_var("DATABASE_ID", "database name on Cloud SQL instance") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def user(self) -> str: | ||
| return get_env_var("DB_USER", "database user for Cloud SQL") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def password(self) -> str: | ||
| return get_env_var("DB_PASSWORD", "database password for Cloud SQL") | ||
|
|
||
| @pytest_asyncio.fixture(loop_scope="function") | ||
| async def sync_engine(self, db_project, db_region, db_instance, db_name): | ||
| sync_engine = PostgresEngine.from_instance( | ||
| project_id=db_project, | ||
| instance=db_instance, | ||
| region=db_region, | ||
| database=db_name, | ||
| ) | ||
| yield sync_engine | ||
| await aexecute(sync_engine, f'DROP TABLE IF EXISTS "{DEFAULT_TABLE_SYNC}"') | ||
| await sync_engine.close() | ||
|
|
||
| @pytest.fixture(scope="function") | ||
| def vectorstore(self, sync_engine): | ||
| """Get an empty vectorstore for unit tests.""" | ||
| sync_engine.init_vectorstore_table( | ||
| DEFAULT_TABLE_SYNC, | ||
| EMBEDDING_SIZE, | ||
| id_column=Column(name="langchain_id", data_type="VARCHAR", nullable=False), | ||
| ) | ||
|
|
||
| vs = PostgresVectorStore.create_sync( | ||
| sync_engine, | ||
| embedding_service=self.get_embeddings(), | ||
| table_name=DEFAULT_TABLE_SYNC, | ||
| ) | ||
| yield vs | ||
|
|
||
|
|
||
| @pytest.mark.filterwarnings("ignore") | ||
| @pytest.mark.asyncio | ||
| class TestStandardSuiteAsync(VectorStoreIntegrationTests): | ||
| @pytest.fixture(scope="module") | ||
| def db_project(self) -> str: | ||
| return get_env_var("PROJECT_ID", "project id for google cloud") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def db_region(self) -> str: | ||
| return get_env_var("REGION", "region for Cloud SQL instance") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def db_instance(self) -> str: | ||
| return get_env_var("INSTANCE_ID", "instance for Cloud SQL") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def db_name(self) -> str: | ||
| return get_env_var("DATABASE_ID", "database name on Cloud SQL instance") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def user(self) -> str: | ||
| return get_env_var("DB_USER", "database user for Cloud SQL") | ||
|
|
||
| @pytest.fixture(scope="module") | ||
| def password(self) -> str: | ||
| return get_env_var("DB_PASSWORD", "database password for Cloud SQL") | ||
|
|
||
| @pytest_asyncio.fixture(loop_scope="function") | ||
| async def async_engine(self, db_project, db_region, db_instance, db_name): | ||
| async_engine = await PostgresEngine.afrom_instance( | ||
| project_id=db_project, | ||
| instance=db_instance, | ||
| region=db_region, | ||
| database=db_name, | ||
| ) | ||
| yield async_engine | ||
| await aexecute(async_engine, f'DROP TABLE IF EXISTS "{DEFAULT_TABLE}"') | ||
| await async_engine.close() | ||
|
|
||
| @pytest_asyncio.fixture(loop_scope="function") | ||
| async def vectorstore(self, async_engine): | ||
| """Get an empty vectorstore for unit tests.""" | ||
| await async_engine.ainit_vectorstore_table( | ||
| DEFAULT_TABLE, | ||
| EMBEDDING_SIZE, | ||
| id_column=Column(name="langchain_id", data_type="VARCHAR", nullable=False), | ||
| ) | ||
|
|
||
| vs = await PostgresVectorStore.create( | ||
| async_engine, | ||
| embedding_service=self.get_embeddings(), | ||
| table_name=DEFAULT_TABLE, | ||
| ) | ||
|
|
||
| yield vs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.