Esempio n. 1
0
def test_update_ingredient(mocker):
    mocked_internal_func = mocker.patch.object(ReceiptStorage,
                                               'update_ingredient')

    receipt_controller = ReceiptController(ReceiptStorage())

    sugar = ModelControllerIngredient(id=-1, name='sugar', calories=100)
    receipt_controller.update_ingredient(sugar)

    mocked_internal_func.assert_called_with(sugar)
Esempio n. 2
0
def test_delete_ingredient(mocker):
    mocked_internal_func = mocker.patch.object(ReceiptStorage,
                                               'delete_ingredient')

    receipt_controller = ReceiptController(ReceiptStorage())

    sugar_id = 1
    receipt_controller.delete_ingredient(sugar_id)

    mocked_internal_func.assert_called_with(sugar_id)
Esempio n. 3
0
def test_get_ingredient_call_storage_properly(mocker):
    mocked_internal_func = mocker.patch.object(ReceiptStorage,
                                               'get_ingredient')
    sugar = ModelControllerIngredient(id=-1, name='sugar', calories=100)
    mocked_internal_func.return_value = sugar
    receipt_controller = ReceiptController(ReceiptStorage())

    sugar_id = 1
    read_ingredient = receipt_controller.get_ingredient(sugar_id)

    mocked_internal_func.assert_called_with(sugar_id)
    assert sugar == read_ingredient
Esempio n. 4
0
def test_set_receipt_call_storage_properly(mocker):
    mocked_internal_func = mocker.patch.object(ReceiptStorage, 'set_receipt')

    receipt_controller = ReceiptController(ReceiptStorage())
    chocolate = ModelControllerIngredient(id=-1, name='choco', calories=100)
    sugar = ModelControllerIngredient(id=-1, name='sugar', calories=100)

    cake = ModelControllerReceipt(-1, 'cake', 'Good receipt',
                                  [chocolate, sugar])

    receipt_controller.set_receipt(cake)

    mocked_internal_func.assert_called_with(cake)
Esempio n. 5
0
def delete_receipt(controller: ReceiptController, index):
    """
    Delete receipt
    :param controller:
    :param index: receipt id to be deleted
    :return: status message
    """
    try:
        controller.delete_receipt(index)
    except Exception as e:
        print(e)
        return Response("Error", status=500, mimetype='application/json')

    return Response("Ok", status=200, mimetype='application/json')
Esempio n. 6
0
def update_receipt(controller: ReceiptController):
    """
    Update a receipt
    :param controller:
    :return:
    """
    try:
        content = json.dumps(request.json)
        receipt = json2obj(content)
        controller.update_receipt(receipt)
    except Exception as e:
        print(e)
        return Response("Error", status=500, mimetype='application/json')

    return Response("Ok", status=200, mimetype='application/json')
Esempio n. 7
0
def update_ingredient(controller: ReceiptController):
    """
    Update ingredient data
    :param controller:
    :return: status message
    """
    try:
        content = json.dumps(request.json)
        ingredient = json.loads(
            content, object_hook=lambda d: ModelControllerIngredient(**d))
        controller.update_ingredient(ingredient)
    except Exception as e:
        print(e)
        return Response("Error", status=500, mimetype='application/json')

    return Response("Ok", status=200, mimetype='application/json')
Esempio n. 8
0
def test_get_receipt(mocker):
    mocked_internal_func = mocker.patch.object(ReceiptStorage, 'get_receipt')

    chocolate = ModelControllerIngredient(id=-1, name='choco', calories=100)
    sugar = ModelControllerIngredient(id=-1, name='sugar', calories=100)
    cake = ModelControllerReceipt(-1, 'cake', 'Good receipt',
                                  [chocolate, sugar])

    mocked_internal_func.return_value = cake

    receipt_controller = ReceiptController(ReceiptStorage())

    sugar_id = 1
    read_receipt = receipt_controller.get_receipt(sugar_id)

    mocked_internal_func.assert_called_with(sugar_id)
    assert cake == read_receipt
Esempio n. 9
0
def get_receipt(controller: ReceiptController, index):
    """
    Get receipts based on receipt id
    :param controller:
    :param index:
    :return: Return response with JSON object found
    """
    try:
        receipt = controller.get_receipt(index)
        response = receipt.to_json()
    except Exception as e:
        print(e)
        return Response("Error", status=500, mimetype='application/json')

    return response
Esempio n. 10
0
def get_ingredient(controller: ReceiptController, index):
    """
    Get ingredient
    :param controller:
    :param index: ingredient id to be obtained
    :return: Return response with JSON object found
    """
    try:
        receipt = controller.get_ingredient(index)
        response = receipt.to_json()
    except Exception as e:
        print(e)
        return Response("Error", status=500, mimetype='application/json')

    return response
Esempio n. 11
0
def set_receipt(controller: ReceiptController):
    """
    Post a receipt
    :param controller:
    :return: receipt id
    """
    try:
        content = json.dumps(request.json)
        receipt = json2obj(content)
        receipt_id = controller.set_receipt(receipt)
        response = {'id': receipt_id}
    except Exception as e:
        print(e)
        return Response("Error", status=500, mimetype='application/json')

    return jsonify(response)
Esempio n. 12
0
def get_receipt_calories(controller: ReceiptController, index):
    """
    Get calories from receipt
    :param controller:
    :param index: receipt id to get calories from
    :return: res containing calories per 100 g
    """
    try:
        receipt = controller.get_receipt(index)
        calories = receipt.get_calories()
        response = {'res': calories}
    except Exception as e:
        print(e)
        return Response("Error", status=500, mimetype='application/json')

    return response
Esempio n. 13
0
def set_ingredient(controller: ReceiptController):
    """
    Post ingredient from json data
    :param controller:
    :return: status message
    """
    try:
        content = json.dumps(request.json)
        ingredient = json.loads(
            content, object_hook=lambda d: ModelControllerIngredient(**d))
        ingredient_id = controller.set_ingredient(ingredient)
        response = {'id': ingredient_id}
    except Exception as e:
        print(e)
        return Response("Error", status=500, mimetype='application/json')

    return jsonify(response)