Skip to content

Commit fd45a6f

Browse files
committed
api tests no longer need hardcoded sql fixture [chapter_05_high_gear_low_gear_ends]
1 parent 6b404b5 commit fd45a6f

File tree

2 files changed

+15
-46
lines changed

2 files changed

+15
-46
lines changed

tests/conftest.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -63,43 +63,6 @@ def postgres_session(postgres_db):
6363
clear_mappers()
6464

6565

66-
@pytest.fixture
67-
def add_stock(postgres_session):
68-
batches_added = set()
69-
skus_added = set()
70-
71-
def _add_stock(lines):
72-
for ref, sku, qty, eta in lines:
73-
postgres_session.execute(
74-
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
75-
" VALUES (:ref, :sku, :qty, :eta)",
76-
dict(ref=ref, sku=sku, qty=qty, eta=eta),
77-
)
78-
[[batch_id]] = postgres_session.execute(
79-
"SELECT id FROM batches WHERE reference=:ref AND sku=:sku",
80-
dict(ref=ref, sku=sku),
81-
)
82-
batches_added.add(batch_id)
83-
skus_added.add(sku)
84-
postgres_session.commit()
85-
86-
yield _add_stock
87-
88-
for batch_id in batches_added:
89-
postgres_session.execute(
90-
"DELETE FROM allocations WHERE batch_id=:batch_id",
91-
dict(batch_id=batch_id),
92-
)
93-
postgres_session.execute(
94-
"DELETE FROM batches WHERE id=:batch_id", dict(batch_id=batch_id),
95-
)
96-
for sku in skus_added:
97-
postgres_session.execute(
98-
"DELETE FROM order_lines WHERE sku=:sku", dict(sku=sku),
99-
)
100-
postgres_session.commit()
101-
102-
10366
@pytest.fixture
10467
def restart_api():
10568
(Path(__file__).parent / "../entrypoints/flask_app.py").touch()

tests/e2e/test_api.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,34 @@ def random_orderid(name=""):
2121
return f"order-{name}-{random_suffix()}"
2222

2323

24+
def post_to_add_batch(ref, sku, qty, eta):
25+
url = config.get_api_url()
26+
r = requests.post(
27+
f"{url}/add_batch", json={"ref": ref, "sku": sku, "qty": qty, "eta": eta}
28+
)
29+
assert r.status_code == 201
30+
31+
32+
@pytest.mark.usefixtures("postgres_db")
2433
@pytest.mark.usefixtures("restart_api")
25-
def test_happy_path_returns_201_and_allocated_batch(add_stock):
34+
def test_happy_path_returns_201_and_allocated_batch():
2635
sku, othersku = random_sku(), random_sku("other")
2736
earlybatch = random_batchref(1)
2837
laterbatch = random_batchref(2)
2938
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-
)
39+
post_to_add_batch(laterbatch, sku, 100, "2011-01-02")
40+
post_to_add_batch(earlybatch, sku, 100, "2011-01-01")
41+
post_to_add_batch(otherbatch, othersku, 100, None)
3742
data = {"orderid": random_orderid(), "sku": sku, "qty": 3}
38-
url = config.get_api_url()
3943

44+
url = config.get_api_url()
4045
r = requests.post(f"{url}/allocate", json=data)
4146

4247
assert r.status_code == 201
4348
assert r.json()["batchref"] == earlybatch
4449

4550

51+
@pytest.mark.usefixtures("postgres_db")
4652
@pytest.mark.usefixtures("restart_api")
4753
def test_unhappy_path_returns_400_and_error_message():
4854
unknown_sku, orderid = random_sku(), random_orderid()

0 commit comments

Comments
 (0)