exam_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import pytest
  2. import requests
  3. import json
  4. OK = 200
  5. CREATED = 201
  6. NOT_FOUND = 404
  7. CONFLICT = 409
  8. WEBSITE_PREFIX = 'http://localhost:8080'
  9. def test_multiple_exam_apis():
  10. new_exam = {
  11. 'name': '期中考试',
  12. 'description': '可以回家做',
  13. 'duration': 86400, # seconds
  14. }
  15. resp = create_exam(new_exam)
  16. assert(resp.status_code == OK)
  17. examId = json.loads(resp.text)['id']
  18. resp = all_exams()
  19. assert(resp.status_code == OK)
  20. exams = json.loads(resp.text)
  21. assert(len(exams) == 1)
  22. non_exist_user = {
  23. 'userName': 'fake'
  24. }
  25. resp = all_exams(non_exist_user) # show the exams available to this given user
  26. exams = json.loads(resp.text)
  27. assert(len(exams) == 0)
  28. resp = get_exam(examId)
  29. assert(resp.status_code == OK)
  30. exam = json.loads(resp.text)
  31. assert(exam['description'] == '可以回家做')
  32. assert(exam['duration'] == 86400)
  33. resp = edit_exam(examId, {
  34. 'duration': 100
  35. })
  36. assert(resp.status_code == OK)
  37. assert(json.loads(resp.text)['duration'] == 100)
  38. from user_test import register_user, delete_user, get_user
  39. resp = register_user('exam-lover', 'iloveexam')
  40. assert(resp.status_code == CREATED)
  41. resp = get_user('exam-lover')
  42. user = json.loads(resp.text)
  43. question = {
  44. 'userId': user['userName'],
  45. 'content': '天上什么星星最亮?',
  46. 'answers': ['太阳', '小天狼星', '北极星'],
  47. 'finalAnswer': ['小天狼星'],
  48. 'type': 'DanXuan'
  49. }
  50. resp = create_question(question)
  51. assert(resp.status_code == OK)
  52. qid = json.loads(resp.text)['id']
  53. resp = get_question(qid)
  54. assert(resp.status_code == OK)
  55. created_question = json.loads(resp.text)
  56. assert(created_question['userId'] == user['userName'])
  57. assert(created_question['finalAnswer'][0] == '小天狼星')
  58. resp = edit_question(qid, {
  59. 'finalAnswer': ['太阳']
  60. })
  61. updated_question = json.loads(resp.text)
  62. assert(updated_question['finalAnswer'][0] == '太阳')
  63. resp = add_question_to_exam(examId, [updated_question])
  64. assert(resp.status_code == OK)
  65. resp = start_exam(examId, user)
  66. assert(resp.status_code == OK)
  67. questions = json.loads(resp.text)
  68. assert(len(questions) == 1) # 1 question is under this exam
  69. resp = end_exam(examId, {
  70. 'startTime': '2021-01-01T17:09:42.411',
  71. 'endTime': '2021-01-01T17:09:50.411',
  72. 'userId': user['userName'],
  73. 'points': 5
  74. })
  75. assert(resp.status_code == OK)
  76. resp = remove_question_from_exam(examId, updated_question)
  77. resp = start_exam(examId, user)
  78. assert(resp.status_code == OK)
  79. questions = json.loads(resp.text)
  80. assert(len(questions) == 0) # 0 question is under this exam after removal
  81. delete_question(updated_question['id'])
  82. remove_exam(examId)
  83. delete_user(user['userName'])
  84. def create_exam(exam):
  85. url = WEBSITE_PREFIX + '/admin/exams'
  86. return requests.post(url, json=exam)
  87. def get_exam(examId):
  88. url = WEBSITE_PREFIX + '/exams/' + examId
  89. return requests.get(url)
  90. def edit_exam(examId, updatedExam):
  91. url = WEBSITE_PREFIX + '/exams/' + examId
  92. return requests.put(url, json=updatedExam)
  93. def remove_exam(examId):
  94. url = WEBSITE_PREFIX + '/exams/' + examId
  95. return requests.delete(url)
  96. def start_exam(examId, user):
  97. url = WEBSITE_PREFIX + '/exams/' + examId + '/start'
  98. return requests.post(url, json=user)
  99. def end_exam(examId, examHistory):
  100. url = WEBSITE_PREFIX + '/exams/' + examId + '/end'
  101. return requests.post(url, json=examHistory)
  102. def all_exams(user = None):
  103. url = WEBSITE_PREFIX + '/exams'
  104. return requests.get(url, json=user)
  105. def get_question(qid):
  106. url = WEBSITE_PREFIX + '/questions/' + qid
  107. return requests.get(url)
  108. def create_question(question):
  109. url = WEBSITE_PREFIX + '/questions/'
  110. return requests.post(url, json=question)
  111. def edit_question(qid, updatedQuestion):
  112. url = WEBSITE_PREFIX + '/questions/' + qid
  113. return requests.put(url, json=updatedQuestion)
  114. def delete_question(qid):
  115. url = WEBSITE_PREFIX + '/questions/' + qid
  116. return requests.delete(url)
  117. def add_question_to_exam(examId, questions):
  118. url = WEBSITE_PREFIX + '/exams/' + examId + '/questions'
  119. return requests.post(url, json=questions)
  120. def remove_question_from_exam(examId, question):
  121. url = WEBSITE_PREFIX + '/exams/' + examId + '/questions'
  122. return requests.delete(url, json=question)