fix: switch filter_by statements to explicit expressions to properly resolve proxy attrs (#8814)

This commit is contained in:
Michael Genson authored and GitHub committed 2026-10-20 15:26:10 -05:00
1 parent a89f032abf
commit e4d70fb7a1
4 files changed
+35 -3

No files matched your search

+1 -1
View File
@@ -299,7 +299,7 @@ class RepositoryGeneric[Schema: MealieModel, Model: SqlAlchemyBase]:
def count_all(self, match_key=None, match_value=None) -> int:
q = select(func.count(self.model.id))
if None not in [match_key, match_value]:
q = q.filter_by(**{match_key: match_value})
q = q.where(getattr(self.model, match_key) == match_value)
return self.session.scalar(q)
def _count_attribute(
+2 -2
View File
@@ -92,9 +92,9 @@ class RepositoryHousehold(GroupRepositoryGeneric[HouseholdInDB, Household]):
def statistics(self, group_id: UUID4, household_id: UUID4) -> HouseholdStatistics:
def model_count(model: type[SqlAlchemyBase], *, filter_household: bool = True) -> int:
stmt = select(func.count(model.id)).filter_by(group_id=group_id)
stmt = select(func.count(model.id)).where(model.group_id == group_id)
if filter_household:
stmt = stmt.filter_by(household_id=household_id)
stmt = stmt.where(model.household_id == household_id)
return self.session.scalar(stmt)
return HouseholdStatistics(
@@ -87,3 +87,22 @@ def test_get_household_recipe_invalid_recipe(api_client: TestClient, unique_user
api_routes.households_self_recipes_recipe_slug(random_string()), headers=unique_user.token
)
assert response.status_code == 404
def test_get_household_statistics(api_client: TestClient, unique_user: TestUser, h2_user: TestUser):
def get_statistics(user: TestUser) -> dict:
response = api_client.get(api_routes.households_statistics, headers=user.token)
assert response.status_code == 200
return response.json()
before = get_statistics(unique_user)
h2_before = get_statistics(h2_user)
unique_user.repos.recipes.create(
Recipe(user_id=unique_user.user_id, group_id=UUID(unique_user.group_id), name=random_string())
)
after = get_statistics(unique_user)
h2_after = get_statistics(h2_user)
assert after["totalRecipes"] == before["totalRecipes"] + 1
assert h2_after["totalRecipes"] == h2_before["totalRecipes"]
@@ -1083,3 +1083,16 @@ def test_recipe_update_ignores_id_in_payload(unique_user: TestUser) -> None:
assert updated.id == recipe.id
assert updated.description == "changed"
def test_recipe_repo_count_all(
unique_db: AllRepositories, unfiltered_database: AllRepositories, unique_ids: tuple[str, str, str]
):
group_id, household_id, user_id = unique_ids
for _ in range(2):
unique_db.recipes.create(Recipe(group_id=group_id, user_id=user_id, name=random_string()))
# group_id is a column on recipes, household_id is an association proxy through the user
assert unfiltered_database.recipes.count_all("group_id", UUID(group_id)) == 2
assert unfiltered_database.recipes.count_all("household_id", UUID(household_id)) == 2
assert unfiltered_database.recipes.count_all("household_id", uuid4()) == 0