-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_surveys.py
93 lines (81 loc) · 2.96 KB
/
sample_surveys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# populate_sample_data.py
import requests
import json
import random
BASE_URL = "http://localhost:5001/api/v1"
def create_survey():
"""
Creates a new survey and returns the response.
"""
survey_data = {
"title": "Personal assessment",
"description": "Help us improve my self awareness",
"questions": [
{
"text": "How trustworthy am I?",
"response_type": "scale",
"response_scale_max": 5,
"creator_answer": 4
},
{
"text": "Am I honest most of the times?",
"response_type": "boolean",
"creator_answer": True
},
{
"text": "Do I take feedback with open arms?",
"response_type": "scale",
"response_scale_max": 10,
"creator_answer": 8
},
{
"text": "Am I approachable?",
"response_type": "boolean",
"creator_answer": False
}
]
}
response = requests.post(f"{BASE_URL}/surveys", json=survey_data)
print("Survey Creation Response:")
print(json.dumps(response.json(), indent=2))
return response.json()
def add_answers(survey_id):
"""
Adds a set of random answers for the given survey ID and returns the response.
"""
answers = [
{"question_id": 1, "answer": random.randint(1, 5)},
{"question_id": 2, "answer": random.choice([True, False])},
{"question_id": 3, "answer": random.randint(1, 10)},
{"question_id": 4, "answer": random.choice([True, False])}
]
response = requests.post(f"{BASE_URL}/surveys/{survey_id}/answers", json={"answers": answers})
print(f"Answer Submission Response for Survey {survey_id}:")
print(json.dumps(response.json(), indent=2))
return response.json()
def show_stats(survey_id, user_code):
"""
Retrieves and displays statistics for the given survey ID and user code.
"""
response = requests.get(f"{BASE_URL}/surveys/{survey_id}/results?user_code={user_code}")
print(f"Statistics for Survey {survey_id} (User Code: {user_code}):")
print(json.dumps(response.json(), indent=2))
return response.json()
if __name__ == "__main__":
# Create a new survey
survey = create_survey()
survey_id = survey['survey_id']
creator_code = survey['user_code']
# Add some random answers
for _ in range(10):
answer_response = add_answers(survey_id)
# Show stats for creator
creator_stats = show_stats(survey_id, creator_code)
# Show stats for a participant (using the user_code from the last answer submission)
print("Last participants stats")
participant_code = answer_response['user_code']
participant_stats = show_stats(survey_id, participant_code)
print(participant_stats)
print("Creator stats")
creator_stats = show_stats(survey_id, creator_code)
print(creator_stats)