Exemple #1
0
def init_data(db_session):
    global __recipe, __group_one, __group_two
    """ Setup one recipe and two groups for the tests"""
    __recipe = data.Recipe(title="Test Recipe")
    db_session.add(__recipe)
    db_session.commit()

    position_one = data.IngredientListEntry.get_position_for_new_group(
        db_session, recipe=__recipe)
    ingredient_one = data.Ingredient("Group One", True)
    db_session.add(ingredient_one)
    db_session.commit()
    __group_one = __add_group(db_session,
                              recipe=__recipe,
                              group=ingredient_one,
                              position=position_one)

    position_two = data.IngredientListEntry.get_position_for_new_group(
        db_session, recipe=__recipe)
    ingredient_two = data.Ingredient("Group Two", True)
    db_session.add(ingredient_two)
    db_session.commit()
    __group_two = __add_group(db_session,
                              recipe=__recipe,
                              group=ingredient_two,
                              position=position_two)

    yield

    for table in (data.Recipe, data.Ingredient, data.IngredientListEntry):
        cleanup(db_session, table)
def test_append_group(db_session):
    """ Add a group to a recipe """

    assert db_session.query(data.Recipe).count() == 0
    assert db_session.query(data.Ingredient).count() == 0

    test_recipe = __setup_recipe(db_session)
    assert db_session.query(data.Recipe).count() == 1
    assert len(test_recipe.ingredientlist) == 0

    the_group = data.Ingredient("For the sauce", True)
    db_session.add(the_group)
    db_session.commit()
    assert db_session.query(data.Ingredient).count() == 1

    position = data.IngredientListEntry.get_position_for_new_group(
        db_session, recipe=test_recipe)
    __add_group(db_session,
                recipe=test_recipe,
                group=the_group,
                position=position)
    assert len(test_recipe.ingredientlist) == 1
    assert test_recipe.ingredientlist[0].position == 0

    # Now add a second and third group to the recipe
    second_group = data.Ingredient("My second group", True)
    db_session.add(second_group)
    db_session.commit()
    second_position = data.IngredientListEntry.get_position_for_new_group(
        db_session, recipe=test_recipe)
    __add_group(db_session,
                recipe=test_recipe,
                group=second_group,
                position=second_position)

    third_group = data.Ingredient("My third group", True)
    db_session.add(third_group)
    db_session.commit()
    third_position = data.IngredientListEntry.get_position_for_new_group(
        db_session, recipe=test_recipe)
    __add_group(db_session,
                recipe=test_recipe,
                group=third_group,
                position=third_position)

    assert db_session.query(data.Ingredient).count() == 3
    assert len(test_recipe.ingredientlist) == 3

    assert test_recipe.ingredientlist[0].ingredient == the_group
    assert test_recipe.ingredientlist[1].ingredient == second_group
    assert test_recipe.ingredientlist[2].ingredient == third_group
Exemple #3
0
def __setup_test_ingredient(session,
                            name: str = "Test Ingredient",
                            group: bool = False):
    ingredient = data.Ingredient(name=name, is_group=group)
    session.add(ingredient)
    session.commit()
    return ingredient
Exemple #4
0
def test_add_to_same_group(db_session):
    """ Now test adding the ingredient to the same group """

    ingredient_one = data.Ingredient("Pepper, red")
    ingredient_two = data.Ingredient("Pepper, green")
    ingredient_three = data.Ingredient("Pepper, orange")

    db_session.add(ingredient_one)
    db_session.add(ingredient_two)
    db_session.add(ingredient_three)
    db_session.commit()

    ingredient_counter = 2

    counter = 1

    for group in (None, __group_one, __group_two):
        counter = 1

        for ingredient in (ingredient_one, ingredient_two, ingredient_three):
            group_position = None

            if group is not None:
                group_position = group.position

            position = data.IngredientListEntry.get_position_for_ingredient(
                db_session, recipe=__recipe, parent=group_position)
            if group:
                expected = group.position + counter * data.IngredientListEntry.GROUP_INGREDIENT_FACTOR
            else:
                expected = counter * data.IngredientListEntry.GROUP_INGREDIENT_FACTOR \
                           + data.IngredientListEntry.GROUP_GLOBAL * data.IngredientListEntry.GROUP_FACTOR

            assert position == expected
            new_entry = data.IngredientListEntry(
                recipe=__recipe,
                unit=data.IngredientUnit.unit_group,
                ingredient=ingredient,
                position=position)
            # yes, the unit is wrong, but using this there's no need to create a dummy unit
            db_session.add(new_entry)
            db_session.commit()
            counter += 1
            ingredient_counter += 1
            assert len(__recipe.ingredientlist) == ingredient_counter
Exemple #5
0
def test_ingredient_uniqueness(db_session):
    """ Test the uniqueness of an ingredient"""

    test_one = "Pepper, red"
    test_two = "Apple, green"

    # Assert that the table is empty before performing test
    assert db_session.query(data.Ingredient).count() == 0

    ingredient_one = __setup_test_ingredient(db_session, test_one)
    ingredient_two = __setup_test_ingredient(db_session, test_two)
    assert db_session.query(data.Ingredient).count() == 2

    # There might be an ingredient AND a group with the same name (difficult to imagine, but there might be a valid
    # reason for this.
    ingredient_group = data.Ingredient(test_one, is_group=True)

    error = add_integrity(db_session, ingredient_group, False)

    if error:
        pytest.fail(error)
#
#  Qisit is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with qisit.  If not, see <https://www.gnu.org/licenses/>.

import pytest

from qisit.core.db import data
from . import cleanup, add_integrity

__recipe = data.Recipe("Test Recipe")
__ingredient = data.Ingredient("Iest Ingredient")


@pytest.fixture(autouse=True, scope="module")
def init_data(db_session):
    db_session.add(__recipe)
    db_session.add(__ingredient)
    db_session.commit()
    yield

    for table in (data.Recipe, data.Ingredient):
        cleanup(db_session, table)


@pytest.mark.parametrize("amount, range_amount, exception_expected",
                         ((None, None, False),