package com.ylx.shopingfundsdetail.mapper;

import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

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

public class ShoppingFundsDetailMapperXmlTest {

    @Test
    public void pcExpenseShoppingFundsSqlFiltersByUserExpenseTypeTimeAndSortsByCreateTimeDesc() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectPcExpenseShoppingFundsDetail"));
        assertTrue(xml.contains("selectPcExpenseShoppingFundsSummary"));
        assertTrue(xml.contains("d.user_id = #{dto.userId}"));
        assertTrue(xml.contains("d.expense_type = 1"));
        assertTrue(xml.contains("d.create_time &gt;= #{dto.startTime}"));
        assertTrue(xml.contains("d.create_time &lt;= #{dto.endTime}"));
        assertTrue(xml.contains("ORDER BY"));
        assertTrue(xml.contains("d.create_time DESC"));
    }

    private String readMapperXml() throws Exception {
        try (InputStream inputStream = getClass().getClassLoader()
                .getResourceAsStream("mapper/giftCard/ShoppingFundsDetailMapper.xml")) {
            assertNotNull(inputStream, "ShoppingFundsDetailMapper.xml should exist in test classpath");
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            return new String(bytes, StandardCharsets.UTF_8);
        }
    }
}
