-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1470 from miahemu/dev
unit test
- Loading branch information
Showing
6 changed files
with
336 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...-server-start/src/test/java/ai/chat2db/server/start/test/core/EnvironmentServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...2db-server-start/src/test/java/ai/chat2db/server/start/test/core/FunctionServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...b-server-start/src/test/java/ai/chat2db/server/start/test/core/JdbcDriverServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...server-start/src/test/java/ai/chat2db/server/start/test/core/OperationLogServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |