Exemplo n.º 1
0
def test_adding_ingredients_that_are_merged_sets_the_amount_to_the_sum_of_all_quantities(
):
    type_mass = UnitTypeFactory(name="masse")
    unit_kg = UnitFactory(type=type_mass, abbreviation="kg", rapport=1000)
    unit_mg = UnitFactory(type=type_mass, abbreviation="mg", rapport=0.001)
    unit_g = UnitFactory(type=type_mass, abbreviation="g", rapport=1)
    ingredient = IngredientFactory()
    FridgeIngredientFactory(
        ingredient=ingredient,
        amount=1250,
        unit=unit_g,
        expiration_date=datetime.date(2030, 7, 20),
    )
    FridgeIngredientFactory(
        ingredient=ingredient,
        amount=12,
        unit=unit_kg,
        expiration_date=datetime.date(2030, 7, 20),
    )
    FridgeIngredientFactory(
        ingredient=ingredient,
        amount=290125,
        unit=unit_mg,
        expiration_date=datetime.date(2030, 7, 20),
    )
    assert float(FridgeIngredient.objects.first().amount) == 13.54
    assert FridgeIngredient.objects.first().unit.abbreviation == "kg"
def _dataset_for_fridge_recipes_tests():
    masse = UnitTypeFactory(name="masse")
    gramme = UnitFactory(abbreviation="g", rapport=1, type=masse)
    carottes = IngredientFactory(name="Carottes")
    tomates = IngredientFactory(name="Tomates")
    oignons = IngredientFactory(name="Oignons")
    FridgeIngredientFactory(
        ingredient=carottes,
        amount=500,
        unit=gramme,
        expiration_date=datetime.date(2130, 1, 1),
    )
    FridgeIngredientFactory(
        ingredient=tomates,
        amount=50,
        unit=gramme,
        expiration_date=datetime.date(2130, 2, 2),
    )
    FridgeIngredientFactory(
        ingredient=oignons,
        amount=60,
        unit=gramme,
        expiration_date=datetime.date(2131, 1, 1),
    )
    return carottes, tomates, oignons, gramme, masse
def test_get_fridge_recipes_returns_recipes_that_have_unsure_ingredients():
    """Unsure ingredients are ingredients that have a unit type different from
    the recipe's ingredient unit type. The conversion between two different unit
    types has not been implemented yet, so the feasibility of the recipe is not sure.
    """  # noqa: D400, D205
    carottes, tomates, _, gramme, _ = _dataset_for_fridge_recipes_tests()
    pieces_type = UnitTypeFactory(name="pièce(s)")
    pieces = UnitFactory(abbreviation="pièce(s)", rapport=1, type=pieces_type)
    navet = IngredientFactory(name="Navet")
    FridgeIngredientFactory(ingredient=navet, amount=1, unit=pieces)
    ingredients_recipes1 = [
        RecipeIngredientFactory(ingredient=carottes, amount=500, unit=gramme),
        RecipeIngredientFactory(ingredient=tomates, amount=50, unit=gramme),
        RecipeIngredientFactory(ingredient=navet, amount=400, unit=gramme),
    ]
    RecipeFactory(ingredients=ingredients_recipes1, title="Recipe 1")
    FridgeIngredientFactory(ingredient=navet, amount=200, unit=gramme)
    ingredients_recipes2 = [
        RecipeIngredientFactory(ingredient=carottes, amount=350, unit=gramme),
        RecipeIngredientFactory(ingredient=tomates, amount=40, unit=gramme),
        RecipeIngredientFactory(ingredient=navet, amount=400, unit=gramme),
    ]
    RecipeFactory(ingredients=ingredients_recipes2, title="Recipe 2")
    url = reverse("recipes_fridge_list")
    request = APIRequestFactory().get(url)
    response = FridgeRecipes.as_view()(request)
    assertContains(response, "Recipe 1")
    assertContains(response, "Recipe 2")
def test_get_fridge_recipes_returns_recipes_for_which_an_ingredient_has_different_units(
):
    carottes, tomates, _, gramme, masse = _dataset_for_fridge_recipes_tests()
    kg = UnitFactory(abbreviation="kg", rapport=1000, type=masse)
    navet = IngredientFactory(name="Navet")
    FridgeIngredientFactory(ingredient=navet,
                            amount=2,
                            unit=kg,
                            expiration_date=datetime.date(2030, 1, 1))
    FridgeIngredientFactory(
        ingredient=navet,
        amount=400,
        unit=gramme,
        expiration_date=datetime.date(2030, 2, 2),
    )
    ingredients_recipes1 = [
        RecipeIngredientFactory(ingredient=carottes, amount=500, unit=gramme),
        RecipeIngredientFactory(ingredient=tomates, amount=50, unit=gramme),
        RecipeIngredientFactory(ingredient=navet, amount=2400, unit=gramme),
    ]
    RecipeFactory(ingredients=ingredients_recipes1, title="Recipe 1")
    ingredients_recipes2 = [
        RecipeIngredientFactory(ingredient=carottes, amount=350, unit=gramme),
        RecipeIngredientFactory(ingredient=tomates, amount=40, unit=gramme),
        RecipeIngredientFactory(ingredient=navet, amount=2.4, unit=kg),
    ]
    RecipeFactory(ingredients=ingredients_recipes2, title="Recipe 2")
    url = reverse("recipes_fridge_list")
    request = APIRequestFactory().get(url)
    response = FridgeRecipes.as_view()(request)
    assertContains(response, "Recipe 1")
    assertContains(response, "Recipe 2")
