Ejemplo n.º 1
0
def test_recipe_serialization():
    # Given
    recipe = Recipe("title", ["labelA", "labelB"],
                    [Ingredient("name", "g", 50)], "preparation")

    # When
    json_string = recipe.to_json()

    # Then
    assert "title" in json_string
    assert "labelA" in json_string
    assert "preparation" in json_string
Ejemplo n.º 2
0
def test_recipe_deserialization():
    # Given
    json_string = """{"py/object": "recipe_organizer.recipe.recipe.Recipe", "title": "title", "labels": ["labelA", "labelB"], "ingredients": [{"py/object": "recipe_organizer.recipe.ingredient.ingredient.Ingredient", "name": "name", "quantity_type": "g", "quantity": 50}], "preparation": "preparation"}"""

    # When
    actual: Recipe = Recipe.from_json(json_string)

    # Then
    assert actual.title == "title"
    assert actual.labels == ["labelA", "labelB"]
    assert actual.preparation == "preparation"
Ejemplo n.º 3
0
def test_save_event_handling(mocks):
    # Given
    recipe_selection = RecipeSelection()
    recipe_selection._selected_recipe_file = "path/to/file"

    recipe = Recipe("title", [], [], "preparation")

    with patch("builtins.open", mock_open(read_data="content")) as mock_file_open:
        file_instance = mock_file_open()
        # When
        recipe_selection.save_recipe_to_file(recipe)

        # Then
        file_instance.write.assert_called_once()
Ejemplo n.º 4
0
    def __load_recipes(self, directory: Path):
        recipes: [Recipe] = []
        file_paths = directory.glob("**/*.json")
        for file_path in file_paths:
            with open(file_path, "r", encoding="utf-8") as file:
                json_data = file.read()
                try:
                    recipe = Recipe.from_json(json_data)
                except KeyError:
                    pass
                else:
                    recipes.append(recipe)

        self.__create_list(recipes)
Ejemplo n.º 5
0
def test_recipe_selection_file_deserialization(mocks):
    # Given
    mock_path = "/path/to/json"

    recipe = Recipe("title", [], [], "preparation")
    mocks["mock_recipe"].from_json.return_value = recipe

    mocks["mock_filedialog"].askopenfilename.return_value = mock_path

    with patch("builtins.open", mock_open(read_data="content")) as mock_file_open:
        recipe_selection = RecipeSelection()

        # When
        recipe_selection.open_recipe_file()

        # Then
        mock_file_open.assert_called_once_with(mock_path, 'r', encoding='utf-8')
        mocks["mock_event_publisher"].broadcast.assert_called_once()
Ejemplo n.º 6
0
def test_recipe_handling(mocks):
    # Given
    parent = None

    ingredients = [
        Ingredient("ingredientA", "g", 50),
        Ingredient("ingredientB", "g", 50)
    ]
    recipe = Recipe("title", [], ingredients, "preparation")

    expected_ingredient_form_count = len(ingredients)
    expected_preparation_entries_count = 1

    patch.object(RecipeForm, "__init__", return_value=None)
    recipe_form = RecipeForm(parent)

    # When
    recipe_form.set_form_values(recipe)

    # Then
    assert mocks["mock_entry"].call_count == expected_preparation_entries_count
    assert mocks[
        "mock_ingredient_form"].call_count == expected_ingredient_form_count
Ejemplo n.º 7
0
    def __write_to_selected_file_and_publish(recipe: Recipe, file_path):
        with open(file_path, "w", encoding="utf-8") as file:
            json_data = recipe.to_json()
            file.write(json_data)

            EventPublisher.broadcast(Event(EventType.SAVED, payload=None))
Ejemplo n.º 8
0
    def __read_recipe_from_file(file_path: str) -> None:
        with open(file_path, "r", encoding="utf-8") as file:
            json_data = file.read()
            recipe = Recipe.from_json(json_data)

            EventPublisher.broadcast(Event(EventType.FILE_READ, payload=recipe))
Ejemplo n.º 9
0
 def get_recipe_from_form(self) -> Recipe:
     return Recipe(
         self._entry_title.get(), [],
         [form.get_ingredient() for form in self._ingredient_forms],
         self._text_preparation.get("1.0", END))