From e94f9122ad8757949da66619d9a28c915641dcb2 Mon Sep 17 00:00:00 2001 From: Nikita Marchant Date: Mon, 24 Oct 2022 16:37:16 +0200 Subject: [PATCH 1/3] Run tests on github actions both for sqlite and postgresql --- .github/workflows/python-tests.yml | 21 +++++++++++++++++++-- search/tests/test_postgresql.py | 28 ++++++++++++++++++++++++++++ setup.cfg | 1 + 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 search/tests/test_postgresql.py diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 513276ae..2a252bf2 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -10,6 +10,18 @@ jobs: runs-on: ubuntu-latest + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + steps: - uses: actions/checkout@v3 - name: Apt update @@ -44,8 +56,13 @@ jobs: - name: Statics checks run: ./manage.py collectstatic --noinput -v 0 - - name: pytest + - name: pytest with SQLite run: pytest -k "not unoconv" - - name: database checks + - name: pytest with PostgreSQL + run: pytest -k "not unoconv" -m postgresql + env: + DB_URL: postgres://postgresql:postgresql@postgresql:5432/postgresql + + - name: Check local dev initialization scripts run: make database diff --git a/search/tests/test_postgresql.py b/search/tests/test_postgresql.py new file mode 100644 index 00000000..a71c1d8f --- /dev/null +++ b/search/tests/test_postgresql.py @@ -0,0 +1,28 @@ +from django.db import connection + +import pytest + + +def needs_postgres(fn): + @pytest.mark.django_db + @pytest.mark.postgresql + @pytest.mark.skipif( + connection.vendor != "postgresql", + reason="This test requires a PostgreSQL database", + ) + def wrapper(*args, **kwargs): + return fn(*args, **kwargs) + + return wrapper + + +@needs_postgres +def test_running_with_postgres(): + """ + This is a dummy test to check if our logic of @needs_postgres is correct + and that the setup of GitHub actions is correct too. + """ + assert True + + +# FIXME: add more tests (see https://github.com/UrLab/DocHub/issues/257) diff --git a/setup.cfg b/setup.cfg index 04393f7b..46627d87 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,6 +8,7 @@ markers = unoconv: uses unoconv (underterministic) webtest: http queries against localhost celery: uses celery tasks + postgresql: needs a postgresql database to run [flake8] ignore = E261 From 7b68f2616ca953b3d6dcbdca30e1a3dc1c337186 Mon Sep 17 00:00:00 2001 From: Nikita Marchant Date: Mon, 24 Oct 2022 17:51:45 +0200 Subject: [PATCH 2/3] Fix db url --- .github/workflows/python-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 2a252bf2..6772f867 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -62,7 +62,7 @@ jobs: - name: pytest with PostgreSQL run: pytest -k "not unoconv" -m postgresql env: - DB_URL: postgres://postgresql:postgresql@postgresql:5432/postgresql + DB_URL: postgres://postgres:postgres@postgres:5432/postgres - name: Check local dev initialization scripts run: make database From 84f8cd19f78ebe57d6f5a6ffe843a74cefd22e6a Mon Sep 17 00:00:00 2001 From: Nikita Marchant Date: Mon, 24 Oct 2022 18:20:52 +0200 Subject: [PATCH 3/3] Map the containter ports to the host and use localhost in the DB_URL --- .github/workflows/python-tests.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 6772f867..f362cb66 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -20,6 +20,9 @@ jobs: --health-interval 10s --health-timeout 5s --health-retries 5 + ports: + # Maps tcp port 5432 on service container to the host + - 5432:5432 steps: @@ -62,7 +65,7 @@ jobs: - name: pytest with PostgreSQL run: pytest -k "not unoconv" -m postgresql env: - DB_URL: postgres://postgres:postgres@postgres:5432/postgres + DB_URL: postgres://postgres:postgres@localhost:5432/postgres - name: Check local dev initialization scripts run: make database