-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 #1450 from miahemu/dev
Some typos and unit tests.
- Loading branch information
Showing
7 changed files
with
321 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
*/ | ||
public interface ChartService { | ||
/** | ||
* Save report | ||
* Create report | ||
* | ||
* @param param | ||
* @return | ||
|
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
180 changes: 180 additions & 0 deletions
180
...hat2db-server-start/src/test/java/ai/chat2db/server/start/test/core/ChartServiceTest.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,180 @@ | ||
package ai.chat2db.server.start.test.core; | ||
|
||
import ai.chat2db.server.domain.api.chart.ChartCreateParam; | ||
import ai.chat2db.server.domain.api.chart.ChartListQueryParam; | ||
import ai.chat2db.server.domain.api.chart.ChartQueryParam; | ||
import ai.chat2db.server.domain.api.chart.ChartUpdateParam; | ||
import ai.chat2db.server.domain.api.model.Chart; | ||
import ai.chat2db.server.domain.api.service.ChartService; | ||
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.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 org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
|
||
public class ChartServiceTest extends TestApplication { | ||
|
||
@Autowired | ||
private ChartService chartService; | ||
|
||
|
||
@Test | ||
public void testCreateWithPermission() { | ||
try { | ||
userLoginIdentity(false, 4L); | ||
userLoginIdentity(true, 3L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
ChartCreateParam createParam = new ChartCreateParam(); | ||
Optional.of(createParam).ifPresent(param -> { | ||
param.setName("chat2db"); | ||
param.setSchema("test"); | ||
param.setDataSourceId(1L); | ||
param.setType("MYSQL"); | ||
param.setDatabaseName("chat2db"); | ||
param.setSchemaName("ali_dbhub"); | ||
param.setDdl("test"); | ||
}); | ||
|
||
DataResult<Long> withPermission = chartService.createWithPermission(createParam); | ||
assertNotNull(withPermission); | ||
|
||
Long id = withPermission.getData(); | ||
chartService.find(id); | ||
|
||
} | ||
|
||
|
||
@Test | ||
public void testUpdateWithPermission() { | ||
try { | ||
userLoginIdentity(false, 1L); | ||
userLoginIdentity(true, 4L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
ChartUpdateParam chartUpdateParam = new ChartUpdateParam(); | ||
Optional.of(chartUpdateParam).ifPresent(param -> { | ||
param.setId(1L); | ||
param.setName("chat2db"); | ||
param.setSchema("test"); | ||
param.setDataSourceId(1L); | ||
param.setType("DM"); | ||
param.setDatabaseName("chat2db"); | ||
param.setSchemaName("ali_dbhub"); | ||
param.setDdl("test"); | ||
}); | ||
|
||
ActionResult actionResult = chartService.updateWithPermission(chartUpdateParam); | ||
assertNotNull(actionResult); | ||
} | ||
|
||
|
||
@Test | ||
public void testFind() { | ||
try { | ||
userLoginIdentity(false, 6L); | ||
userLoginIdentity(true, 8L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
DataResult<Chart> result = chartService.find(2L); | ||
assertNotNull(result.getData()); | ||
} | ||
|
||
|
||
@Test | ||
public void testQueryExistent() { | ||
try { | ||
userLoginIdentity(false, 7L); | ||
userLoginIdentity(true, 9L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
ChartQueryParam chartQueryParam = new ChartQueryParam(); | ||
chartQueryParam.setId(1L); | ||
chartQueryParam.setUserId(1L); | ||
|
||
DataResult<Chart> chartDataResult = chartService.queryExistent(chartQueryParam); | ||
DataResult<Chart> queryExistent = chartService.queryExistent(chartDataResult.getData().getId()); | ||
assertNotNull(chartDataResult); | ||
assertEquals(chartDataResult, queryExistent); | ||
} | ||
|
||
|
||
@Test | ||
public void testListQuery() { | ||
try { | ||
userLoginIdentity(false, 8L); | ||
userLoginIdentity(true, 10L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
ChartListQueryParam param = new ChartListQueryParam(); | ||
param.setIdList(Arrays.asList(4L, 5L, 6L)); | ||
param.setUserId(1L); | ||
|
||
ListResult<Chart> listQuery = chartService.listQuery(param); | ||
assertNotNull(listQuery); | ||
|
||
} | ||
|
||
|
||
@Test | ||
public void testQueryByIds() { | ||
try { | ||
userLoginIdentity(false, 9L); | ||
userLoginIdentity(true, 11L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
ListResult<Chart> chartListResult = chartService.queryByIds(Arrays.asList(1L, 2L, 3L)); | ||
assertNotNull(chartListResult); | ||
} | ||
|
||
@Test | ||
public void testDeleteWithPermission() { | ||
try { | ||
userLoginIdentity(false, 10L); | ||
userLoginIdentity(true, 12L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
ActionResult actionResult = chartService.deleteWithPermission(3L); | ||
assertNotNull(actionResult); | ||
} | ||
|
||
/** | ||
* 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(); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
...at2db-server-start/src/test/java/ai/chat2db/server/start/test/core/ConfigServiceTest.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,135 @@ | ||
package ai.chat2db.server.start.test.core; | ||
|
||
import ai.chat2db.server.domain.api.model.Config; | ||
import ai.chat2db.server.domain.api.param.SystemConfigParam; | ||
import ai.chat2db.server.domain.api.service.ConfigService; | ||
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 org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class ConfigServiceTest extends TestApplication { | ||
|
||
@Autowired | ||
private ConfigService configService; | ||
|
||
@Test | ||
public void testCreate() { | ||
try { | ||
userLoginIdentity(true, 1L); | ||
userLoginIdentity(false, 2L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
SystemConfigParam systemConfigParam = new SystemConfigParam(); | ||
Optional.ofNullable(systemConfigParam).ifPresent(param -> { | ||
param.setCode(RandomCodeGenerator.generateRandomCode(6)); | ||
param.setContent(RandomCodeGenerator.generateRandomCode(6)); | ||
param.setSummary(RandomCodeGenerator.generateRandomCode(6)); | ||
}); | ||
|
||
ActionResult actionResult = configService.create(systemConfigParam); | ||
assertNotNull(actionResult); | ||
} | ||
|
||
@Test | ||
public void testUpdate() { | ||
try { | ||
userLoginIdentity(true, 4L); | ||
userLoginIdentity(false, 5L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
SystemConfigParam systemConfigParam = new SystemConfigParam(); | ||
systemConfigParam.setCode(RandomCodeGenerator.generateRandomCode(6)); | ||
systemConfigParam.setContent(RandomCodeGenerator.generateRandomCode(6)); | ||
systemConfigParam.setSummary(RandomCodeGenerator.generateRandomCode(6)); | ||
|
||
ActionResult update = configService.update(systemConfigParam); | ||
assertNotNull(update); | ||
|
||
} | ||
|
||
@Test | ||
public void testCreateOrUpdate() { | ||
try { | ||
userLoginIdentity(true, 3L); | ||
userLoginIdentity(false, 6L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
SystemConfigParam systemConfigParam = new SystemConfigParam(); | ||
systemConfigParam.setCode(RandomCodeGenerator.generateRandomCode(6)); | ||
systemConfigParam.setContent(RandomCodeGenerator.generateRandomCode(6)); | ||
systemConfigParam.setSummary(RandomCodeGenerator.generateRandomCode(6)); | ||
ActionResult orUpdate = configService.createOrUpdate(systemConfigParam); | ||
assertNotNull(orUpdate); | ||
|
||
} | ||
|
||
@Test | ||
public void testFind() { | ||
try { | ||
userLoginIdentity(true, 9L); | ||
userLoginIdentity(false, 4L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
DataResult<Config> configDataResult = configService.find("4TxfzW"); | ||
assertNotNull(configDataResult.getData()); | ||
} | ||
|
||
@Test | ||
public void testDelete() { | ||
try { | ||
userLoginIdentity(true, 11L); | ||
userLoginIdentity(false, 12L); | ||
} catch (Exception e) { | ||
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage()); | ||
} | ||
|
||
ActionResult result = configService.delete("4TxfzW"); | ||
assertNotNull(result); | ||
} | ||
|
||
/** | ||
* 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(); | ||
} | ||
|
||
public class RandomCodeGenerator { | ||
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
private static final SecureRandom RANDOM = new SecureRandom(); | ||
|
||
public static String generateRandomCode(int length) { | ||
StringBuilder sb = new StringBuilder(length); | ||
for (int i = 0; i < length; i++) { | ||
sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length()))); | ||
} | ||
return sb.toString(); | ||
} | ||
} | ||
} |
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