Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
14 changes: 14 additions & 0 deletions backend/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[flake8]
max-line-length = 120
exclude =
.git,
__pycache__,
venv,
env,
.venv,
migrations,
.pytest_cache
ignore =
E203, # whitespace before ':'
W503, # line break before binary operator
F401 # imported but unused (common in __init__.py files)
44 changes: 44 additions & 0 deletions backend/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.PHONY: test test-unit test-integration test-coverage lint format install-dev

# Install development dependencies
install-dev:
pip install -r requirements-dev.txt

# Run all tests
test:
pytest

# Run only unit tests
test-unit:
pytest -m "not integration and not slow"

# Run integration tests
test-integration:
pytest -m integration

# Run tests with coverage report
test-coverage:
pytest --cov=src --cov-report=html --cov-report=term

# Lint code
lint:
flake8 src tests
black --check src tests
isort --check-only src tests

# Format code
format:
black src tests
isort src tests

# Run tests in watch mode (requires pytest-xdist)
test-watch:
pytest --looponfail

# Clean up test artifacts
clean:
rm -rf .pytest_cache
rm -rf htmlcov
rm -rf .coverage
find . -type d -name __pycache__ -exec rm -rf {} +
find . -type f -name "*.pyc" -delete
96 changes: 96 additions & 0 deletions backend/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Testing Guide for Voice Marketing Agents

## Quick Start

1. **Setup (one-time):**
```bash
cd backend
python setup_tests.py
```

2. **Run tests:**
```bash
pytest
```

## Available Commands

- `make test` - Run all tests
- `make test-unit` - Run unit tests only
- `make test-coverage` - Run tests with coverage report
- `make lint` - Check code quality
- `make format` - Format code

## Test Structure

- `tests/test_models/` - Database model tests
- `tests/test_services/` - AI service tests
- `tests/test_api/` - API endpoint tests
- `tests/test_agents/` - Agent logic tests

## Writing New Tests

1. Add test files with `test_` prefix
2. Use provided fixtures from `conftest.py`
3. Mock external services (LLM, STT, TTS)
4. Follow existing test patterns

## Coverage Requirements

- Minimum 80% code coverage
- All new features must include tests
- Critical paths must be tested

## Troubleshooting

If tests fail:
1. Check you're in the `backend/` directory
2. Ensure dependencies are installed: `pip install -r requirements-dev.txt`
3. Check that main application runs: `python -m src.main`
#!/bin/bash

# Test runner script for CI/CD

set -e

echo "🧪 Starting Voice Marketing Agents Test Suite"
echo "=============================================="

# Check if we're in a virtual environment
if [[ -z "${VIRTUAL_ENV}" ]]; then
echo "⚠️ Warning: Not running in a virtual environment"
fi

# Install test dependencies
echo "📦 Installing test dependencies..."
pip install -r requirements-dev.txt

# Run code formatting checks
echo "🎨 Checking code formatting..."
black --check src tests || {
echo "❌ Code formatting issues found. Run 'make format' to fix."
exit 1
}

# Run import sorting checks
echo "📚 Checking import sorting..."
isort --check-only src tests || {
echo "❌ Import sorting issues found. Run 'make format' to fix."
exit 1
}

# Run linting
echo "🔍 Running linting..."
flake8 src tests || {
echo "❌ Linting issues found."
exit 1
}

# Run unit tests
echo "⚡ Running unit tests..."
pytest tests/ -v --tb=short --cov=src --cov-report=term-missing --cov-fail-under=80

echo ""
echo "✅ All tests passed!"
echo "📊 Coverage report generated in htmlcov/"
echo "🎉 Voice Marketing Agents is ready for deployment!"
68 changes: 68 additions & 0 deletions backend/project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[tool.black]
line-length = 120
target-version = ['py311']
include = '\.pyi?
#!/bin/bash

# Test runner script for CI/CD

set -e

echo "🧪 Starting Voice Marketing Agents Test Suite"
echo "=============================================="

