Skip to content

Commit

Permalink
Merge pull request #1470 from miahemu/dev
Browse files Browse the repository at this point in the history
unit test
  • Loading branch information
miahemu authored Jul 22, 2024
2 parents 60e47c2 + 82bb7c2 commit 298060b
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void testPreConnect() {
param.setUrl(dialectProperties.getUrl());
param.setPassword(dialectProperties.getPassword());
param.setPort(String.valueOf(dialectProperties.getPort()));
param.setHost("183.247.151.185");
param.setHost("localhost");
param.setSsh(new SSHInfo());
param.setSsl(new SSLInfo());
param.setExtendInfo(new ArrayList<KeyValue>());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.param.DlCountParam;
import ai.chat2db.server.domain.api.param.DlExecuteParam;
import ai.chat2db.server.domain.api.param.OrderByParam;
import ai.chat2db.server.domain.api.param.UpdateSelectResultParam;
import ai.chat2db.server.domain.api.service.DlTemplateService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
Expand All @@ -12,11 +15,15 @@
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import ai.chat2db.spi.model.ExecuteResult;
import ai.chat2db.spi.model.Header;
import ai.chat2db.spi.model.OrderBy;
import ai.chat2db.spi.model.ResultOperation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -140,16 +147,90 @@ public void testExecuteUpdate() {
@Test
public void testCount() {

userLoginIdentity(true, 8L);

for (DialectProperties dialectProperties : dialectPropertiesList) {
Long dataSourceId = TestUtils.nextLong();
Long consoleId = TestUtils.nextLong();
TestUtils.buildContext(dialectProperties, dataSourceId, consoleId);

DlCountParam param = new DlCountParam();
DataResult<Long> count = null;
if (dialectProperties.getDbType().equals("MYSQL")) {
param.setSql("select * from test_data");
count = dlTemplateService.count(param);
System.out.println("Mysql Total rows:" + count.getData());
Assertions.assertTrue(count.getSuccess(), count.errorMessage());
} else if (dialectProperties.getDbType().equals("ORACLE")) {
param.setSql("select * from TEST_USER.DEMO");
count = dlTemplateService.count(param);
System.out.println("Oracle Total rows:" + count.getData());
Assertions.assertTrue(count.getSuccess(), count.errorMessage());
}else if (dialectProperties.getDbType().equals("POSTGRESQL")) {
param.setSql("select * from test.reference_table");
count = dlTemplateService.count(param);
System.out.println("PG Total rows:" + count.getData());
Assertions.assertTrue(count.getSuccess(), count.errorMessage());
} else if (dialectProperties.getDbType().equals("MARIADB")) {
param.setSql("select * from test.test_data");
count = dlTemplateService.count(param);
System.out.println("Mariadb Total rows:" + count.getData());
Assertions.assertTrue(count.getSuccess(), count.errorMessage());
}
}

}

@Test
public void testUpdateSelectResult() {

userLoginIdentity(true, 8L);

for (DialectProperties dialectProperties : dialectPropertiesList) {
Long dataSourceId = TestUtils.nextLong();
Long consoleId = TestUtils.nextLong();
TestUtils.buildContext(dialectProperties, dataSourceId, consoleId);

UpdateSelectResultParam param = new UpdateSelectResultParam();
param.setTableName("test_data");
param.setHeaderList(new ArrayList<Header>());
param.setOperations(new ArrayList<ResultOperation>());

DataResult<String> result = dlTemplateService.updateSelectResult(param);
System.out.println("result:" + result.getData());
Assertions.assertTrue(result.getSuccess(), result.errorMessage());
}

}

@Test
public void testGetOrderBySql() {

userLoginIdentity(false, 4L);

for (DialectProperties dialectProperties : dialectPropertiesList) {
Long dataSourceId = TestUtils.nextLong();
Long consoleId = TestUtils.nextLong();
TestUtils.buildContext(dialectProperties, dataSourceId, consoleId);

if (dialectProperties.getDbType().equals("MYSQL")) {
ArrayList<OrderBy> orderByList = new ArrayList<>();
OrderBy orderBy1 = new OrderBy();
orderBy1.setColumnName("number");
orderBy1.setAsc(true);
orderByList.add(orderBy1);

OrderByParam orderByParam = new OrderByParam();
orderByParam.setOrderByList(orderByList);
orderByParam.setOriginSql("select * from test_data");

DataResult<String> result = dlTemplateService.getOrderBySql(orderByParam);
System.out.println("Mysql Final Sql:" + result.getData());
Assertions.assertTrue(result.getSuccess(), result.errorMessage());

}
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.model.Environment;
import ai.chat2db.server.domain.api.param.EnvironmentPageQueryParam;
import ai.chat2db.server.domain.api.service.EnvironmentService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.start.test.dialect.DialectProperties;
import ai.chat2db.server.start.test.dialect.TestUtils;
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
import ai.chat2db.server.tools.base.wrapper.result.PageResult;
import ai.chat2db.server.tools.common.model.Context;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
* @author Juechen
* @version : EnvironmentServiceTest.java
*/
public class EnvironmentServiceTest extends TestApplication {

@Autowired
private EnvironmentService environmentService;

@Autowired
private List<DialectProperties> dialectPropertiesList;

@Test
public void testListQuery() {

userLoginIdentity(false, 6L);

for (DialectProperties dialectProperties : dialectPropertiesList) {
Long dataSourceId = TestUtils.nextLong();
Long consoleId = TestUtils.nextLong();
TestUtils.buildContext(dialectProperties, dataSourceId, consoleId);

ArrayList<Long> list = new ArrayList<>();
list.add(1L);
list.add(2L);
list.add(3L);

ListResult<Environment> query = environmentService.listQuery(list);
Assertions.assertTrue(query.getSuccess(), query.getErrorMessage());
Assertions.assertFalse(query.getData().isEmpty(), "Result should not be empty for non-empty input list");
}
}

@Test
public void testPageQuery() {

userLoginIdentity(false, 3L);

for (DialectProperties dialectProperties : dialectPropertiesList) {
Long dataSourceId = TestUtils.nextLong();
Long consoleId = TestUtils.nextLong();
TestUtils.buildContext(dialectProperties, dataSourceId, consoleId);

EnvironmentPageQueryParam param = new EnvironmentPageQueryParam();
param.setSearchKey("release");
// param.setSearchKey("test");
param.setPageNo(1);
param.setPageSize(10);

PageResult<Environment> query = environmentService.pageQuery(param);
Assertions.assertTrue(query.getSuccess(), query.getErrorMessage());
Assertions.assertFalse(query.getData().isEmpty(), "Result should not be empty for non-empty input list");
}
}

/**
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
*
* @param isAdmin
* @param userId
*/
private static void userLoginIdentity(boolean isAdmin, Long userId) {
Context context = Context.builder().loginUser(
LoginUser.builder().admin(isAdmin).id(userId).build()
).build();
ContextUtils.setContext(context);
Dbutils.setSession();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.service.FunctionService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.start.test.dialect.DialectProperties;
import ai.chat2db.server.start.test.dialect.TestUtils;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
import ai.chat2db.server.tools.common.model.Context;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import ai.chat2db.spi.model.Function;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* @author Juechen
* @version : FunctionServiceTest.java
*/
public class FunctionServiceTest extends TestApplication {

@Autowired
private FunctionService functionService;

@Autowired
private List<DialectProperties> dialectPropertiesList;

@Test
public void testFunctions() {

userLoginIdentity(false, 3L);

for (DialectProperties dialectProperties : dialectPropertiesList) {
Long dataSourceId = TestUtils.nextLong();
Long consoleId = TestUtils.nextLong();
TestUtils.buildContext(dialectProperties, dataSourceId, consoleId);

ListResult<Function> functions = functionService.functions(dialectProperties.getDatabaseName(), null);
Assertions.assertTrue(functions.getSuccess(), functions.errorMessage());

if (dialectProperties.getDbType().equals("MYSQL")) {
DataResult<Function> detail = functionService.detail(dialectProperties.getDatabaseName(), null, "add_numbers");
Assertions.assertTrue(detail.getSuccess(), detail.errorMessage());
}

}
}


/**
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
*
* @param isAdmin
* @param userId
*/
private static void userLoginIdentity(boolean isAdmin, Long userId) {
Context context = Context.builder().loginUser(
LoginUser.builder().admin(isAdmin).id(userId).build()
).build();
ContextUtils.setContext(context);
Dbutils.setSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.service.JdbcDriverService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.common.model.Context;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import ai.chat2db.spi.config.DBConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
* @author Juechen
* @version : JdbcDriverServiceTest.java
*/
public class JdbcDriverServiceTest extends TestApplication {

@Autowired
private JdbcDriverService jdbcDriverService;

@Test
public void testGetDrivers() {

userLoginIdentity(false, 2L);

String dbType = "POSTGRESQL";
DataResult<DBConfig> drivers = jdbcDriverService.getDrivers(dbType);
Assertions.assertTrue(drivers.success(), drivers.errorMessage());
}

@Test
public void testUpload() {

userLoginIdentity(false, 2L);

String dbType = "MYSQL";
ActionResult result = jdbcDriverService.upload(dbType, "com.mysql.cj.jdbc.Driver", "mysql-connector-java-8.0.30.jar");
Assertions.assertTrue(result.success(), result.errorMessage());
}

@Test
public void testDownload() {
userLoginIdentity(false, 5L);

String dbType = "ORACLE";
DataResult<DBConfig> drivers = jdbcDriverService.getDrivers(dbType);
Assertions.assertTrue(drivers.success(), drivers.errorMessage());
}



/**
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
*
* @param isAdmin
* @param userId
*/
private static void userLoginIdentity(boolean isAdmin, Long userId) {
Context context = Context.builder().loginUser(
LoginUser.builder().admin(isAdmin).id(userId).build()
).build();
ContextUtils.setContext(context);
Dbutils.setSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.service.OperationLogService;
import ai.chat2db.server.start.test.TestApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
* @author Juechen
* @version : OperationLogServiceTest.java
*/
public class OperationLogServiceTest extends TestApplication {

@Autowired
private OperationLogService operationLogService;

@Test
public void testCreate() {
operationLogService.create(null);
}

@Test
public void testQueryPage() {
operationLogService.queryPage(null);
}

}

0 comments on commit 298060b

Please sign in to comment.