Skip to content

Commit fdafb60

Browse files
committed
First cut of csv thing with no repositories etc [first_cut_csvs]
1 parent 7526014 commit fdafb60

5 files changed

Lines changed: 112 additions & 2 deletions

File tree

‎src/allocation/service_layer/services.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ def is_valid_sku(sku, batches):
1616

1717

1818
def add_batch(
19-
ref: str, sku: str, qty: int, eta: Optional[date],
19+
ref: str,
20+
sku: str,
21+
qty: int,
22+
eta: Optional[date],
2023
uow: unit_of_work.AbstractUnitOfWork,
2124
):
2225
with uow:
@@ -25,7 +28,9 @@ def add_batch(
2528

2629

2730
def allocate(
28-
orderid: str, sku: str, qty: int,
31+
orderid: str,
32+
sku: str,
33+
qty: int,
2934
uow: unit_of_work.AbstractUnitOfWork,
3035
) -> str:
3136
line = OrderLine(orderid, sku, qty)

‎src/bin/allocate-from-csv‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
import csv
3+
import sys
4+
from datetime import datetime
5+
from pathlib import Path
6+
7+
from allocation.domain import model
8+
9+
10+
def load_batches(batches_path):
11+
batches = []
12+
with batches_path.open() as inf:
13+
reader = csv.DictReader(inf)
14+
for row in reader:
15+
if row["eta"]:
16+
eta = datetime.strptime(row["eta"], "%Y-%m-%d").date()
17+
else:
18+
eta = None
19+
batches.append(
20+
model.Batch(
21+
ref=row["ref"], sku=row["sku"], qty=int(row["qty"]), eta=eta
22+
)
23+
)
24+
return batches
25+
26+
27+
def main(folder):
28+
batches_path = Path(folder) / "batches.csv"
29+
orders_path = Path(folder) / "orders.csv"
30+
allocations_path = Path(folder) / "allocations.csv"
31+
32+
batches = load_batches(batches_path)
33+
34+
with orders_path.open() as inf, allocations_path.open("w") as outf:
35+
reader = csv.DictReader(inf)
36+
writer = csv.writer(outf)
37+
writer.writerow(["orderid", "sku", "batchref"])
38+
for row in reader:
39+
orderid, sku = row["orderid"], row["sku"]
40+
qty = int(row["qty"])
41+
line = model.OrderLine(orderid, sku, qty)
42+
batchref = model.allocate(line, batches)
43+
writer.writerow([line.orderid, line.sku, batchref])
44+
45+
46+
if __name__ == "__main__":
47+
main(sys.argv[1])

‎src/setup.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
name="allocation",
55
version="0.1",
66
packages=["allocation"],
7+
scripts=[
8+
"bin/allocate-from-csv",
9+
],
710
)

‎tests/conftest.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# pylint: disable=redefined-outer-name
2+
import csv
3+
import shutil
24
import time
5+
import tempfile
36
from pathlib import Path
47

58
import pytest
@@ -73,3 +76,20 @@ def restart_api():
7376
(Path(__file__).parent / "../src/allocation/entrypoints/flask_app.py").touch()
7477
time.sleep(0.5)
7578
wait_for_webapp_to_come_up()
79+
80+
81+
@pytest.fixture
82+
def make_csv():
83+
tempdir = tempfile.mkdtemp()
84+
85+
def _make_csv(filename, lines):
86+
path = Path(tempdir) / filename
87+
with path.open("w") as f:
88+
writer = csv.writer(f)
89+
for line in lines:
90+
writer.writerow(line)
91+
return path
92+
93+
yield _make_csv
94+
95+
shutil.rmtree(tempdir)

‎tests/e2e/test_csv.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import csv
2+
import subprocess
3+
import uuid
4+
5+
6+
def random_ref(prefix):
7+
return prefix + "-" + uuid.uuid4().hex[:10]
8+
9+
10+
def test_cli_app_reads_csvs_with_batches_and_orders_and_outputs_allocations(make_csv):
11+
sku1, sku2 = random_ref("s1"), random_ref("s2")
12+
batch1, batch2, batch3 = random_ref("b1"), random_ref("b2"), random_ref("b3")
13+
order_ref = random_ref("o")
14+
make_csv("batches.csv", [
15+
["ref", "sku", "qty", "eta"],
16+
[batch1, sku1, 100, ""],
17+
[batch2, sku2, 100, "2011-01-01"],
18+
[batch3, sku2, 100, "2011-01-02"],
19+
])
20+
orders_csv = make_csv("orders.csv", [
21+
["orderid", "sku", "qty"],
22+
[order_ref, sku1, 3],
23+
[order_ref, sku2, 12],
24+
])
25+
26+
subprocess.run(["allocate-from-csv", orders_csv.parent])
27+
28+
expected_output_csv = orders_csv.parent / "allocations.csv"
29+
with open(expected_output_csv) as f:
30+
rows = list(csv.reader(f))
31+
assert rows == [
32+
["orderid", "sku", "qty", "batchref"],
33+
[order_ref, sku1, "3", batch1],
34+
[order_ref, sku2, "12", batch2],
35+
]

0 commit comments

Comments
 (0)