Skip to content

Commit 35d3a7e

Browse files
committed
first api tests [first_api_test]
1 parent 95099b9 commit 35d3a7e

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎config.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
def get_postgres_uri():
4+
host = os.environ.get('DB_HOST', 'localhost')
5+
port = 54321 if host == 'localhost' else 5432
6+
password = os.environ.get('DB_PASSWORD', 'abc123')
7+
user, db_name = 'allocation', 'allocation'
8+
return f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
9+
10+
11+
def get_api_url():
12+
host = os.environ.get('API_HOST', 'localhost')
13+
port = 5005 if host == 'localhost' else 80
14+
return f"http://{host}:{port}"
15+

‎conftest.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
# pylint: disable=redefined-outer-name
2+
import time
3+
from pathlib import Path
4+
15
import pytest
26
from sqlalchemy import create_engine
37
from sqlalchemy.orm import sessionmaker, clear_mappers
48

59
from orm import metadata, start_mappers
10+
from config import get_postgres_uri
611

712

813
@pytest.fixture
@@ -18,3 +23,62 @@ def session(in_memory_db):
1823
yield sessionmaker(bind=in_memory_db)()
1924
clear_mappers()
2025

26+
27+
@pytest.fixture(scope='session')
28+
def postgres_db():
29+
engine = create_engine(get_postgres_uri())
30+
metadata.create_all(engine)
31+
return engine
32+
33+
34+
@pytest.fixture
35+
def postgres_session(postgres_db):
36+
start_mappers()
37+
yield sessionmaker(bind=postgres_db)()
38+
clear_mappers()
39+
40+
41+
@pytest.fixture
42+
def add_stock(postgres_session):
43+
batches_added = set()
44+
skus_added = set()
45+
46+
def _add_stock(lines):
47+
for ref, sku, qty, eta in lines:
48+
postgres_session.execute(
49+
'INSERT INTO batches (reference, sku, _purchased_quantity, eta)'
50+
' VALUES (:ref, :sku, :qty, :eta)',
51+
dict(ref=ref, sku=sku, qty=qty, eta=eta),
52+
)
53+
[[batch_id]] = postgres_session.execute(
54+
'SELECT id FROM batches WHERE reference=:ref AND sku=:sku',
55+
dict(ref=ref, sku=sku),
56+
)
57+
batches_added.add(batch_id)
58+
skus_added.add(sku)
59+
postgres_session.commit()
60+
61+
yield _add_stock
62+
63+
for batch_id in batches_added:
64+
postgres_session.execute(
65+
'DELETE FROM allocations WHERE batch_id=:batch_id',
66+
dict(batch_id=batch_id),
67+
)
68+
postgres_session.execute(
69+
'DELETE FROM batches WHERE id=:batch_id',
70+
dict(batch_id=batch_id),
71+
)
72+
for sku in skus_added:
73+
postgres_session.execute(
74+
'DELETE FROM order_lines WHERE sku=:sku',
75+
dict(sku=sku),
76+
)
77+
postgres_session.commit()
78+
79+
80+
@pytest.fixture
81+
def restart_api():
82+
(Path(__file__).parent / 'flask_app.py').touch()
83+
time.sleep(0.3)
84+

‎test_api.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import uuid
2+
import pytest
3+
import requests
4+
5+
import config
6+
7+
def random_suffix():
8+
return uuid.uuid4().hex[:6]
9+
10+
def random_sku(name=''):
11+
return f'sku-{name}-{random_suffix()}'
12+
13+
def random_batchref(name=''):
14+
return f'batch-{name}-{random_suffix()}'
15+
16+
def random_orderid(name=''):
17+
return f'order-{name}-{random_suffix()}'
18+
19+
20+
@pytest.mark.usefixtures('restart_api')
21+
def test_api_returns_allocation(add_stock):
22+
sku, othersku = random_sku(), random_sku('other')
23+
batch1, batch2, batch3 = random_batchref(1), random_batchref(2), random_batchref(3)
24+
add_stock([
25+
(batch1, sku, 100, '2011-01-02'),
26+
(batch2, sku, 100, '2011-01-01'),
27+
(batch3, othersku, 100, None),
28+
])
29+
data = {'orderid': random_orderid(), 'sku': sku, 'qty': 3}
30+
url = config.get_api_url()
31+
r = requests.post(f'{url}/allocate', json=data)
32+
assert r.status_code == 201
33+
assert r.json()['batchref'] == batch2

0 commit comments

Comments
 (0)