setup_db.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import requests
  2. import json
  3. import random
  4. import string
  5. from user_test import get_user, get_all_users
  6. from group_test import get_group_by_name, get_group, create_group, add_user_to_group
  7. from exam_test import create_exam, create_question, get_question, add_question_to_exam
  8. from category_test import create_material, get_material, create_category, get_category, add_material_to_category, update_category, create_question_category, get_question_category, questions_in_category, add_question_to_question_category, update_question_category
  9. from learningmaterial_test import add_comment
  10. USERNAME_RANDOM_LENGTH = 10
  11. def _random_string(length):
  12. letters = string.ascii_letters
  13. return ''.join(random.choice(letters) for i in range(length))
  14. def _text(resp):
  15. return json.loads(resp.text)
  16. # 创建 100 个 前缀为namePrefix 密码为password的 用户
  17. def add_dummy_users(namePrefix = 'testuser-', password = 'test', number = 100):
  18. groups = add_groups()
  19. users = []
  20. nicknames = ['小强', '小张', '小李子', '大王']
  21. firstNames = ['一', '二', '三', '四', '五']
  22. lastNames = ['张', '王', '李', '杜', '靳']
  23. for i in range(number):
  24. user_name = namePrefix + _random_string(USERNAME_RANDOM_LENGTH)
  25. # register_user(user_name, password)
  26. new_user = {
  27. 'userName': user_name,
  28. 'password': password,
  29. 'nickName': nicknames[i % len(nicknames)],
  30. 'studyId': 'U0000' + str(i),
  31. 'email': 'test@gmail.com',
  32. 'firstName': firstNames[i % len(firstNames)],
  33. 'lastName': lastNames[i % len(lastNames)],
  34. 'phone': '13060901234',
  35. 'birthday': '1999-01-01',
  36. }
  37. register_user(new_user)
  38. user = get_user(user_name)
  39. random_group = groups[random.randint(0, len(groups)-1)]
  40. add_user_to_group(random_group, _text(user))
  41. users.append(_text(user)['userName'])
  42. print('created ' + str(number) + ' users: ' + ','.join(users))
  43. global all_users
  44. all_users = users
  45. return users
  46. # 创建 3个二级的group
  47. # orgtest-level1
  48. # orgtest-level2-a orgtest-level2-b orgtest-level2-c
  49. def add_groups(namePrefix = 'orgtest-', number=3):
  50. # creating level1 group
  51. level1_name = namePrefix + 'level1-' + _random_string(3)
  52. level1_group_body = {
  53. 'name': level1_name
  54. }
  55. create_group(level1_group_body)
  56. level1_group = _text(get_group_by_name(level1_name))
  57. groups = []
  58. for i in range(number):
  59. level2_group_body = {
  60. 'name': namePrefix + 'level2-' + _random_string(3),
  61. 'parentGroupId': level1_group['id']
  62. }
  63. resp = create_group(level2_group_body)
  64. groupId = _text(resp)['id']
  65. groups.append(groupId)
  66. print ('created ' + str(number) + ' groups: ' + ','.join(groups))
  67. return groups
  68. def randomly_pick_user():
  69. global all_users
  70. rand_index = random.randint(0, len(all_users) - 1)
  71. return all_users[rand_index]
  72. def create_exams(namePrefix='测试考试-', number=5):
  73. exams = []
  74. for i in range(number):
  75. new_exam = {
  76. 'name': namePrefix + _random_string(3),
  77. 'description': '可以回家做',
  78. 'duration': random.randint(0, 863) * 100, # seconds
  79. }
  80. resp = create_exam(new_exam)
  81. exams.append(_text(resp)['id'])
  82. print ('created ' + str(number) + ' exams: ' + ','.join(exams))
  83. return exams
  84. def create_questions(namePrefix='qtest-', number=10):
  85. exams = create_exams()
  86. questions = []
  87. question_categories = create_question_categories()
  88. for i in range(number):
  89. question = {
  90. 'userId': randomly_pick_user(),
  91. 'content': namePrefix + '天上什么星星最亮?-' + str(i),
  92. 'answers': ['太阳', '小天狼星', '北极星'],
  93. 'finalAnswer': ['小天狼星'],
  94. 'type': 'DanXuan' # 单选题
  95. }
  96. resp = create_question(question)
  97. random_exam_id = exams[random.randint(0, len(exams)-1)]
  98. # add newly created question to a random exam
  99. add_question_to_exam(random_exam_id, [_text(resp)])
  100. questions.append(_text(resp)['id'])
  101. tiankong_question = {
  102. 'userId': randomly_pick_user(),
  103. 'content': namePrefix + '天上$PH$最亮?-' + str(i),
  104. 'finalAnswer': ['小天狼星'],
  105. 'type': 'TianKong' # 填空题
  106. }
  107. resp = create_question(tiankong_question)
  108. random_exam_id = exams[random.randint(0, len(exams)-1)]
  109. # add newly created question to a random exam
  110. add_question_to_exam(random_exam_id, [_text(resp)])
  111. random_category_id = question_categories[random.randint(0, len(question_categories)-1)]
  112. add_question_to_question_category(random_category_id, _text(resp))
  113. questions.append(_text(resp)['id'])
  114. print ('created ' + str(number) + ' questions: ' + ','.join(questions))
  115. return questions
  116. def create_question_categories(namePrefix= '知识类-', number=5):
  117. resp = create_question_category(namePrefix + _random_string(3))
  118. question_root_id = _text(resp)['id']
  119. categories = []
  120. names = ['科普类', '技术类', '历史类', '天文类', '兴趣类']
  121. for i in range(number):
  122. child = create_question_category(names[i % len(names)] + _random_string(3))
  123. child_id = _text(child)['id']
  124. update_question_category(child_id, {
  125. 'parentId': question_root_id
  126. })
  127. categories.append(child_id)
  128. print ('created ' + str(number) + ' question categories: ' + ','.join(categories))
  129. return categories
  130. def create_categories(namePrefix= 'testCategory-', number=5):
  131. resp = create_category(namePrefix + 'root-' + _random_string(3))
  132. root_id = _text(resp)['id']
  133. categories = []
  134. for i in range(number):
  135. child = create_category(namePrefix + 'child-' + _random_string(3))
  136. child_id = _text(child)['id']
  137. update_category(child_id, {
  138. 'parentId': root_id
  139. })
  140. categories.append(child_id)
  141. print ('created ' + str(number) + ' categories: ' + ','.join(categories))
  142. return categories
  143. def create_learning_materials(namePrefix='测试资料-', number=20):
  144. categories = create_categories()
  145. materials = []
  146. for i in range(number):
  147. user = randomly_pick_user()
  148. material = create_material({
  149. 'name': namePrefix + _random_string(5),
  150. 'userId': randomly_pick_user(),
  151. 'description': '好看的资料',
  152. 'type': 'ARTICLE',
  153. 'contents': '十年高考三年模拟-' + _random_string(20),
  154. 'links': [_random_string(3), _random_string(3), _random_string(3)]
  155. })
  156. materialId = _text(material)['id']
  157. materials.append(materialId)
  158. random_category = categories[random.randint(0, len(categories)-1)]
  159. add_material_to_category(random_category, _text(material))
  160. create_comments(materialId)
  161. print ('created ' + str(number) + ' materials: ' + ','.join(materials))
  162. return materials
  163. def create_comments(materialId, namePrefix='[自动生成]', number=5):
  164. dummy_comments = ['我去,这也太酷了!', '我太爱学这个了,老牛了', '这太有意思了,收藏了', '好文,顶一个', '我踩']
  165. for i in range(number):
  166. comment = {
  167. 'userId': randomly_pick_user(),
  168. 'content': dummy_comments[i % len(dummy_comments)]
  169. }
  170. add_comment(materialId, comment)
  171. print('created ' + str(number) + ' comments to material: ' + materialId)
  172. def register_user(userData):
  173. url = 'http://localhost:8080/user/register'
  174. return requests.post(url, json=userData)
  175. add_dummy_users()
  176. create_questions()
  177. create_learning_materials()