Skip to content

Commit 7878524

Browse files
committed
Add print_warning, print_bold
Rewriting of questionnaire init Add of tests about json into questionnaire_unit_test.py and json test files into ./json_test_files questionnaire_unit_test.md completed with the json tests
1 parent c5088c6 commit 7878524

12 files changed

Lines changed: 705 additions & 490 deletions

‎README.md‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
# Unit Tests Practice
44

5-
#### This repository has been prepared to practice the `unittest` automated testing framework through different angles
5+
#### This repository has been prepared to practice the `unittest` automated testing framework
66
#### It is based on the use of simple quizzes importer and viewer.
77

88
---
99

1010
## questionnaire_import.py
1111
>python ./questionnaire_import.py
1212
13-
Loads some quizzes from a sample list in the program and converts them into a specific json [format](./questionnaire.md#expected-json-schema) before to save them
13+
Loads some quizzes from a sample list in the program and converts them into a specific [json format](./questionnaire.md#expected-json-schema) before to save them
1414
into the [json_questionnaires](./json_questionnaires) folder
1515

1616
More info : [questionnaire_import.md](./questionnaire_import.md)
@@ -20,7 +20,7 @@ More info : [questionnaire_import.md](./questionnaire_import.md)
2020
## questionnaire.py
2121
>python ./questionnaire.py <questionnaire.json>
2222
23-
Displays the quizz in argument and counts the number of good answers
23+
Displays the quiz in argument and counts the number of good answers
2424

2525
More info : [questionnaire.md](./questionnaire.md)
2626

@@ -33,11 +33,12 @@ Uses the `unittest` automated framework but not only to test questionnaire.py
3333

3434
More info : [questionnaire_unit_test.md](./questionnaire_unit_test.md)
3535

36-
Uses :
37-
- initial_json_test_file.json
38-
- second_json_test_file.json
39-
- json_file_without_extension
40-
- empty.json
36+
Uses the files:
37+
- json_00_empty.json
38+
- json_00_without_extension
39+
- json_01_good_format.json
40+
- json_01_another_good_format.json
41+
- pytest.ini
4142

4243
and files into [json_test_files](./json_test_files) folder
4344

File renamed without changes.

json_good_format.json renamed to json_test_files/json_01_another_good_format.json

File renamed without changes.

‎questionnaire.md‎

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Questionnaire.py
44

5-
Displays the quizz in argument and counts the number of good answers
5+
Displays the quiz in argument and counts the number of good answers
66

77
```commandline
88
python questionnaire.py <questionnaire.json>
@@ -14,11 +14,12 @@ python questionnaire.py <questionnaire.json>
1414
python questionnaire.py .\json_questionnaires\animaux_leschats_confirme.json
1515
```
1616
```commandline
17+
=== QUESTIONNAIRE ===
1718
Catégorie : Animaux
1819
Difficulté : confirmé
19-
Titre : Les chats
20+
Titre : Les Chats
2021
21-
QUESTION 1
22+
QUESTION 1/20
2223
À quelle classe d'animaux vertébrés regroupant près de 5.400 espèces appartient le chat ?
2324
1 - Mammifères
2425
2 - Reptiles
@@ -39,22 +40,24 @@ Questionnaire.py can accept a json file from :
3940
* an URL
4041

4142
Questionnaire.py tries to load the json file in argument or print :
42-
- [```Error in argument```] if no one or more than one argument is passed
43-
- [```Incorrect extension```] if the file as no json extension
44-
- [```Invalid Path```] if the path to the file can not be reached
45-
- [```No File```] if the file can not be find into the path folder
46-
- [```Invalid URL```] if the path to the server can not be reached
47-
- [```File not found```] if the file can not be reached on the distant server
48-
- [```Incompatible or no data in JSON file```] if the json file cannot be loaded and decoded
49-
- [```Incompatible Json schema```] if the loaded json file does not match the expected schema
50-
- [```The title of the quizz is missing```] if the ```titre``` property is an empty string
51-
52-
These messages may also be printed but are **not critical** for the quiz
53-
- [```Skipped question : nothing to ask in question```] if the ```titre``` property of question is an empty string
54-
- [```Skipped question : no one or more than one good answer in question```] if a question has no one or more than
55-
one ```boolean``` at ```True``` in its possible answers
56-
57-
At least, if all questions have been skipped, [```No compatible question in this quizz```] will be printed.
43+
- [`Error in argument`] if __no one or more than one argument is passed__
44+
- [`Incorrect extension`] if __the file as no json extension__
45+
- [`Invalid permissions or path`] if __the path to the file can not be reached__
46+
- [`Invalid permissions or path`] if __the system has not the permission to open the file__
47+
- [`Invalid URL`] if __the path to the server can not be reached__
48+
- [`File not found`] if __the file can not be reached on the distant server__
49+
- [`Incompatible or no data in JSON file`] if __the json file cannot be loaded and decoded__
50+
- [`Incompatible Json schema`] if __the loaded json file does not match the expected schema__
51+
- [`The title of the quizz is missing and mandatory`] if __the `titre` property is an empty string__
52+
- [`The questions of the quizz are missing and mandatory`] if __the `questions` property is an empty array__
53+
54+
These warnings may also be printed but are __not critical__ to use the quiz
55+
- [`Skipped question : question is empty`] if __the `titre` property of a question is an empty string__
56+
- [`Skipped answer : answer is empty`] if __the first parameter of a `choix` in a question is an empty string__
57+
- [`Skipped question : no one or more than one good answers`] if a question has no one or more than one `boolean`
58+
at `true` in the possible answers of a single question
59+
60+
At least, if all questions have been skipped, [`Désolé, aucune question n'est compatible avec ce quizz`] will be printed.
5861

5962
---
6063
## Expected Json Schema
@@ -68,38 +71,52 @@ a ```boolean``` only
6871

6972
```json lines
7073
schema = {
71-
"$schema": "http://json-schema.org/draft/2020-12/schema",
72-
"type": "object",
73-
"required": [ "titre", "questions" ],
74-
"properties": {
75-
"categorie": {"type": "string"},
76-
"titre": {"type": "string"},
77-
"questions": {
78-
"type": "array",
79-
"default": [],
80-
"items": {
81-
"type": "object",
82-
"required": ["titre","choix"],
83-
"properties": {
84-
"titre": {"type": "string"},
85-
"choix": {
86-
"type": "array",
87-
"minItems": 2,
88-
"items": {
74+
"$schema": "https://json-schema.org/draft/2020-12/schema",
75+
"$id": "https://json-schema.org/draft/2020-12/schema",
76+
"type": "object",
77+
"required": ["titre", "questions"],
78+
"properties": {
79+
"categorie": {"type": "string"},
80+
"titre": {"type": "string"},
81+
"questions": {
82+
"type": "array",
83+
"default": [],
84+
"items": {
85+
"type": "object",
86+
"required": ["titre", "choix"],
87+
"properties": {
88+
"titre": {"type": "string"},
89+
"choix": {
8990
"type": "array",
9091
"minItems": 2,
91-
"prefixItems": [
92-
{"type": "string"},
93-
{"type": "boolean"}
94-
],
95-
"items": false
92+
"items": {
93+
"type": "array",
94+
"minItems": 2,
95+
"prefixItems": [
96+
{"type": "string"},
97+
{"type": "boolean"}
98+
],
99+
"items": False
100+
}
96101
}
97102
}
98103
}
99-
}
100-
},
101-
"difficulte": {"type": "string"}
104+
},
105+
"difficulte": {"type": "string"}
106+
}
102107
}
103108
}
104109
```
110+
Note : The `json-schema.org/draft/2020-12/` allows the `prefixItems` property to specify the types of the first
111+
items into this part and accepts to define `items` at false to mean that nothing more than the prefix items can be
112+
added into this part. This is however not supported by `json-schema.org/draft/2019-09/` used by a lot of json
113+
validator online.
105114

115+
Therefore, it is necessary to specify `[]` in place of `False` into the last `items` property to test
116+
the json files into json validator using `json-schema.org/draft/2019-09/`. The type of prefixItems can unfortunatly be
117+
verified by the schema with `json-schema.org/draft/2020-12/` only.
118+
119+
Sample : https://www.jsonschemavalidator.net/s/fhyE6WC0
120+
121+
122+
[README.md](./README.md) > [questionnaire.md](./questionnaire.md)

‎questionnaire.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,15 @@ class Questionnaire:
100100
"titre": {"type": "string"},
101101
"choix": {
102102
"type": "array",
103+
"default": [],
103104
"minItems": 2,
104105
"items": {
105106
"type": "array",
106107
"minItems": 2,
107-
"prefixItems": [{"type": "string"}, {"type": "boolean"}],
108+
"prefixItems": [
109+
{"type": "string"},
110+
{"type": "boolean"}
111+
],
108112
"items": False
109113
}
110114
}

‎questionnaire_import.md‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ Loads some quizzes from a sample list in the program :
88
* openquizzdb_241.json
99
* openquizzdb_90.json
1010

11-
and converts them into a specific json [format](./questionnaire.md#expected-json-schema) before to save them
11+
and converts them into a specific [json format](./questionnaire.md#expected-json-schema) before to save them
1212
into the [json_questionnaires](./json_questionnaires) folder
1313

1414
Those quizzes were initially downloaded from http://www.openquizzdb.org/ or https://www.kiwime.com/oqdb/files/ but the
15-
API that allowed to do it for free has been disconnected and is no longer available.\
16-
Those quizzes have thus been transferred on www.codeavecjonathan.com to be manipulated through this exercise
15+
API which allowed to do it for free has been disconnected and is no longer available.\
16+
Those quizzes have thus been transferred on www.codeavecjonathan.com to be manipulated through this exercise.
1717

1818

1919
### Sample of the initial json quiz format :
@@ -69,20 +69,20 @@ Those quizzes have thus been transferred on www.codeavecjonathan.com to be manip
6969
}
7070
}
7171
```
72-
Each json quiz has 3 categories of difficulty : ```débutant```, ```confirmé``` or ```expert```
73-
( beginner, advanced or expert )
72+
Each json quiz has 3 categories of difficulty : `débutant`, `confirmé` or `expert`
73+
*[ beginner, advanced or expert ]*
7474

75-
Each category includes 10 questions with 4 possible answers to be offered to the user.
75+
Each category includes 10 questions with 4 answer choices to be offered to the user.
7676

77-
The good answer can be found in the ```réponse``` key.
77+
The good answer can be found under the `réponse` key.
7878
___
7979
### How to use it :
8080

8181
> python questionnaire_import.py
8282
83-
loads json quiz files and converts them into a new json format by creating one file per difficulty category
83+
loads json quizzes and converts them to a new json format by creating one file per difficulty category.
8484

85-
### Sample of the json quizz format after import :
85+
### Sample of the json quiz format after import :
8686
```json
8787
{
8888
"categorie" : "Animaux",
@@ -114,4 +114,7 @@ These last ones are compiled to have 4 propositions with a boolean parameter
114114
indicating if the answer is the good one.
115115

116116
---
117-
### The schema must so match with the [expected jdon schema](./questionnaire.md#expected-json-schema) of [./questionnaire.py](./questionnaire.md)
117+
### The schema must so match with the [expected json schema](./questionnaire.md#expected-json-schema) of [./questionnaire.py](./questionnaire.md)
118+
119+
120+
[README.md](./README.md) > [questionnaire_import.md](./questionnaire_import.md)

0 commit comments

Comments
 (0)