diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 856e5a6..65e566a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,6 +20,18 @@ jobs: FLAKE8: flake8 PYTEST: pytest COVERAGE: coverage + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..3662b39 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,26 @@ +import os + +import pytest +from yhttp.ext.dbmanager import PostgresqlManager + + +CICD = 'CI' in os.environ \ + and os.environ['CI'] \ + and 'GITHUB_RUN_ID' in os.environ + + +@pytest.fixture +def cicd(): + return CICD + + +@pytest.fixture +def dbmanager(cicd): + host = CICD and 'localhost' or None + user = CICD and 'postgres' or None + pass_ = CICD and 'postgres' or None + return PostgresqlManager( + host=os.environ.get('YHTTP_DB_DEFAULT_HOST', host), + user=os.environ.get('YHTTP_DB_DEFAULT_ADMINUSER', user), + password=os.environ.get('YHTTP_DB_DEFAULT_ADMINPASS', pass_), + ) diff --git a/tests/test_cli.py b/tests/test_cli.py index e185ad0..1e45802 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,9 +1,12 @@ +import os + from bddcli import Given, Application as CLIApplication, status, stderr, \ when, stdout import easycli from yhttp.core import Application from yhttp.ext.dbmanager import install +from .conftest import CICD class Bar(easycli.SubCommand): @@ -21,10 +24,17 @@ def __call__(self, args): install(app, cliarguments=[Bar]) -def test_applicationcli(): +def test_applicationcli(cicd): app.ready() cliapp = CLIApplication('example', 'tests.test_cli:app.climain') - with Given(cliapp, 'db'): + env = os.environ.copy() + if cicd: + env['YHTTP_DB_DEFAULT_HOST'] = host + env['YHTTP_DB_DEFAULT_ADMINUSER'] = user + env['YHTTP_DB_DEFAULT_ADMINPASS'] = pass_ + + + with Given(cliapp, 'db', environ=env): assert stderr == '' assert status == 0 diff --git a/tests/test_dbmanager.py b/tests/test_dbmanager.py index 1e87ec9..27052d0 100644 --- a/tests/test_dbmanager.py +++ b/tests/test_dbmanager.py @@ -4,25 +4,22 @@ DBNAME = 'yhttp-postgesql-testing' -def test_dbmanager(): - # Instance with postgres identity framework. - dbm = PostgresqlManager() - +def test_dbmanager(dbmanager): # Cleanup - dbm.dropifexists(DBNAME) + dbmanager.dropifexists(DBNAME) - assert dbm.exists('postgres') - assert not dbm.exists(DBNAME) + assert dbmanager.exists('postgres') + assert not dbmanager.exists(DBNAME) - dbm.create(DBNAME) - assert dbm.exists(DBNAME) + dbmanager.create(DBNAME) + assert dbmanager.exists(DBNAME) - dbm.create(DBNAME, dropifexists=True) - assert dbm.exists(DBNAME) + dbmanager.create(DBNAME, dropifexists=True) + assert dbmanager.exists(DBNAME) - dbm.drop(DBNAME) - assert not dbm.exists(DBNAME) + dbmanager.drop(DBNAME) + assert not dbmanager.exists(DBNAME) - dbm.create(DBNAME) - dbm.dropifexists(DBNAME) - assert not dbm.exists(DBNAME) + dbmanager.create(DBNAME) + dbmanager.dropifexists(DBNAME) + assert not dbmanager.exists(DBNAME)