From 8bdc1564be8fda789b853f75a5fc1a9b77e618cf Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Mon, 20 Sep 2021 21:07:48 +0200 Subject: [PATCH] fix find profile query #51 --- ki/actions/seed.py | 11 +++++++---- ki/handlers/find_profiles.py | 5 +++-- ki/test/test_find_profiles_endpoint.py | 8 ++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/ki/actions/seed.py b/ki/actions/seed.py index 5140ea3..ff02112 100644 --- a/ki/actions/seed.py +++ b/ki/actions/seed.py @@ -139,12 +139,15 @@ def seed(dev: bool): freetext="1001010010111!!!", skills=[(Skill.skill_id_php, 5)]) - seed_user("jutta", - visible=True, - languages=[("fr", 5)], - skills=[(Skill.skill_id_php, 3), (Skill.skill_id_mysql, 4)]) + all_skills = Skill.query.all() + all_profile_skills = [] + for skill in all_skills: + all_profile_skills.append((skill.id, 3)) + + seed_user("jutta", visible=True, languages=[("fr", 5)], skills=all_profile_skills) seed_user("giesela", visible=True, skills=[(Skill.skill_id_mysql, 3), (Skill.skill_id_postgresql, 5)]) seed_user("bertha", visible=False, skills=[(Skill.skill_id_sqlite, 3), (Skill.skill_id_postgresql, 5)]) + seed_user("monique", visible=True, languages=[("fr", 4)]) db.session.commit() diff --git a/ki/handlers/find_profiles.py b/ki/handlers/find_profiles.py index a4063bf..1a1b007 100644 --- a/ki/handlers/find_profiles.py +++ b/ki/handlers/find_profiles.py @@ -18,7 +18,8 @@ def find_profiles(): if page_size > 100: return make_response({"messages": {"page_size": "Die maximale Anzahl Einträge pro Seite beträgt 100"}}, 400) - query = Profile.query.filter(Profile.visible.is_(True)) \ + query = Profile.query.distinct(Profile.id) \ + .filter(Profile.visible.is_(True)) \ .join(Profile.skills, isouter=True).join(ProfileSkill.skill, isouter=True) \ .join(Profile.languages, isouter=True).join(ProfileLanguage.language, isouter=True) @@ -32,7 +33,7 @@ def find_profiles(): nickname = request.args.get("nickname") query = query.filter(Profile.nickname.like(f"%{nickname}%")) - count = query.distinct(Profile.id).count() + count = query.count() offset = (page - 1) * page_size db_profiles = query.limit(page_size).offset(offset).all() diff --git a/ki/test/test_find_profiles_endpoint.py b/ki/test/test_find_profiles_endpoint.py index aad5c78..a87181d 100644 --- a/ki/test/test_find_profiles_endpoint.py +++ b/ki/test/test_find_profiles_endpoint.py @@ -36,6 +36,9 @@ class TestFindProfilesEndpoint(ApiTest): self.assertEqual(response.status_code, 200) self.assertDictContainsSubset({"total": 4}, response.json) self.assertDictContainsSubset({"nickname": "dirtydieter"}, response.json["profiles"][0]) + self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][1]) + self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][2]) + self.assertDictContainsSubset({"nickname": "monique"}, response.json["profiles"][3]) def test_find_dieter(self): token = self.login("peter", "geheim")["token"] @@ -67,8 +70,9 @@ class TestFindProfilesEndpoint(ApiTest): response = self.client.get("/users/profiles?search=post", headers={"Authorization": "Bearer " + token}) self.assertEqual(response.status_code, 200) - self.assertDictContainsSubset({"total": 1}, response.json) - self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][0]) + self.assertDictContainsSubset({"total": 2}, response.json) + self.assertDictContainsSubset({"nickname": "jutta"}, response.json["profiles"][0]) + self.assertDictContainsSubset({"nickname": "giesela"}, response.json["profiles"][1]) def test_find_php_franzosen(self): token = self.login("peter", "geheim")["token"]