def _dataset_for_fridge_recipes_tests():
    masse = UnitTypeFactory(name="masse")
    gramme = UnitFactory(abbreviation="g", rapport=1, type=masse)
    carottes = IngredientFactory(name="Carottes")
    tomates = IngredientFactory(name="Tomates")
    oignons = IngredientFactory(name="Oignons")
    return carottes, tomates, oignons, gramme, masse
Exemplo n.º 6
0
def test_adding_ingredients_that_can_be_merged_merges_them_into_one():
    """Two ingredients that have the same name, date of expiration and unit type
    should be merged into one instance, which should have its amount equal to
    the sum of the two previous quantities.
    """  # noqa: D400 D205
    type_mass = UnitTypeFactory(name="masse")
    unit_kg = UnitFactory(type=type_mass, abbreviation="kg", rapport=1000)
    unit_g = UnitFactory(type=type_mass, abbreviation="g", rapport=1)
    ingredient = IngredientFactory()
    FridgeIngredientFactory(ingredient=ingredient,
                            unit=unit_kg,
                            expiration_date=datetime.date(2030, 7, 20))
    FridgeIngredientFactory(ingredient=ingredient,
                            unit=unit_g,
                            expiration_date=datetime.date(2030, 7, 20))
    FridgeIngredientFactory(ingredient=ingredient,
                            unit=unit_kg,
                            expiration_date=datetime.date(2030, 7, 20))
    assert FridgeIngredient.objects.count() == 1
Exemplo n.º 7
0
def test_adding_mergeable_fridge_ingredient_returns_correct_data():
    IngredientFactory(name="deuxieme ingrédient")
    unit_type = UnitTypeFactory(name="masse")
    UnitFactory(abbreviation="g", rapport=1, type=unit_type)
    UnitFactory(abbreviation="kg", rapport=1000, type=unit_type)
    request_data = {
        "ingredient": "deuxieme ingrédient",
        "amount": "10.00",
        "expiration_date": "2020-07-20",
        "unit": "g",
    }
    url = _get_fridge_ingredients_list_absolute_url()
    request_post = APIRequestFactory().post(url, request_data)
    response_post = FridgeIngredientViewSet.as_view({"post": "create"})(request_post)
    assert response_post.status_code == 201
    expected_id = response_post.data["id"]
    expected_response = {
        "id": expected_id,
        "ingredient": "deuxieme ingrédient",
        "expiration_date": "2020-07-20",
        "amount": "10.00",
        "unit": "g",
    }
    assert response_post.data == expected_response
    request_data_mergeable = {
        "ingredient": "deuxieme ingrédient",
        "amount": "0.10",
        "expiration_date": "2020-07-20",
        "unit": "kg",
    }
    url = _get_fridge_ingredients_list_absolute_url()
    request_post = APIRequestFactory().post(url, request_data_mergeable)
    response_post = FridgeIngredientViewSet.as_view({"post": "create"})(request_post)
    assert response_post.status_code == 201
    expected_response = {
        "id": expected_id,
        "ingredient": "deuxieme ingrédient",
        "expiration_date": "2020-07-20",
        "amount": "0.11",
        "unit": "kg",
    }
    assert response_post.data == expected_response
Exemplo n.º 8
0
def test_adding_ingredients_that_cannot_be_merged_does_not_merge_them():
    type_mass = UnitTypeFactory(name="masse")
    type_volume = UnitTypeFactory(name="volume")
    unit_kg = UnitFactory(type=type_mass, abbreviation="kg", rapport=1000)
    unit_cl = UnitFactory(type=type_volume, abbreviation="cl", rapport=0.01)
    ingredient = IngredientFactory()
    ingredient2 = IngredientFactory()
    FridgeIngredientFactory(ingredient=ingredient,
                            unit=unit_kg,
                            expiration_date=datetime.date(2030, 7, 20))
    FridgeIngredientFactory(ingredient=ingredient2,
                            unit=unit_kg,
                            expiration_date=datetime.date(2030, 7, 20))
    FridgeIngredientFactory(ingredient=ingredient,
                            unit=unit_cl,
                            expiration_date=datetime.date(2030, 7, 20))
    FridgeIngredientFactory(ingredient=ingredient,
                            unit=unit_kg,
                            expiration_date=datetime.date(2031, 7, 20))
    assert FridgeIngredient.objects.count() == 4
