From 0a8377c54894948f6194d3ad8d249a3e46b1272a Mon Sep 17 00:00:00 2001 From: Christian Beiwinkel Date: Tue, 4 Jun 2024 19:27:03 +0200 Subject: [PATCH] some tests were failing, and the contribution guide was a little dated --- CONTRIBUTING.md | 15 +++++++++++---- docker-compose.test.yml | 14 ++++++++++++++ routing_packager_app/api_v1/models.py | 12 ++++++------ tests/jobs/test_jobs.py | 4 ++-- tests/users/test_users.py | 6 +++--- 5 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 docker-compose.test.yml diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a3c22b1..fa9653f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -41,14 +41,21 @@ pre-commit install You'll need a few things to run the tests: - PostreSQL installation with a DB named `gis_test` (or define another db name using `POSTGRES_DB_TEST`) **and PostGIS enabled** -- Redis database, best done with `docker run --name redis -p 6379:6379 -d redis:6.0`, then you can use the project's defaults, i.e. `REDIS_URL=redis://localhost:6379/0` -- some fake SMTP service to handle email tests, our recommendations: - - [fake-smtp-server](https://www.npmjs.com/package/fake-smtp-server): NodeJS app with a frontend on `http://localhost:1080` and SMTP port 1025 - - pure Python one-liner in a separate terminal window: `sudo python -m smtpd -n -c DebuggingServer localhost:1025` +- Redis database + +Both can be quickly spun up by using the provided `docker-compose.test.yml`: + +```bash +docker compose -f docker-compose.test.yml up -d +``` + +You'll also need some fake SMTP service to handle email tests, our recommendation: [fake-smtp-server](https://www.npmjs.com/package/fake-smtp-server), +a NodeJS app with a frontend on `http://localhost:1080` and SMTP port 1025 We use `pytest` in this project with `coverage`: ```bash +export API_CONFIG=test pytest --cov=routing_packager_app ``` diff --git a/docker-compose.test.yml b/docker-compose.test.yml new file mode 100644 index 0000000..a464307 --- /dev/null +++ b/docker-compose.test.yml @@ -0,0 +1,14 @@ +services: + postgres: + image: kartoza/postgis:14 + environment: + POSTGRES_USER: admin + POSTGRES_PASS: admin + POSTGRES_DB: gis_test + ALLOW_IP_RANGE: 0.0.0.0/0 + ports: + - 5432:5432 + redis: + image: redis:6.2 + ports: + - 6379:6379 diff --git a/routing_packager_app/api_v1/models.py b/routing_packager_app/api_v1/models.py index 9733fac..8c14281 100644 --- a/routing_packager_app/api_v1/models.py +++ b/routing_packager_app/api_v1/models.py @@ -43,13 +43,13 @@ class JobCreate(JobBase): class Job(JobBase, table=True): __tablename__ = "jobs" - id: Optional[int] = Field(primary_key=True) - arq_id: Optional[str] = Field(nullable=True) - user_id: int = Field(default=None, foreign_key="users.id") + id: int | None = Field(default=None, primary_key=True) + arq_id: str | None = Field(nullable=True) + user_id: int | None = Field(default=None, foreign_key="users.id") status: Statuses = Field(nullable=False) zip_path: str = Field(nullable=True) - last_started: Optional[datetime] = Field(nullable=True) # did it ever run? - last_finished: Optional[datetime] = Field( + last_started: datetime | None = Field(nullable=True) # did it ever run? + last_finished: datetime | None = Field( sa_column=Column(DateTime(), nullable=True) ) # did it ever finish? @@ -81,7 +81,7 @@ class User(UserBase, table=True): __tablename__ = "users" - id: Optional[int] = Field(primary_key=True) + id: int | None = Field(default=None, primary_key=True) password: str = Field(sa_column=Column(PasswordType(schemes=("pbkdf2_sha512",)), nullable=False)) jobs: List[Job] = Relationship(back_populates="user") diff --git a/tests/jobs/test_jobs.py b/tests/jobs/test_jobs.py index 78ffec1..a7b53e0 100644 --- a/tests/jobs/test_jobs.py +++ b/tests/jobs/test_jobs.py @@ -5,7 +5,7 @@ from routing_packager_app import SETTINGS from routing_packager_app.api_v1.models import Job -from routing_packager_app.constants import Providers, Statuses +from routing_packager_app.constants import Providers from routing_packager_app.utils.file_utils import make_package_path from ..utils_ import create_new_job, DEFAULT_ARGS_POST @@ -74,7 +74,7 @@ def test_job_get_jobs(get_client, basic_auth_header): # parameterize the ones that should work with the default params @pytest.mark.parametrize( "key_value", - (("bbox", "0,0,1,1"), ("provider", Providers.OSM), ("status", Statuses.QUEUED), ("update", False)), + (("bbox", "0,0,1,1"), ("provider", "osm"), ("status", "Queued"), ("update", False)), ) def test_job_get_jobs_all_params(key_value, get_client, basic_auth_header): # default diff --git a/tests/users/test_users.py b/tests/users/test_users.py index a05b7f4..1d409da 100644 --- a/tests/users/test_users.py +++ b/tests/users/test_users.py @@ -47,7 +47,7 @@ def test_new_user_missing_field_error(missing_field, get_client: TestClient, bas response = create_new_user(get_client, data, basic_auth_header, False) assert response.status_code == 422 - assert response.json()["detail"][0].get("msg") == "field required" + assert response.json()["detail"][0].get("msg") == "Field required" @pytest.mark.parametrize("email", ("user@email.12", "user.me", "uuser@email:", "user@email.")) @@ -57,7 +57,7 @@ def test_new_user_wrong_email_error(email, get_client: TestClient, basic_auth_he response = create_new_user(get_client, auth_header=basic_auth_header, data=data, must_succeed=False) assert response.status_code == 422 - assert response.json()["detail"][0].get("msg") == "value is not a valid email address" + assert response.json()["detail"][0].get("msg")[:34] == "value is not a valid email address" def test_new_user_unauthorized(get_client, basic_auth_header): @@ -192,6 +192,6 @@ def test_admin_user_created(get_client, get_session: Session): assert response.json()[0]["email"] == expected_email statement = select(User).where(User.id == 1) - admin_user = get_session.exec(statement) + admin_user = get_session.exec(statement).first() assert admin_user is not None assert admin_user.email == expected_email