package com.ruoyi.web.modules.trading.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.trading.mapper.BizDistributorMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
@DisplayName("DistributorCodeGenerator")
class DistributorCodeGeneratorTest
{
    @Mock
    private BizDistributorMapper mapper;

    @InjectMocks
    private DistributorCodeGenerator generator;

    @Test
    @DisplayName("首条编号为01")
    void firstCode()
    {
        when(mapper.selectMaxDistributorCodeForUpdate()).thenReturn(null);
        assertEquals("01", generator.allocateNextCode());
    }

    @Test
    @DisplayName("编号用尽")
    void codeExhausted()
    {
        when(mapper.selectMaxDistributorCodeForUpdate()).thenReturn("99");
        ServiceException ex = assertThrows(ServiceException.class, () -> generator.allocateNextCode());
        assertTrue(ex.getMessage().contains("上限"));
    }
}
