import requests import json import random import string from user_test import get_user, get_all_users from group_test import get_group_by_name, get_group, create_group, add_user_to_group from exam_test import create_exam, create_question, get_question, add_question_to_exam 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 from learningmaterial_test import add_comment USERNAME_RANDOM_LENGTH = 10 def _random_string(length): letters = string.ascii_letters return ''.join(random.choice(letters) for i in range(length)) def _text(resp): return json.loads(resp.text) # 创建 100 个 前缀为namePrefix 密码为password的 用户 def add_dummy_users(namePrefix = 'testuser-', password = 'test', number = 100): groups = add_groups() users = [] nicknames = ['小强', '小张', '小李子', '大王'] firstNames = ['一', '二', '三', '四', '五'] lastNames = ['张', '王', '李', '杜', '靳'] for i in range(number): user_name = namePrefix + _random_string(USERNAME_RANDOM_LENGTH) # register_user(user_name, password) new_user = { 'userName': user_name, 'password': password, 'nickName': nicknames[i % len(nicknames)], 'studyId': 'U0000' + str(i), 'email': 'test@gmail.com', 'firstName': firstNames[i % len(firstNames)], 'lastName': lastNames[i % len(lastNames)], 'phone': '13060901234', 'birthday': '1999-01-01', } register_user(new_user) user = get_user(user_name) random_group = groups[random.randint(0, len(groups)-1)] add_user_to_group(random_group, _text(user)) users.append(_text(user)['userName']) print('created ' + str(number) + ' users: ' + ','.join(users)) global all_users all_users = users return users # 创建 3个二级的group # orgtest-level1 # orgtest-level2-a orgtest-level2-b orgtest-level2-c def add_groups(namePrefix = 'orgtest-', number=3): # creating level1 group level1_name = namePrefix + 'level1-' + _random_string(3) level1_group_body = { 'name': level1_name } create_group(level1_group_body) level1_group = _text(get_group_by_name(level1_name)) groups = [] for i in range(number): level2_group_body = { 'name': namePrefix + 'level2-' + _random_string(3), 'parentGroupId': level1_group['id'] } resp = create_group(level2_group_body) groupId = _text(resp)['id'] groups.append(groupId) print ('created ' + str(number) + ' groups: ' + ','.join(groups)) return groups def randomly_pick_user(): global all_users rand_index = random.randint(0, len(all_users) - 1) return all_users[rand_index] def create_exams(namePrefix='测试考试-', number=5): exams = [] for i in range(number): new_exam = { 'name': namePrefix + _random_string(3), 'description': '可以回家做', 'duration': random.randint(0, 863) * 100, # seconds } resp = create_exam(new_exam) exams.append(_text(resp)['id']) print ('created ' + str(number) + ' exams: ' + ','.join(exams)) return exams def create_questions(namePrefix='qtest-', number=10): exams = create_exams() questions = [] question_categories = create_question_categories() for i in range(number): question = { 'userId': randomly_pick_user(), 'content': namePrefix + '天上什么星星最亮?-' + str(i), 'answers': ['太阳', '小天狼星', '北极星'], 'finalAnswer': ['小天狼星'], 'type': 'DanXuan' # 单选题 } resp = create_question(question) random_exam_id = exams[random.randint(0, len(exams)-1)] # add newly created question to a random exam add_question_to_exam(random_exam_id, [_text(resp)]) questions.append(_text(resp)['id']) tiankong_question = { 'userId': randomly_pick_user(), 'content': namePrefix + '天上$PH$最亮?-' + str(i), 'finalAnswer': ['小天狼星'], 'type': 'TianKong' # 填空题 } resp = create_question(tiankong_question) random_exam_id = exams[random.randint(0, len(exams)-1)] # add newly created question to a random exam add_question_to_exam(random_exam_id, [_text(resp)]) random_category_id = question_categories[random.randint(0, len(question_categories)-1)] add_question_to_question_category(random_category_id, _text(resp)) questions.append(_text(resp)['id']) print ('created ' + str(number) + ' questions: ' + ','.join(questions)) return questions def create_question_categories(namePrefix= '知识类-', number=5): resp = create_question_category(namePrefix + _random_string(3)) question_root_id = _text(resp)['id'] categories = [] names = ['科普类', '技术类', '历史类', '天文类', '兴趣类'] for i in range(number): child = create_question_category(names[i % len(names)] + _random_string(3)) child_id = _text(child)['id'] update_question_category(child_id, { 'parentId': question_root_id }) categories.append(child_id) print ('created ' + str(number) + ' question categories: ' + ','.join(categories)) return categories def create_categories(namePrefix= 'testCategory-', number=5): resp = create_category(namePrefix + 'root-' + _random_string(3)) root_id = _text(resp)['id'] categories = [] for i in range(number): child = create_category(namePrefix + 'child-' + _random_string(3)) child_id = _text(child)['id'] update_category(child_id, { 'parentId': root_id }) categories.append(child_id) print ('created ' + str(number) + ' categories: ' + ','.join(categories)) return categories def create_learning_materials(namePrefix='测试资料-', number=20): categories = create_categories() materials = [] for i in range(number): user = randomly_pick_user() material = create_material({ 'name': namePrefix + _random_string(5), 'userId': randomly_pick_user(), 'description': '好看的资料', 'type': 'ARTICLE', 'contents': '十年高考三年模拟-' + _random_string(20), 'links': [_random_string(3), _random_string(3), _random_string(3)] }) materialId = _text(material)['id'] materials.append(materialId) random_category = categories[random.randint(0, len(categories)-1)] add_material_to_category(random_category, _text(material)) create_comments(materialId) print ('created ' + str(number) + ' materials: ' + ','.join(materials)) return materials def create_comments(materialId, namePrefix='[自动生成]', number=5): dummy_comments = ['我去,这也太酷了!', '我太爱学这个了,老牛了', '这太有意思了,收藏了', '好文,顶一个', '我踩'] for i in range(number): comment = { 'userId': randomly_pick_user(), 'content': dummy_comments[i % len(dummy_comments)] } add_comment(materialId, comment) print('created ' + str(number) + ' comments to material: ' + materialId) def register_user(userData): url = 'http://localhost:8080/user/register' return requests.post(url, json=userData) add_dummy_users() create_questions() create_learning_materials()