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.mockito.Mockito.when;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.trading.mapper.BizMarketStallMapper;
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("StallCodeGenerator")
class StallCodeGeneratorTest
{
    @Mock
    private BizMarketStallMapper mapper;

    @InjectMocks
    private StallCodeGenerator generator;

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

    @Test
    @DisplayName("递增补零")
    void increment()
    {
        when(mapper.selectMaxStallCodeForUpdate()).thenReturn("08");
        assertEquals("09", generator.allocateNextCode());
    }

    @Test
    @DisplayName("编号上限")
    void exhausted()
    {
        when(mapper.selectMaxStallCodeForUpdate()).thenReturn("99");
        assertThrows(ServiceException.class, () -> generator.allocateNextCode());
    }
}
