mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
test: 简化测试代码。
This commit is contained in:
parent
c7e06c59f9
commit
8724fc0b42
@ -21,6 +21,8 @@ import com.mybatisflex.core.query.QueryWrapper;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.column;
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.number;
|
||||||
import static com.mybatisflex.core.query.QueryMethods.sum;
|
import static com.mybatisflex.core.query.QueryMethods.sum;
|
||||||
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
|
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
|
||||||
|
|
||||||
@ -31,10 +33,6 @@ public class ArithmeticQueryColumnTest {
|
|||||||
return dialect.forSelectByQuery(queryWrapper);
|
return dialect.forSelectByQuery(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testAddWithCondition(){
|
|
||||||
|
|
||||||
}
|
|
||||||
@Test
|
@Test
|
||||||
public void testAdd() {
|
public void testAdd() {
|
||||||
QueryWrapper query = new QueryWrapper()
|
QueryWrapper query = new QueryWrapper()
|
||||||
@ -145,5 +143,18 @@ public class ArithmeticQueryColumnTest {
|
|||||||
Assert.assertEquals(sql, "SELECT (`id` / 100) AS `x100` FROM `tb_account`");
|
Assert.assertEquals(sql, "SELECT (`id` / 100) AS `x100` FROM `tb_account`");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testComplexAdd() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(column("ABS(?)", -1).add(number(7)))
|
||||||
|
.from(ACCOUNT);
|
||||||
|
|
||||||
|
String sql = queryWrapper.toSQL();
|
||||||
|
|
||||||
|
System.out.println(sql);
|
||||||
|
|
||||||
|
Assert.assertEquals("SELECT ABS(-1) + 7 FROM `tb_account`", sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,71 +0,0 @@
|
|||||||
package com.mybatisflex.test;
|
|
||||||
|
|
||||||
import com.mybatisflex.annotation.Id;
|
|
||||||
import com.mybatisflex.annotation.KeyType;
|
|
||||||
import com.mybatisflex.annotation.Table;
|
|
||||||
import com.mybatisflex.core.activerecord.Model;
|
|
||||||
import com.mybatisflex.core.keygen.KeyGenerators;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
@Table("demo")
|
|
||||||
public class Demo extends Model<Demo> {
|
|
||||||
@Id(keyType = KeyType.Generator, value = KeyGenerators.snowFlakeId)
|
|
||||||
private String id;
|
|
||||||
|
|
||||||
public static Demo create() {
|
|
||||||
return new Demo();
|
|
||||||
}
|
|
||||||
|
|
||||||
private BigDecimal firstField;
|
|
||||||
private String userId;
|
|
||||||
private BigDecimal secondField;
|
|
||||||
private String foreignId;
|
|
||||||
|
|
||||||
public String getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Demo setId(String id) {
|
|
||||||
this.id = id;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BigDecimal getFirstField() {
|
|
||||||
return firstField;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Demo setFirstField(BigDecimal firstField) {
|
|
||||||
this.firstField = firstField;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Demo setUserId(String userId) {
|
|
||||||
this.userId = userId;
|
|
||||||
return this;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public BigDecimal getSecondField() {
|
|
||||||
return secondField;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Demo setSecondField(BigDecimal secondField) {
|
|
||||||
this.secondField = secondField;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getForeignId() {
|
|
||||||
return foreignId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Demo setForeignId(String foreignId) {
|
|
||||||
this.foreignId = foreignId;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
package com.mybatisflex.test;
|
|
||||||
|
|
||||||
import com.mybatisflex.core.query.QueryMethods;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
public class ArithmeticQueryColumnTest {
|
|
||||||
@Test
|
|
||||||
public void testAddWithCondition() {
|
|
||||||
BigDecimal bigDecimal = Demo.create()
|
|
||||||
.select(QueryMethods.sum(Demo::getFirstField)
|
|
||||||
.add(QueryMethods.sum(QueryMethods.if_(QueryMethods.column(Demo::getForeignId).eq("1"), QueryMethods.column(Demo::getSecondField), QueryMethods.number(0)))))
|
|
||||||
.where(Demo::getUserId).eq("1").objAsOpt(BigDecimal.class).orElse(BigDecimal.ZERO);
|
|
||||||
System.out.println(bigDecimal);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
CREATE TABLE `demo`
|
|
||||||
(
|
|
||||||
`id` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
|
|
||||||
`first_field` decimal(10, 2) DEFAULT NULL,
|
|
||||||
`foreign_id` varchar(255) collate utf8mb4_general_ci default null,
|
|
||||||
second_field decimal(10, 2) DEFAULT NULL,
|
|
||||||
user_id varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
|
|
||||||
PRIMARY KEY (`id`)
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
|
||||||
|
|
||||||
|
|
||||||
INSERT INTO `demo` (`id`, `first_field`, `foreign_id`, second_field, user_id)
|
|
||||||
values ('1', 10.12, '1', 12.2, '1');
|
|
||||||
INSERT INTO `demo` (`id`, `first_field`, `foreign_id`, second_field, user_id)
|
|
||||||
values ('2', 10.12, '2', 12.2, '1');
|
|
||||||
INSERT INTO `demo` (`id`, `first_field`, `foreign_id`, second_field, user_id)
|
|
||||||
values ('3', 10.12, '3', 12.2, '1');
|
|
||||||
INSERT INTO `demo` (`id`, `first_field`, `foreign_id`, second_field, user_id)
|
|
||||||
values ('4', 10.12, '4', 12.2, '1');
|
|
||||||
INSERT INTO `demo` (`id`, `first_field`, `foreign_id`, second_field, user_id)
|
|
||||||
values ('5', 10.12, '5', 12.2, '1');
|
|
||||||
INSERT INTO `demo` (`id`, `first_field`, `foreign_id`, second_field, user_id)
|
|
||||||
values ('6', 10.12, '6', NUll, '1');
|
|
||||||
INSERT INTO `demo` (`id`, `first_field`, `foreign_id`, second_field, user_id)
|
|
||||||
values ('7', NULL, '7', 12.2, '1');
|
|
||||||
Loading…
x
Reference in New Issue
Block a user