From d69bde1bdfb8de7b99b701ba6abbd6e2be349c10 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Mon, 9 Sep 2024 20:35:09 +0000 Subject: [PATCH] tests --- .../test_cookbook_repository.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/unit_tests/repository_tests/test_cookbook_repository.py diff --git a/tests/unit_tests/repository_tests/test_cookbook_repository.py b/tests/unit_tests/repository_tests/test_cookbook_repository.py new file mode 100644 index 00000000000..f465a6940e7 --- /dev/null +++ b/tests/unit_tests/repository_tests/test_cookbook_repository.py @@ -0,0 +1,53 @@ +from uuid import UUID + +import pytest +from slugify import slugify + +from mealie.schema.cookbook.cookbook import SaveCookBook +from tests.utils.factories import random_string +from tests.utils.fixture_schemas import TestUser + + +def cookbook_data(user: TestUser, **kwargs): + data = { + "name": random_string(), + "group_id": UUID(user.group_id), + "household_id": UUID(user.household_id), + } | kwargs + + return SaveCookBook(**data) + + +@pytest.mark.parametrize("use_create_many", [True, False]) +def test_create_cookbook_ignores_slug(unique_user: TestUser, use_create_many: bool): + bad_slug = random_string() + cb_data = cookbook_data(unique_user, slug=bad_slug) + if use_create_many: + result = unique_user.repos.cookbooks.create_many([cb_data]) + assert len(result) == 1 + cb = result[0] + else: + cb = unique_user.repos.cookbooks.create(cb_data) + assert cb.slug == slugify(cb.name) != bad_slug + + +@pytest.mark.parametrize("use_create_many", [True, False]) +def test_create_cookbook_duplicate_name(unique_user: TestUser, use_create_many: bool): + cb_1_data = cookbook_data(unique_user) + cb_2_data = cookbook_data(unique_user, name=cb_1_data.name) + + cb_1 = unique_user.repos.cookbooks.create(cb_1_data) + unique_user.repos.session.commit() + + if use_create_many: + result = unique_user.repos.cookbooks.create_many([cb_2_data]) + assert len(result) == 1 + cb_2 = result[0] + else: + cb_2 = unique_user.repos.cookbooks.create(cb_2_data) + + assert cb_1.id != cb_2.id + assert cb_1.group_id == cb_2.group_id + assert cb_1.household_id != cb_2.household_id + assert cb_1.name == cb_2.name + assert cb_1.slug != cb_2.slug