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 3 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.venv
.pytest_cache/
__pycache__
.vscode
.vscode
.env
23 changes: 19 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
from flask import Flask, jsonify, request
from models import Experience, Education, Skill

from validation import validate_experience, validate_education, validate_skill
app = Flask(__name__)

data = {
Expand Down Expand Up @@ -48,7 +48,12 @@ def experience():
return jsonify()

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

return jsonify({})

Expand All @@ -61,7 +66,12 @@ def education():
return jsonify({})

if request.method == 'POST':
return jsonify({})
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

return jsonify({})

Expand All @@ -75,6 +85,11 @@ def skill():
return jsonify({})

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

return jsonify({})
39 changes: 39 additions & 0 deletions test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,42 @@ def test_skill():

response = app.test_client().get('/resume/skill')
assert response.json[item_id] == 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
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']))