diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7f39dde2ae1..6dded69a39f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,5 +14,4 @@ repos: # Ruff version. rev: v0.3.5 hooks: - - id: ruff - id: ruff-format diff --git a/dev/code-generation/utils/template.py b/dev/code-generation/utils/template.py index a5ffeaa45e4..b752214755b 100644 --- a/dev/code-generation/utils/template.py +++ b/dev/code-generation/utils/template.py @@ -8,9 +8,7 @@ from rich.logging import RichHandler FORMAT = "%(message)s" -logging.basicConfig( - level=logging.INFO, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()] -) +logging.basicConfig(level=logging.INFO, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]) log = logging.getLogger("rich") @@ -69,9 +67,7 @@ def find_start_end(file_text: list[str], gen_id: str) -> tuple[int, int, str]: raise Exception("Could not find start and end of code generation block") if start > end: - raise Exception( - f"Start ({start=}) of code generation block is after end ({end=})" - ) + raise Exception(f"Start ({start=}) of code generation block is after end ({end=})") return start, end, indentation diff --git a/tests/unit_tests/repository_tests/test_pagination.py b/tests/unit_tests/repository_tests/test_pagination.py index 904b07da6bb..d8b5899bb80 100644 --- a/tests/unit_tests/repository_tests/test_pagination.py +++ b/tests/unit_tests/repository_tests/test_pagination.py @@ -56,9 +56,7 @@ def __lt__(self, other): return other.obj < self.obj -def get_label_position_from_label_id( - label_id: UUID4, label_settings: list[ShoppingListMultiPurposeLabelOut] -) -> int: +def get_label_position_from_label_id(label_id: UUID4, label_settings: list[ShoppingListMultiPurposeLabelOut]) -> int: for label_setting in label_settings: if label_setting.label_id == label_id: return label_setting.position @@ -101,9 +99,7 @@ def test_repository_pagination(database: AllRepositories, unique_user: TestUser) assert result.id not in seen -def test_pagination_response_and_metadata( - database: AllRepositories, unique_user: TestUser -): +def test_pagination_response_and_metadata(database: AllRepositories, unique_user: TestUser): group = database.groups.get_one(unique_user.group_id) assert group @@ -140,7 +136,9 @@ def test_pagination_guides(database: AllRepositories, unique_user: TestUser): seeder.seed_foods("en-US") foods_repo = database.ingredient_foods.by_group(unique_user.group_id) # type: ignore - foods_route = "/foods" # this doesn't actually have to be accurate, it's just a placeholder to test for query params + foods_route = ( + "/foods" # this doesn't actually have to be accurate, it's just a placeholder to test for query params + ) query = PaginationQuery(page=1, per_page=1) @@ -157,9 +155,7 @@ def test_pagination_guides(database: AllRepositories, unique_user: TestUser): assert last_page_of_results.previous is not None random_page = randint(2, first_page_of_results.total_pages - 1) - query = PaginationQuery( - page=random_page, per_page=1, filter_string="createdAt>2021-02-22" - ) + query = PaginationQuery(page=random_page, per_page=1, filter_string="createdAt>2021-02-22") random_page_of_results = foods_repo.page_all(query) random_page_of_results.set_pagination_guides(foods_route, query.model_dump()) @@ -179,27 +175,21 @@ def test_pagination_guides(database: AllRepositories, unique_user: TestUser): @pytest.fixture(scope="function") def query_units(database: AllRepositories, unique_user: TestUser): unit_1 = database.ingredient_units.create( - SaveIngredientUnit( - name="test unit 1", group_id=unique_user.group_id, use_abbreviation=True - ) + SaveIngredientUnit(name="test unit 1", group_id=unique_user.group_id, use_abbreviation=True) ) # wait a moment so we can test datetime filters time.sleep(0.25) unit_2 = database.ingredient_units.create( - SaveIngredientUnit( - name="test unit 2", group_id=unique_user.group_id, use_abbreviation=False - ) + SaveIngredientUnit(name="test unit 2", group_id=unique_user.group_id, use_abbreviation=False) ) # wait a moment so we can test datetime filters time.sleep(0.25) unit_3 = database.ingredient_units.create( - SaveIngredientUnit( - name="test unit 3", group_id=unique_user.group_id, use_abbreviation=False - ) + SaveIngredientUnit(name="test unit 3", group_id=unique_user.group_id, use_abbreviation=False) ) unit_ids = [unit.id for unit in [unit_1, unit_2, unit_3]] @@ -211,9 +201,7 @@ def query_units(database: AllRepositories, unique_user: TestUser): units_repo.delete(unit_id) -def test_pagination_filter_basic( - query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit] -): +def test_pagination_filter_basic(query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit]): units_repo = query_units[0] unit_2 = query_units[2] @@ -284,14 +272,10 @@ def test_pagination_filter_null(database: AllRepositories, unique_user: TestUser assert recipe_made.id in result_ids -def test_pagination_filter_in( - query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit] -): +def test_pagination_filter_in(query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit]): units_repo, unit_1, unit_2, unit_3 = query_units - query = PaginationQuery( - page=1, per_page=-1, query_filter=f"name IN [{unit_1.name}, {unit_2.name}]" - ) + query = PaginationQuery(page=1, per_page=-1, query_filter=f"name IN [{unit_1.name}, {unit_2.name}]") unit_results = units_repo.page_all(query).items result_ids = {unit.id for unit in unit_results} @@ -299,9 +283,7 @@ def test_pagination_filter_in( assert unit_2.id in result_ids assert unit_3.id not in result_ids - query = PaginationQuery( - page=1, per_page=-1, query_filter=f"name NOT IN [{unit_1.name}, {unit_2.name}]" - ) + query = PaginationQuery(page=1, per_page=-1, query_filter=f"name NOT IN [{unit_1.name}, {unit_2.name}]") unit_results = units_repo.page_all(query).items result_ids = {unit.id for unit in unit_results} @@ -309,9 +291,7 @@ def test_pagination_filter_in( assert unit_2.id not in result_ids assert unit_3.id in result_ids - query = PaginationQuery( - page=1, per_page=-1, query_filter=f'name IN ["{unit_3.name}"]' - ) + query = PaginationQuery(page=1, per_page=-1, query_filter=f'name IN ["{unit_3.name}"]') unit_results = units_repo.page_all(query).items result_ids = {unit.id for unit in unit_results} @@ -320,9 +300,7 @@ def test_pagination_filter_in( assert unit_3.id in result_ids -def test_pagination_filter_in_advanced( - database: AllRepositories, unique_user: TestUser -): +def test_pagination_filter_in_advanced(database: AllRepositories, unique_user: TestUser): slug1, slug2 = (random_string(10) for _ in range(2)) tags = [ @@ -377,9 +355,7 @@ def test_pagination_filter_in_advanced( ) ) - query = PaginationQuery( - page=1, per_page=-1, query_filter=f"tags.name IN [{tag_1.name}]" - ) + query = PaginationQuery(page=1, per_page=-1, query_filter=f"tags.name IN [{tag_1.name}]") recipe_results = database.recipes.page_all(query).items assert len(recipe_results) == 2 recipe_ids = {recipe.id for recipe in recipe_results} @@ -388,9 +364,7 @@ def test_pagination_filter_in_advanced( assert recipe_2.id not in recipe_ids assert recipe_1_2.id in recipe_ids - query = PaginationQuery( - page=1, per_page=-1, query_filter=f"tags.name IN [{tag_1.name}, {tag_2.name}]" - ) + query = PaginationQuery(page=1, per_page=-1, query_filter=f"tags.name IN [{tag_1.name}, {tag_2.name}]") recipe_results = database.recipes.page_all(query).items assert len(recipe_results) == 3 recipe_ids = {recipe.id for recipe in recipe_results} @@ -413,9 +387,7 @@ def test_pagination_filter_in_advanced( assert recipe_1_2.id in recipe_ids -def test_pagination_filter_like( - query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit] -): +def test_pagination_filter_like(query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit]): units_repo, unit_1, unit_2, unit_3 = query_units query = PaginationQuery(page=1, per_page=-1, query_filter=r'name LIKE "test u_it%"') @@ -446,9 +418,7 @@ def test_pagination_filter_like( assert unit_3.id in result_ids -def test_pagination_filter_keyword_namespace_conflict( - database: AllRepositories, unique_user: TestUser -): +def test_pagination_filter_keyword_namespace_conflict(database: AllRepositories, unique_user: TestUser): recipe_rating_1 = database.recipes.create( Recipe( user_id=unique_user.user_id, @@ -497,16 +467,12 @@ def test_pagination_filter_keyword_namespace_conflict( assert recipe_rating_3.id in result_ids -def test_pagination_filter_logical_namespace_conflict( - database: AllRepositories, unique_user: TestUser -): +def test_pagination_filter_logical_namespace_conflict(database: AllRepositories, unique_user: TestUser): categories = [ CategorySave(group_id=unique_user.group_id, name=random_string(10)), CategorySave(group_id=unique_user.group_id, name=random_string(10)), ] - category_1, category_2 = [ - database.categories.create(category) for category in categories - ] + category_1, category_2 = [database.categories.create(category) for category in categories] # Bootstrap the database with recipes slug = random_string() @@ -542,9 +508,7 @@ def test_pagination_filter_logical_namespace_conflict( ) # "recipeCategory" has the substring "or" in it, which shouldn't break queries - query = PaginationQuery( - page=1, per_page=-1, query_filter=f'recipeCategory.id = "{category_1.id}"' - ) + query = PaginationQuery(page=1, per_page=-1, query_filter=f'recipeCategory.id = "{category_1.id}"') recipe_results = database.recipes.by_group(unique_user.group_id).page_all(query).items # type: ignore assert len(recipe_results) == 1 recipe_ids = {recipe.id for recipe in recipe_results} @@ -770,21 +734,14 @@ def test_pagination_order_by_nested_model( alphabet = ["a", "b", "c", "d", "e"] labels = database.group_multi_purpose_labels.create_many( [ - MultiPurposeLabelSave( - group_id=unique_user.group_id, name=letter + f"_{random_string()}" - ) + MultiPurposeLabelSave(group_id=unique_user.group_id, name=letter + f"_{random_string()}") for letter in alphabet ] ) random.shuffle(labels) sorted_foods = database.ingredient_foods.create_many( - [ - SaveIngredientFood( - group_id=unique_user.group_id, name=random_string(), label_id=label.id - ) - for label in labels - ] + [SaveIngredientFood(group_id=unique_user.group_id, name=random_string(), label_id=label.id) for label in labels] ) sorted_foods.sort(key=lambda x: x.label.name, reverse=order_direction is OrderDirection.desc) # type: ignore @@ -801,18 +758,14 @@ def test_pagination_order_by_nested_model( assert query.items == sorted_foods -def test_pagination_order_by_doesnt_filter( - database: AllRepositories, unique_user: TestUser -): +def test_pagination_order_by_doesnt_filter(database: AllRepositories, unique_user: TestUser): current_time = datetime.now() label = database.group_multi_purpose_labels.create( MultiPurposeLabelSave(name=random_string(), group_id=unique_user.group_id) ) food_with_label = database.ingredient_foods.create( - SaveIngredientFood( - name=random_string(), label_id=label.id, group_id=unique_user.group_id - ) + SaveIngredientFood(name=random_string(), label_id=label.id, group_id=unique_user.group_id) ) food_without_label = database.ingredient_foods.create( SaveIngredientFood(name=random_string(), group_id=unique_user.group_id) @@ -858,9 +811,7 @@ def test_pagination_order_by_nulls( MultiPurposeLabelSave(name=random_string(), group_id=unique_user.group_id) ) food_with_label = database.ingredient_foods.create( - SaveIngredientFood( - name=random_string(), label_id=label.id, group_id=unique_user.group_id - ) + SaveIngredientFood(name=random_string(), label_id=label.id, group_id=unique_user.group_id) ) food_without_label = database.ingredient_foods.create( SaveIngredientFood(name=random_string(), group_id=unique_user.group_id) @@ -885,9 +836,7 @@ def test_pagination_order_by_nulls( assert query.items[1] == food_without_label -def test_pagination_shopping_list_items_with_labels( - database: AllRepositories, unique_user: TestUser -): +def test_pagination_shopping_list_items_with_labels(database: AllRepositories, unique_user: TestUser): # create a shopping list and populate it with some items with labels, and some without labels shopping_list = database.group_shopping_lists.create( ShoppingListSave( @@ -898,18 +847,13 @@ def test_pagination_shopping_list_items_with_labels( ) labels = database.group_multi_purpose_labels.create_many( - [ - MultiPurposeLabelSave(name=random_string(), group_id=unique_user.group_id) - for _ in range(8) - ] + [MultiPurposeLabelSave(name=random_string(), group_id=unique_user.group_id) for _ in range(8)] ) random.shuffle(labels) label_settings = database.shopping_list_multi_purpose_labels.create_many( [ - ShoppingListMultiPurposeLabelCreate( - shopping_list_id=shopping_list.id, label_id=label.id, position=i - ) + ShoppingListMultiPurposeLabelCreate(shopping_list_id=shopping_list.id, label_id=label.id, position=i) for i, label in enumerate(labels) ] ) @@ -936,9 +880,7 @@ def test_pagination_shopping_list_items_with_labels( ) ) - without_labels_positions = list( - range(len(with_labels_positions), random_int(5, 10)) - ) + without_labels_positions = list(range(len(with_labels_positions), random_int(5, 10))) random.shuffle(without_labels_positions) items_without_labels = database.group_shopping_list_item.create_many( [ @@ -972,9 +914,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): tomorrow = date.today() + timedelta(days=1) day_after_tomorrow = date.today() + timedelta(days=2) - mealplan_today = CreatePlanEntry( - date=today, entry_type="breakfast", title=random_string(), text=random_string() - ) + mealplan_today = CreatePlanEntry(date=today, entry_type="breakfast", title=random_string(), text=random_string()) mealplan_tomorrow = CreatePlanEntry( date=tomorrow, entry_type="breakfast", @@ -985,9 +925,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): for mealplan_to_create in [mealplan_today, mealplan_tomorrow]: data = mealplan_to_create.model_dump() data["date"] = data["date"].strftime("%Y-%m-%d") - response = api_client.post( - api_routes.groups_mealplans, json=data, headers=unique_user.token - ) + response = api_client.post(api_routes.groups_mealplans, json=data, headers=unique_user.token) assert response.status_code == 201 ## Yesterday @@ -996,9 +934,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): "perPage": -1, "queryFilter": f"date >= {yesterday.strftime('%Y-%m-%d')}", } - response = api_client.get( - api_routes.groups_mealplans, params=params, headers=unique_user.token - ) + response = api_client.get(api_routes.groups_mealplans, params=params, headers=unique_user.token) assert response.status_code == 200 response_json = response.json() @@ -1012,9 +948,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): "perPage": -1, "queryFilter": f"date > {yesterday.strftime('%Y-%m-%d')}", } - response = api_client.get( - api_routes.groups_mealplans, params=params, headers=unique_user.token - ) + response = api_client.get(api_routes.groups_mealplans, params=params, headers=unique_user.token) assert response.status_code == 200 response_json = response.json() @@ -1029,9 +963,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): "perPage": -1, "queryFilter": f"date >= {today.strftime('%Y-%m-%d')}", } - response = api_client.get( - api_routes.groups_mealplans, params=params, headers=unique_user.token - ) + response = api_client.get(api_routes.groups_mealplans, params=params, headers=unique_user.token) assert response.status_code == 200 response_json = response.json() @@ -1045,9 +977,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): "perPage": -1, "queryFilter": f"date > {today.strftime('%Y-%m-%d')}", } - response = api_client.get( - api_routes.groups_mealplans, params=params, headers=unique_user.token - ) + response = api_client.get(api_routes.groups_mealplans, params=params, headers=unique_user.token) assert response.status_code == 200 response_json = response.json() @@ -1062,9 +992,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): "perPage": -1, "queryFilter": f"date >= {tomorrow.strftime('%Y-%m-%d')}", } - response = api_client.get( - api_routes.groups_mealplans, params=params, headers=unique_user.token - ) + response = api_client.get(api_routes.groups_mealplans, params=params, headers=unique_user.token) assert response.status_code == 200 response_json = response.json() @@ -1078,9 +1006,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): "perPage": -1, "queryFilter": f"date > {tomorrow.strftime('%Y-%m-%d')}", } - response = api_client.get( - api_routes.groups_mealplans, params=params, headers=unique_user.token - ) + response = api_client.get(api_routes.groups_mealplans, params=params, headers=unique_user.token) assert response.status_code == 200 response_json = response.json() @@ -1092,9 +1018,7 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): "perPage": -1, "queryFilter": f"date >= {day_after_tomorrow.strftime('%Y-%m-%d')}", } - response = api_client.get( - api_routes.groups_mealplans, params=params, headers=unique_user.token - ) + response = api_client.get(api_routes.groups_mealplans, params=params, headers=unique_user.token) assert response.status_code == 200 response_json = response.json() assert len(response_json["items"]) == 0 @@ -1104,17 +1028,13 @@ def test_pagination_filter_dates(api_client: TestClient, unique_user: TestUser): "perPage": -1, "queryFilter": f"date > {day_after_tomorrow.strftime('%Y-%m-%d')}", } - response = api_client.get( - api_routes.groups_mealplans, params=params, headers=unique_user.token - ) + response = api_client.get(api_routes.groups_mealplans, params=params, headers=unique_user.token) assert response.status_code == 200 response_json = response.json() assert len(response_json["items"]) == 0 -def test_pagination_filter_booleans( - query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit] -): +def test_pagination_filter_booleans(query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit]): units_repo = query_units[0] unit_1 = query_units[1] @@ -1128,9 +1048,7 @@ def test_pagination_filter_booleans( assert unit_results[0].id == unit_1.id -def test_pagination_filter_advanced( - query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit] -): +def test_pagination_filter_advanced(query_units: tuple[RepositoryUnit, IngredientUnit, IngredientUnit, IngredientUnit]): units_repo, unit_1, unit_2, unit_3 = query_units dt = str(unit_3.created_at.isoformat()) # type: ignore @@ -1153,16 +1071,12 @@ def test_pagination_filter_advanced( assert unit_3.id not in result_ids -def test_pagination_filter_advanced_frontend_sort( - database: AllRepositories, unique_user: TestUser -): +def test_pagination_filter_advanced_frontend_sort(database: AllRepositories, unique_user: TestUser): categories = [ CategorySave(group_id=unique_user.group_id, name=random_string(10)), CategorySave(group_id=unique_user.group_id, name=random_string(10)), ] - category_1, category_2 = [ - database.categories.create(category) for category in categories - ] + category_1, category_2 = [database.categories.create(category) for category in categories] slug1, slug2 = (random_string(10) for _ in range(2)) tags = [ @@ -1320,26 +1234,18 @@ def test_pagination_filter_advanced_frontend_sort( @pytest.mark.parametrize( "qf", [ - pytest.param( - '(name="test name" AND useAbbreviation=f))', id="unbalanced parenthesis" - ), + pytest.param('(name="test name" AND useAbbreviation=f))', id="unbalanced parenthesis"), pytest.param('id="this is not a valid UUID"', id="invalid UUID"), pytest.param( 'createdAt="this is not a valid datetime format"', id="invalid datetime format", ), pytest.param('name IS "test name"', id="IS can only be used with NULL or NONE"), - pytest.param( - 'name IS NOT "test name"', id="IS NOT can only be used with NULL or NONE" - ), + pytest.param('name IS NOT "test name"', id="IS NOT can only be used with NULL or NONE"), pytest.param('name IN "test name"', id="IN must use a list of values"), pytest.param('name NOT IN "test name"', id="NOT IN must use a list of values"), - pytest.param( - 'name CONTAINS ALL "test name"', id="CONTAINS ALL must use a list of values" - ), - pytest.param( - 'createdAt LIKE "2023-02-25"', id="LIKE is only valid for string columns" - ), + pytest.param('name CONTAINS ALL "test name"', id="CONTAINS ALL must use a list of values"), + pytest.param('createdAt LIKE "2023-02-25"', id="LIKE is only valid for string columns"), pytest.param( 'createdAt NOT LIKE "2023-02-25"', id="NOT LIKE is only valid for string columns", @@ -1352,15 +1258,11 @@ def test_pagination_filter_advanced_frontend_sort( ), ], ) -def test_malformed_query_filters( - api_client: TestClient, unique_user: TestUser, qf: str -): +def test_malformed_query_filters(api_client: TestClient, unique_user: TestUser, qf: str): # verify that improper queries throw 400 errors route = "/api/units" - response = api_client.get( - route, params={"queryFilter": qf}, headers=unique_user.token - ) + response = api_client.get(route, params={"queryFilter": qf}, headers=unique_user.token) assert response.status_code == 400 @@ -1370,9 +1272,7 @@ def test_pagination_filter_nested(api_client: TestClient, user_tuple: list[TestU for i, user in enumerate(user_tuple): for _ in range(random_int(3, 5)): slug: str = random_string() - response = api_client.post( - api_routes.recipes, json={"name": slug}, headers=user.token - ) + response = api_client.post(api_routes.recipes, json={"name": slug}, headers=user.token) assert response.status_code == 201 slugs[i].append(slug) @@ -1401,9 +1301,7 @@ def test_pagination_filter_nested(api_client: TestClient, user_tuple: list[TestU "perPage": -1, "queryFilter": f'recipe.user.id="{user.user_id}"', } - response = api_client.get( - api_routes.recipes_timeline_events, params=params, headers=user.token - ) + response = api_client.get(api_routes.recipes_timeline_events, params=params, headers=user.token) assert response.status_code == 200 events_data: list[dict] = response.json()["items"]