Skip to content

Commit a1f00af

Browse files
authored
Merge pull request #14 from wey-gu/decouple_more
2 parents 07be216 + c87a49d commit a1f00af

34 files changed

+3359
-494
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
pip install -e ".[dev]"
3636
pip install types-psutil
3737
38-
- name: Run full development workflow
38+
- name: Run stable tests and examples
3939
run: |
4040
python scripts/dev.py
4141
env:
@@ -45,7 +45,13 @@ jobs:
4545
if: matrix.python-version == '3.11'
4646
run: |
4747
pip install coverage[toml] pytest-cov
48-
pytest tests/ examples/ --cov=py_pglite --cov-report=xml --cov-report=term-missing
48+
pytest tests/ examples/ --cov=py_pglite --cov-report=xml --cov-report=term-missing --ignore=examples/testing-patterns/test_performance_benchmarks.py -k "not test_custom_configuration_patterns"
49+
50+
- name: Run stress tests (optional)
51+
continue-on-error: true
52+
if: matrix.python-version == '3.11'
53+
run: |
54+
python scripts/dev.py --stress
4955
5056
- name: Upload coverage to Codecov
5157
if: matrix.python-version == '3.11'

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,8 @@ cython_debug/
191191
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
192192
# refer to https://docs.cursor.com/context/ignore-files
193193
.cursorignore
194-
.cursorindexingignore
194+
.cursorindexingignore
195+
196+
# tests
197+
198+
perf-tests/

CONTRIBUTING.md

Lines changed: 125 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -113,59 +113,115 @@ make fmt # Auto-fix formatting
113113

114114
## 📁 **Project Structure**
115115

116-
```
116+
```bash
117117
py-pglite/
118-
├── py_pglite/ # 📦 Core package
119-
│ ├── __init__.py # Public API
120-
│ ├── manager.py # PGlite management
121-
│ ├── config.py # Configuration
122-
│ ├── sqlalchemy/ # SQLAlchemy integration
123-
│ ├── django/ # Django integration
124-
│ └── pytest_plugin.py # Pytest plugin
118+
├── py_pglite/ # 📦 Core package
119+
│ ├── __init__.py # Public API
120+
│ ├── manager.py # Framework-agnostic PGlite management
121+
│ ├── config.py # Robust configuration system
122+
│ ├── utils.py # Framework-agnostic utilities
123+
│ ├── sqlalchemy/ # SQLAlchemy integration
124+
│ │ ├── manager.py # Enhanced SQLAlchemy manager
125+
│ │ ├── fixtures.py # Pytest fixtures
126+
│ │ └── utils.py # SQLAlchemy utilities
127+
│ ├── django/ # Django integration
128+
│ │ ├── backend.py # Custom database backend
129+
│ │ ├── fixtures.py # Django fixtures
130+
│ │ └── utils.py # Django utilities
131+
│ └── pytest_plugin.py # Auto-discovery pytest plugin
125132
126-
├── tests/ # 🧪 Core tests
127-
│ ├── test_core_manager.py # Manager tests
128-
│ ├── test_advanced.py # Advanced features
129-
│ └── test_framework_isolation.py # Framework isolation
133+
├── tests/ # 🧪 Core tests (88 tests)
134+
│ ├── test_core_manager.py # Manager lifecycle & process management
135+
│ ├── test_advanced.py # Advanced usage patterns
136+
│ ├── test_configuration.py # 🆕 Configuration validation & edge cases
137+
│ ├── test_connection_management.py # 🆕 Connection pooling & lifecycle
138+
│ ├── test_reliability.py # 🆕 Error recovery & resilience
139+
│ ├── test_django_backend.py # 🆕 Django backend & decoupling
140+
│ ├── test_fastapi_integration.py # FastAPI patterns
141+
│ └── test_framework_isolation.py # Framework isolation validation
130142
131-
├── examples/ # 📚 Examples & demos
132-
│ ├── quickstart/ # ⚡ Instant demos
133-
│ └── testing-patterns/ # 🧪 Production examples
143+
├── examples/ # 📚 Examples & demos (51 tests)
144+
│ ├── quickstart/ # ⚡ Instant demos
145+
│ │ ├── demo_instant.py # 5-line PostgreSQL demo
146+
│ │ ├── simple_fastapi.py # FastAPI integration
147+
│ │ └── simple_performance.py # Performance comparison
148+
│ └── testing-patterns/ # 🧪 Production examples
149+
│ ├── sqlalchemy/ # SQLAlchemy patterns (2 tests)
150+
│ ├── django/ # Django patterns (10 tests)
151+
│ └── test_fixtures_showcase.py # Advanced patterns (8 tests)
134152
135-
├── scripts/ # 🔧 Development tools
136-
│ └── dev.py # Unified development script
153+
├── scripts/ # 🔧 Development tools
154+
│ └── dev.py # Unified development script
137155
138-
└── Makefile # 🎯 Convenience commands
156+
└── Makefile # 🎯 Convenience commands
139157
```
140158

141159
---
142160

143161
## 🧪 **Testing Strategy**
144162

145-
### **Core Tests** (`tests/`)
163+
### **Comprehensive Test Coverage (139 Total Tests)**
146164

147-
- **Manager lifecycle** - Start/stop, configuration
148-
- **Framework isolation** - SQLAlchemy/Django separation
149-
- **Advanced features** - Complex scenarios
150-
- **FastAPI integration** - REST API patterns
165+
**Core Tests** (`tests/` - 88 tests)
151166

152-
### **Example Tests** (`examples/`)
167+
- **🏗️ Manager lifecycle** (`test_core_manager.py`) - Process management, configuration
168+
- **⚙️ Configuration validation** (`test_configuration.py`) - Edge cases, validation, performance
169+
- **🔗 Connection management** (`test_connection_management.py`) - Pooling, lifecycle, concurrency
170+
- **🛡️ Reliability & recovery** (`test_reliability.py`) - Error handling, process recovery, edge cases
171+
- **🌟 Django backend** (`test_django_backend.py`) - Django integration, decoupling validation
172+
- **🚀 FastAPI integration** (`test_fastapi_integration.py`) - REST API patterns
173+
- **🔀 Framework isolation** (`test_framework_isolation.py`) - SQLAlchemy/Django separation
174+
- **💎 Advanced features** (`test_advanced.py`) - Complex scenarios, manual management
153175

154-
- **SQLAlchemy patterns** - Real ORM usage
155-
- **Django patterns** - Real Django models
156-
- **Quickstart demos** - User experience validation
176+
**Example Tests** (`examples/` - 51 tests)
157177

158-
### **Framework Isolation**
178+
- **🎯 SQLAlchemy patterns** (2 tests) - Real ORM usage, modern SQLAlchemy 2.0
179+
- **⭐ Django patterns** (10 tests) - Django ORM, pytest-django, advanced features
180+
- **🎪 Advanced patterns** (8 tests) - Performance, PostgreSQL features, transactions
181+
- **⚡ Quickstart validation** (31 tests) - User experience, FastAPI, utilities
182+
183+
### **Quality Assurance Features**
159184

160185
```bash
161-
# Test SQLAlchemy alone
186+
# Framework isolation validation
187+
pytest -m sqlalchemy -p no:django # Pure SQLAlchemy (no Django bleeding)
188+
pytest -m django -p no:sqlalchemy # Pure Django (no SQLAlchemy bleeding)
189+
190+
# Comprehensive coverage areas
191+
pytest tests/test_configuration.py # Config validation & edge cases
192+
pytest tests/test_reliability.py # Error recovery & resilience
193+
pytest tests/test_connection_management.py # Connection pooling & cleanup
194+
195+
# Real-world scenario validation
196+
pytest examples/testing-patterns/ # Production usage patterns
197+
```
198+
199+
### **Battle-Tested Scenarios**
200+
201+
Our test suite validates these critical scenarios:
202+
203+
-**Process recovery** - Manager restart, cleanup, resource management
204+
-**Connection storms** - Concurrent access, pool exhaustion, timeout handling
205+
-**Memory stability** - Long-running suites, large datasets, cleanup validation
206+
-**Unicode data** - International character sets, special characters
207+
-**Framework decoupling** - Zero bleeding between SQLAlchemy/Django components
208+
-**Configuration robustness** - Edge cases, validation, invalid inputs
209+
-**Production patterns** - FastAPI + SQLAlchemy, Django models, complex queries
210+
211+
### **Framework Isolation Testing**
212+
213+
```bash
214+
# Test SQLAlchemy isolation
162215
pytest examples/testing-patterns/sqlalchemy/ -p no:django
163216

