From 37f57eadeabd232051d0cf036a23254a3338353a Mon Sep 17 00:00:00 2001 From: jaschop <1k5o@tuta.io> Date: Fri, 20 Oct 2023 12:24:16 +0200 Subject: [PATCH 1/2] Refactored seed --dev script expanded seed_user to handle all user seeding reordered user seeding, which broke some tests made tests resistant to seeding order --- ki/actions/seed.py | 141 ++++++++++++++----------------- ki/test/test_profile_endpoint.py | 36 ++++---- 2 files changed, 83 insertions(+), 94 deletions(-) diff --git a/ki/actions/seed.py b/ki/actions/seed.py index e314a37..eeafdb1 100644 --- a/ki/actions/seed.py +++ b/ki/actions/seed.py @@ -25,22 +25,30 @@ def seed_contacttypes(): db.session.add(ContactType(id=int(contacttype["id"]), name=contacttype["name"])) -def seed_user(nickname, - visible=False, - skills=[], - languages=[], +def seed_user(auth_id, + nickname=None, + pronouns="", + visible=True, volunteerwork="", availability_status=False, freetext="", availability_text="", - availability_hours_per_week=42): + availability_hours_per_week=42, + skills=[], + searchtopics=[], + languages=[], + address=None, + contacts=[]): + if not nickname: + nickname = auth_id + app.logger.info(f"seeding {nickname} \\o/") - user = User(auth_id=nickname) + user = User(auth_id=auth_id) db.session.add(user) profile = Profile(nickname=nickname, - pronouns="", + pronouns=pronouns, volunteerwork=volunteerwork, availability_status=availability_status, availability_text=availability_text, @@ -53,13 +61,30 @@ def seed_user(nickname, skill = ProfileSkill(profile=profile, skill_id=skill_data[0], level=skill_data[1]) db.session.add(skill) + for skill_id in searchtopics: + searchtopic = ProfileSearchtopic(profile=profile, skill_id=skill_id) + db.session.add(searchtopic) + for language_data in languages: language = ProfileLanguage(profile=profile, language_id=language_data[0], level=language_data[1]) db.session.add(language) - db.session.add(profile) + if address: + _address = Address(name=address[0], + street=address[1], + house_number=address[2], + additional=address[3], + postcode=address[4], + city=address[5], + country=address[6], + profile=profile) + db.session.add(_address) - return profile + for contact_data in contacts: + contact = Contact(profile=profile, contacttype_id=contact_data[0], content=contact_data[1]) + db.session.add(contact) + + db.session.add(profile) def seed(dev: bool): @@ -94,80 +119,40 @@ def seed(dev: bool): db.session.add(Language(id=iso["639-1"], name=iso["Sprache"])) if dev: - app.logger.info("seeding peter :)") + seed_user("klaus", visible=False) - peter = User(auth_id="peter") - db.session.add(peter) - - peters_profile = Profile(nickname="peternichtlustig", - pronouns="Herr Dr. Dr.", - volunteerwork="Gartenverein", - availability_status=True, - availability_hours_per_week=42, - availability_text="Immer", - freetext="Ich mag Kaffee", - user=peter) - db.session.add(peters_profile) - - matrix_contact = Contact(profile=peters_profile, contacttype_id=4, content="@peter:wtf-eg.de") - db.session.add(matrix_contact) - - email_contact = Contact(profile=peters_profile, contacttype_id=1, content="peter@wtf-eg.de") - db.session.add(email_contact) - - peters_address = Address(name="Peter Nichtlustig", - street="Waldweg", - house_number="23i", - additional="Hinterhaus", - postcode="13337", - city="Bielefeld", - country="Deutschland", - profile=peters_profile) - db.session.add(peters_address) - - peters_python_skill = ProfileSkill(profile=peters_profile, skill_id=3, level=3) - db.session.add(peters_python_skill) - - peters_php_skill = ProfileSkill(profile=peters_profile, skill_id=1, level=5) - db.session.add(peters_php_skill) - - peters_python_searchtopic = ProfileSearchtopic(profile=peters_profile, skill_id=3) - db.session.add(peters_python_searchtopic) - - peters_php_searchtopic = ProfileSearchtopic(profile=peters_profile, skill_id=1) - db.session.add(peters_php_searchtopic) - - peter_de = ProfileLanguage(profile=peters_profile, language_id="de", level=5) - db.session.add(peter_de) - - peter_fr = ProfileLanguage(profile=peters_profile, language_id="fr", level=3) - db.session.add(peter_fr) - - seed_user("klaus") for i in range(1, 20): - seed_user(f"babsi{i}", visible=True) + seed_user(f"babsi{i}") - dieter = seed_user("dirtydieter", - visible=True, - volunteerwork="Müll sammeln", - availability_status=True, - availability_hours_per_week=24, - availability_text="Nur Nachts!", - freetext="1001010010111!!!", - skills=[(Skill.skill_id_php, 5)]) + seed_user("peter", + nickname="peternichtlustig", + visible=False, + pronouns="Herr Dr. Dr.", + volunteerwork="Gartenverein", + availability_status=True, + availability_hours_per_week=42, + availability_text="Immer", + freetext="Ich mag Kaffee", + skills=[(3, 3), (1, 5)], + searchtopics=[3, 1], + languages=[("de", 5), ("fr", 3)], + address=("Peter Nichtlustig", "Waldweg", "23i", "Hinterhaus", "13337", "Bielefeld", "Deutschland"), + contacts=[(4, "@peter:wtf-eg.de"), (1, "peter@wtf-eg.de")]) - dieters_address = Address(name="Friedrich Witzig", profile=dieter) - db.session.add(dieters_address) + seed_user("dirtydieter", + volunteerwork="Müll sammeln", + availability_status=True, + availability_hours_per_week=24, + availability_text="Nur Nachts!", + freetext="1001010010111!!!", + skills=[(1, 5)], + address=("Friedrich Witzig", "", "", "", "", "", "")) - all_skills = Skill.query.all() - all_profile_skills = [] - for skill in all_skills: - all_profile_skills.append((skill.id, 3)) + all_skills = [(skill.id, 3) for skill in Skill.query.all()] + seed_user("jutta", languages=[("fr", 5)], skills=all_skills) - 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)]) + seed_user("giesela", skills=[(9, 3), (10, 5)]) + seed_user("bertha", visible=False, skills=[(11, 3), (10, 5)]) + seed_user("monique", languages=[("fr", 4)]) db.session.commit() diff --git a/ki/test/test_profile_endpoint.py b/ki/test/test_profile_endpoint.py index 066aff7..61f247d 100644 --- a/ki/test/test_profile_endpoint.py +++ b/ki/test/test_profile_endpoint.py @@ -20,7 +20,8 @@ class TestProfileEndpoint(ApiTest): self.assertEqual(login_response.status_code, 200) self.assertIn("token", login_response.json) - response = self.client.post("/users/1/profile", + babsi = User.query.filter(User.auth_id == "babsi1").first() + response = self.client.post(f"/users/{babsi.id}/profile", data=json.dumps({}), content_type="application/json", headers={"Authorization": "Bearer " + login_response.json["token"]}) @@ -102,14 +103,15 @@ class TestProfileEndpoint(ApiTest): "level": 2 }] } - response = self.client.post("/users/1/profile", + peter = User.query.filter(User.auth_id == "peter").first() + response = self.client.post(f"/users/{peter.id}/profile", data=json.dumps(data), content_type="application/json", headers={"Authorization": "Bearer " + token}) self.assertEqual(response.status_code, 200) with app.app_context(): - user = User.query.filter(User.id == 1).first() + user = User.query.filter(User.id == peter.id).first() profile = user.profile self.assertEqual("Hebbert", profile.nickname) self.assertEqual("Monsieur", profile.pronouns) @@ -183,7 +185,8 @@ class TestProfileEndpoint(ApiTest): def test_get_visible_proifle(self): token = self.login("peter", "geheim")["token"] - response = self.client.get("/users/3/profile", headers={"Authorization": f"Bearer {token}"}) + babsi = User.query.filter(User.auth_id == "babsi1").first() + response = self.client.get(f"/users/{babsi.id}/profile", headers={"Authorization": f"Bearer {token}"}) self.assertEqual(response.status_code, 200) @@ -194,14 +197,15 @@ class TestProfileEndpoint(ApiTest): self.assertEqual(login_response.status_code, 200) self.assertIn("token", login_response.json) - response = self.client.get("/users/1/profile", + peter = User.query.filter(User.auth_id == "peter").first() + response = self.client.get(f"/users/{peter.id}/profile", headers={"Authorization": "Bearer " + login_response.json["token"]}) - + profile_id = peter.profile.id self.assertEqual(response.status_code, 200) self.assertDictEqual( response.json, { "profile": { - "user_id": 1, + "user_id": peter.id, "nickname": "peternichtlustig", "pronouns": "Herr Dr. Dr.", "availability_status": True, @@ -218,12 +222,12 @@ class TestProfileEndpoint(ApiTest): "id": 1, "name": "Peter Nichtlustig", "postcode": "13337", - "profile_id": 1, + "profile_id": profile_id, "street": "Waldweg" }, "contacts": [{ "id": 1, - "profile_id": 1, + "profile_id": profile_id, "contacttype": { "id": 4, "name": "Matrix" @@ -231,7 +235,7 @@ class TestProfileEndpoint(ApiTest): "content": "@peter:wtf-eg.de" }, { "id": 2, - "profile_id": 1, + "profile_id": profile_id, "contacttype": { "id": 1, "name": "E-Mail" @@ -239,7 +243,7 @@ class TestProfileEndpoint(ApiTest): "content": "peter@wtf-eg.de" }], "skills": [{ - "profile_id": 1, + "profile_id": profile_id, "skill": { "id": 1, "name": "PHP", @@ -247,7 +251,7 @@ class TestProfileEndpoint(ApiTest): }, "level": 5 }, { - "profile_id": 1, + "profile_id": profile_id, "skill": { "id": 3, "name": "Python", @@ -256,14 +260,14 @@ class TestProfileEndpoint(ApiTest): "level": 3 }], "searchtopics": [{ - "profile_id": 1, + "profile_id": profile_id, "skill": { "id": 1, "name": "PHP", "icon_url": "/skills/1/icon" } }, { - "profile_id": 1, + "profile_id": profile_id, "skill": { "id": 3, "name": "Python", @@ -271,7 +275,7 @@ class TestProfileEndpoint(ApiTest): } }], "languages": [{ - "profile_id": 1, + "profile_id": profile_id, "language": { "id": "de", "name": "Deutsch", @@ -279,7 +283,7 @@ class TestProfileEndpoint(ApiTest): }, "level": 5 }, { - "profile_id": 1, + "profile_id": profile_id, "language": { "id": "fr", "name": "Französisch", From 274f984994b54ca177f47e65f4708a5accb7fe2c Mon Sep 17 00:00:00 2001 From: jaschop <1k5o@tuta.io> Date: Fri, 20 Oct 2023 12:27:41 +0200 Subject: [PATCH 2/2] minor cleanups removed clutter from models.py (skill_ids only used for seeding) changed default value of update_profile handler (caused a crash during testing) --- ki/handlers/update_profile.py | 10 +++++----- ki/models.py | 7 ------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/ki/handlers/update_profile.py b/ki/handlers/update_profile.py index 567f153..377b4c5 100644 --- a/ki/handlers/update_profile.py +++ b/ki/handlers/update_profile.py @@ -151,11 +151,11 @@ def update_profile(user_id: int): profile.freetext = request.json.get("freetext", "") profile.visible = request.json.get("visible", False) - update_address(profile, request.json.get("address", {})) - update_contacts(profile, request.json.get("contacts", {})) - update_skills(profile, request.json.get("skills", {})) - update_searchtopics(profile, request.json.get("searchtopics")) - update_languages(profile, request.json.get("languages", {})) + update_address(profile, request.json.get("address")) + update_contacts(profile, request.json.get("contacts", [])) + update_skills(profile, request.json.get("skills", [])) + update_searchtopics(profile, request.json.get("searchtopics", [])) + update_languages(profile, request.json.get("languages", [])) db.session.commit() diff --git a/ki/models.py b/ki/models.py index c4bac66..65961a1 100644 --- a/ki/models.py +++ b/ki/models.py @@ -139,13 +139,6 @@ class Address(db.Model): class Skill(db.Model): - skill_id_php = 1 - skill_id_python = 3 - skill_id_sqlalchemy = 7 - skill_id_mysql = 9 - skill_id_postgresql = 10 - skill_id_sqlite = 11 - __tablename__ = "skill" id = Column(Integer, primary_key=True)