Skip to content

Commit

Permalink
增加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 1, 2025
1 parent 332c641 commit f9953d4
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include: ../../../TestDynCodeGenRelation/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SID,RELATION_NAME,ENTITY_NAME1,ENTITY_ID1,ENTITY_NAME2,ENTITY_ID2,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MODULE_ID,MODULE_NAME,MODULE_VERSION,DISPLAY_NAME,BASE_MODULE_ID,BASE_PACKAGE_NAME,ENTITY_PACKAGE_NAME,MAVEN_GROUP_ID,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROP_META_ID,ENTITY_META_ID,IS_MANDATORY,PROP_NAME,DISPLAY_NAME,STD_SQL_TYPE,PRECISION,SCALE,PROP_ID,UI_SHOW,UI_CONTROL,DOMAIN_ID,STD_DOMAIN_NAME,DICT_NAME,DYN_PROP_MAPPING,TAGS_TEXT,DEFAULT_VALUE,EXT_CONFIG,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SEQ_NAME,SEQ_TYPE,IS_UUID,NEXT_VALUE,STEP_SIZE,CACHE_SIZE,MAX_VALUE,RESET_TYPE,DEL_FLAG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_chgType,ENTITY_META_ID,MODULE_ID,ENTITY_NAME,DISPLAY_NAME,TABLE_NAME,QUERY_SPACE,STORE_TYPE,TAGS_TEXT,IS_EXTERNAL,STATUS,EXT_CONFIG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,@var:NopDynEntityMeta@entityMetaId,@var:NopDynModule@moduleId,UserEntity,User Entity,,,10,,false,1,,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynEntityMeta@entityMetaId_1,@var:NopDynModule@moduleId,RoleEntity,Role Entity,,,10,,false,1,,0,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_chgType,REL_META_ID,ENTITY_META_ID,REF_ENTITY_META_ID,RELATION_NAME,RELATION_DISPLAY_NAME,RELATION_TYPE,MIDDLE_TABLE_NAME,MIDDLE_ENTITY_NAME,LEFT_PROP_NAME,RIGHT_PROP_NAME,REF_SET_KEY_PROP,REF_SET_SORT,STATUS,TAGS_TEXT,EXT_CONFIG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,@var:NopDynEntityRelationMeta@relMetaId,@var:NopDynEntityMeta@entityMetaId,@var:NopDynEntityMeta@entityMetaId_1,userRoles,测试用户对多对关联,m2m,,,sid,userId,,,1,pub,,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynEntityRelationMeta@relMetaId_1,@var:NopDynEntityMeta@entityMetaId_1,@var:NopDynEntityMeta@entityMetaId,roleUsers,测试角色对多对关联,m2m,,,sid,roleId,,,1,pub,,0,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_chgType,MODULE_ID,MODULE_NAME,MODULE_VERSION,DISPLAY_NAME,BASE_MODULE_ID,BASE_PACKAGE_NAME,ENTITY_PACKAGE_NAME,MAVEN_GROUP_ID,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME
A,@var:NopDynModule@moduleId,relation-demo,1,Demo Module,,,,,10,0,autotest-ref,*,autotest-ref,*
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_chgType,PROP_META_ID,ENTITY_META_ID,IS_MANDATORY,PROP_NAME,DISPLAY_NAME,STD_SQL_TYPE,PRECISION,SCALE,PROP_ID,UI_SHOW,UI_CONTROL,DOMAIN_ID,STD_DOMAIN_NAME,DICT_NAME,DYN_PROP_MAPPING,TAGS_TEXT,DEFAULT_VALUE,EXT_CONFIG,STATUS,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,@var:NopDynPropMeta@propMetaId_3,@var:NopDynEntityMeta@entityMetaId_1,true,roleName,roleName Display,VARCHAR,100,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynPropMeta@propMetaId_2,@var:NopDynEntityMeta@entityMetaId,true,roleId,roleId Display,VARCHAR,100,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynPropMeta@propMetaId_1,@var:NopDynEntityMeta@entityMetaId,true,userAge,userAge Display,INTEGER,0,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynPropMeta@propMetaId,@var:NopDynEntityMeta@entityMetaId,true,userName,userName Display,VARCHAR,100,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
A,@var:NopDynPropMeta@propMetaId_4,@var:NopDynEntityMeta@entityMetaId_1,true,roleKey,roleKey Display,VARCHAR,100,0,1,,,,,,,,,,1,0,autotest-ref,*,autotest-ref,*,
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_chgType,SEQ_NAME,SEQ_TYPE,IS_UUID,NEXT_VALUE,STEP_SIZE,CACHE_SIZE,MAX_VALUE,RESET_TYPE,DEL_FLAG,VERSION,CREATED_BY,CREATE_TIME,UPDATED_BY,UPDATE_TIME,REMARK
A,default,seq,0,1,1,100,,,0,0,autotest-ref,*,autotest-ref,*,
6 changes: 6 additions & 0 deletions nop-dyn/nop-dyn-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<groupId>io.github.entropy-cloud</groupId>
<artifactId>nop-biz</artifactId>
</dependency>

