Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add model validation #35

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 15 additions & 30 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from flask import Flask, jsonify, request
from models import Experience, Education, Skill
from validation import validate_experience, validate_education, validate_skill
from spell_check import spell_check

app = Flask(__name__)
Expand Down Expand Up @@ -57,26 +58,12 @@ def experience(index=None):
return jsonify({"error": "Experience not found"}), 404
return jsonify(data["experience"]), 200


if request.method == "POST":
if request.method == 'POST':
json_data = request.json
try:
new_experience = request.get_json()
if not new_experience:
return jsonify({"error": "No data provided"}), 400
# validate required fields
required_fields = [
"title",
"company",
"start_date",
"end_date",
"description",
"logo",
]
if not all(field in new_experience for field in required_fields):
return jsonify({"error": "Missing required fields"}), 400

experience_obj = Experience(**new_experience)
data["experience"].append(experience_obj)
validated_data = validate_experience(json_data)

data["experience"].append(validated_data)
return jsonify({"id": len(data["experience"]) - 1}), 201

except (ValueError, TypeError, KeyError) as e:
Expand All @@ -103,14 +90,17 @@ def education():
'''
Handles education requests
'''
if request.method == "GET":
return jsonify({})

if request.method == 'GET':
return jsonify(data['education']), 200

if request.method == "POST":
return jsonify({})
if request.method == 'POST':
json_data = request.json
try:
validated_data = validate_education(json_data)
return jsonify(validated_data)
except ValueError as e:
return jsonify({"error": str(e)}), 400


@app.route("/resume/skill", methods=["GET", "POST"])
Expand All @@ -124,14 +114,9 @@ def skill():
if request.method == 'POST':
json_data = request.json
try:
# extract the data from the request
name = json_data["name"]
proficiency = json_data["proficiency"]
logo = json_data["logo"]

new_skill = Skill(name, proficiency, logo)
validated_data = validate_skill(json_data)

data["skill"].append(new_skill)
data["skill"].append(validated_data)

# return ID of new skill
return jsonify(
Expand Down
43 changes: 40 additions & 3 deletions test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,47 @@ def test_skill():
item_id = app.test_client().post('/resume/skill',
json=example_skill).json['id']

response = app.test_client().get('/resume/skill')
assert response.json[item_id] == example_skill

response = app.test_client().get(f'/resume/skill/{item_id}')
assert response.json == example_skill


def test_model_validation():
'''
Test that the model validation returns a valid response
'''
data = {
"experience": {
"title": "Software Developer",
"company": "A Cooler Company",
"start_date": "October 2022",
"end_date": "Present",
"description": "Writing JavaScript Code",
"logo": "example-logo.png"
},
"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 debate team at university"
},
"skill": {
"name": "JavaScript",
"proficiency": "2-4 years",
"logo": "example-logo.png"
}
}
response_education = app.test_client().post('/resume/education',
json=data['education'])
response_experience = app.test_client().post('/resume/experience',
json=data['experience'])
response_skill = app.test_client().post('/resume/skill',
json=data['skill'])
assert response_education.status_code == 200
assert response_experience.status_code == 200
assert response_skill.status_code == 200

def test_spell_check():
'''
Expand All @@ -96,3 +132,4 @@ def test_spell_check():
json=example_education)

assert response.json['description'] == "I was head of the debate team at university"

65 changes: 65 additions & 0 deletions validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from models import Experience, Education, Skill

def validate_experience(json_data: dict):
'''
Validates the experience
'''
if 'spell_check' in json_data:
del json_data['spell_check']
try:
return Experience(**json_data)
except Exception as e:
raise ValueError(f"Invalid experience format: {e}")

def validate_education(json_data: dict):
'''
Validates the education
'''
if 'spell_check' in json_data:
del json_data['spell_check']
try:
return Education(**json_data)
except Exception as e:
raise ValueError(f"Invalid education format: {e}")

def validate_skill(json_data: dict):
'''
Validates the skill
'''
if 'spell_check' in json_data:
del json_data['spell_check']
try:
return Skill(**json_data)
except Exception as e:
raise ValueError(f"Invalid skill format: {e}")


data = {
"experience": {
"title": "Software Developer",
"company": "A Cooler Company",
"start_date": "October 2022",
"end_date": "Present",
"description": "Writing JavaScript Code",
"logo": "example-logo.png",
"spell_check": True
},
"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 debate team at university"
},
"skill": {
"name": "JavaScript",
"proficiency": "2-4 years",
"logo": "example-logo.png"
}
}

print(validate_experience(data['experience']))
print(validate_education(data['education']))
print(validate_skill(data['skill']))
Loading