package com.njcn.gather.aireport.testdevice.mapper;

import org.apache.ibatis.annotations.Select;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.util.Arrays;

class TestDeviceMapperTest {

    @Test
    void selectQueriesShouldContainUpdateUserNameMapping() throws Exception {
        assertSelectContainsUpdateUserName("selectDevicePage");
        assertSelectContainsUpdateUserName("selectDeviceDetail");
    }

    private void assertSelectContainsUpdateUserName(String methodName) throws Exception {
        Method method = Arrays.stream(TestDeviceMapper.class.getMethods())
                .filter(item -> item.getName().equals(methodName))
                .findFirst()
                .orElseThrow(NoSuchMethodException::new);
        Select select = method.getAnnotation(Select.class);
        Assertions.assertNotNull(select);
        String sql = String.join(" ", select.value());
        Assertions.assertTrue(sql.contains("updateByName"),
                methodName + " should select updateByName");
        Assertions.assertTrue(sql.contains("d.update_by = updater.id"),
                methodName + " should join update user");
    }
}
