package com.ylx.point.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 PointUserLogMapperXmlTest {

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

        assertTrue(xml.contains("selectPcUserPointDetailList"));
        assertTrue(xml.contains("pul.open_id = #{dto.openId}"));
        assertTrue(xml.contains("dto.pointProject != '全部'"));
        assertTrue(xml.contains("COALESCE(NULLIF(pul.activity_name, ''), pul.task_name) = #{dto.pointProject}"));
        assertTrue(xml.contains("pul.create_time &gt;= #{dto.startTime}"));
        assertTrue(xml.contains("pul.create_time &lt;= #{dto.endTime}"));
        assertTrue(xml.contains("ORDER BY"));
        assertTrue(xml.contains("pul.create_time DESC"));
    }

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