Skip to content

Commit

Permalink
Fix: github actions postgresql connection
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Aug 18, 2024
1 parent b0d80db commit bc12da3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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_),
)
14 changes: 12 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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

Expand Down
29 changes: 13 additions & 16 deletions tests/test_dbmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit bc12da3

Please sign in to comment.