package com.cf.imes.module.system.service.funds.incomeandexpense;

import com.cf.imes.framework.test.core.ut.BaseMockitoUnitTest;
import com.cf.imes.module.system.dal.dataobject.incomeandexpense.IncomeExpenseDetailsDO;
import com.cf.imes.module.system.dal.mysql.incomeandexpense.IncomeExpenseDetailsMapper;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * {@link IncomeExpenseServiceImpl} 的单元测试类
 *
 * @author Gqr
 */
class IncomeExpenseServiceImplTest extends BaseMockitoUnitTest {

    @InjectMocks
    private IncomeExpenseServiceImpl incomeExpenseService;

    @Mock
    private IncomeExpenseDetailsMapper incomeExpenseDetailsMapper;

    @Test
    void createDetail_success() {
        // given
        IncomeExpenseDetailsDO detailDO = new IncomeExpenseDetailsDO();

        doAnswer(invocation -> {
            IncomeExpenseDetailsDO arg = invocation.getArgument(0);
            arg.setId(100L); // 手动回填 ID
            return 1;
        }).when(incomeExpenseDetailsMapper).insert(any(IncomeExpenseDetailsDO.class));

        // when
        Long id = incomeExpenseService.createDetail(detailDO);

        // then
        assertNotNull(id);
        assertEquals(100L, id);

        verify(incomeExpenseDetailsMapper, times(1))
                .insert(any(IncomeExpenseDetailsDO.class));
    }
}