Ejemplo n.º 1
0
def place_order(order: dict, day: int):
    errors = __validate(order)

    if errors:
        errors_combined = '[' + ', '.join(errors) + ']'
        abort(400, f'Invalid request! Errors found: {errors_combined}')

    placed_order = service.place_order(customer_id=order['customer'],
                                       milk=order['order']['milk'],
                                       skins=order['order']['skins'],
                                       day=day)

    res = {
        'milk': placed_order.milk_allocated,
        'skins': placed_order.skins_allocated,
    }

    if placed_order.status == OrderStatus.succeeded:
        return make_response(res, 201)
    if placed_order.status == OrderStatus.partially_succeeded:
        return make_response(res, 206)
    abort(404, 'Not enough resources in stock')
Ejemplo n.º 2
0
def __perform_order_placing(day: int,
                            milk: float,
                            skins: int,
                            produced_stock: StockReport,
                            allocated_stock: StockReport,
                            monkeypatch) -> Order:
    saved = {'order': None}

    def fake_save_order(order):
        saved['order'] = order

    monkeypatch.setattr('shepherd.storage.find_all_labyaks', lambda: [])
    monkeypatch.setattr('shepherd.product_calculator.production_report', lambda h, d: produced_stock)

    monkeypatch.setattr('shepherd.storage.stock_allocated', lambda d: allocated_stock)
    monkeypatch.setattr('shepherd.storage.save_order', fake_save_order)

    order = service.place_order('me', milk, skins, day)

    assert order == saved['order']
    assert order.customer_id == 'me'
    assert order.milk_requested == milk
    assert order.skins_requested == skins
    return order
Ejemplo n.º 3
0
def test_raise_exception_on_negative_skins_amount():
    with pytest.raises(NegativeValueException):
        service.place_order('me', 1, -1, 1)
Ejemplo n.º 4
0
def test_raise_exception_on_empty_order():
    with pytest.raises(EmptyOrderException):
        service.place_order('me', 0, 0,1)