# Check if we're in a virtual environment
if [[ -z "${VIRTUAL_ENV}" ]]; then
echo "⚠️ Warning: Not running in a virtual environment"
fi

# Install test dependencies
echo "📦 Installing test dependencies..."
pip install -r requirements-dev.txt

# Run code formatting checks
echo "🎨 Checking code formatting..."
black --check src tests || {
echo "❌ Code formatting issues found. Run 'make format' to fix."
exit 1
}

# Run import sorting checks
echo "📚 Checking import sorting..."
isort --check-only src tests || {
echo "❌ Import sorting issues found. Run 'make format' to fix."
exit 1
}

# Run linting
echo "🔍 Running linting..."
flake8 src tests || {
echo "❌ Linting issues found."
exit 1
}

# Run unit tests
echo "⚡ Running unit tests..."
pytest tests/ -v --tb=short --cov=src --cov-report=term-missing --cov-fail-under=80

echo ""
echo "✅ All tests passed!"
echo "📊 Coverage report generated in htmlcov/"
echo "🎉 Voice Marketing Agents is ready for deployment!"

extend-exclude = '''
/(
migrations
| \.pytest_cache
)/
'''

[tool.isort]
profile = "black"
line_length = 120
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true
skip_glob = ["migrations/*"]
Empty file added backend/pytest.ini
Empty file.
18 changes: 18 additions & 0 deletions backend/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Testing frameworks
pytest>=7.4.0
pytest-asyncio>=0.21.0
pytest-mock>=3.11.0
httpx>=0.24.0 # For testing FastAPI async endpoints
factory-boy>=3.3.0 # For creating test data
faker>=19.0.0 # For generating fake data

# Test coverage
pytest-cov>=4.1.0

# Code quality
black>=23.0.0
isort>=5.12.0
flake8>=6.0.0

# Database testing
pytest-postgresql>=5.0.0 # For isolated test database
86 changes: 86 additions & 0 deletions backend/setup_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""
Setup script to ensure the testing environment is ready.
Run this before running tests for the first time.
"""
import os
import sys
import subprocess

def install_test_dependencies():
"""Install test dependencies."""
print("📦 Installing test dependencies...")
try:
subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements-dev.txt"], check=True)
print("✅ Test dependencies installed successfully!")
except subprocess.CalledProcessError:
print("❌ Failed to install test dependencies")
return False
return True

def create_test_directories():
"""Create necessary test directories."""
print("📁 Creating test directories...")
directories = [
"tests",
"tests/test_models",
"tests/test_services",
"tests/test_api",
"tests/test_agents",
"tests/utils"
]

for dir_path in directories:
os.makedirs(dir_path, exist_ok=True)
# Create __init__.py files for proper Python packages
init_file = os.path.join(dir_path, "__init__.py")
if not os.path.exists(init_file):
with open(init_file, "w") as f:
f.write("# Test package\n")

print("✅ Test directories created!")

def check_environment():
"""Check if the environment is ready for testing."""
print("🔍 Checking environment...")

# Check if we're in the backend directory
if not os.path.exists("src"):
print("❌ Please run this script from the backend directory")
return False

# Check if main application files exist
required_files = [
"src/main.py",
"src/core/database.py",
"src/models/agent.py"
]

for file_path in required_files:
if not os.path.exists(file_path):
print(f"❌ Required file missing: {file_path}")
return False

print("✅ Environment looks good!")
return True

def main():
"""Main setup function."""
print("🧪 Setting up Voice Marketing Agents Test Environment")
print("=" * 50)

if not check_environment():
sys.exit(1)

create_test_directories()

if not install_test_dependencies():
sys.exit(1)

print("\n🎉 Test environment setup complete!")
print("\nNext steps:")
print("1. Run tests: pytest")
print("2. Run with coverage: pytest --cov=src")
print("3. Use Makefile commands: make test, make test-coverage")

if __name__ == "__main__":
main()
Loading