-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
47 lines (33 loc) · 1.42 KB
/
conftest.py
File metadata and controls
47 lines (33 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Pytest global config.
This repo is typically run with Postgres in Docker. When running pytest on the host
(Windows/macOS/Linux), the in-container hostname `db` won't resolve.
We default DATABASE_URL to the docker-compose port mapping (localhost:5434) so
`backend.settings` can boot without extra env juggling.
"""
from __future__ import annotations
import os
from urllib.parse import urlparse
import pytest
from rest_framework.test import APIClient
@pytest.fixture
def client() -> APIClient:
"""DRF API client fixture.
Many tests in this repo expect `.force_authenticate()` which is provided by
`rest_framework.test.APIClient`, not Django's default test client.
"""
return APIClient()
def _should_rewrite_database_url(url: str) -> bool:
try:
parsed = urlparse(url)
except Exception:
return False
return (parsed.scheme in {"postgres", "postgresql"}) and (parsed.hostname == "db")
DEFAULT_DOCKER_DB_URL = "postgresql://limify:limify_password@localhost:5434/limify"
# Ensure DATABASE_URL exists for backend.settings
if not os.getenv("DATABASE_URL"):
os.environ["DATABASE_URL"] = DEFAULT_DOCKER_DB_URL
else:
# If someone exported the in-docker URL (host=db), rewrite for host pytest runs.
# When pytest is executed inside the container, RUNNING_IN_DOCKER=1 is present.
if not os.getenv("RUNNING_IN_DOCKER") and _should_rewrite_database_url(os.environ["DATABASE_URL"]):
os.environ["DATABASE_URL"] = DEFAULT_DOCKER_DB_URL