Skip to content

Commit 4c913d6

Browse files
committed
api tests no longer need hardcoded sql fixture [api_tests_with_no_sql]
1 parent c76dcec commit 4c913d6

File tree

2 files changed

+15
-45
lines changed

2 files changed

+15
-45
lines changed

conftest.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,45 +64,6 @@ def postgres_session(postgres_db):
6464
clear_mappers()
6565

6666

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

test_api.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,31 @@ def random_orderid(name=''):
1717
return f'order-{name}-{random_suffix()}'
1818

1919

20+
def post_to_add_batch(ref, sku, qty, eta):
21+
url = config.get_api_url()
22+
r = requests.post(
23+
f'{url}/add_batch',
24+
json={'ref': ref, 'sku': sku, 'qty': qty, 'eta': eta}
25+
)
26+
assert r.status_code == 201
27+
28+
29+
@pytest.mark.usefixtures('postgres_db')
2030
@pytest.mark.usefixtures('restart_api')
21-
def test_happy_path_returns_201_and_allocated_batch(add_stock):
31+
def test_happy_path_returns_201_and_allocated_batch():
2232
sku, othersku = random_sku(), random_sku('other')
2333
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-
])
34+
post_to_add_batch(batch1, sku, 100, '2011-01-02')
35+
post_to_add_batch(batch2, sku, 100, '2011-01-01')
36+
post_to_add_batch(batch3, othersku, 100, None)
2937
data = {'orderid': random_orderid(), 'sku': sku, 'qty': 3}
3038
url = config.get_api_url()
3139
r = requests.post(f'{url}/allocate', json=data)
3240
assert r.status_code == 201
3341
assert r.json()['batchref'] == batch2
3442

3543

44+
@pytest.mark.usefixtures('postgres_db')
3645
@pytest.mark.usefixtures('restart_api')
3746
def test_unhappy_path_returns_400_and_error_message():
3847
unknown_sku, orderid = random_sku(), random_orderid()

0 commit comments

Comments
 (0)