From 8a4c113e54650acf752c0600c564bdc4c36016e8 Mon Sep 17 00:00:00 2001 From: Nhat Minh Luu <54903938+nluu175@users.noreply.github.com> Date: Sun, 6 Oct 2024 22:38:24 -0600 Subject: [PATCH 1/2] Add implementation for all Education endpoints and tests --- app.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-- test_pytest.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 0f6e668..2372c65 100644 --- a/app.py +++ b/app.py @@ -64,14 +64,68 @@ def education(): Handles education requests ''' if request.method == 'GET': - return jsonify({}) + return jsonify(data['education']), 200 if request.method == 'POST': - return jsonify({}) + new_education = request.json + + if ( + 'course' not in new_education + or 'school' not in new_education + or 'start_date' not in new_education + or 'end_date' not in new_education + or 'grade' not in new_education + or 'logo' not in new_education + ): + return jsonify({'error': 'Invalid input, all fields (course, school, start_date, end_date, grade, logo) are required'}), 400 + + data['education'].append(new_education) + + return jsonify({'message': 'Education added', 'data': new_education, 'index': len(data['education']) - 1}), 201 return jsonify({}) +@app.route('/resume/education/', methods=['GET', 'PUT', 'DELETE']) +def education_at_id(education_id=None): + ''' + Handles education requests at a specific ID + ''' + if request.method == 'GET': + if 0 <= education_id < len(data['education']): + return jsonify(data['education'][education_id]), 200 + else: + return jsonify({'error': 'Education not found'}), 404 + + if request.method == 'PUT': + if 0 <= education_id < len(data['education']): + updated_data = request.get_json() + + course = updated_data.get('course') + school = updated_data.get('school') + start_date = updated_data.get('start_date') + end_date = updated_data.get('end_date') + grade = updated_data.get('grade') + logo = updated_data.get('logo') + + if not (course and school and start_date and end_date and grade and logo): + return jsonify({'error': 'Invalid input, all fields (course, school, start_date, end_date, grade, logo) are required'}), 400 + + data['education'][education_id] = Education(course, school, start_date, end_date, grade, logo) + + return jsonify({'message': 'Education updated', 'data': data['education'][education_id]}), 200 + else: + return jsonify({'error': 'Education not found'}), 404 + + if request.method == 'DELETE': + if 0 <= education_id < len(data['education']): + deleted_education = data['education'].pop(education_id) + + return jsonify({"message": "Education deleted", "data": deleted_education}), 200 + else: + return jsonify({"error": "Education not found"}), 404 + + @app.route('/resume/skill', methods=['GET', 'POST']) def skill(): ''' diff --git a/test_pytest.py b/test_pytest.py index c3d6f08..70c0531 100644 --- a/test_pytest.py +++ b/test_pytest.py @@ -72,3 +72,57 @@ def test_skill(): response = app.test_client().get('/resume/skill') assert response.json[item_id] == example_skill + + +def delete_education(): + ''' + Add a new education and then delete it. + + Check that the education is no longer in the list. + ''' + example_education = { + "course": "Computer Science", + "end_date": "July 2022", + "grade": "80%", + "logo": "updated-logo.png", + "school": "University of Tech", + "start_date": "September 2019" + } + + added_item_id = app.test_client().post('/resume/education', json=example_education).json['id'] + + app.test_client().delete(f'/resume/education/{added_item_id}') + + response = app.test_client().get(f'/resume/education/{added_item_id}') + assert response.json['message'] == "Education not found" + + +def edit_education(): + ''' + Add a new education and then edit it. + + Check that the education is updated in the list. + ''' + example_education = { + "course": "Computer Science", + "end_date": "July 2022", + "grade": "80%", + "logo": "updated-logo.png", + "school": "University of Tech", + "start_date": "September 2019" + } + + edited_education = { + "course": "NEW Computer Science", + "end_date": "July 2022", + "grade": "NEW 80%", + "logo": "updated-logo.png", + "school": "NEW University of Tech", + "start_date": "September 2019" + } + + index = app.test_client().post('/resume/education', json=example_education).json['id'] + app.test_client().put(f'/resume/education/{index}', json=edited_education) + response = app.test_client().get(f'/resume/education/{index}') + + assert response["name"] == edited_education["name"] \ No newline at end of file From d81de17c16dc0ec7207ecd8d465701cefd30a84f Mon Sep 17 00:00:00 2001 From: Nhat Minh Luu <54903938+nluu175@users.noreply.github.com> Date: Sun, 6 Oct 2024 22:41:30 -0600 Subject: [PATCH 2/2] Fix a field name in test --- test_pytest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_pytest.py b/test_pytest.py index 70c0531..7d3b79c 100644 --- a/test_pytest.py +++ b/test_pytest.py @@ -125,4 +125,4 @@ def edit_education(): app.test_client().put(f'/resume/education/{index}', json=edited_education) response = app.test_client().get(f'/resume/education/{index}') - assert response["name"] == edited_education["name"] \ No newline at end of file + assert response["course"] == edited_education["course"] \ No newline at end of file