"""Child profile API tests.""" from datetime import date def _calc_age(birth_date: date) -> int: today = date.today() return today.year - birth_date.year - ( (today.month, today.day) < (birth_date.month, birth_date.day) ) def test_list_profiles_empty(auth_client): response = auth_client.get("/api/profiles") assert response.status_code == 200 data = response.json() assert data["profiles"] == [] assert data["total"] == 0 def test_create_update_delete_profile(auth_client): payload = { "name": "小明", "birth_date": "2020-05-12", "gender": "male", "interests": ["太空", "机器人"], "growth_themes": ["勇气"], } response = auth_client.post("/api/profiles", json=payload) assert response.status_code == 201 data = response.json() assert data["name"] == payload["name"] assert data["gender"] == payload["gender"] assert data["interests"] == payload["interests"] assert data["growth_themes"] == payload["growth_themes"] assert data["age"] == _calc_age(date.fromisoformat(payload["birth_date"])) profile_id = data["id"] update_payload = {"growth_themes": ["分享", "独立"]} response = auth_client.put(f"/api/profiles/{profile_id}", json=update_payload) assert response.status_code == 200 data = response.json() assert data["growth_themes"] == update_payload["growth_themes"] response = auth_client.delete(f"/api/profiles/{profile_id}") assert response.status_code == 200 assert response.json()["message"] == "Deleted" def test_profile_limit_and_duplicate(auth_client): # 先测试重复名称(在达到限制前) response = auth_client.post( "/api/profiles", json={"name": "孩子1", "gender": "female"}, ) assert response.status_code == 201 response = auth_client.post( "/api/profiles", json={"name": "孩子1", "gender": "female"}, ) assert response.status_code == 409 # 重复名称 # 继续创建到上限 for i in range(2, 6): response = auth_client.post( "/api/profiles", json={"name": f"孩子{i}", "gender": "female"}, ) assert response.status_code == 201 # 测试数量限制 response = auth_client.post( "/api/profiles", json={"name": "孩子6", "gender": "female"}, ) assert response.status_code == 400 # 超过5个限制