async def test_place_order_with_empty_string(client, delivery): delivery.persist() body = {"wanted:lait": ""} # User deleted the field value. resp = await client.post(f"/distribution/{delivery.id}/commander", body=body) assert resp.status == 302 delivery = Delivery.load(id=delivery.id) assert not delivery.orders
async def test_place_order_with_session(client, delivery): delivery.persist() body = {"wanted:lait": "3"} resp = await client.post(f"/distribution/{delivery.id}/commander", body=body) assert resp.status == 302 delivery = Delivery.load(id=delivery.id) assert "fractal-brocolis" in delivery.orders.keys() assert delivery.orders["fractal-brocolis"].products["lait"].wanted == 3
async def test_place_empty_order_should_delete_previous(client, delivery): delivery.orders["fractal-brocolis"] = Order( products={"lait": ProductOrder(wanted=1)} ) delivery.persist() resp = await client.post(f"/distribution/{delivery.id}/commander", body={}) assert resp.status == 302 delivery = Delivery.load(delivery.id) assert not delivery.orders
def test_wrong_datetime_raise_valueerror(): with pytest.raises(ValueError): Delivery( name="Andines", order_before=now(), to_date=now(), from_date="pouet", contact="*****@*****.**", )
async def test_cannot_place_order_on_closed_delivery(client, delivery, monkeypatch): monkeypatch.setattr("copanier.config.STAFF", ["*****@*****.**"]) delivery.order_before = datetime.now() - timedelta(days=1) delivery.persist() body = {"wanted:lait": "3"} resp = await client.post(f"/distribution/{delivery.id}/commander", body=body) assert resp.status == 302 delivery = Delivery.load(id=delivery.id) assert not delivery.orders
def test_can_create_delivery_with_products(): delivery = Delivery( name="Andines", from_date=now(), to_date=now(), order_before=now(), products=[Product(name="Lait", ref="123", price=1.5)], contact="*****@*****.**", ) assert len(delivery.products) == 1 assert delivery.products[0].ref == "123"
async def test_post_adjust_product(client, delivery): delivery.order_before = datetime.now() - timedelta(days=1) delivery.products[0].packing = 6 delivery.orders["fractal-brocolis"] = Order(products={"lait": ProductOrder(wanted=2)}) delivery.persist() assert delivery.status == delivery.ADJUSTMENT body = {"fractal-brocolis": "1"} resp = await client.post(f"/distribution/{delivery.id}/ajuster/lait", body=body) assert resp.status == 302 delivery = Delivery.load(id=delivery.id) assert delivery.orders["fractal-brocolis"].products["lait"].wanted == 2 assert delivery.orders["fractal-brocolis"].products["lait"].adjustment == 1
def test_can_create_delivery(): delivery = Delivery( name="Andines", from_date=now(), to_date=now(), order_before=now(), contact="*****@*****.**", ) assert delivery.name == "Andines" assert delivery.where == "Marché de la Briche" assert delivery.from_date.year == now().year assert not delivery.id
async def test_create_delivery(client): assert not list(Delivery.all()) body = { "name": "Andines", "where": "Marché de la Briche", "date": "2019-02-23", "from_time": "18:30:00", "to_time": "20:00:00", "order_before": "2019-02-12", "contact": "*****@*****.**", } resp = await client.post("/distribution", body=body) assert resp.status == 302 assert len(list(Delivery.all())) == 1 delivery = list(Delivery.all())[0] assert delivery.name == "Andines" assert delivery.from_date.year == 2019 assert delivery.from_date.hour == 18 assert delivery.from_date.minute == 30 assert delivery.to_date.year == 2019 assert delivery.to_date.hour == 20 assert delivery.to_date.minute == 0
async def test_only_staff_can_adjust_product(client, delivery, monkeypatch): delivery.order_before = datetime.now() - timedelta(days=1) delivery.products[0].packing = 6 delivery.orders["fractal-brocolis"] = Order(products={"lait": ProductOrder(wanted=2)}) delivery.persist() monkeypatch.setattr("copanier.config.STAFF", ["*****@*****.**"]) resp = await client.get(f"/distribution/{delivery.id}/ajuster/lait") assert resp.status == 302 body = {"fractal-brocolis": "1"} resp = await client.post(f"/distribution/{delivery.id}/ajuster/lait", body=body) assert resp.status == 302 delivery = Delivery.load(id=delivery.id) assert delivery.orders["fractal-brocolis"].products["lait"].wanted == 2 assert delivery.orders["fractal-brocolis"].products["lait"].adjustment == 0
def delivery(): return Delivery( name="CRAC d'automne", contact="*****@*****.**", from_date=datetime.now() + timedelta(days=10), to_date=datetime.now() + timedelta(days=10), order_before=datetime.now() + timedelta(days=7), products=[ Product( name="Lait", producer="ferme-du-coin", ref="lait", price=1.5, ) ], producers={ "ferme-du-coin": Producer(name="Ferme du coin", id="ferme-du-coin") }, )
async def test_place_empty_order(client, delivery): delivery.persist() resp = await client.post(f"/distribution/{delivery.id}/commander", body={}) assert resp.status == 302 delivery = Delivery.load(id=delivery.id) assert not delivery.orders
def pytest_runtest_setup(item): for path in Delivery.get_root().glob("*.yml"): path.unlink()
def test_can_load_delivery(delivery): delivery.name = "Corto" delivery.persist() loaded = Delivery.load(delivery.id) assert loaded.name == "Corto"