package cn.iocoder.yudao.module.mes.service.moldrecordsubject;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecordsubject.vo.MoldRecordSubjectPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecordsubject.vo.MoldRecordSubjectSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrecordsubject.MoldRecordSubjectDO;
import cn.iocoder.yudao.module.mes.dal.mysql.moldrecordsubject.MoldRecordSubjectMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MOLD_RECORD_SUBJECT_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

/**
 * {@link MoldRecordSubjectServiceImpl} 的单元测试类
 *
 * @author 内蒙必硕
 */
@Import(MoldRecordSubjectServiceImpl.class)
public class MoldRecordSubjectServiceImplTest extends BaseDbUnitTest {

    @Resource
    private MoldRecordSubjectServiceImpl moldRecordSubjectService;

    @Resource
    private MoldRecordSubjectMapper moldRecordSubjectMapper;

    @Test
    public void testCreateMoldRecordSubject_success() {
        // 准备参数
        MoldRecordSubjectSaveReqVO createReqVO = randomPojo(MoldRecordSubjectSaveReqVO.class).setId(null);

        // 调用
        Long moldRecordSubjectId = moldRecordSubjectService.createMoldRecordSubject(createReqVO);
        // 断言
        assertNotNull(moldRecordSubjectId);
        // 校验记录的属性是否正确
        MoldRecordSubjectDO moldRecordSubject = moldRecordSubjectMapper.selectById(moldRecordSubjectId);
        assertPojoEquals(createReqVO, moldRecordSubject, "id");
    }

    @Test
    public void testUpdateMoldRecordSubject_success() {
        // mock 数据
        MoldRecordSubjectDO dbMoldRecordSubject = randomPojo(MoldRecordSubjectDO.class);
        moldRecordSubjectMapper.insert(dbMoldRecordSubject);// @Sql: 先插入出一条存在的数据
        // 准备参数
        MoldRecordSubjectSaveReqVO updateReqVO = randomPojo(MoldRecordSubjectSaveReqVO.class, o -> {
            o.setId(dbMoldRecordSubject.getId()); // 设置更新的 ID
        });

        // 调用
        moldRecordSubjectService.updateMoldRecordSubject(updateReqVO);
        // 校验是否更新正确
        MoldRecordSubjectDO moldRecordSubject = moldRecordSubjectMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, moldRecordSubject);
    }

    @Test
    public void testUpdateMoldRecordSubject_notExists() {
        // 准备参数
        MoldRecordSubjectSaveReqVO updateReqVO = randomPojo(MoldRecordSubjectSaveReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> moldRecordSubjectService.updateMoldRecordSubject(updateReqVO), MOLD_RECORD_SUBJECT_NOT_EXISTS);
    }

    @Test
    public void testDeleteMoldRecordSubject_success() {
        // mock 数据
        MoldRecordSubjectDO dbMoldRecordSubject = randomPojo(MoldRecordSubjectDO.class);
        moldRecordSubjectMapper.insert(dbMoldRecordSubject);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Long id = dbMoldRecordSubject.getId();

        // 调用
        moldRecordSubjectService.deleteMoldRecordSubject(id);
       // 校验数据不存在了
       assertNull(moldRecordSubjectMapper.selectById(id));
    }

    @Test
    public void testDeleteMoldRecordSubject_notExists() {
        // 准备参数
        Long id = randomLongId();

        // 调用, 并断言异常
        assertServiceException(() -> moldRecordSubjectService.deleteMoldRecordSubject(id), MOLD_RECORD_SUBJECT_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值，然后删除 @Disabled 注解
    public void testGetMoldRecordSubjectPage() {
       // mock 数据
       MoldRecordSubjectDO dbMoldRecordSubject = randomPojo(MoldRecordSubjectDO.class, o -> { // 等会查询到
           o.setSubject(null);
           o.setExpect(null);
       });
       moldRecordSubjectMapper.insert(dbMoldRecordSubject);
       // 测试 subject 不匹配
       moldRecordSubjectMapper.insert(cloneIgnoreId(dbMoldRecordSubject, o -> o.setSubject(null)));
       // 测试 expect 不匹配
       moldRecordSubjectMapper.insert(cloneIgnoreId(dbMoldRecordSubject, o -> o.setExpect(null)));
       // 准备参数
       MoldRecordSubjectPageReqVO reqVO = new MoldRecordSubjectPageReqVO();
       reqVO.setSubject(null);
       reqVO.setExpect(null);

       // 调用
       PageResult<MoldRecordSubjectDO> pageResult = moldRecordSubjectService.getMoldRecordSubjectPage(reqVO);
       // 断言
       assertEquals(1, pageResult.getTotal());
       assertEquals(1, pageResult.getList().size());
       assertPojoEquals(dbMoldRecordSubject, pageResult.getList().get(0));
    }

}