Skip to content

Commit a66628e

Browse files
committed
fix(adapter-postgres): #1687 Add vector embedding validation and tests
1 parent c988a2f commit a66628e

File tree

5 files changed

+627
-1
lines changed

5 files changed

+627
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# PostgreSQL Adapter Tests
2+
3+
This directory contains tests for the PostgreSQL adapter with vector extension support.
4+
5+
## Prerequisites
6+
7+
- Docker installed and running
8+
- Node.js and pnpm installed
9+
- Bash shell (for Unix/Mac) or Git Bash (for Windows)
10+
11+
## Test Environment
12+
13+
The tests run against a PostgreSQL instance with the `pgvector` extension enabled. We use Docker to ensure a consistent test environment:
14+
15+
- PostgreSQL 16 with pgvector extension
16+
- Test database: `eliza_test`
17+
- Port: 5433 (to avoid conflicts with local PostgreSQL)
18+
- Vector dimensions: 1536 (OpenAI compatible)
19+
20+
## Running Tests
21+
22+
The easiest way to run tests is using the provided script:
23+
24+
```bash
25+
./run_tests.sh
26+
```
27+
28+
This script will:
29+
1. Start the PostgreSQL container with vector extension
30+
2. Wait for the database to be ready
31+
3. Run the test suite
32+
33+
## Manual Setup
34+
35+
If you prefer to run tests manually:
36+
37+
1. Start the test database:
38+
```bash
39+
docker compose -f docker-compose.test.yml up -d
40+
```
41+
42+
2. Wait for the database to be ready (about 30 seconds)
43+
44+
3. Run tests:
45+
```bash
46+
pnpm vitest vector-extension.test.ts
47+
```
48+
49+
## Test Structure
50+
51+
- `vector-extension.test.ts`: Main test suite for vector operations
52+
- `docker-compose.test.yml`: Docker configuration for test database
53+
- `run_tests.sh`: Helper script to run tests
54+
55+
## Troubleshooting
56+
57+
1. If tests fail with connection errors:
58+
- Check if Docker is running
59+
- Verify port 5433 is available
60+
- Wait a bit longer for database initialization
61+
62+
2. If vector operations fail:
63+
- Check if pgvector extension is properly loaded
64+
- Verify schema initialization
65+
- Check vector dimensions match (1536 for OpenAI)
66+
67+
## Notes
68+
69+
- Tests automatically clean up after themselves
70+
- Each test run starts with a fresh database
71+
- Vector extension is initialized as part of the schema setup
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json
2+
version: '3.8'
3+
services:
4+
postgres-test:
5+
image: pgvector/pgvector:pg16
6+
environment:
7+
POSTGRES_USER: postgres
8+
POSTGRES_PASSWORD: postgres
9+
POSTGRES_DB: eliza_test
10+
ports:
11+
- "5433:5432"
12+
healthcheck:
13+
test: ["CMD-SHELL", "pg_isready -U postgres"]
14+
interval: 5s
15+
timeout: 5s
16+
retries: 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/bin/bash
2+
3+
# Color output
4+
RED='\033[0;31m'
5+
GREEN='\033[0;32m'
6+
YELLOW='\033[1;33m'
7+
NC='\033[0m' # No Color
8+
9+
# Get script directory
10+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
11+
SCHEMA_PATH="$SCRIPT_DIR/../../schema.sql"
12+
13+
echo -e "${YELLOW}Starting PostgreSQL test environment...${NC}"
14+
15+
# Determine Docker Compose command
16+
if [[ "$OSTYPE" == "darwin"* ]]; then
17+
DOCKER_COMPOSE_CMD="docker compose"
18+
else
19+
DOCKER_COMPOSE_CMD="docker-compose"
20+
fi
21+
22+
# Stop any existing containers
23+
echo -e "${YELLOW}Cleaning up existing containers...${NC}"
24+
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml down
25+
26+
# Start fresh container
27+
echo -e "${YELLOW}Starting PostgreSQL container...${NC}"
28+
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml up -d
29+
30+
# Function to check if PostgreSQL is ready
31+
check_postgres() {
32+
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml exec -T postgres-test pg_isready -U postgres
33+
}
34+
35+
# Wait for PostgreSQL to be ready
36+
echo -e "${YELLOW}Waiting for PostgreSQL to be ready...${NC}"
37+
RETRIES=30
38+
until check_postgres || [ $RETRIES -eq 0 ]; do
39+
echo -e "${YELLOW}Waiting for PostgreSQL to be ready... ($RETRIES attempts left)${NC}"
40+
RETRIES=$((RETRIES-1))
41+
sleep 1
42+
done
43+
44+
if [ $RETRIES -eq 0 ]; then
45+
echo -e "${RED}Failed to connect to PostgreSQL${NC}"
46+
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml logs
47+
exit 1
48+
fi
49+
50+
echo -e "${GREEN}PostgreSQL is ready!${NC}"
51+
52+
# Load schema
53+
echo -e "${YELLOW}Loading database schema...${NC}"
54+
if [ ! -f "$SCHEMA_PATH" ]; then
55+
echo -e "${RED}Schema file not found at: $SCHEMA_PATH${NC}"
56+
exit 1
57+
fi
58+
59+
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml exec -T postgres-test psql -U postgres -d eliza_test -f - < "$SCHEMA_PATH"
60+
if [ $? -ne 0 ]; then
61+
echo -e "${RED}Failed to load schema${NC}"
62+
exit 1
63+
fi
64+
echo -e "${GREEN}Schema loaded successfully!${NC}"
65+
66+
# Run the tests
67+
echo -e "${YELLOW}Running tests...${NC}"
68+
pnpm vitest vector-extension.test.ts
69+
70+
# Capture test exit code
71+
TEST_EXIT_CODE=$?
72+
73+
# Clean up
74+
echo -e "${YELLOW}Cleaning up test environment...${NC}"
75+
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml down
76+
77+
# Exit with test exit code
78+
if [ $TEST_EXIT_CODE -eq 0 ]; then
79+
echo -e "${GREEN}Tests completed successfully!${NC}"
80+
else
81+
echo -e "${RED}Tests failed!${NC}"
82+
fi
83+
84+
exit $TEST_EXIT_CODE

0 commit comments

Comments
 (0)