diff --git a/.gitignore b/.gitignore index d7e8300..2bc45f3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .venv .pytest_cache/ __pycache__ -.vscode \ No newline at end of file +.vscode +.env diff --git a/app.py b/app.py index f8a88a5..1d287ad 100644 --- a/app.py +++ b/app.py @@ -3,8 +3,8 @@ ''' from flask import Flask, jsonify, request - -from models import Education, Experience, Skill +from models import Experience, Education, Skill +from spell_check import spell_check app = Flask(__name__) @@ -57,6 +57,7 @@ def experience(index=None): return jsonify({"error": "Experience not found"}), 404 return jsonify(data["experience"]), 200 + if request.method == "POST": try: new_experience = request.get_json() @@ -78,7 +79,7 @@ def experience(index=None): data["experience"].append(experience_obj) return jsonify({"id": len(data["experience"]) - 1}), 201 - except TypeError as e: + except (ValueError, TypeError, KeyError) as e: return jsonify({"error": f"Invalid data format: {str(e)}"}), 400 except Exception as e: return jsonify({"error": f"Internal error: {str(e)}"}), 500 @@ -86,6 +87,17 @@ def experience(index=None): return jsonify({"error": "Method not allowed"}), 405 + +@app.route('/resume/spell_check', methods=['POST']) +def spell_check(): + json_data = request.json + if json_data.get('description') and isinstance(json_data.get('description'), str): + json_data['description'] = spell_check(json_data['description']) + return jsonify({ + "before": request.json, + "after": json_data + }) + @app.route("/resume/education", methods=["GET", "POST"]) def education(): ''' @@ -100,8 +112,6 @@ def education(): if request.method == "POST": return jsonify({}) - return jsonify({}) - @app.route("/resume/skill", methods=["GET", "POST"]) def skill(): @@ -128,11 +138,8 @@ def skill(): {"id": len(data["skill"]) - 1} ), 201 - except KeyError: - return jsonify({"error": "Invalid request"}), 400 - - except TypeError as e: - return jsonify({"error": str(e)}), 400 + except (ValueError, TypeError, KeyError) as e: + return jsonify({"error": f"Invalid request: {str(e)}"}), 400 return jsonify({}) diff --git a/spell_check.py b/spell_check.py new file mode 100644 index 0000000..7269f8b --- /dev/null +++ b/spell_check.py @@ -0,0 +1,13 @@ +""" +Module for spell checking functionality using autocorrect library. +""" + +from autocorrect import Speller + +spell = Speller(lang='en') + +def spell_check(text): + ''' + Spell checks the text + ''' + return spell(text) diff --git a/test_pytest.py b/test_pytest.py index c309113..969b788 100644 --- a/test_pytest.py +++ b/test_pytest.py @@ -3,7 +3,7 @@ ''' from app import app - + def test_client(): ''' Makes a request and checks the message received is the same @@ -75,3 +75,24 @@ def test_skill(): response = app.test_client().get(f'/resume/skill/{item_id}') assert response.json == example_skill + +def test_spell_check(): + ''' + Test that the spell check endpoint returns a valid response + ''' + + example_education = { + "course": "Engineering", + "school": "NYU", + "start_date": "October 2022", + "end_date": "August 2024", + "grade": "86%", + "logo": "example-logo.png", + "description": "I was head of the debaite team at university", + "spell_check": True + } + + response = app.test_client().post('/resume/education', + json=example_education) + + assert response.json['description'] == "I was head of the debate team at university"