<dependency>
<groupId>io.github.entropy-cloud</groupId>
<artifactId>nop-graphql-orm</artifactId>
</dependency>

<dependency>
<groupId>io.github.entropy-cloud</groupId>
<artifactId>nop-file-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
/**
* Copyright (c) 2017-2024 Nop Platform. All rights reserved.
* Author: [email protected]
* Blog: https://www.zhihu.com/people/canonical-entropy
* Gitee: https://gitee.com/canonical-entropy/nop-entropy
* Github: https://github.com/entropy-cloud/nop-entropy
*/
package io.nop.dyn.service.codegen;

import io.nop.api.core.annotations.autotest.EnableSnapshot;
import io.nop.api.core.annotations.autotest.NopTestConfig;
import io.nop.api.core.annotations.core.Description;
import io.nop.api.core.beans.ApiRequest;
import io.nop.api.core.beans.ApiResponse;
import io.nop.api.core.beans.FieldSelectionBean;
import io.nop.api.core.util.FutureHelper;
import io.nop.autotest.junit.JunitAutoTestCase;
import io.nop.commons.type.StdSqlType;
import io.nop.core.reflect.bean.BeanTool;
import io.nop.dao.api.IDaoProvider;
import io.nop.dao.jdbc.IJdbcTemplate;
import io.nop.dyn.dao.NopDynDaoConstants;
import io.nop.dyn.dao.entity.NopDynEntityMeta;
import io.nop.dyn.dao.entity.NopDynEntityRelationMeta;
import io.nop.dyn.dao.entity.NopDynModule;
import io.nop.dyn.dao.entity.NopDynPropMeta;
import io.nop.graphql.core.IGraphQLExecutionContext;
import io.nop.graphql.core.ast.GraphQLOperationType;
import io.nop.graphql.core.engine.IGraphQLEngine;
import io.nop.orm.IOrmTemplate;
import io.nop.orm.model.OrmRelationType;
import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.Iterator;
import java.util.List;

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