164-
# Test Django alone
165-
pytest examples/testing-patterns/django/
217+
# Test Django isolation
218+
pytest examples/testing-patterns/django/ -p no:sqlalchemy
166219

167220
# Test framework coexistence
168221
pytest tests/test_framework_isolation.py
222+
223+
# Test decoupling fix
224+
pytest tests/test_django_backend.py::TestDjangoBackendDecoupling
169225
```
170226

171227
---
@@ -322,6 +378,24 @@ def test_users(pglite_session):
322378

323379
## 🔧 **Known Issues & Solutions**
324380

381+
### **Django Backend Decoupling (Fixed in v0.3.0+)**
382+
383+
**Issue:** Django backend was calling `manager.wait_for_ready()` but the base `PGliteManager` only had `wait_for_ready_basic()`, causing framework coupling.
384+
385+
**Cause:** Django integration was inadvertently depending on SQLAlchemy-specific methods, breaking the framework-agnostic design.
386+
387+
**Solution:** Added `wait_for_ready()` method to base `PGliteManager` that delegates to `wait_for_ready_basic()` for API consistency.
388+
389+
```python
390+
# Now works perfectly across all frameworks
391+
def test_django_backend_ready(db):
392+
# Django backend uses base manager with consistent API
393+
manager = get_pglite_manager()
394+
manager.wait_for_ready() # ✅ Works in both SQLAlchemy and Django
395+
```
396+
397+
**Validation:** Comprehensive Django backend tests added (`test_django_backend.py`) with 9 tests covering decoupling, imports, and error handling.
398+
325399
### **Connection Timeouts (Fixed in v0.2.0+)**
326400

327401
**Issue:** `psycopg.errors.ConnectionTimeout` when creating tables or running DDL operations.
@@ -336,6 +410,26 @@ engine = manager.get_engine()
336410
SQLModel.metadata.create_all(engine) # ✅ Works
337411
```
338412

413+
**Additional Improvements:**
414+
415+
- **Connection pooling** - StaticPool and NullPool support with proper configuration
416+
- **Timeout handling** - Configurable timeouts with robust retry logic
417+
- **Process recovery** - Automatic cleanup and restart on failures
418+
- **Resource management** - Comprehensive socket and memory cleanup
419+
420+
### **Framework Isolation (Enhanced in v0.3.0+)**
421+
422+
**Validation:** py-pglite now has comprehensive framework isolation testing:
423+
424+
```bash
425+
# These work perfectly without interference
426+
pytest -m sqlalchemy -p no:django # Pure SQLAlchemy
427+
pytest -m django -p no:sqlalchemy # Pure Django
428+
pytest tests/test_framework_isolation.py # Validation suite
429+
```
430+
431+
**Coverage:** 139 total tests including edge cases, error recovery, and production scenarios.
432+
339433
---
340434

341435
## 🎉 **Release Process**

0 commit comments

Comments
 (0)