Exemplo n.º 1
0
def test_ingredient_refs(db_session):
    """ Test the backrefs"""

    global __recipe

    assert db_session.query(data.IngredientListEntry).count() == 0

    entry_1 = data.IngredientListEntry(recipe=__recipe, unit=data.IngredientUnit.unit_group, ingredient=__ingredient,
                                       name="Ingredient, green", position=1)
    entry_2 = data.IngredientListEntry(recipe=__recipe, unit=data.IngredientUnit.unit_group, ingredient=__ingredient,
                                       name="Ingredient, blue", position=2)

    db_session.add(entry_1)
    db_session.add(entry_2)
    db_session.commit()

    assert db_session.query(data.IngredientListEntry).count() == 2

    assert len(__recipe.ingredientlist) == 2
    assert len(__ingredient.recipes) == 1
    assert __ingredient.recipes[0] == __recipe
    assert entry_1.recipe == __recipe
    assert entry_2.recipe == __recipe

    # Delete the recipe -> auto delete should be in progress

    db_session.delete(__recipe)
    db_session.commit()

    assert db_session.query(data.IngredientListEntry).count() == 0
    assert len(__ingredient.recipes) == 0

    __recipe = data.Recipe("Test Recipe")
    cleanup(db_session, data.IngredientListEntry)
Exemplo n.º 2
0
def test_unique_position(db_session):
    """ A position has to be unique for a given recipe """
    assert db_session.query(data.IngredientListEntry).count() == 0

    entry_1 = data.IngredientListEntry(recipe=__recipe, unit=data.IngredientUnit.unit_group, ingredient=__ingredient,
                                       name="Ingredient, green", position=15)
    entry_2 = data.IngredientListEntry(recipe=__recipe, unit=data.IngredientUnit.unit_group, ingredient=__ingredient,
                                       name="Ingredient, blue", position=15)

    db_session.add(entry_1)

    error = add_integrity(db_session, entry_2, True)
    cleanup(db_session, data.IngredientListEntry)

    if error:
        pytest.fail(error)
def __add_group(session, recipe: data.Recipe, group: data.Ingredient,
                position: int):
    """ Convenience method """
    new_entry = data.IngredientListEntry(recipe=recipe,
                                         ingredient=group,
                                         position=position,
                                         unit=data.IngredientUnit.unit_group)
    session.add(new_entry)
    session.commit()
Exemplo n.º 4
0
def test_position(db_session, position: int, exception_expected: bool):
    """ Test valid positions """
    assert db_session.query(data.IngredientListEntry).count() == 0

    entry = data.IngredientListEntry(recipe=__recipe, unit=data.IngredientUnit.unit_group, ingredient=__ingredient,
                                     position=position)

    error = add_integrity(db_session, entry, exception_expected)

    # cleanup(db_session, data.IngredientList)
    if error:
        pytest.fail(error)
Exemplo n.º 5
0
def test_amounts(db_session, amount: float, range_amount: float, exception_expected):
    """ Test the validity of the amounts / range """

    assert db_session.query(data.IngredientListEntry).count() == 0

    entry = data.IngredientListEntry(recipe=__recipe, unit=data.IngredientUnit.unit_group, ingredient=__ingredient,
                                     amount=amount, range_amount=range_amount)

    error = add_integrity(db_session, entry, exception_expected)
    cleanup(db_session, data.IngredientListEntry)
    if error:
        pytest.fail(error)
Exemplo n.º 6
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
Exemplo n.º 7
0
def test_auto_delete(db_session):
    """ Test if there's a RESTRICT when deleting the ingredient """
    assert db_session.query(data.IngredientListEntry).count() == 0

    entry = data.IngredientListEntry(recipe=__recipe, unit=data.IngredientUnit.unit_group, ingredient=__ingredient)

    db_session.add(entry)
    db_session.commit()

    error = None

    try:
        db_session.delete(__ingredient)
        db_session.commit()
        error = "No exception raised"
    except Exception as e:
        pass
    finally:
        db_session.rollback()

    cleanup(db_session, data.IngredientListEntry)
    if error:
        pytest.fail(error)
