测试类数据随机

beetlsql3-dev
Mlxa0324 3 years ago
parent a4d4ed77ee
commit 2c9a894840

@ -2,8 +2,11 @@ package com.ibeetl.jlw.web;
import base.BaseTest;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.ibeetl.admin.test.util.test.TestEnvUtil;
import com.ibeetl.jlw.dao.ResourcesQuestionSnapshotDao;
import com.ibeetl.jlw.dao.StudentDao;
import com.ibeetl.jlw.entity.ResourcesQuestionSnapshot;
import com.ibeetl.jlw.entity.Student;
import com.ibeetl.jlw.enums.TeacherOpenCourseQuestionLogFromTypeEnum;
import org.junit.Assert;
@ -15,9 +18,12 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import static cn.hutool.json.JSONUtil.toJsonStr;
import static com.ibeetl.admin.test.util.test.RandomUtils.randomSet;
import static org.apache.commons.lang3.StringUtils.join;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class TeacherOpenCourseQuestionLogControllerTest extends BaseTest {
@ -27,6 +33,8 @@ class TeacherOpenCourseQuestionLogControllerTest extends BaseTest {
@Resource
StudentDao studentDao;
@Resource
ResourcesQuestionSnapshotDao resourcesQuestionSnapshotDao;
/**
*
@ -38,6 +46,39 @@ class TeacherOpenCourseQuestionLogControllerTest extends BaseTest {
void addQuestionLog() throws Exception {
String params = "{\"1574684340254842880\": [\"错\"], \"1574684340271620097\": [\"D\",\"A\", \"C\"]}";
// 通过查询数据库来random实体类属性
// 随机取一个学生的登录信息
List<ResourcesQuestionSnapshot> questionSnapshots = resourcesQuestionSnapshotDao.execute(
"select t.* \n" +
"from resources_question_snapshot t \n" +
"where t.question_status = 1\t\n" +
"order by RAND() \n" +
"limit 10 ");
// 随机取题目ID和答案
Map<Long, TreeSet<String>> answerRandomMap = new ConcurrentHashMap<>();
questionSnapshots.stream().forEach(item -> {
TreeSet<String> answer = new TreeSet<>();
// 单选
if (item.getQuestionType().equals(1)) {
answer.add(RandomUtil.randomEle(new String[]{"A", "B", "C", "D"}));
}
// 多选
if (item.getQuestionType().equals(2)) {
String[] multipleOptions = ObjectUtil.isEmpty(item.getQuestionOptionE())
? new String[]{"A", "B", "C", "D"} : new String[]{"A", "B", "C", "D", "E"};
Set<String> randomAnswerSet = RandomUtil
.randomEleSet(Arrays.asList(multipleOptions), RandomUtil.randomInt(2, multipleOptions.length));
// 取前N项
answer.add(join(new TreeSet<>(randomAnswerSet).toArray(), ","));
}
// 判断
if (item.getQuestionType().equals(3)) {
answer.add(RandomUtil.randomEle(Arrays.asList("对", "错")));
}
answerRandomMap.put(item.getResourcesQuestionSnapshotId(), answer);
});
// 随机取一个学生的登录信息
List<Student> studentList = studentDao.execute(
"select org_id, user_id " +
@ -60,7 +101,7 @@ class TeacherOpenCourseQuestionLogControllerTest extends BaseTest {
RequestBuilder rb = MockMvcRequestBuilders.post(MODEL + "/addQuestionLog.json")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.param("fromType", fromTypeEnum.name())
.content(params);
.content(toJsonStr(answerRandomMap));
//发送请求,验证返回结果
String result = mvc.perform(rb)
.andExpect(status().isOk())

Loading…
Cancel
Save