From 00e805d53582e801630d68c0b7e46d487bd8749a Mon Sep 17 00:00:00 2001 From: stijndcl Date: Fri, 17 Jun 2022 00:43:55 +0200 Subject: [PATCH] Fiddle with action --- .github/workflows/python.yml | 15 +++++++++++++++ .gitignore | 3 +++ database/engine.py | 34 ++++++++++++++++++++++------------ pyproject.toml | 7 ++++++- settings.py | 1 + tests/conftest.py | 15 +++++++++++++++ tests/test_dummy.py | 7 +++++-- 7 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 tests/conftest.py diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 33624a9..9ded31d 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -24,6 +24,18 @@ jobs: tests: needs: [dependencies] runs-on: ubuntu-latest + services: + postgres: + image: postgres:14 + env: + POSTGRES_DB: didier_action + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 steps: - uses: actions/checkout@v2 - name: Setup Python @@ -41,6 +53,9 @@ jobs: run: pip3 install -r requirements.txt -r requirements-dev.txt - name: Run Pytest run: pytest tests + env: + DB_TEST_SQLITE: false + DB_NAME: didier_action linting: needs: [dependencies] runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index a14d6d0..518f9fa 100644 --- a/.gitignore +++ b/.gitignore @@ -154,3 +154,6 @@ cython_debug/ # PyCharm .idea/ + +# SQLite testing database +tests.db \ No newline at end of file diff --git a/database/engine.py b/database/engine.py index fbdbfd2..2c1815f 100644 --- a/database/engine.py +++ b/database/engine.py @@ -6,17 +6,27 @@ from sqlalchemy.orm import sessionmaker import settings -encoded_password = quote_plus(settings.DB_PASSWORD) -engine = create_async_engine( - URL.create( - drivername="postgresql+asyncpg", - username=settings.DB_USERNAME, - password=encoded_password, - host=settings.DB_HOST, - port=settings.DB_PORT, - database=settings.DB_NAME, - ), - pool_pre_ping=True, -) +# Run local tests against SQLite instead of Postgres +if settings.DB_TEST_SQLITE: + engine = create_async_engine( + URL.create( + drivername="sqlite+aiosqlite", + database="tests.db", + ), + connect_args={"check_same_thread": False}, + ) +else: + encoded_password = quote_plus(settings.DB_PASSWORD) + engine = create_async_engine( + URL.create( + drivername="postgresql+asyncpg", + username=settings.DB_USERNAME, + password=encoded_password, + host=settings.DB_HOST, + port=settings.DB_PORT, + database=settings.DB_NAME, + ), + pool_pre_ping=True, + ) DBSession = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession, expire_on_commit=False) diff --git a/pyproject.toml b/pyproject.toml index a5ef1e7..c4f2266 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,4 +18,9 @@ max-line-length = 120 good-names = ["i"] [tool.pytest.ini_options] -asyncio_mode = "auto" \ No newline at end of file +asyncio_mode = "auto" +env = [ + "DB_NAME = didier-tests", + "DB_USERNAME = postgres", + "DB_HOST = postgres" +] \ No newline at end of file diff --git a/settings.py b/settings.py index 5ef9350..cd7e541 100644 --- a/settings.py +++ b/settings.py @@ -14,6 +14,7 @@ DB_USERNAME: str = env.str("DB_USERNAME", "postgres") DB_PASSWORD: str = env.str("DB_PASSWORD", "") DB_HOST: str = env.str("DB_HOST", "localhost") DB_PORT: int = env.int("DB_PORT", "5432") +DB_TEST_SQLITE: bool = env.bool("DB_TEST_SQLITE", True) """Discord""" DISCORD_TOKEN: str = env.str("DISC_TOKEN") diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..40d3189 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,15 @@ +import pytest + +from alembic import command, config + + +@pytest.fixture(scope="session") +def tables(): + """Initialize a database before the tests, and then tear it down again + Starts from an empty database and runs through all the migrations to check those as well + while we're at it + """ + alembic_config = config.Config("alembic.ini") + command.upgrade(alembic_config, "head") + yield + command.downgrade(alembic_config, "base") diff --git a/tests/test_dummy.py b/tests/test_dummy.py index f4f5361..5f57aa2 100644 --- a/tests/test_dummy.py +++ b/tests/test_dummy.py @@ -1,2 +1,5 @@ -def test_dummy(): - assert True +import settings + + +def test_dummy(tables): + assert settings.DB_TEST_SQLITE