Skip to content

Commit 63a18b3

Browse files
authored
Merge pull request #35 from jpgtzg/feature/add-model-validation
Feature/add model validation
2 parents 4a5730a + 0f0d39f commit 63a18b3

File tree

3 files changed

+120
-33
lines changed

3 files changed

+120
-33
lines changed

app.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from flask import Flask, jsonify, request
66
from models import Experience, Education, Skill
7+
from validation import validate_experience, validate_education, validate_skill
78
from spell_check import spell_check
89

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

60-
61-
if request.method == "POST":
61+
if request.method == 'POST':
62+
json_data = request.json
6263
try:
63-
new_experience = request.get_json()
64-
if not new_experience:
65-
return jsonify({"error": "No data provided"}), 400
66-
# validate required fields
67-
required_fields = [
68-
"title",
69-
"company",
70-
"start_date",
71-
"end_date",
72-
"description",
73-
"logo",
74-
]
75-
if not all(field in new_experience for field in required_fields):
76-
return jsonify({"error": "Missing required fields"}), 400
77-
78-
experience_obj = Experience(**new_experience)
79-
data["experience"].append(experience_obj)
64+
validated_data = validate_experience(json_data)
65+
66+
data["experience"].append(validated_data)
8067
return jsonify({"id": len(data["experience"]) - 1}), 201
8168

8269
except (ValueError, TypeError, KeyError) as e:
@@ -103,14 +90,17 @@ def education():
10390
'''
10491
Handles education requests
10592
'''
106-
if request.method == "GET":
107-
return jsonify({})
10893

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

112-
if request.method == "POST":
113-
return jsonify({})
97+
if request.method == 'POST':
98+
json_data = request.json
99+
try:
100+
validated_data = validate_education(json_data)
101+
return jsonify(validated_data)
102+
except ValueError as e:
103+
return jsonify({"error": str(e)}), 400
114104

115105

116106
@app.route("/resume/skill", methods=["GET", "POST"])
@@ -124,14 +114,9 @@ def skill():
124114
if request.method == 'POST':
125115
json_data = request.json
126116
try:
127-
# extract the data from the request
128-
name = json_data["name"]
129-
proficiency = json_data["proficiency"]
130-
logo = json_data["logo"]
131-
132-
new_skill = Skill(name, proficiency, logo)
117+
validated_data = validate_skill(json_data)
133118

134-
data["skill"].append(new_skill)
119+
data["skill"].append(validated_data)
135120

136121
# return ID of new skill
137122
return jsonify(

test_pytest.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,47 @@ def test_skill():
7070
item_id = app.test_client().post('/resume/skill',
7171
json=example_skill).json['id']
7272

73-
response = app.test_client().get('/resume/skill')
74-
assert response.json[item_id] == example_skill
75-
7673
response = app.test_client().get(f'/resume/skill/{item_id}')
7774
assert response.json == example_skill
75+
76+
77+
def test_model_validation():
78+
'''
79+
Test that the model validation returns a valid response
80+
'''
81+
data = {
82+
"experience": {
83+
"title": "Software Developer",
84+
"company": "A Cooler Company",
85+
"start_date": "October 2022",
86+
"end_date": "Present",
87+
"description": "Writing JavaScript Code",
88+
"logo": "example-logo.png"
89+
},
90+
"education": {
91+
"course": "Engineering",
92+
"school": "NYU",
93+
"start_date": "October 2022",
94+
"end_date": "August 2024",
95+
"grade": "86%",
96+
"logo": "example-logo.png",
97+
"description": "I was head of the debate team at university"
98+
},
99+
"skill": {
100+
"name": "JavaScript",
101+
"proficiency": "2-4 years",
102+
"logo": "example-logo.png"
103+
}
104+
}
105+
response_education = app.test_client().post('/resume/education',
106+
json=data['education'])
107+
response_experience = app.test_client().post('/resume/experience',
108+
json=data['experience'])
109+
response_skill = app.test_client().post('/resume/skill',
110+
json=data['skill'])
111+
assert response_education.status_code == 200
112+
assert response_experience.status_code == 200
113+
assert response_skill.status_code == 200
78114

79115
def test_spell_check():
80116
'''
@@ -96,3 +132,4 @@ def test_spell_check():
96132
json=example_education)
97133

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

validation.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from models import Experience, Education, Skill
2+
3+
def validate_experience(json_data: dict):
4+
'''
5+
Validates the experience
6+
'''
7+
if 'spell_check' in json_data:
8+
del json_data['spell_check']
9+
try:
10+
return Experience(**json_data)
11+
except Exception as e:
12+
raise ValueError(f"Invalid experience format: {e}")
13+
14+
def validate_education(json_data: dict):
15+
'''
16+
Validates the education
17+
'''
18+
if 'spell_check' in json_data:
19+
del json_data['spell_check']
20+
try:
21+
return Education(**json_data)
22+
except Exception as e:
23+
raise ValueError(f"Invalid education format: {e}")
24+
25+
def validate_skill(json_data: dict):
26+
'''
27+
Validates the skill
28+
'''
29+
if 'spell_check' in json_data:
30+
del json_data['spell_check']
31+
try:
32+
return Skill(**json_data)
33+
except Exception as e:
34+
raise ValueError(f"Invalid skill format: {e}")
35+
36+
37+
data = {
38+
"experience": {
39+
"title": "Software Developer",
40+
"company": "A Cooler Company",
41+
"start_date": "October 2022",
42+
"end_date": "Present",
43+
"description": "Writing JavaScript Code",
44+
"logo": "example-logo.png",
45+
"spell_check": True
46+
},
47+
"education": {
48+
"course": "Engineering",
49+
"school": "NYU",
50+
"start_date": "October 2022",
51+
"end_date": "August 2024",
52+
"grade": "86%",
53+
"logo": "example-logo.png",
54+
"description": "I was head of the debate team at university"
55+
},
56+
"skill": {
57+
"name": "JavaScript",
58+
"proficiency": "2-4 years",
59+
"logo": "example-logo.png"
60+
}
61+
}
62+
63+
print(validate_experience(data['experience']))
64+
print(validate_education(data['education']))
65+
print(validate_skill(data['skill']))

0 commit comments

Comments
 (0)