-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathutils.py
56 lines (46 loc) · 1.91 KB
/
utils.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Get fixtures for the database connection."""
import os
from contextlib import asynccontextmanager, contextmanager
import psycopg
from typing_extensions import AsyncGenerator, Generator
# Env variables match the default settings in the docker-compose file
# located in the root of the repository: [root]/docker-compose.yml
# Non-standard ports are used to avoid conflicts with other local postgres
# instances.
# To spint up the postgres service for testing, run:
# cd [root]/docker-compose.yml
# docker-compose up pgvector
POSTGRES_USER = os.environ.get("POSTGRES_USER", "langchain")
POSTGRES_HOST = os.environ.get("POSTGRES_HOST", "localhost")
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "langchain")
POSTGRES_DB = os.environ.get("POSTGRES_DB", "langchain")
POSTGRES_PORT = os.environ.get("POSTGRES_PORT", "6024")
DSN = (
f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}"
f":{POSTGRES_PORT}/{POSTGRES_DB}"
)
# Connection string used primarily by the vectorstores tests
# it's written to work with SQLAlchemy (takes a driver name)
# It is also running on a postgres instance that has the pgvector extension
VECTORSTORE_CONNECTION_STRING = (
f"postgresql+psycopg://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}"
f":{POSTGRES_PORT}/{POSTGRES_DB}"
)
@asynccontextmanager
async def asyncpg_client() -> AsyncGenerator[psycopg.AsyncConnection, None]:
# Establish a connection to your test database
conn = await psycopg.AsyncConnection.connect(conninfo=DSN)
try:
yield conn
finally:
# Cleanup: close the connection after the test is done
await conn.close()
@contextmanager
def syncpg_client() -> Generator[psycopg.Connection, None, None]:
# Establish a connection to your test database
conn = psycopg.connect(conninfo=DSN)
try:
yield conn
finally:
# Cleanup: close the connection after the test is done
conn.close()