Skip to content

Commit d8a078b

Browse files
committed
fix conftest waits, requirements.txt and travis config
1 parent 6641e38 commit d8a078b

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ language: python
33
python: 3.8
44

55
install:
6-
- pip3 install sqlalchemy
6+
- pip3 install -r requirements.txt
77

88
script:
9-
- make test
9+
- make all
1010

1111
branches:
1212
except:

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
build:
2+
docker-compose build
3+
4+
up:
5+
docker-compose up -d app
6+
17
test:
28
pytest --tb=short
39

4-
watch-tests:
5-
ls *.py | entr pytest --tb=short
10+
all: build up test

conftest.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
from pathlib import Path
44

55
import pytest
6+
import requests
7+
from requests.exceptions import ConnectionError
8+
from sqlalchemy.exc import OperationalError
69
from sqlalchemy import create_engine
710
from sqlalchemy.orm import sessionmaker, clear_mappers
811

912
from orm import metadata, start_mappers
10-
from config import get_postgres_uri
13+
import config
1114

1215

1316
@pytest.fixture
@@ -24,9 +27,32 @@ def session(in_memory_db):
2427
clear_mappers()
2528

2629

30+
def wait_for_postgres_to_come_up(engine):
31+
deadline = time.time() + 10
32+
while time.time() < deadline:
33+
try:
34+
return engine.connect()
35+
except OperationalError:
36+
time.sleep(0.5)
37+
pytest.fail('Postgres never came up')
38+
39+
40+
def wait_for_webapp_to_come_up():
41+
deadline = time.time() + 10
42+
url = config.get_api_url()
43+
while time.time() < deadline:
44+
try:
45+
return requests.get(url)
46+
except ConnectionError:
47+
time.sleep(0.5)
48+
pytest.fail('API never came up')
49+
50+
51+
2752
@pytest.fixture(scope='session')
2853
def postgres_db():
29-
engine = create_engine(get_postgres_uri())
54+
engine = create_engine(config.get_postgres_uri())
55+
wait_for_postgres_to_come_up(engine)
3056
metadata.create_all(engine)
3157
return engine
3258

@@ -80,5 +106,6 @@ def _add_stock(lines):
80106
@pytest.fixture
81107
def restart_api():
82108
(Path(__file__).parent / 'flask_app.py').touch()
83-
time.sleep(0.3)
109+
time.sleep(0.5)
110+
wait_for_webapp_to_come_up()
84111

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pytest
22
sqlalchemy
33
flask
44
psycopg2
5+
requests

0 commit comments

Comments
 (0)