Skip to content

Commit

Permalink
Merge pull request #1455 from miahemu/dev
Browse files Browse the repository at this point in the history
Some typos and unit tests.
  • Loading branch information
miahemu authored Jul 3, 2024
2 parents 12b618e + 50c6d17 commit b66d77a
Show file tree
Hide file tree
Showing 17 changed files with 1,041 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DashboardCreateParam {
private String schema;

/**
* Whether it has been deleted, y means deleted, n means not deleted
* Whether it has been deleted, 'Y' means deleted, 'N' means not deleted
*/
private String deleted;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class DatabaseCreateParam {

private String name;

private String newName;

private String comment;

private String charset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,11 @@ public interface DatabaseService {
*/
ActionResult modifySchema( SchemaOperationParam request);

/**
* Export database
*
* @param param
* @return
*/
String exportDatabase(DatabaseExportParam param) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public DataResult<Sql> createDatabase(Database database) {
@Override
public ActionResult modifyDatabase(DatabaseCreateParam param) {
Chat2DBContext.getDBManage().modifyDatabase(Chat2DBContext.getConnection(), param.getName(),
param.getName());
param.getNewName());
return ActionResult.isSuccess();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void testQueryExistent() {
DataResult<Chart> chartDataResult = chartService.queryExistent(chartQueryParam);
DataResult<Chart> queryExistent = chartService.queryExistent(chartDataResult.getData().getId());
assertNotNull(chartDataResult);
assertNotNull(queryExistent);
assertEquals(chartDataResult, queryExistent);
}

Expand All @@ -124,7 +125,7 @@ public void testQueryByIds() {
// userLoginIdentity(true, 11L);

ListResult<Chart> chartListResult = chartService.queryByIds(Arrays.asList(1L, 2L, 3L));
assertNotNull(chartListResult);
assertNotNull(chartListResult.getData());
}

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

import ai.chat2db.server.domain.api.param.ConsoleCloseParam;
import ai.chat2db.server.domain.api.param.ConsoleConnectParam;
import ai.chat2db.server.domain.api.service.ConsoleService;
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.ActionResult;
import ai.chat2db.spi.sql.Chat2DBContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

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

public class ConsoleServiceTest extends TestApplication {

@Autowired
private ConsoleService consoleService;

@Autowired
private List<DialectProperties> dialectPropertiesList;

@Test
public void testCreateAndCloseConsole() {
// MYSQL ORACLE POSTGRESQL
for (DialectProperties dialectProperties : dialectPropertiesList) {
Long dataSourceId = TestUtils.nextLong();
Long consoleId = TestUtils.nextLong();
TestUtils.buildContext(dialectProperties, dataSourceId, consoleId);

// creat
ConsoleConnectParam consoleCreateParam = new ConsoleConnectParam();
consoleCreateParam.setDataSourceId(dataSourceId);
consoleCreateParam.setConsoleId(consoleId);
consoleCreateParam.setDatabaseName(dialectProperties.getDatabaseName());
ActionResult console = consoleService.createConsole(consoleCreateParam);
assertNotNull(console);

// close
ConsoleCloseParam consoleCloseParam = new ConsoleCloseParam();
consoleCloseParam.setDataSourceId(dataSourceId);
consoleCloseParam.setConsoleId(consoleId);
consoleService.closeConsole(consoleCloseParam);
Chat2DBContext.removeContext();
}
}


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

import ai.chat2db.server.domain.api.model.Dashboard;
import ai.chat2db.server.domain.api.param.dashboard.DashboardCreateParam;
import ai.chat2db.server.domain.api.param.dashboard.DashboardPageQueryParam;
import ai.chat2db.server.domain.api.param.dashboard.DashboardQueryParam;
import ai.chat2db.server.domain.api.param.dashboard.DashboardUpdateParam;
import ai.chat2db.server.domain.api.service.DashboardService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.tools.base.enums.YesOrNoEnum;
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
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.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

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

public class DashboardServiceTest extends TestApplication {

@Autowired
private DashboardService dashboardService;

@Test
public void testCreateWithPermission() {
userLoginIdentity(false, 9L);
// userLoginIdentity(true, 11L);

DashboardCreateParam dashboardCreateParam = new DashboardCreateParam();
dashboardCreateParam.setName("chat2db");
dashboardCreateParam.setSchema("test");
dashboardCreateParam.setDescription("This is a test!");
dashboardCreateParam.setDeleted(YesOrNoEnum.NO.getLetter());
dashboardCreateParam.setUserId(5L);
dashboardCreateParam.setChartIds(new ArrayList<Long>());

DataResult<Long> withPermission = dashboardService.createWithPermission(dashboardCreateParam);
assertNotNull(withPermission);
}

@Test
public void testUpdateWithPermission() {
// Note: Only administrators can edit this.
userLoginIdentity(true, 9L);

DashboardUpdateParam dashboardUpdateParam = new DashboardUpdateParam();
dashboardUpdateParam.setId(1L);
dashboardUpdateParam.setName("chat2db");
dashboardUpdateParam.setSchema("test");
dashboardUpdateParam.setDescription("This is a test!");
dashboardUpdateParam.setDeleted(YesOrNoEnum.NO.getLetter());
dashboardUpdateParam.setUserId(5L);
dashboardUpdateParam.setChartIds(new ArrayList<Long>());

ActionResult actionResult = dashboardService.updateWithPermission(dashboardUpdateParam);
assertNotNull(actionResult);

}

@Test
public void testFind() {
userLoginIdentity(false, 4L);
// userLoginIdentity(true, 2L);

DataResult<Dashboard> find = dashboardService.find(2L);
assertNotNull(find.getData());
}

@Test
public void testQueryExistent() {
userLoginIdentity(false, 8L);

DashboardQueryParam param = new DashboardQueryParam();
param.setId(5L);
param.setUserId(9L);

DataResult<Dashboard> existent = dashboardService.queryExistent(param);
DataResult<Dashboard> dashboardDataResult = dashboardService.queryExistent(5L);
assertNotNull(existent.getData());
assertNotNull(dashboardDataResult.getData());
assertEquals(existent, dashboardDataResult);
}

@Test
public void testDeleteWithPermission() {
userLoginIdentity(false, 7L);
// userLoginIdentity(true, 4L);

DataResult<Dashboard> dashboardDataResult = dashboardService.find(4L);
if (dashboardDataResult.getData() != null) {
ActionResult actionResult = dashboardService.deleteWithPermission(dashboardDataResult.getData().getId());
assertNotNull(actionResult);
}

}

@Test
public void testQueryPage() {
userLoginIdentity(false, 12L);

DashboardPageQueryParam param = new DashboardPageQueryParam();
param.setUserId(5L);
param.setSearchKey("chat");

PageResult<Dashboard> queryPage = dashboardService.queryPage(param);
assertNotNull(queryPage.getData());
}


/**
* 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();
}
}
Loading

0 comments on commit b66d77a

Please # to comment.