Exemplo n.º 8
0
    def __import_ingredients(self, gourmet_ingredient: gdata.Ingredients,
                             qisit_recipe: data.Recipe):
        """
        Import an ingredient into a recipe

        Args:
            gourmet_ingredient (): Gourmet's ingredient
            qisit_recipe (): The recipe to import to

        Returns:

        """
        _translate = self._translate

        # Test if the ingredient is part of a group
        gourmet_inggroup = nullify(gourmet_ingredient.inggroup)
        group_item = None

        if gourmet_inggroup:
            # The ingredient is part of the group stored in gourmet_inggroup
            group = data.Ingredient.get_or_add_ingredient(self._qisit,
                                                          gourmet_inggroup,
                                                          is_group=True)

            # Find out if there's already a group with the name in the ingredient list. Note: assumes that there's
            # only one group (or none) with the name stored in gourmet_inggroup. This is a safe assumption, two
            # (or more) groups of ingredients with the same name make no sense, although Qisit's db design would
            # theoretically allow this (there's no way to prevent it with a simple CHECK constraint)
            group_item = self._qisit.query(data.IngredientListEntry).filter(
                data.IngredientListEntry.recipe == qisit_recipe,
                data.IngredientListEntry.ingredient == group).first()
            if not group_item:
                # New group for the recipe
                group_position = data.IngredientListEntry.get_position_for_new_group(
                    self._qisit, qisit_recipe)
                group_item = data.IngredientListEntry(
                    recipe=qisit_recipe,
                    ingredient=group,
                    unit=data.IngredientUnit.unit_group,
                    position=group_position)
                self._qisit.add(group_item)
                self._qisit.merge(group_item)

        # Convert the ingredient data
        qisit_amount = gourmet_ingredient.amount
        qisit_range_amount = gourmet_ingredient.rangeamount

        # For an empty (or None/NULL) unit there's a special unit/unit_name: the base unit, singular ""
        if not gourmet_ingredient.unit:
            gourmet_unit_name = ""
        else:
            gourmet_unit_name = gourmet_ingredient.unit.strip()

        qisit_ingredient_unit = None

        if gourmet_unit_name in data.IngredientUnit.unit_dict:
            qisit_ingredient_unit = data.IngredientUnit.unit_dict[
                gourmet_unit_name]
        else:
            # A (yet) unknown unit. Well, time to take a guess - volume? mass? quantity? Probably unspecific
            qisit_ingredient_unit = data.IngredientUnit(
                type_=data.IngredientUnit.UnitType.UNSPECIFIC,
                name=gourmet_unit_name,
                factor=None,
                cldr=False,
                description=_translate("ImportGourmet",
                                       f"{gourmet_unit_name} (imported)"))
            self._qisit.add(qisit_ingredient_unit)
            self._qisit.merge(qisit_ingredient_unit)
            data.IngredientUnit.unit_dict[
                gourmet_unit_name] = qisit_ingredient_unit
            self._imported_ingredient_units += 1

        qisit_name = nullify(gourmet_ingredient.item)

        gourmet_ingkey = nullify(gourmet_ingredient.ingkey)

        if gourmet_ingkey is None:
            # Such an ingredient shouldn't be in the database. Unfortunately Gourmet doesn't check input
            # very thoroughly...
            if qisit_name:
                # This shouldn't be possible, but better safe than sorry
                gourmet_ingkey = qisit_name
            else:
                return

        group_position = None
        if group_item is not None:
            group_position = group_item.position
        qisit_ingredient = data.Ingredient.get_or_add_ingredient(
            self._qisit, gourmet_ingkey)
        qisit_position = data.IngredientListEntry.get_position_for_ingredient(
            self._qisit, recipe=qisit_recipe, parent=group_position)
        # Time to put everything together
        qisit_ingredient_list_entry = data.IngredientListEntry(
            recipe=qisit_recipe,
            unit=qisit_ingredient_unit,
            ingredient=qisit_ingredient,
            amount=qisit_amount,
            range_amount=qisit_range_amount,
            name=qisit_name,
            optional=gourmet_ingredient.optional,
            position=qisit_position)
        self._qisit.add(qisit_ingredient_list_entry)
        self._qisit.merge(qisit_ingredient_list_entry)