Skip to content

Commit 07350f5

Browse files
refactor tests suite in order to run it locally outside docker (#102)
* refactor tests suite in order to run it locally outside docker * lint * remove service * try pg install * use python 3.9 for pre-commit to avoid deprecation issue form pydocstyle * revert --------- Co-authored-by: Jonathan Healy <jonathan.d.healy@gmail.com>
1 parent 16a25f6 commit 07350f5

File tree

9 files changed

+124
-106
lines changed

9 files changed

+124
-106
lines changed

.github/workflows/cicd.yaml

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,15 @@ on:
55
pull_request:
66
branches: [main]
77

8+
89
jobs:
910
test:
1011
runs-on: ubuntu-latest
1112
strategy:
1213
matrix:
13-
python-version: ["3.8", "3.9", "3.10", "3.11"]
14+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
1415
timeout-minutes: 20
1516

16-
services:
17-
db_service:
18-
image: ghcr.io/stac-utils/pgstac:v0.7.10
19-
env:
20-
POSTGRES_USER: username
21-
POSTGRES_PASSWORD: password
22-
POSTGRES_DB: postgis
23-
POSTGRES_HOST: localhost
24-
POSTGRES_PORT: 5432
25-
PGUSER: username
26-
PGPASSWORD: password
27-
PGDATABASE: postgis
28-
ALLOW_IP_RANGE: 0.0.0.0/0
29-
# Set health checks to wait until postgres has started
30-
options: >-
31-
--health-cmd pg_isready
32-
--health-interval 10s
33-
--health-timeout 10s
34-
--health-retries 10
35-
--log-driver none
36-
ports:
37-
# Maps tcp port 5432 on service container to the host
38-
- 5432:5432
39-
4017
steps:
4118
- name: Check out repository code
4219
uses: actions/checkout@v4
@@ -55,20 +32,16 @@ jobs:
5532
python -m pip install pre-commit
5633
pre-commit run --all-files
5734
58-
- name: Install
35+
- name: install lib postgres
36+
uses: nyurik/action-setup-postgis@v1
37+
38+
- name: Install dependencies
5939
run: |
60-
pip install .[dev,server]
40+
python -m pip install --upgrade pip
41+
python -m pip install .[dev,server]
6142
6243
- name: Run test suite
63-
run: make test
64-
env:
65-
ENVIRONMENT: testing
66-
POSTGRES_USER: username
67-
POSTGRES_PASS: password
68-
POSTGRES_DBNAME: postgis
69-
POSTGRES_HOST_READER: localhost
70-
POSTGRES_HOST_WRITER: localhost
71-
POSTGRES_PORT: 5432
44+
run: python -m pytest --cov stac_fastapi.pgstac --cov-report xml --cov-report term-missing
7245

7346
validate:
7447
runs-on: ubuntu-latest
@@ -90,17 +63,23 @@ jobs:
9063
--log-driver none
9164
ports:
9265
- 5432:5432
66+
9367
steps:
9468
- name: Check out repository code
9569
uses: actions/checkout@v4
70+
9671
- name: Setup Python
9772
uses: actions/setup-python@v5
9873
with:
9974
python-version: "3.10"
10075
cache: pip
10176
cache-dependency-path: setup.py
77+
10278
- name: Install stac-fastapi and stac-api-validator
103-
run: pip install .[server] stac-api-validator==0.4.1
79+
run: |
80+
python -m pip install --upgrade pip
81+
python -m pip install .[server] stac-api-validator==0.4.1
82+
10483
- name: Load data and validate
10584
run: python -m stac_fastapi.pgstac.app & ./scripts/wait-for-it.sh localhost:8080 && python ./scripts/ingest_joplin.py http://localhost:8080 && ./scripts/validate http://localhost:8080
10685
env:
@@ -120,15 +99,21 @@ jobs:
12099
runs-on: ubuntu-latest
121100
steps:
122101
- uses: actions/checkout@v4
102+
123103
- name: Setup Python
124104
uses: actions/setup-python@v5
125105
with:
126106
python-version: "3.10"
127107
cache: pip
128108
cache-dependency-path: setup.py
109+
129110
- name: Install with documentation dependencies
130-
run: pip install .[docs,dev,server]
111+
run: |
112+
python -m pip install --upgrade pip
113+
python -m pip install .[docs,dev,server]
114+
131115
- name: Generate API docs
132116
run: make docs
117+
133118
- name: Build documentation
134119
run: mkdocs build --strict

.pre-commit-config.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ repos:
33
rev: 5.12.0
44
hooks:
55
- id: isort
6-
language_version: python3.8
6+
language_version: python
7+
78
- repo: https://github.com/psf/black
89
rev: 22.12.0
910
hooks:
1011
- id: black
1112
args: ["--safe"]
12-
language_version: python3.8
13+
language_version: python
14+
1315
- repo: https://github.com/pycqa/flake8
1416
rev: 6.0.0
1517
hooks:
1618
- id: flake8
17-
language_version: python3.8
19+
language_version: python
1820
args: [
1921
# E501 let black handle all line length decisions
2022
# W503 black conflicts with "line break before operator" rule
@@ -26,7 +28,7 @@ repos:
2628
rev: v2.1.1
2729
hooks:
2830
- id: pydocstyle
29-
language_version: python3.8
31+
language_version: python
3032
exclude: ".*(test|scripts).*"
3133
args:
3234
[
@@ -42,11 +44,12 @@ repos:
4244
# - id: mypy
4345
# language_version: python3.8
4446
# args: [--no-strict-optional, --ignore-missing-imports]
47+
4548
- repo: https://github.com/PyCQA/pydocstyle
4649
rev: 6.3.0
4750
hooks:
4851
- id: pydocstyle
49-
language_version: python3.8
52+
language_version: python
5053
exclude: ".*(test|scripts).*"
5154
#args: [
5255
# Don't require docstrings for tests

Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
FROM python:3.8-slim as base
1+
ARG PYTHON_VERSION=3.11
2+
3+
FROM python:${PYTHON_VERSION}-slim as base
24

35
# Any python libraries that require system libraries to be installed will likely
46
# need the following packages in order to build
@@ -16,6 +18,6 @@ WORKDIR /app
1618

1719
COPY . /app
1820

19-
RUN pip install -e .[dev,server]
21+
RUN python -m pip install -e .[server]
2022

21-
CMD ["uvicorn", "stac_fastapi.pgstac.app:app", "--host", "0.0.0.0", "--port", "8080"]
23+
CMD ["uvicorn", "stac_fastapi.pgstac.app:app", "--host", "0.0.0.0", "--port", "8080"]

Dockerfile.tests

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ARG PYTHON_VERSION=3.11
2+
3+
FROM python:${PYTHON_VERSION}-slim as base
4+
5+
# Any python libraries that require system libraries to be installed will likely
6+
# need the following packages in order to build
7+
RUN apt-get update && \
8+
apt-get -y upgrade && \
9+
apt-get install -y build-essential git libpq-dev postgresql-15-postgis-3 && \
10+
apt-get clean && \
11+
rm -rf /var/lib/apt/lists/*
12+
13+
RUN useradd -ms /bin/bash newuser
14+
USER newuser
15+
16+
WORKDIR /app
17+
COPY . /app
18+
19+
RUN python -m pip install -e .[dev,server] --user

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ run = docker-compose run --rm \
1010
-e APP_PORT=${APP_PORT} \
1111
app
1212

13+
runtests = docker-compose run --rm tests
14+
1315
.PHONY: image
1416
image:
1517
docker-compose build
@@ -28,7 +30,7 @@ docker-shell:
2830

2931
.PHONY: test
3032
test:
31-
$(run) /bin/bash -c 'export && ./scripts/wait-for-it.sh database:5432 && cd /app/tests/ && pytest -vvv --log-cli-level $(LOG_LEVEL)'
33+
$(runtests) /bin/bash -c 'export && python -m pytest /app/tests/api/test_api.py --log-cli-level $(LOG_LEVEL)'
3234

3335
.PHONY: run-database
3436
run-database:

docker-compose.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ services:
33
app:
44
container_name: stac-fastapi-pgstac
55
image: stac-utils/stac-fastapi-pgstac
6-
platform: linux/amd64
76
build: .
87
environment:
98
- APP_HOST=0.0.0.0
@@ -31,6 +30,19 @@ services:
3130
- database
3231
command: bash -c "./scripts/wait-for-it.sh database:5432 && python -m stac_fastapi.pgstac.app"
3332

33+
tests:
34+
container_name: stac-fastapi-pgstac-test
35+
image: stac-utils/stac-fastapi-pgstac-test
36+
build:
37+
context: .
38+
dockerfile: Dockerfile.tests
39+
environment:
40+
- ENVIRONMENT=local
41+
- DB_MIN_CONN_SIZE=1
42+
- DB_MAX_CONN_SIZE=1
43+
- USE_API_HYDRATE=${USE_API_HYDRATE:-false}
44+
command: bash -c "python -m pytest -s -vv"
45+
3446
database:
3547
container_name: stac-db
3648
image: ghcr.io/stac-utils/pgstac:v0.7.10

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dev": [
2525
"pystac[validation]",
2626
"pypgstac[psycopg]==0.7.*",
27+
"pytest-postgresql",
2728
"pytest",
2829
"pytest-cov",
2930
"pytest-asyncio>=0.17,<0.23.0",

tests/api/test_api.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ async def search(query: Dict[str, Any]) -> List[Item]:
636636

637637

638638
@pytest.mark.asyncio
639-
async def test_wrapped_function(load_test_data) -> None:
639+
async def test_wrapped_function(load_test_data, database) -> None:
640640
# Ensure wrappers, e.g. Planetary Computer's rate limiting, work.
641641
# https://github.com/gadomski/planetary-computer-apis/blob/2719ccf6ead3e06de0784c39a2918d4d1811368b/pccommon/pccommon/redis.py#L205-L238
642642

@@ -672,7 +672,16 @@ async def get_collection(
672672
collection_id, request=request, **kwargs
673673
)
674674

675-
settings = Settings(testing=True)
675+
settings = Settings(
676+
postgres_user=database.user,
677+
postgres_pass=database.password,
678+
postgres_host_reader=database.host,
679+
postgres_host_writer=database.host,
680+
postgres_port=database.port,
681+
postgres_dbname=database.dbname,
682+
testing=True,
683+
)
684+
676685
extensions = [
677686
TransactionExtension(client=TransactionsClient(), settings=settings),
678687
FieldsExtension(),

0 commit comments

Comments
 (0)