Exemplo n.º 9
0
def test_adding_fridge_ingredient():
    IngredientFactory(name="premier ingrédient")
    UnitFactory(abbreviation="g")
    request_data = {
        "ingredient": "premier ingrédient",
        "amount": "10.00",
        "unit": "g",
        "expiration_date": "2020-07-20",
    }
    url = _get_fridge_ingredients_list_absolute_url()
    request_post = APIRequestFactory().post(url, request_data, format="json")
    assert FridgeIngredient.objects.count() == 0
    response_post = FridgeIngredientViewSet.as_view({"post": "create"})(request_post)
    assert response_post.status_code == 201
    assert FridgeIngredient.objects.count() == 1
def test_get_fridge_recipes_returns_correct_fields_with_unsure_ingredients():
    carottes, tomates, oignons, gramme, _ = _dataset_for_fridge_recipes_tests()
    pieces_type = UnitTypeFactory(name="pièce(s)")
    pieces = UnitFactory(abbreviation="pièce(s)", rapport=1, type=pieces_type)
    ingredients_recipes1 = [
        RecipeIngredientFactory(ingredient=carottes, amount=3, unit=pieces),
        RecipeIngredientFactory(ingredient=tomates, amount=50, unit=gramme),
        RecipeIngredientFactory(ingredient=oignons, amount=2, unit=pieces),
    ]
    recipe = RecipeFactory(ingredients=ingredients_recipes1, title="Recipe 1")
    url = reverse("recipes_fridge_list")
    request = APIRequestFactory().get(url)
    response = FridgeRecipes.as_view()(request)
    _check_recipe_fields(response, recipe)
    assert set(
        response.data[0]["unsure_ingredients"]) == {"Carottes", "Oignons"}
Exemplo n.º 11
0
def test_updating_recipe_deserializes_correctly_all_fields():
    masse = UnitTypeFactory(name="masse")
    UnitFactory(abbreviation="g", type=masse)
    ingredients, categories = [], []
    for _ in range(5):
        ingredients.append(RecipeIngredientFactory())
        categories.append(CategoryFactory())
    recipe = RecipeFactory(ingredients=ingredients[:3], categories=categories[:3])
    url = _get_recipes_detail_absolute_url(recipe.id)
    request_data = {
        "title": "titre modifié",
        "description": "description modifiée",
        "duration": "04:00",
        "ingredients": [
            {
                "ingredient": ingredients[0].ingredient.name,
                "amount": "5.0",
                "unit": "g",
            },
            {"ingredient": ingredients[4].ingredient.name, "amount": "2", "unit": "g"},
        ],
        "categories": [categories[4].name],
    }
    request_put = APIRequestFactory().put(url, data=request_data, format="json")
    response_put = RecipeViewSet.as_view({"put": "update"})(request_put, pk=recipe.id)
    assert response_put.status_code == 200
    recipe_modified = Recipe.objects.first()
    assert recipe_modified.title == "titre modifié"
    assert recipe_modified.description == "description modifiée"
    assert recipe_modified.duration == datetime.timedelta(seconds=14400)
    assert recipe_modified.ingredients.count() == 2
    recipe_modified.ingredients.get(
        ingredient__name=ingredients[0].ingredient.name,
        amount=5.0,
        unit__abbreviation="g",
    )
    recipe_modified.ingredients.get(
        ingredient__name=ingredients[4].ingredient.name,
        amount=2,
        unit__abbreviation="g",
    )
    assert recipe_modified.categories.count() == 1
    assert recipe_modified.categories.first().name == categories[4].name
Exemplo n.º 12
0
def test_adding_fridge_ingredient_deserializes_correctly_all_fields():
    IngredientFactory(name="deuxieme ingrédient")
    UnitFactory(abbreviation="g")
    request_data = {
        "ingredient": "deuxieme ingrédient",
        "amount": Decimal("10.00"),
        "expiration_date": datetime.date(2020, 7, 20),
        "unit": "g",
    }
    url = _get_fridge_ingredients_list_absolute_url()
    request_post = APIRequestFactory().post(url, request_data)
    response_post = FridgeIngredientViewSet.as_view({"post": "create"})(request_post)
    assert response_post.status_code == 201
    ingredient = FridgeIngredient.objects.first()
    assert isinstance(ingredient.id, uuid.UUID)
    assert ingredient.amount == Decimal("10.00")
    assert ingredient.expiration_date == datetime.date(2020, 7, 20)
    assert ingredient.ingredient.name == "deuxieme ingrédient"
    assert ingredient.unit.abbreviation == "g"
Exemplo n.º 13
0
def _add_recipe():
    CategoryFactory(name="dessert")
    IngredientFactory(name="premier ingrédient")
    IngredientFactory(name="deuxième ingrédient")
    IngredientFactory(name="troisième ingrédient")
    masse = UnitTypeFactory(name="masse")
    UnitFactory(abbreviation="kg", type=masse)
    request_data = {
        "title": "title recipe",
        "description": "description recipe",
        "duration": "00:03",
        "ingredients": [
            {"ingredient": "deuxième ingrédient", "amount": "10.0", "unit": "kg"}
        ],
        "categories": ["dessert"],
    }
    url = _get_recipes_list_absolute_url()
    request_post = APIRequestFactory().post(url, data=request_data, format="json")
    response_post = RecipeViewSet.as_view({"post": "create"})(request_post)
    assert response_post.status_code == 201