@Disabled
@NopTestConfig(localDb = true, initDatabaseSchema = true)
public class TestDynCodeGenRelation2 extends JunitAutoTestCase {

@Inject
IDaoProvider daoProvider;

@Inject
DynCodeGen codeGen;

@Inject
IOrmTemplate ormTemplate;

@Inject
IGraphQLEngine graphQLEngine;

@Inject
IJdbcTemplate jdbcTemplate;

public void testGen(OrmRelationType relationType) {
ormTemplate.runInSession(() -> {
saveRelModule(relationType);
codeGen.generateForAllModules();
codeGen.reloadModel();
});
}

@EnableSnapshot
@Test
public void testOneToManyRelation() {

testGen(OrmRelationType.o2m);
// ApiRequest<?> request = new ApiRequest<>();
// request.setSelection(FieldSelectionBean.fromProp("roleKey", "roleName", "roleUsers.userName"));
// IGraphQLExecutionContext gqlContext =
// graphQLEngine.newRpcContext(GraphQLOperationType.query, "RoleEntity__findList", request);
// @SuppressWarnings("unchecked")
// ApiResponse<?> response = FutureHelper.syncGet(graphQLEngine.executeRpcAsync(gqlContext));
// assertEquals(true, response.isOk());
// List<?> items = BeanTool.castBeanToType(response.getData(), List.class);
// assertEquals(1, items.size());

// {deptName=2222, deptUsers=[{userId=7ab81}, {id=dasda2}]}

Map<Object, Object> mapData = new HashMap<>();
mapData.put("roleKey", "test1");
mapData.put("roleName", "test1");
Map<Object, Object> deptUserData = new HashMap<>();
deptUserData.put("id", "1");
mapData.put("roleUsers", List.of(deptUserData));

ApiRequest<?> request = ApiRequest.build(Map.of("data", mapData));
request.setSelection(FieldSelectionBean.fromProp("roleUsers"));
IGraphQLExecutionContext context = graphQLEngine.newRpcContext(GraphQLOperationType.mutation,
"RoleEntity__save", request);
Object result = FutureHelper.syncGet(graphQLEngine.executeRpcAsync(context));
System.out.println(result);
}

@EnableSnapshot
@Test
public void testManyToManyRelation() {

testGen(OrmRelationType.m2m);

ApiRequest<?> request = new ApiRequest<>();
request.setSelection(FieldSelectionBean.fromProp("roleKey", "roleName", "roleUsers.id"));
IGraphQLExecutionContext gqlContext = graphQLEngine.newRpcContext(GraphQLOperationType.query,
"RoleEntity__findList",
request);
@SuppressWarnings("unchecked")
ApiResponse<?> response = FutureHelper.syncGet(graphQLEngine.executeRpcAsync(gqlContext));
assertEquals(true, response.isOk());
List<?> items = BeanTool.castBeanToType(response.getData(), List.class);
assertEquals(1, items.size());
}

private void saveRelModule(OrmRelationType ormRelationType) {
NopDynModule module = new NopDynModule();
module.setModuleName("relation-demo");
module.setDisplayName("Demo Module");
module.setStatus(NopDynDaoConstants.MODULE_STATUS_PUBLISHED);

userEntityMeta(module);
roleEntityMeta(module);
// middleEntityMeta(module);

switch (ormRelationType) {
case o2m:
addOneToManyRelation(module);
break;
case o2o:
case m2o:
addManyToOneRelation(module, ormRelationType);
break;
case m2m:
addManyToManyRelation(module);
break;
}
daoProvider.daoFor(NopDynModule.class).flushSession();
}

private void middleEntityMeta(NopDynModule module) {
NopDynEntityMeta entityMeta = new NopDynEntityMeta();
entityMeta.setEntityName("UserManyRole");
entityMeta.setDisplayName("User Many Role");
entityMeta.setModule(module);
entityMeta.setStatus(1);
entityMeta.setIsExternal(false);
entityMeta.setStoreType(NopDynDaoConstants.ENTITY_STORE_TYPE_REAL);

addProp(entityMeta, "userId", StdSqlType.VARCHAR, 32);
addProp(entityMeta, "roleId", StdSqlType.VARCHAR, 32);

daoProvider.daoFor(NopDynEntityMeta.class).saveEntity(entityMeta);
module.getEntityMetas().add(entityMeta);
}

private void userEntityMeta(NopDynModule module) {

NopDynEntityMeta entityMeta = new NopDynEntityMeta();
entityMeta.setEntityName("UserEntity");
entityMeta.setDisplayName("User Entity");
entityMeta.setModule(module);
entityMeta.setStatus(1);
entityMeta.setIsExternal(false);
entityMeta.setStoreType(NopDynDaoConstants.ENTITY_STORE_TYPE_REAL);

addProp(entityMeta, "userName", StdSqlType.VARCHAR, 100);
addProp(entityMeta, "userAge", StdSqlType.INTEGER, 0);
addProp(entityMeta, "roleId", StdSqlType.VARCHAR, 100);

daoProvider.daoFor(NopDynEntityMeta.class).saveEntity(entityMeta);
module.getEntityMetas().add(entityMeta);
}


private void roleEntityMeta(NopDynModule module) {
NopDynEntityMeta entityMeta = new NopDynEntityMeta();
entityMeta.setEntityName("RoleEntity");
entityMeta.setDisplayName("Role Entity");
entityMeta.setModule(module);
entityMeta.setStatus(1);
entityMeta.setIsExternal(false);
entityMeta.setStoreType(NopDynDaoConstants.ENTITY_STORE_TYPE_REAL);

addProp(entityMeta, "roleName", StdSqlType.VARCHAR, 100);
addProp(entityMeta, "roleKey", StdSqlType.VARCHAR, 100);

daoProvider.daoFor(NopDynEntityMeta.class).saveEntity(entityMeta);
module.getEntityMetas().add(entityMeta);
}

private void addRelation(OrmRelationType relationType, String relationName, String displayName,
String leftPropName, String rightPropName, NopDynEntityMeta leftEntity, NopDynEntityMeta rightEntity, String middleTableName) {

NopDynEntityRelationMeta relationMeta = new NopDynEntityRelationMeta();
relationMeta.setEntityMetaId(leftEntity.getEntityMetaId());
relationMeta.setRefEntityMetaId(rightEntity.getEntityMetaId());
relationMeta.setRelationName(relationName);
relationMeta.setRelationDisplayName(displayName);
relationMeta.setRelationType(relationType.name());
relationMeta.setLeftPropName(leftPropName);
relationMeta.setRightPropName(rightPropName);
relationMeta.setStatus(1);
relationMeta.setTagsText("pub");
// relationMeta.setMiddleTableName(middleTableName);
leftEntity.getRelationMetasForEntity().add(relationMeta);
}

private void addManyToOneRelation(NopDynModule module, OrmRelationType relationType) {
Iterator<NopDynEntityMeta> iterator = module.getEntityMetas().iterator();
NopDynEntityMeta userEntity = iterator.next();
NopDynEntityMeta roleEntity = iterator.next();

addRelation(relationType, "userRole", "测试" + relationType.name() + "关联",
"roleId", "sid", userEntity, roleEntity, null);
}

private void addOneToManyRelation(NopDynModule module) {
Iterator<NopDynEntityMeta> iterator = module.getEntityMetas().iterator();
NopDynEntityMeta userEntity = iterator.next();
NopDynEntityMeta roleEntity = iterator.next();

addRelation(OrmRelationType.o2m, "roleUsers", "测试一对多关联",
"sid", "roleId", roleEntity, userEntity, null);
}

private void addManyToManyRelation(NopDynModule module) {
Iterator<NopDynEntityMeta> iterator = module.getEntityMetas().iterator();
NopDynEntityMeta userEntity = iterator.next();
NopDynEntityMeta roleEntity = iterator.next();
//NopDynEntityMeta middleEntity = iterator.next();

//module.getEntityMetas().remove(middleEntity);

addRelation(OrmRelationType.m2m, "userRoles", "测试用户对多对关联",
"sid", "userId", userEntity, roleEntity, "USER_MANY_ROLE");

addRelation(OrmRelationType.m2m, "roleUsers", "测试角色对多对关联",
"sid", "roleId", roleEntity, userEntity, "USER_MANY_ROLE");
}

private NopDynPropMeta addProp(NopDynEntityMeta entityMeta, String propName, StdSqlType sqlType, int precision) {
NopDynPropMeta propMeta = new NopDynPropMeta();
propMeta.setPropId(1);
propMeta.setPropName(propName);
propMeta.setDisplayName(propName + " Display");
propMeta.setPrecision(precision);
propMeta.setIsMandatory(true);
propMeta.setStatus(1);
propMeta.setStdSqlType(sqlType.getName());
entityMeta.getPropMetas().add(propMeta);
return propMeta;
}
}

0 comments on commit f9953d4

Please sign in to comment.