Skip to content

Commit fda077d

Browse files
committed
first api tests [first_api_test]
1 parent b635a15 commit fda077d

3 files changed

Lines changed: 120 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+
4+
def get_postgres_uri():
5+
host = os.environ.get("DB_HOST", "localhost")
6+
port = 54321 if host == "localhost" else 5432
7+
password = os.environ.get("DB_PASSWORD", "abc123")
8+
user, db_name = "allocation", "allocation"
9+
return f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
10+
11+
12+
def get_api_url():
13+
host = os.environ.get("API_HOST", "localhost")
14+
port = 5005 if host == "localhost" else 80
15+
return f"http://{host}:{port}"

‎conftest.py‎

Lines changed: 62 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
@@ -17,3 +22,60 @@ def session(in_memory_db):
1722
start_mappers()
1823
yield sessionmaker(bind=in_memory_db)()
1924
clear_mappers()
25+
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", dict(batch_id=batch_id),
70+
)
71+
for sku in skus_added:
72+
postgres_session.execute(
73+
"DELETE FROM order_lines WHERE sku=:sku", dict(sku=sku),
74+
)
75+
postgres_session.commit()
76+
77+
78+
@pytest.fixture
79+
def restart_api():
80+
(Path(__file__).parent / "flask_app.py").touch()
81+
time.sleep(0.3)

‎test_api.py‎

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

0 commit comments

Comments
 (0)