This document provides comprehensive information about testing sqlite-vec-client.
tests/
├── __init__.py # Test package initialization
├── conftest.py # Pytest fixtures and configuration
├── test_utils.py # Unit tests for utility functions
├── test_validation.py # Unit tests for validation functions
├── test_security.py # Security tests (SQL injection, validation)
└── test_client.py # Integration tests for SQLiteVecClient
Tests for individual functions and modules in isolation.
Markers: @pytest.mark.unit
Files:
test_utils.py- Tests for serialization/deserialization functionstest_validation.py- Tests for input validation functionstest_security.py- Security tests for SQL injection prevention and input validation
Run unit tests only:
pytest -m unitTests for complete workflows and client operations.
Markers: @pytest.mark.integration
Files:
test_client.py- Tests for SQLiteVecClient methods
Run integration tests only:
pytest -m integrationCommon fixtures available in conftest.py:
- temp_db: Provides a temporary database file path
- client: Provides a SQLiteVecClient instance with temp database
- client_with_table: Provides a client with table already created (dim=3)
- sample_embeddings: 3D sample embeddings for testing
- sample_texts: Sample text strings
- sample_metadata: Sample metadata dictionaries
Example usage:
def test_example(client_with_table, sample_texts, sample_embeddings):
rowids = client_with_table.add(texts=sample_texts, embeddings=sample_embeddings)
assert len(rowids) == 3# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_client.py
# Run specific test class
pytest tests/test_client.py::TestAddRecords
# Run specific test method
pytest tests/test_client.py::TestAddRecords::test_add_single_record# Run with coverage (terminal output)
pytest --cov=sqlite_vec_client
# Generate HTML coverage report
pytest --cov=sqlite_vec_client --cov-report=html
# Generate XML coverage report (for CI)
pytest --cov=sqlite_vec_client --cov-report=xml
# Show missing lines
pytest --cov=sqlite_vec_client --cov-report=term-missingView HTML coverage report:
# Open htmlcov/index.html in your browser# Run only unit tests
pytest -m unit
# Run only integration tests
pytest -m integration
# Run slow tests
pytest -m slow
# Exclude slow tests
pytest -m "not slow"- Test files:
test_*.py - Test classes:
Test* - Test functions:
test_*
import pytest
from sqlite_vec_client import SQLiteVecClient
@pytest.mark.integration
class TestMyFeature:
"""Tests for my feature."""
def test_basic_functionality(self, client_with_table):
"""Test basic functionality."""
# Arrange
texts = ["test"]
embeddings = [[1.0, 2.0, 3.0]]
# Act
rowids = client_with_table.add(texts=texts, embeddings=embeddings)
# Assert
assert len(rowids) == 1
assert client_with_table.count() == 1def test_invalid_input(self, client):
"""Test that invalid input raises error."""
with pytest.raises(ValidationError, match="positive integer"):
client.create_table(dim=0)- Target: 80%+ overall coverage
- Current: 91% coverage
- Critical paths: 100% coverage for validation and security functions
Tests are automatically run on:
- Every commit (via pre-commit hooks)
- Pull requests (via GitHub Actions)
- Before releases
Issue: Tests fail with "no such table" error
Solution: Ensure client_with_table fixture is used for tests requiring a table
Issue: Coverage report not generated
Solution: Install pytest-cov: pip install pytest-cov
Issue: Tests hang or timeout
Solution: Check for unclosed database connections, use fixtures properly
- Use fixtures for common setup/teardown
- Test one thing per test function
- Use descriptive names for test functions
- Add docstrings to explain what is being tested
- Test edge cases (empty lists, None values, invalid inputs)
- Clean up resources (fixtures handle this automatically)
- Mock external dependencies when appropriate
For performance-critical operations, use the @pytest.mark.slow marker:
@pytest.mark.slow
def test_large_batch_insert(client_with_table):
"""Test inserting 10,000 records."""
texts = [f"text_{i}" for i in range(10000)]
embeddings = [[0.1, 0.2, 0.3] for _ in range(10000)]
rowids = client_with_table.add(texts=texts, embeddings=embeddings)
assert len(rowids) == 10000Run without slow tests:
pytest -m "not slow"