def test_update_receipt():
    receipt_storage = ReceiptStorage()
    receipt_storage.redo_db()

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

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

    cake_id = receipt_storage.set_receipt(cake)

    read_cake = receipt_storage.get_receipt(cake_id)
    read_cake.name = "updated_cake"
    read_cake.description = "Good receipt updated"

    read_cake.ingredients[0].name = "chocoUpdated"
    read_cake.ingredients[0].calories = 200

    receipt_storage.update_receipt(read_cake)

    updated_cake = receipt_storage.get_receipt(cake_id)

    assert read_cake.id == updated_cake.id
    assert read_cake.name == updated_cake.name
    assert read_cake.description == updated_cake.description

    assert len(read_cake.ingredients) == 2
    assert read_cake.ingredients[0].name == updated_cake.ingredients[0].name
    assert read_cake.ingredients[0].calories == updated_cake.ingredients[
        0].calories

    assert read_cake.ingredients[1].name == updated_cake.ingredients[1].name
    assert read_cake.ingredients[1].calories == updated_cake.ingredients[
        1].calories
Beispiel #2
0
def test_calculate_receipt_calories():
    chocolate = ModelControllerIngredient(id=-1, name='choco', calories=100)
    sugar = ModelControllerIngredient(id=-1, name='sugar', calories=333)

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

    assert cake.get_calories() == chocolate.calories + sugar.calories
def test_update_ingredient_ok(client, mocker):
    mocked_update_ingredient = mocker.patch.object(ReceiptController, 'update_ingredient')
    mocked_update_ingredient.return_value = 1

    milk = ModelControllerIngredient(id=1, name='milk', calories=100)

    response = client.put("/api/v1/ingredients/", data=milk.to_json(), content_type='application/json')

    # TODO it should check that imput parameter is milk but it returns a comparison error
    mocked_update_ingredient.assert_called()

    assert response.status_code == 200
Beispiel #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)
def test_delete_receipt():
    receipt_storage = ReceiptStorage()
    receipt_storage.redo_db()

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

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

    cake_id = receipt_storage.set_receipt(cake)

    read_cake = receipt_storage.delete_receipt(cake_id)
    assert read_cake is None
Beispiel #6
0
    def get_receipt(self, receipt_id):
        session = self.Session()

        try:
            read_receipt = session.query(Receipt) \
                .filter(Receipt.id == receipt_id) \
                .first()

            if read_receipt is not None:
                ingredients = []
                for ingredient in read_receipt.ingredients:
                    ingredients.append(ModelControllerIngredient(ingredient.id,
                                                                 ingredient.name,
                                                                 ingredient.calories))

                receipt = ModelControllerReceipt(read_receipt.id,
                                                 read_receipt.name,
                                                 read_receipt.description,
                                                 ingredients)
        except Exception as e:
            print(e)
            raise UnknownErrorInStorageSystem()
        finally:
            session.close()

        return receipt
def test_update_ingredient():
    receipt_storage = ReceiptStorage()
    receipt_storage.redo_db()

    ingredient = ModelControllerIngredient(id=-1, name='salt', calories=100)
    ingredient_id = receipt_storage.set_ingredient(ingredient)

    update_ingredient = ModelControllerIngredient(id=ingredient_id,
                                                  name='salt2',
                                                  calories=200)
    receipt_storage.update_ingredient(update_ingredient)
    read_ingredient = receipt_storage.get_ingredient(ingredient_id)

    assert read_ingredient.id == update_ingredient.id
    assert read_ingredient.name == update_ingredient.name
    assert read_ingredient.calories == update_ingredient.calories
Beispiel #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
def test_delete_ingredient():
    receipt_storage = ReceiptStorage()
    receipt_storage.redo_db()

    ingredient = ModelControllerIngredient(id=-1, name='salt', calories=100)
    ingredient_id = receipt_storage.set_ingredient(ingredient)
    receipt_storage.delete_ingredient(ingredient_id)
    read_ingredient = receipt_storage.get_ingredient(ingredient_id)
    assert read_ingredient is None
Beispiel #10
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)
Beispiel #11
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
def test_update_receipt_ok(client, mocker):
    mocked_update_receipt = mocker.patch.object(ReceiptController, 'update_receipt')
    mocked_update_receipt.return_value = 1

    brown_sugar = ModelControllerIngredient(1, 'brown_sugar', 100)
    rice_with_milk = ModelControllerReceipt(1, 'milk', 'Good receipt', [brown_sugar])

    response = client.put("/api/v1/receipts/", data=rice_with_milk.to_json(), content_type='application/json')

    # TODO it should check that imput parameter is rice_with_milk but it returns a comparison error
    mocked_update_receipt.assert_called()

    assert response.status_code == 200
Beispiel #13
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')
Beispiel #14
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)
Beispiel #15
0
    def get_ingredient(self, ingredient_id):
        session = self.Session()

        ingredient = None
        try:
            read_ingredient = session.query(Ingredient) \
                .filter(Ingredient.id == ingredient_id) \
                .first()

            if read_ingredient is not None:
                ingredient = ModelControllerIngredient(read_ingredient.id,
                                                       read_ingredient.name,
                                                       read_ingredient.calories)
        except Exception as e:
            print(e)
            raise UnknownErrorInStorageSystem()
        finally:
            session.close()

        return ingredient
def test_update_not_stored_ingredient_raise_error():
    receipt_storage = ReceiptStorage()
    ingredient = ModelControllerIngredient(id=-1, name='salt', calories=100)

    with pytest.raises(ElementNotPresent):
        receipt_storage.update_ingredient(ingredient)
## Email: 
## Status: development
##################################################

# pytest
import pytest
from pytest_mock import mocker

# modules
from app.main import app
from app.controller.receipt_controller import ReceiptController
from app.controller.model_controller_receipt import ModelControllerReceipt
from app.controller.model_controller_ingredient import ModelControllerIngredient


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

# TODO add error test in case controller raise exceptions
@pytest.fixture
def client(mocker):
    yield app.test_client() # tests run here


def test_get_api_ready(client):
    response = client.get("/")

    assert response.status_code == 200
    assert response.data == b"CookAPI ready!"