Skip to content

Commit c891fc5

Browse files
Enable local testing for all cachier backends with Docker (#287)
* Add a script for running all cores locallly * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * shorten some line * safe import of optional dependencies in mongodb core tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * safer import of opt deps in mongo tests * fix test dependencies * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more stupid docstring * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * move test_missing_mongetter and mark it as a mongodb core test * fixes and readme updates * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * shorter comment * no need for this --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ca7f1ad commit c891fc5

16 files changed

Lines changed: 1012 additions & 313 deletions

.github/workflows/ci-test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ jobs:
9090
# show running containers
9191
docker ps -a
9292
93+
- name: Install MongoDB core test dependencies
94+
if: matrix.backend == 'mongodb'
95+
run: |
96+
python -m pip install -e . -r tests/mongodb_requirements.txt
97+
9398
- name: Unit tests (DB)
9499
if: matrix.backend == 'mongodb'
95100
run: pytest -m "mongo" --cov=cachier --cov-report=term --cov-report=xml:cov.xml

CLAUDE.md

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ ______________________________________________________________________
161161
pip install -e .
162162
pip install -r tests/requirements.txt
163163
# For specific backends:
164-
pip install -r tests/sql_requirements.txt
164+
pip install -r tests/mongodb_requirements.txt
165165
pip install -r tests/redis_requirements.txt
166+
pip install -r tests/sql_requirements.txt
166167
```
167168
- **Run all tests:** `pytest`
168169
- **Run backend-specific tests:** `pytest -m <backend>` (mongo, redis, sql, memory, pickle, maxage)
@@ -177,41 +178,54 @@ ______________________________________________________________________
177178
- **Run example:** `python examples/redis_example.py`
178179
- **Update requirements:** Edit `tests/*_requirements.txt` as needed (sql_requirements.txt, redis_requirements.txt).
179180

180-
### MongoDB Local Testing
181+
### Local Testing with Docker
181182

182-
**Quick Start for MongoDB Testing:**
183+
**Quick Start - Test Any Backend Locally:**
183184

184185
```bash
185-
# Option 1: Using the shell script (recommended)
186-
./scripts/test-mongo-local.sh # MongoDB tests only
187-
./scripts/test-mongo-local.sh --mode also-local # MongoDB + memory, pickle, maxage tests
188-
189-
# Option 2: Using make
190-
make test-mongo-local
191-
192-
# Option 3: Using docker-compose
193-
docker-compose -f scripts/docker-compose.mongodb.yml up -d
194-
CACHIER_TEST_HOST=localhost CACHIER_TEST_PORT=27017 CACHIER_TEST_VS_DOCKERIZED_MONGO=true pytest -m mongo
195-
docker-compose -f scripts/docker-compose.mongodb.yml down
186+
# Test single backend
187+
./scripts/test-local.sh mongo
188+
./scripts/test-local.sh redis
189+
./scripts/test-local.sh sql
190+
191+
# Test multiple backends
192+
./scripts/test-local.sh mongo redis
193+
./scripts/test-local.sh external # All external (mongo, redis, sql)
194+
./scripts/test-local.sh all # All backends
195+
196+
# Test with options
197+
./scripts/test-local.sh mongo redis -v -k # Verbose, keep containers running
196198
```
197199

198-
**Available Options:**
199-
200-
- `./scripts/test-mongo-local.sh` - Run MongoDB tests only (default)
201-
- `./scripts/test-mongo-local.sh --mode also-local` - Include memory, pickle, and maxage tests
202-
- `./scripts/test-mongo-local.sh --keep-running` - Keep MongoDB running after tests
203-
- `./scripts/test-mongo-local.sh --verbose` - Show verbose output
204-
- `./scripts/test-mongo-local.sh --coverage-html` - Generate HTML coverage report
205-
206200
**Make Targets:**
207201

208-
- `make test-mongo-local` - Run MongoDB tests with Docker
209-
- `make test-mongo-inmemory` - Run with in-memory MongoDB (default)
210-
- `make mongo-start` - Start MongoDB container
211-
- `make mongo-stop` - Stop MongoDB container
212-
- `make mongo-logs` - View MongoDB logs
213-
214-
**Note:** By default, MongoDB tests use `pymongo_inmemory` which doesn't require Docker. The above commands let you test against a real MongoDB instance matching the CI environment.
202+
- `make test-local CORES="mongo redis"` - Test specified cores
203+
- `make test-all-local` - Test all backends with Docker
204+
- `make test-external` - Test all external backends
205+
- `make test-mongo-local` - Test MongoDB only
206+
- `make test-redis-local` - Test Redis only
207+
- `make test-sql-local` - Test SQL only
208+
- `make services-start` - Start all Docker containers
209+
- `make services-stop` - Stop all Docker containers
210+
211+
**Available Cores:**
212+
213+
- `mongo` - MongoDB backend
214+
- `redis` - Redis backend
215+
- `sql` - PostgreSQL backend
216+
- `memory` - Memory backend (no Docker)
217+
- `pickle` - Pickle backend (no Docker)
218+
- `all` - All backends
219+
- `external` - All external backends (mongo, redis, sql)
220+
- `local` - All local backends (memory, pickle)
221+
222+
**Options:**
223+
224+
- `-v, --verbose` - Verbose pytest output
225+
- `-k, --keep-running` - Keep containers running after tests
226+
- `-h, --html-coverage` - Generate HTML coverage report
227+
228+
**Note:** External backends (MongoDB, Redis, SQL) require Docker. Memory and pickle backends work without Docker.
215229

216230
______________________________________________________________________
217231

Makefile

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Cachier Makefile
22
# Development tasks and shortcuts
33

4-
.PHONY: help test test-all test-mongo-local test-mongo-docker test-mongo-inmemory \
5-
test-mongo-also-local mongo-start mongo-stop mongo-logs lint type-check format clean \
4+
.PHONY: help test test-all test-local test-all-local test-external \
5+
test-mongo-local test-mongo-docker test-mongo-inmemory test-mongo-also-local \
6+
test-redis-local test-sql-local \
7+
services-start services-stop services-logs \
8+
mongo-start mongo-stop mongo-logs lint type-check format clean \
69
install install-dev install-all
710

811
# Default target
@@ -11,9 +14,16 @@ help:
1114
@echo ""
1215
@echo "Testing:"
1316
@echo " make test - Run all tests"
17+
@echo " make test-local CORES='...' - Run tests for specified cores with Docker"
18+
@echo " make test-all-local - Run all backend tests with Docker"
19+
@echo " make test-external - Run all external backend tests (mongo, redis, sql)"
1420
@echo " make test-mongo-local - Run MongoDB tests with Docker"
21+
@echo " make test-redis-local - Run Redis tests with Docker"
22+
@echo " make test-sql-local - Run SQL tests with Docker"
1523
@echo " make test-mongo-also-local - Run MongoDB + memory, pickle, maxage tests with Docker"
1624
@echo " make test-mongo-inmemory - Run MongoDB tests with in-memory backend"
25+
@echo " make services-start - Start all test containers"
26+
@echo " make services-stop - Stop all test containers"
1727
@echo " make mongo-start - Start MongoDB container"
1828
@echo " make mongo-stop - Stop MongoDB container"
1929
@echo " make mongo-logs - View MongoDB container logs"
@@ -42,8 +52,9 @@ install-dev:
4252
install-all:
4353
pip install -e .[all]
4454
pip install -r tests/requirements.txt
45-
pip install -r tests/sql_requirements.txt
55+
pip install -r tests/mongodb_requirements.txt
4656
pip install -r tests/redis_requirements.txt
57+
pip install -r tests/sql_requirements.txt
4758

4859
# Testing targets
4960
test:
@@ -66,6 +77,39 @@ test-mongo-also-local:
6677
@echo "Running MongoDB tests with local core tests..."
6778
./scripts/test-mongo-local.sh --mode also-local
6879

80+
# New unified testing targets
81+
test-local:
82+
@echo "Running tests for cores: $(CORES)"
83+
./scripts/test-local.sh $(CORES)
84+
85+
test-all-local:
86+
@echo "Running all backend tests with Docker..."
87+
./scripts/test-local.sh all
88+
89+
test-external:
90+
@echo "Running all external backend tests..."
91+
./scripts/test-local.sh external
92+
93+
test-redis-local:
94+
@echo "Running Redis tests with Docker..."
95+
./scripts/test-local.sh redis
96+
97+
test-sql-local:
98+
@echo "Running SQL tests with Docker..."
99+
./scripts/test-local.sh sql
100+
101+
# Service management
102+
services-start:
103+
@echo "Starting all test services..."
104+
docker-compose -f scripts/docker-compose.all-cores.yml up -d
105+
106+
services-stop:
107+
@echo "Stopping all test services..."
108+
docker-compose -f scripts/docker-compose.all-cores.yml down
109+
110+
services-logs:
111+
docker-compose -f scripts/docker-compose.all-cores.yml logs -f
112+
69113
# MongoDB container management
70114
mongo-start:
71115
@echo "Starting MongoDB container..."

README.rst

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -541,22 +541,25 @@ Running MongoDB tests against a live MongoDB instance
541541

542542
.. code-block:: bash
543543
544-
./scripts/test-mongo-local.sh # MongoDB tests only (default)
545-
./scripts/test-mongo-local.sh --mode also-local # MongoDB + memory, pickle, maxage tests
544+
# Test MongoDB only
545+
./scripts/test-local.sh mongo
546+
547+
# Test MongoDB with local backends
548+
./scripts/test-local.sh mongo memory pickle
546549
547550
This script automatically handles Docker container lifecycle, environment variables, and cleanup. Additional options:
548551

549-
- ``--mode also-local``: Include memory, pickle, and maxage tests alongside MongoDB tests
550-
- ``--keep-running``: Keep MongoDB container running after tests
551-
- ``--verbose``: Show verbose output
552-
- ``--coverage-html``: Generate HTML coverage report
552+
- ``-v, --verbose``: Show verbose output
553+
- ``-k, --keep-running``: Keep containers running after tests
554+
- ``-h, --html-coverage``: Generate HTML coverage report
553555

554556
**Option 2: Using Make**
555557

556558
.. code-block:: bash
557559
558-
make test-mongo-local # Run tests with Docker MongoDB
559-
make test-mongo-inmemory # Run tests with in-memory MongoDB (default)
560+
make test-mongo-local # Run MongoDB tests with Docker
561+
make test-all-local # Run all backends with Docker
562+
make test-mongo-inmemory # Run with in-memory MongoDB (default)
560563
561564
**Option 3: Manual setup**
562565

@@ -578,6 +581,28 @@ Contributors are encouraged to test against a real MongoDB instance before submi
578581
**HOWEVER, the tests run against a live MongoDB instance when you submit a PR are the determining tests for deciding whether your code functions correctly against MongoDB.**
579582

580583

584+
Testing all backends locally
585+
-----------------------------
586+
587+
To test all cachier backends (MongoDB, Redis, SQL, Memory, Pickle) locally with Docker:
588+
589+
.. code-block:: bash
590+
591+
# Test all backends at once
592+
./scripts/test-local.sh all
593+
594+
# Test only external backends (MongoDB, Redis, SQL)
595+
./scripts/test-local.sh external
596+
597+
# Test specific combinations
598+
./scripts/test-local.sh mongo redis
599+
600+
# Keep containers running for debugging
601+
./scripts/test-local.sh all -k
602+
603+
The unified test script automatically manages Docker containers, installs required dependencies, and runs the appropriate test suites. See ``scripts/README-local-testing.md`` for detailed documentation.
604+
605+
581606
Adding documentation
582607
--------------------
583608

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ line-length = 79
7575
# --- ruff ---
7676

7777
[tool.ruff]
78-
target-version = "py38"
78+
target-version = "py39"
7979
line-length = 79
8080
# Exclude a variety of commonly ignored directories.
8181
exclude = [

0 commit comments

Comments
 (0)