Skip to content

Commit 7788340

Browse files
author
Alexander Engström
committed
Updated the JSON validation test to provide better error messages
1 parent b8788e4 commit 7788340

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

data/questions.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,64 @@
815815
"explanation": "The division is performed from left to right. '12 / 4' equals 3, and then '3 / 3' equals 1.",
816816
"correctAnswer": "1.0",
817817
"incorrectAnswers": ["4", "0", "9", "0.5", "2.0"]
818+
},
819+
{
820+
"difficulty": "beginner",
821+
"question": "What is the result of the following Python expression?",
822+
"codeSnippet": "7 % 2",
823+
"explanation": "The modulo operator '%' in Python returns the remainder of the division. Here, 7 % 2 results in 1 since 7 divided by 2 leaves a remainder of 1.",
824+
"correctAnswer": "1",
825+
"incorrectAnswers": ["3.5", "2", "0", "7", "None"]
826+
},
827+
{
828+
"difficulty": "beginner",
829+
"question": "What will be the output of the following Python code snippet?",
830+
"codeSnippet": "my_list = ['a', 'b', 'c', 'd']\nprint(len(my_list))",
831+
"explanation": "The 'len()' function in Python returns the number of items in an object. Here, 'my_list' contains four items, so 'len(my_list)' returns 4.",
832+
"correctAnswer": "4",
833+
"incorrectAnswers": [
834+
"3",
835+
"'abcd'",
836+
"['a', 'b', 'c', 'd']",
837+
"None",
838+
"AttributeError"
839+
]
840+
},
841+
{
842+
"difficulty": "beginner",
843+
"question": "What does the following Python code output?",
844+
"codeSnippet": "print('Python' + 'Quiz')",
845+
"explanation": "The '+' operator concatenates two strings in Python. Here, 'Python' + 'Quiz' results in the string 'PythonQuiz'.",
846+
"correctAnswer": "'PythonQuiz'",
847+
"incorrectAnswers": [
848+
"'QuizPython'",
849+
"'Python Quiz'",
850+
"'PythonQuizPython'",
851+
"'Quiz'",
852+
"ValueError"
853+
]
854+
},
855+
{
856+
"difficulty": "beginner",
857+
"question": "What is the output of the following Python code snippet?",
858+
"codeSnippet": "a = 10\nb = 20\nprint(a > b)",
859+
"explanation": "The '>' operator checks if the value on the left is greater than the value on the right. Here, 'a > b' is 'False' since 10 is not greater than 20.",
860+
"correctAnswer": "False",
861+
"incorrectAnswers": ["True", "'10 > 20'", "'20 > 10'", "None", "10"]
862+
},
863+
{
864+
"difficulty": "beginner",
865+
"question": "What does the following Python code snippet output?",
866+
"codeSnippet": "my_dict = {'a': 1, 'b': 2}\nprint(my_dict['b'])",
867+
"explanation": "In Python, a dictionary value can be accessed by using its corresponding key. Here, 'my_dict['b']' accesses the value associated with the key 'b', which is 2.",
868+
"correctAnswer": "2",
869+
"incorrectAnswers": [
870+
"1",
871+
"'a'",
872+
"{'a': 1, 'b': 2}",
873+
"KeyError",
874+
"RecursionError"
875+
]
818876
}
819877
],
820878
"medium": [

scripts/validate_json.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@ def validate_json(file_path):
1717
question_count[category] += 1
1818

1919
try:
20-
assert category == question["difficulty"]
21-
assert "question" in question
22-
assert "codeSnippet" in question
23-
assert "explanation" in question
24-
assert "correctAnswer" in question
25-
assert "incorrectAnswers" in question
26-
assert len(question["incorrectAnswers"]) == 5
27-
assert question["correctAnswer"] not in question["incorrectAnswers"]
20+
assert category == question["difficulty"], "Question have incorrect category"
21+
assert "question" in question, "The question doesnt have the key 'question'"
22+
assert "codeSnippet" in question, "The question doesnt have the key 'codeSnippet'"
23+
assert "explanation" in question, "The question doesnt have the key 'explanation'"
24+
assert "correctAnswer" in question, "The question doesnt have the key 'correctAnswer'"
25+
assert "incorrectAnswers" in question, "The question doesnt have the key 'incorrectAnswers'"
26+
assert len(
27+
question["incorrectAnswers"]) == 5, "The question doesnt have five incorrect options"
28+
assert question["correctAnswer"] not in question[
29+
"incorrectAnswers"], "The correct answer is also one of the incorrect options"
2830
assert len(set(question["incorrectAnswers"])) == len(
29-
question["incorrectAnswers"])
31+
question["incorrectAnswers"]), "There is a duplicate incorrect answer"
3032

3133
except AssertionError as err:
3234
raise Exception(
33-
f"Invalid question data: {err}\n{question}")
35+
f"\n\nInvalid question data: {err}\n{question}")
3436

3537
print("JSON is valid!")
3638
total_questions = 0

0 commit comments

Comments
 (0)