package com.ruoyi.web.modules.circulation.mapper;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("BizTradeMarketMapper.xml")
class BizTradeMarketMapperXmlTest
{

    @Test
    @DisplayName("名称模糊 SQL")
    void nameLike() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("market_name like concat('%', #{marketName}, '%')"));
    }

    @Test
    @DisplayName("运营状态精确 SQL")
    void operateStatusExact() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("operate_status = #{operateStatus}"));
    }

    @Test
    @DisplayName("列表排序")
    void orderBy() throws Exception
    {
        String xml = readMapperXml();
        assertTrue(xml.contains("order by create_time desc, id desc"));
        assertTrue(xml.contains("del_flag = '0'"));
    }

    private static String readMapperXml()
    {
        try (InputStream in = BizTradeMarketMapperXmlTest.class.getClassLoader()
                .getResourceAsStream("mapper/circulation/BizTradeMarketMapper.xml"))
        {
            if (in == null)
            {
                throw new IllegalStateException("mapper xml not found");
            }
            return IOUtils.toString(in, StandardCharsets.UTF_8);
        }
        catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }
    }
}
