package com.baomidou.mybatisplus.test.h2;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.test.h2.entity.H2Student;
import com.baomidou.mybatisplus.test.h2.mapper.H2StudentMapper;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * OrderBy LastSql 混合测试
 *
 * @author Dervish
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = {"classpath:h2/spring-test-h2.xml"})
public class LastSqlTest {

    @Resource
    private H2StudentMapper mapper;

    @Test
    @Order(1)
    public void delete(){
        int res = mapper.delete(new LambdaUpdateWrapper<H2Student>().last(" and 1 = 2 ").eq(H2Student::getId, 0));
        Assertions.assertTrue(res <= 0);
    }

    @Test
    @Order(2)
    public void selectCount(){
        long result = mapper.selectCount(new LambdaQueryWrapper<H2Student>().last("where 1 =2"));
        Assertions.assertTrue(result <= 0);
    }

    @Test
    @Order(3)
    public void selectList(){
        List<H2Student> h2Students = mapper.selectList(new LambdaQueryWrapper<H2Student>().last("limit 1"));
        Assertions.assertTrue(h2Students.size() == 1);
    }

    @Test
    @Order(4)
    public void selectMaps(){
        List<Map<String, Object>> maps = mapper.selectMaps(new LambdaQueryWrapper<H2Student>().last("limit 1"));
        Assertions.assertTrue(maps.size() == 1);
    }

    @Test
    @Order(5)
    public void selectMapsPage(){
        IPage page = Page.of(0, 10);
        IPage res = mapper.selectMapsPage(page,new QueryWrapper<H2Student>().last(" /* testSql */ ").comment("test"));
    }

    @Test
    @Order(6)
    public void selectObjs(){
        List<Object> objects = mapper.selectObjs(new QueryWrapper<H2Student>().last(" limit 1"));
        Assertions.assertTrue(objects.size() == 1);
    }

    @Test
    @Order(7)
    public void SelectOne(){
        H2Student h2Student = mapper.selectOne(new QueryWrapper<H2Student>().last(" where 1 = 2"));
        Assertions.assertTrue(h2Student == null);
    }

    @Test
    @Order(8)
    public void selectPage(){
        IPage page = Page.of(0, 10);
        mapper.selectPage(page,new QueryWrapper<H2Student>().last(" /* testSql */ "));
    }

    @Test
    @Order(9)
    public void update(){
        int res = mapper.update(null, new UpdateWrapper<H2Student>().set("name","dog").last(" where 1 =2 "));
        Assertions.assertTrue(res <=  0);
    }
}

@@ -1200,24 +1200,25 @@
 oid delete()
+ 
 %7B%0A        in
@@ -1423,16 +1423,17 @@
 tCount()
+ 
 %7B%0A      
@@ -1627,16 +1627,17 @@
 ctList()
+ 
 %7B%0A      
@@ -1853,16 +1853,17 @@
 ctMaps()
+ 
 %7B%0A      
@@ -2077,24 +2077,25 @@
 ctMapsPage()
+ 
 %7B%0A        IP
@@ -2132,20 +2132,8 @@
     
- IPage res =
  map
@@ -2148,32 +2148,33 @@
 ctMapsPage(page,
+ 
 new QueryWrapper
@@ -2288,16 +2288,17 @@
 ctObjs()
+ 
 %7B%0A      
@@ -2499,16 +2499,17 @@
 ectOne()
+ 
 %7B%0A      
@@ -2711,16 +2711,17 @@
 ctPage()
+ 
 %7B%0A      
@@ -2782,16 +2782,17 @@
 ge(page,
+ 
 new Quer
@@ -2894,16 +2894,17 @@
 update()
+ 
 %7B%0A      
@@ -2977,16 +2977,17 @@
 (%22name%22,
+ 
 %22dog%22).l
@@ -3045,18 +3045,807 @@
 (res %3C= 
- 
 0
+);%0A    %7D%0A%0A    @Test%0A    @Order(10)%0A    public void selectListOrderBy() %7B%0A        List%3CH2Student%3E h2Students = mapper.selectList(null);%0A        Assertions.assertEquals(h2Students.size(), 6);%0A    %7D%0A%0A    @Test%0A    @Order(11)%0A    public void selectPageOrderBy() %7B%0A        mapper.selectPage(Page.of(0, 10), null);%0A    %7D%0A%0A    @Test%0A    @Order(12)%0A    public void selectMapsOrderBy() %7B%0A        List%3CMap%3CString, Object%3E%3E maps = mapper.selectMaps(null);%0A        Assertions.assertEquals(maps.size(), 6);%0A    %7D%0A%0A    @Test%0A    @Order(12)%0A    public void selectMapsPageOrderBy() %7B%0A        mapper.selectMapsPage(Page.of(0, 10), null);%0A    %7D%0A%0A    @Test%0A    @Order(12)%0A    public void selectObjsOrderBy() %7B%0A        List%3CObject%3E objs = mapper.selectObjs(null);%0A        Assertions.assertEquals(objs.size(), 6
 );%0A    %7D

添加存在 order by 测试方法