Skip to content

Commit

Permalink
[test] test case update
Browse files Browse the repository at this point in the history
  • Loading branch information
VampireAchao committed Jun 25, 2023
1 parent a53087a commit 6000c1f
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 315 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,25 @@ public static String getGetterName(String propertyName) {
* @param <T> 对象类型
*/
public static <T, R> R copyProperties(T source, R target) {
if (source == null || target ==null) {
if (source == null || target == null) {
return target;
}

Class<T> sourceType = SerFunc.<Class<?>, Class<T>>cast().apply(source.getClass());
Map<String, Map.Entry<SerFunc<T, Object>, SerBiCons<T, Object>>> sourcePropertyGetterSetterMap = LambdaHelper.getPropertyGetterSetterMap(sourceType);
Map<String, Map.Entry<SerFunc<T, Object>, SerBiCons<T, Object>>> sourcePropertyGetterSetterMap =
LambdaHelper.getPropertyGetterSetterMap(sourceType);

Class<R> targetType = SerFunc.<Class<?>, Class<R>>cast().apply(target.getClass());
Map<String, Map.Entry<SerFunc<R, Object>, SerBiCons<R, Object>>> targetPropertyGetterSetterMap = LambdaHelper.getPropertyGetterSetterMap(targetType);
Map<String, Map.Entry<SerFunc<R, Object>, SerBiCons<R, Object>>> targetPropertyGetterSetterMap =
LambdaHelper.getPropertyGetterSetterMap(targetType);

for (Map.Entry<String, Map.Entry<SerFunc<T, Object>, SerBiCons<T, Object>>> sourceEntry : sourcePropertyGetterSetterMap.entrySet()) {
for (Map.Entry<String, Map.Entry<SerFunc<T, Object>, SerBiCons<T, Object>>> sourceEntry :
sourcePropertyGetterSetterMap.entrySet()) {
String property = sourceEntry.getKey();
Map.Entry<SerFunc<T, Object>, SerBiCons<T, Object>> sourceGetterSetter = sourceEntry.getValue();
Map.Entry<SerFunc<R, Object>, SerBiCons<R, Object>> targetGetterSetter = targetPropertyGetterSetterMap.get(property);
Map.Entry<SerFunc<T, Object>, SerBiCons<T, Object>> sourceGetterSetter =
sourceEntry.getValue();
Map.Entry<SerFunc<R, Object>, SerBiCons<R, Object>> targetGetterSetter =
targetPropertyGetterSetterMap.get(property);

if (targetGetterSetter == null) {
continue;
Expand All @@ -150,7 +155,9 @@ public static <T, R> R copyProperties(T source, R target) {
LambdaExecutable sourceGetterLambda = LambdaHelper.resolve(sourceGetter);
LambdaExecutable targetGetterLambda = LambdaHelper.resolve(targetGetter);

if (!Opp.of(sourceGetterLambda.getReturnType()).map(Type::getTypeName).equals(Opp.of(targetGetterLambda.getReturnType()).map(Type::getTypeName))) {
if (!Opp.of(sourceGetterLambda.getReturnType())
.map(Type::getTypeName)
.equals(Opp.of(targetGetterLambda.getReturnType()).map(Type::getTypeName))) {
continue;
}

Expand All @@ -161,7 +168,6 @@ public static <T, R> R copyProperties(T source, R target) {
return target;
}


public static <T, R> R copyProperties(T source, Class<R> targetType) {
R target = ReflectHelper.newInstance(targetType);
if (Objects.isNull(source)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.test.autoconfigure.MybatisPlusTest;
Expand All @@ -34,8 +35,14 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author VampireAchao
* @author VampireAchao Cason
* @since 2023/4/14
*/
@MybatisPlusTest
Expand Down Expand Up @@ -67,7 +74,219 @@ void test() {
Assertions.assertEquals("阿超", dbUser.getName().getNickname());
}

public static class JsonFieldHandler extends AbstractJsonFieldHandler {
@Test
void eqTest() {
Name name = new Name();
name.setUsername("VampireAchao");
name.setNickname("阿超");

UserInfoWithJsonName user = new UserInfoWithJsonName();
user.setName(name);
Database.saveFewSql(Lists.of(user));
Database.updateFewSql(Lists.of(user));

QueryCondition<UserInfoWithJsonName> wrapper =
QueryCondition.query(UserInfoWithJsonName.class).eq(UserInfoWithJsonName::getName, name);
val list = Database.list(wrapper);

assertEquals(1, list.size(), "Query should return exactly one result");
assertEquals(
name, list.get(0).getName(), "Returned user's name should match the expected name");
}

@Test
void activeEqTest() {
Name name = new Name();
name.setUsername("Cason");
name.setNickname("JAY");

UserInfoWithJsonName user = new UserInfoWithJsonName();
user.setName(name);

Database.saveFewSql(Lists.of(user));

QueryCondition<UserInfoWithJsonName> wrapper =
QueryCondition.query(UserInfoWithJsonName.class)
.activeEq(UserInfoWithJsonName::getName, name);

val list = Database.list(wrapper);

assertEquals(1, list.size(), "Query should return exactly one result");
assertEquals(
name.getUsername(),
list.get(0).getName().getUsername(),
"Returned user's username should match the expected username");
}

@Test
void orTest() {
Name name1 = new Name();
name1.setUsername("Cason");
name1.setNickname("JAY");

Name name2 = new Name();
name2.setUsername("Alice");
name2.setNickname("AL");

Name name3 = new Name();
name3.setUsername("Bob");
name3.setNickname("BB");

UserInfoWithJsonName user1 = new UserInfoWithJsonName();
user1.setName(name1);

UserInfoWithJsonName user2 = new UserInfoWithJsonName();
user2.setName(name2);

UserInfoWithJsonName user3 = new UserInfoWithJsonName();
user3.setName(name3);

Database.saveFewSql(Lists.of(user1, user2, user3));

QueryCondition<UserInfoWithJsonName> wrapper =
(QueryCondition<UserInfoWithJsonName>)
QueryCondition.query(UserInfoWithJsonName.class)
.in(UserInfoWithJsonName::getName, Lists.of(name1, name3))
.or(i -> i.eq(UserInfoWithJsonName::getName, user2.getName()));

val list = Database.list(wrapper);

assertEquals(3, list.size(), "Query should return exactly two results");

List<String> usernames =
list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList());

assertTrue(
usernames.contains(name1.getUsername()),
"Returned users should contain the first expected username");
assertTrue(
usernames.contains(name3.getUsername()),
"Returned users should contain the third expected username");
assertTrue(
usernames.contains(name2.getUsername()),
"Returned users should not contain the second username");
}

@Test
void InTest() {
Name name1 = new Name();
name1.setUsername("Cason");
name1.setNickname("JAY");

Name name2 = new Name();
name2.setUsername("Alice");
name2.setNickname("AL");

Name name3 = new Name();
name3.setUsername("Bob");
name3.setNickname("BB");

UserInfoWithJsonName user1 = new UserInfoWithJsonName();
user1.setName(name1);

UserInfoWithJsonName user2 = new UserInfoWithJsonName();
user2.setName(name2);

UserInfoWithJsonName user3 = new UserInfoWithJsonName();
user3.setName(name3);

Database.saveFewSql(Lists.of(user1, user2, user3));

QueryCondition<UserInfoWithJsonName> wrapper =
QueryCondition.query(UserInfoWithJsonName.class)
.activeIn(UserInfoWithJsonName::getName, Lists.of(name1, name3));

val list = Database.list(wrapper);

assertEquals(2, list.size(), "Query should return exactly two results");

List<String> usernames =
list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList());

assertTrue(
usernames.contains(name1.getUsername()),
"Returned users should contain the first expected username");
assertTrue(
usernames.contains(name3.getUsername()),
"Returned users should contain the third expected username");
assertFalse(
usernames.contains(name2.getUsername()),
"Returned users should not contain the second username");
}

@Test
void activeInTest() {
Name name1 = new Name();
name1.setUsername("Cason");
name1.setNickname("JAY");

Name name2 = new Name();
name2.setUsername("Alice");
name2.setNickname("AL");

Name name3 = new Name();
name3.setUsername("Bob");
name3.setNickname("BB");

UserInfoWithJsonName user1 = new UserInfoWithJsonName();
user1.setName(name1);

UserInfoWithJsonName user2 = new UserInfoWithJsonName();
user2.setName(name2);

UserInfoWithJsonName user3 = new UserInfoWithJsonName();
user3.setName(name3);

Database.saveFewSql(Lists.of(user1, user2, user3));

QueryCondition<UserInfoWithJsonName> wrapper =
QueryCondition.query(UserInfoWithJsonName.class)
.activeIn(UserInfoWithJsonName::getName, Lists.of(name1, name3));

val list = Database.list(wrapper);

assertEquals(2, list.size(), "Query should return exactly two results");

List<String> usernames =
list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList());

assertTrue(
usernames.contains(name1.getUsername()),
"Returned users should contain the first expected username");
assertTrue(
usernames.contains(name3.getUsername()),
"Returned users should contain the third expected username");
assertFalse(
usernames.contains(name2.getUsername()),
"Returned users should not contain the second username");
}

@Test
void selectTest() {
Name name = new Name();
name.setUsername("VampireAchao");
name.setNickname("阿超");

UserInfoWithJsonName user = new UserInfoWithJsonName();
user.setName(name);
Database.saveFewSql(Lists.of(user));
Database.updateFewSql(Lists.of(user));

LambdaQueryWrapper<UserInfoWithJsonName> wrapper =
QueryCondition.query(UserInfoWithJsonName.class)
.select(UserInfoWithJsonName::getName)
.eq(UserInfoWithJsonName::getName, name);
val list = Database.list(wrapper);
assertEquals(1, list.size(), "Query should return exactly one result");

UserInfoWithJsonName dbUser = list.get(0);
assertEquals(
user.getName().getUsername(), dbUser.getName().getUsername(), "Username should match");
assertEquals(
user.getName().getNickname(), dbUser.getName().getNickname(), "Nickname should match");
}

public static class JsonFieldHandler extends AbstractJsonFieldHandler<Object> {

ObjectMapper objectMapper = new ObjectMapper();

Expand All @@ -94,8 +313,13 @@ static class UserInfoWithJsonName {
}

@Data
static class Name {
static class Name implements Serializable, Comparable<Name> {
private String username;
private String nickname;

@Override
public int compareTo(Name o) {
return username.compareTo(o.username);
}
}
}
Loading

0 comments on commit 6000c1f

Please sign in to comment.