-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add testing with vcr.py and a compose file
- Loading branch information
Showing
10 changed files
with
2,163 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,30 @@ | ||
name: CI | ||
on: [workflow_dispatch, pull_request, push] | ||
name: Tests | ||
|
||
on: | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
test: | ||
if: false | ||
pytest: | ||
runs-on: ubuntu-latest | ||
steps: [uses: fastai/workflows/nbdev-ci@master] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
- name: Install uv | ||
run: pip install uv | ||
- name: Create venv | ||
run: uv venv | ||
- name: Install dependencies | ||
run: | | ||
uv sync | ||
- name: Start docker-compose | ||
run: docker compose up -d | ||
- name: Run Test | ||
run: uv run pytest | ||
- name: Logs | ||
run: docker compose logs | ||
- name: Stop docker-compose | ||
run: docker compose down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
services: | ||
db: | ||
image: timescale/timescaledb-ha:pg16 | ||
ports: | ||
- "5432:5432" | ||
environment: | ||
- POSTGRES_PASSWORD=postgres | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_DB=postgres | ||
- TIMESCALEDB_TELEMETRY=off | ||
volumes: | ||
- ./data:/var/lib/postgresql/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,4 +116,5 @@ dev-dependencies = [ | |
"pandas>=2.2.3", | ||
"pytest-asyncio>=0.24.0", | ||
"pyright>=1.1.386", | ||
"vcrpy>=6.0.2", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import os | ||
from typing import Any | ||
|
||
import vcr | ||
|
||
vcr_cassette_path = os.path.join(os.path.dirname(__file__), "vcr_cassettes") | ||
|
||
|
||
def remove_set_cookie_header(response: dict[str, Any]): | ||
""" | ||
Removes the Set-Cookie header from a VCR.py response object to improve cassette consistency. | ||
This function can be used as a before_record callback in your VCR configuration | ||
to ensure that Set-Cookie headers are stripped from responses before they are | ||
recorded to cassettes. | ||
Args: | ||
response (vcr.request.Response): The VCR.py response object to modify | ||
Returns: | ||
vcr.request.Response: The modified response object with Set-Cookie headers removed | ||
Example: | ||
import vcr | ||
# Configure VCR with the callback | ||
vcr = vcr.VCR( | ||
before_record_response=remove_set_cookie_header, | ||
match_on=['uri', 'method'] | ||
) | ||
with vcr.use_cassette('tests/fixtures/my_cassette.yaml'): | ||
# Make your HTTP requests here | ||
pass | ||
""" | ||
|
||
# Get the headers from the response | ||
headers = response["headers"] | ||
|
||
# Headers to remove (case-insensitive) | ||
headers_to_remove = ["set-cookie", "Set-Cookie"] | ||
|
||
# Remove Set-Cookie headers if they exist | ||
for header in headers_to_remove: | ||
if header in headers: | ||
del headers[header] | ||
|
||
return response | ||
|
||
|
||
http_recorder = vcr.VCR( | ||
cassette_library_dir=vcr_cassette_path, | ||
record_mode="once", | ||
filter_headers=["authorization", "cookie"], | ||
before_record_response=remove_set_cookie_header, | ||
) |
Oops, something went wrong.