Skip to content
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

fix: PGVector_embedding_validation (#1687) #1750

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions packages/adapter-postgres/src/__tests__/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# PostgreSQL Adapter Tests

This directory contains tests for the PostgreSQL adapter with vector extension support.

## Prerequisites

- Docker installed and running
- Node.js and pnpm installed
- Bash shell (for Unix/Mac) or Git Bash (for Windows)

## Test Environment

The tests run against a PostgreSQL instance with the `pgvector` extension enabled. We use Docker to ensure a consistent test environment:

- PostgreSQL 16 with pgvector extension
- Test database: `eliza_test`
- Port: 5433 (to avoid conflicts with local PostgreSQL)
- Vector dimensions: 1536 (OpenAI compatible)

## Running Tests

The easiest way to run tests is using the provided script:

```bash
./run_tests.sh
```

This script will:
1. Start the PostgreSQL container with vector extension
2. Wait for the database to be ready
3. Run the test suite

## Manual Setup

If you prefer to run tests manually:

1. Start the test database:
```bash
docker compose -f docker-compose.test.yml up -d
```

2. Wait for the database to be ready (about 30 seconds)

3. Run tests:
```bash
pnpm vitest vector-extension.test.ts
```

## Test Structure

- `vector-extension.test.ts`: Main test suite for vector operations
- `docker-compose.test.yml`: Docker configuration for test database
- `run_tests.sh`: Helper script to run tests

## Troubleshooting

1. If tests fail with connection errors:
- Check if Docker is running
- Verify port 5433 is available
- Wait a bit longer for database initialization

2. If vector operations fail:
- Check if pgvector extension is properly loaded
- Verify schema initialization
- Check vector dimensions match (1536 for OpenAI)

## Notes

- Tests automatically clean up after themselves
- Each test run starts with a fresh database
- Vector extension is initialized as part of the schema setup
16 changes: 16 additions & 0 deletions packages/adapter-postgres/src/__tests__/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json
version: '3.8'
services:
postgres-test:
image: pgvector/pgvector:pg16
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: eliza_test
ports:
- "5433:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
78 changes: 78 additions & 0 deletions packages/adapter-postgres/src/__tests__/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash

# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Get script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCHEMA_PATH="$SCRIPT_DIR/../../schema.sql"

echo -e "${YELLOW}Starting PostgreSQL test environment...${NC}"

# Determine Docker Compose command
if [[ "$OSTYPE" == "darwin"* ]]; then
DOCKER_COMPOSE_CMD="docker compose"
else
DOCKER_COMPOSE_CMD="docker-compose"
fi

# Stop any existing containers
echo -e "${YELLOW}Cleaning up existing containers...${NC}"
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml down

# Start fresh container
echo -e "${YELLOW}Starting PostgreSQL container...${NC}"
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml up -d

# Function to check if PostgreSQL is ready
check_postgres() {
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml exec -T postgres-test pg_isready -U postgres
}

# Wait for PostgreSQL to be ready
echo -e "${YELLOW}Waiting for PostgreSQL to be ready...${NC}"
RETRIES=30
until check_postgres || [ $RETRIES -eq 0 ]; do
echo -e "${YELLOW}Waiting for PostgreSQL to be ready... ($RETRIES attempts left)${NC}"
RETRIES=$((RETRIES-1))
sleep 1
done

if [ $RETRIES -eq 0 ]; then
echo -e "${RED}Failed to connect to PostgreSQL${NC}"
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml logs
exit 1
fi

echo -e "${GREEN}PostgreSQL is ready!${NC}"

# Load schema
echo -e "${YELLOW}Loading database schema...${NC}"
if [ ! -f "$SCHEMA_PATH" ]; then
echo -e "${RED}Schema file not found at: $SCHEMA_PATH${NC}"
exit 1
fi

# Fix: Check exit code directly instead of using $?
if ! $DOCKER_COMPOSE_CMD -f docker-compose.test.yml exec -T postgres-test psql -U postgres -d eliza_test -f - < "$SCHEMA_PATH"; then
echo -e "${RED}Failed to load schema${NC}"
exit 1
fi
echo -e "${GREEN}Schema loaded successfully!${NC}"

# Run the tests
echo -e "${YELLOW}Running tests...${NC}"
if ! pnpm vitest vector-extension.test.ts; then
echo -e "${RED}Tests failed!${NC}"
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml down
exit 1
fi

echo -e "${GREEN}Tests completed successfully!${NC}"

# Clean up
echo -e "${YELLOW}Cleaning up test environment...${NC}"
$DOCKER_COMPOSE_CMD -f docker-compose.test.yml down
Loading
Loading