๐ง Development Guide
Welcome to the development guide! Here's everything you need to know about contributing to and developing the Basic gRPC Service.
๐ ๏ธ Development Setup
Prerequisites
- Python 3.10+
- buf CLI (optional, for protobuf management)
- mkcert for local TLS certificates
- grpcurl for testing
Environment Setup
# Clone the repository
git clone <your-repo-url>
cd basic-grpc-service-python
# Set up Python path for development
export PYTHONPATH="$PYTHONPATH:$(pwd):$(pwd)/sdk:$(pwd)/services:$(pwd)/utils"
# Install in development mode with all dependencies
python -m pip install -e ".[dev,docs]"
# Generate TLS certificates
mkcert -cert-file ./certs/local.crt -key-file ./certs/local.key localhost 127.0.0.1
๐ฆ Project Structure
basic-grpc-service-python/
โโโ ๐ proto/ # Protocol Buffer definitions
โ โโโ basic/v1/basic.proto # Main service definitions
โ โโโ basic/service/v1/ # Message types and events
โโโ ๐ sdk/ # Generated Python code
โ โโโ basic/ # Auto-generated from protos
โ โโโ cloudevents/ # CloudEvents protobuf
โโโ ๐ services/ # Service implementations
โ โโโ __init__.py # Package initialization
โ โโโ basic_service.py # Main service logic
โโโ ๐ utils/ # Utility modules
โ โโโ __init__.py # Package exports
โ โโโ eliza.py # Eliza chatbot implementation
โ โโโ some.py # Helper utilities
โโโ ๐ tests/ # ๐งช Comprehensive test suite
โ โโโ conftest.py # pytest fixtures and configuration
โ โโโ test_eliza.py # Eliza chatbot tests (40+ cases)
โ โโโ test_basic_service.py # gRPC service tests (40+ cases)
โ โโโ htmlcov/ # Coverage reports (generated)
โโโ ๐ docs/ # MkDocs documentation
โโโ ๐ scripts/ # Development automation
โ โโโ run_tests.sh # ๐งช Enhanced test runner
โ โโโ setup_docs.sh # Documentation setup
โ โโโ gen_ref_pages.py # API reference generation
โโโ ๐ certs/ # TLS certificates
โโโ ๐ server.py # Main server entry point
โโโ ๐ pyproject.toml # Python project configuration
โโโ ๐ก๏ธ buf.yaml # Buf configuration
โโโ โ๏ธ buf.gen.yaml # Code generation config
๐งช Testing Workflow
Quick Testing
# Run all tests with our enhanced runner
./scripts/run_tests.sh
# Run with coverage reporting
./scripts/run_tests.sh --coverage
# Fast runs during development (skip dependency install)
./scripts/run_tests.sh --fast
Manual Testing Commands
# Run specific test files
python -m pytest tests/test_eliza.py -v
python -m pytest tests/test_basic_service.py -v --asyncio-mode=auto
# Run specific test classes or methods
python -m pytest tests/test_basic_service.py::TestHelloMethod -v
python -m pytest tests/test_eliza.py::TestGoodbyeDetection::test_goodbye_simple -v
# Debug failing tests with maximum verbosity
python -m pytest tests/test_basic_service.py::TestBackgroundMethod -vvv -s
Test Coverage Requirements
- Services (
services/) โ 95%+ coverage required - Utils (
utils/) โ 90%+ coverage required - Overall project โ 90%+ coverage required
# Check coverage
./scripts/run_tests.sh --coverage
# View detailed HTML coverage report
python -m http.server 8000 -d htmlcov/
๐ Code Generation
This project uses Buf for protobuf management and code generation.
Regenerating Python Code
When you modify .proto files, regenerate the Python code:
# Using buf (recommended)
buf generate
# Or using protoc directly (if you don't have buf)
protoc --python_out=sdk --pyi_out=sdk --grpc_python_out=sdk proto/basic/v1/*.proto
Generated Files
The code generation creates:
- Regular Python modules (
*_pb2.py) - gRPC service stubs (
*_pb2_grpc.py) - Type stubs (
*.pyi) for better IDE support - Package
__init__.pyfiles automatically
๐งช Manual Service Testing
Start the Development Server
Testing with grpcurl
# Test the Hello method
grpcurl -insecure -d '{"message": "test"}' 127.0.0.1:8443 basic.v1.BasicService/Hello
# Test bidirectional streaming (Talk)
echo '{"message": "Hello Eliza"}' | \
grpcurl -insecure -d @ 127.0.0.1:8443 basic.v1.BasicService/Talk
# Test server streaming (Background)
grpcurl -insecure -d '{"processes": 3}' \
127.0.0.1:8443 basic.v1.BasicService/Background
Health Checks
# Server health
grpcurl -insecure 127.0.0.1:8443 grpc.health.v1.Health/Check
# Service-specific health
grpcurl -insecure -d '{"service": "basic.v1.BasicService"}' \
127.0.0.1:8443 grpc.health.v1.Health/Check
๐ Documentation Development
This project uses MkDocs with the Material theme for documentation.
Building Documentation
# Install documentation dependencies (already included in [docs])
pip install -e ".[docs]"
# Serve documentation locally with hot reload
mkdocs serve
# Build static documentation
mkdocs build
# Deploy to GitHub Pages
mkdocs gh-deploy
Documentation Structure
docs/- Markdown documentation filesdocs/testing.md- ๐งช Comprehensive testing guidescripts/gen_ref_pages.py- Auto-generates API reference from docstringsmkdocs.yml- MkDocs configuration
Writing Documentation
- Use emoji for visual appeal ๐จ
- Include code examples with proper syntax highlighting
- Add cross-references between sections
- Keep it conversational but professional
- Test all code examples in documentation
๐จ Code Style
Docstring Style
We use a fun, engaging docstring style with emojis and personality:
def example_function(param: str) -> str:
"""
๐ This function does something awesome!
A detailed description of what this function does, why it exists,
and how it fits into the bigger picture. Include personality and
make it fun to read!
Args:
param (str): Description of the parameter with examples
Returns:
str: What gets returned and when
Raises:
ValueError: When something goes wrong and why
Example:
>>> result = example_function("test")
>>> print(result)
"processed: test"
Note:
Any important notes, warnings, or tips for users!
"""
Type Hints
- Use type hints everywhere
- Import types from
typingwhen needed - Use generic types for containers (
List[str],Dict[str, Any])
Testing Standards
- Descriptive test names:
test_hello_with_empty_message_returns_valid_response - Test behavior, not implementation: Focus on what the code should do
- Include edge cases: Empty inputs, special characters, error conditions
- Use proper async patterns:
@pytest.mark.asynciofor async tests - Mock external dependencies: Use
AsyncMockfor gRPC contexts
๐ Debugging
Logging
The server uses structured JSON logging:
Common Issues
-
Import Errors: Make sure
PYTHONPATHincludes all necessary directories -
Certificate Issues: Regenerate with
mkcert -
Port Conflicts: Check if port 8443 is already in use
-
Test Failures: Use our enhanced test runner for debugging
Debugging Tests
# Run specific failing test with maximum detail
python -m pytest tests/test_basic_service.py::TestBackgroundMethod::test_background_timestamps -vvv -s
# Use Python debugger
python -m pytest tests/test_eliza.py --pdb
# Check test discovery
python -m pytest --collect-only
๐ Deployment
Pre-deployment Checklist
# 1. All tests must pass
./scripts/run_tests.sh --coverage
# 2. Coverage should be above 90%
# (Check the coverage report output)
# 3. Documentation should build successfully
mkdocs build
# 4. Manual testing should work
python server.py
# Test with grpcurl...
# 5. No linting errors
# (Add your preferred linter here)
Building for Production
GitHub Pages Deployment
๐ค Contributing
Development Workflow
- Fork the repository
- Create a feature branch
- Set up development environment
- Make your changes with proper documentation
- Write tests for new functionality
- Run test suite and ensure all pass
- Update documentation if needed
- Submit a pull request
Pull Request Guidelines
- Include clear descriptions of changes
- Add comprehensive tests for new features
- Maintain 90%+ test coverage
- Update documentation for new features
- Maintain the fun, engaging tone in docstrings
- Test with both pytest and MkDocs
- All tests must pass:
./scripts/run_tests.sh --coverage
Code Review Checklist
For Reviewers:
- โ All tests pass
- โ Coverage remains above 90%
- โ Documentation updated
- โ Code follows project style
- โ No breaking changes (or properly documented)
For Contributors:
- โ Self-review your changes
- โ Run full test suite locally
- โ Update relevant documentation
- โ Test examples in documentation work
- โ Check that new features are properly tested
๐ฏ Development Tips
Efficient Development Loop
# 1. Make code changes
# 2. Run tests quickly
./scripts/run_tests.sh --fast
# 3. For major changes, run full test suite
./scripts/run_tests.sh --coverage
# 4. Test manually if needed
python server.py
grpcurl -insecure -d '{"message": "test"}' 127.0.0.1:8443 basic.v1.BasicService/Hello
IDE Setup Tips
- Set PYTHONPATH in your IDE to include
sdk,services,utils - Enable type checking for better development experience
- Configure pytest as your test runner with
--asyncio-mode=auto
Ready to contribute? The codebase is designed to be approachable and fun to work with! ๐
For detailed testing information, check out our comprehensive Testing Guide!