Skip to content

Commit f4b039e

Browse files
authored
Merge pull request #1450 from miahemu/dev
Some typos and unit tests.
2 parents debbf3a + 820e2d5 commit f4b039e

File tree

7 files changed

+321
-6
lines changed

7 files changed

+321
-6
lines changed

chat2db-server/chat2db-server-domain/chat2db-server-domain-api/src/main/java/ai/chat2db/server/domain/api/service/ChartService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
public interface ChartService {
2222
/**
23-
* Save report
23+
* Create report
2424
*
2525
* @param param
2626
* @return

chat2db-server/chat2db-server-domain/chat2db-server-domain-core/src/main/java/ai/chat2db/server/domain/core/util/PermissionUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class PermissionUtils {
1414

1515
/**
16-
* Verify whether the currently logged in user has permission to operate on the current content
16+
* Verify whether the currently logged-in user has permission to operate on the current content
1717
*
1818
* @param createUserId The creator of the current content
1919
*/

chat2db-server/chat2db-server-domain/chat2db-server-domain-repository/src/main/java/ai/chat2db/server/domain/repository/entity/ChartDO.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ public class ChartDO implements Serializable {
7373
private String schemaName;
7474

7575
/**
76-
* ddl content
76+
* DDL content
7777
*/
7878
private String ddl;
7979

8080
/**
81-
* Whether it has been deleted, y means deleted, n means not deleted
81+
* Whether it has been deleted, 'Y' means deleted, 'N' means not deleted
8282
*/
8383
private String deleted;
8484

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package ai.chat2db.server.start.test.core;
2+
3+
import ai.chat2db.server.domain.api.chart.ChartCreateParam;
4+
import ai.chat2db.server.domain.api.chart.ChartListQueryParam;
5+
import ai.chat2db.server.domain.api.chart.ChartQueryParam;
6+
import ai.chat2db.server.domain.api.chart.ChartUpdateParam;
7+
import ai.chat2db.server.domain.api.model.Chart;
8+
import ai.chat2db.server.domain.api.service.ChartService;
9+
import ai.chat2db.server.domain.repository.Dbutils;
10+
import ai.chat2db.server.start.test.TestApplication;
11+
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
12+
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
13+
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
14+
import ai.chat2db.server.tools.common.model.Context;
15+
import ai.chat2db.server.tools.common.model.LoginUser;
16+
import ai.chat2db.server.tools.common.util.ContextUtils;
17+
import org.junit.jupiter.api.Test;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
20+
import java.util.Arrays;
21+
import java.util.Optional;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
26+
27+
public class ChartServiceTest extends TestApplication {
28+
29+
@Autowired
30+
private ChartService chartService;
31+
32+
33+
@Test
34+
public void testCreateWithPermission() {
35+
try {
36+
userLoginIdentity(false, 4L);
37+
userLoginIdentity(true, 3L);
38+
} catch (Exception e) {
39+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
40+
}
41+
42+
ChartCreateParam createParam = new ChartCreateParam();
43+
Optional.of(createParam).ifPresent(param -> {
44+
param.setName("chat2db");
45+
param.setSchema("test");
46+
param.setDataSourceId(1L);
47+
param.setType("MYSQL");
48+
param.setDatabaseName("chat2db");
49+
param.setSchemaName("ali_dbhub");
50+
param.setDdl("test");
51+
});
52+
53+
DataResult<Long> withPermission = chartService.createWithPermission(createParam);
54+
assertNotNull(withPermission);
55+
56+
Long id = withPermission.getData();
57+
chartService.find(id);
58+
59+
}
60+
61+
62+
@Test
63+
public void testUpdateWithPermission() {
64+
try {
65+
userLoginIdentity(false, 1L);
66+
userLoginIdentity(true, 4L);
67+
} catch (Exception e) {
68+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
69+
}
70+
71+
ChartUpdateParam chartUpdateParam = new ChartUpdateParam();
72+
Optional.of(chartUpdateParam).ifPresent(param -> {
73+
param.setId(1L);
74+
param.setName("chat2db");
75+
param.setSchema("test");
76+
param.setDataSourceId(1L);
77+
param.setType("DM");
78+
param.setDatabaseName("chat2db");
79+
param.setSchemaName("ali_dbhub");
80+
param.setDdl("test");
81+
});
82+
83+
ActionResult actionResult = chartService.updateWithPermission(chartUpdateParam);
84+
assertNotNull(actionResult);
85+
}
86+
87+
88+
@Test
89+
public void testFind() {
90+
try {
91+
userLoginIdentity(false, 6L);
92+
userLoginIdentity(true, 8L);
93+
} catch (Exception e) {
94+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
95+
}
96+
97+
DataResult<Chart> result = chartService.find(2L);
98+
assertNotNull(result.getData());
99+
}
100+
101+
102+
@Test
103+
public void testQueryExistent() {
104+
try {
105+
userLoginIdentity(false, 7L);
106+
userLoginIdentity(true, 9L);
107+
} catch (Exception e) {
108+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
109+
}
110+
111+
ChartQueryParam chartQueryParam = new ChartQueryParam();
112+
chartQueryParam.setId(1L);
113+
chartQueryParam.setUserId(1L);
114+
115+
DataResult<Chart> chartDataResult = chartService.queryExistent(chartQueryParam);
116+
DataResult<Chart> queryExistent = chartService.queryExistent(chartDataResult.getData().getId());
117+
assertNotNull(chartDataResult);
118+
assertEquals(chartDataResult, queryExistent);
119+
}
120+
121+
122+
@Test
123+
public void testListQuery() {
124+
try {
125+
userLoginIdentity(false, 8L);
126+
userLoginIdentity(true, 10L);
127+
} catch (Exception e) {
128+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
129+
}
130+
131+
ChartListQueryParam param = new ChartListQueryParam();
132+
param.setIdList(Arrays.asList(4L, 5L, 6L));
133+
param.setUserId(1L);
134+
135+
ListResult<Chart> listQuery = chartService.listQuery(param);
136+
assertNotNull(listQuery);
137+
138+
}
139+
140+
141+
@Test
142+
public void testQueryByIds() {
143+
try {
144+
userLoginIdentity(false, 9L);
145+
userLoginIdentity(true, 11L);
146+
} catch (Exception e) {
147+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
148+
}
149+
150+
ListResult<Chart> chartListResult = chartService.queryByIds(Arrays.asList(1L, 2L, 3L));
151+
assertNotNull(chartListResult);
152+
}
153+
154+
@Test
155+
public void testDeleteWithPermission() {
156+
try {
157+
userLoginIdentity(false, 10L);
158+
userLoginIdentity(true, 12L);
159+
} catch (Exception e) {
160+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
161+
}
162+
163+
ActionResult actionResult = chartService.deleteWithPermission(3L);
164+
assertNotNull(actionResult);
165+
}
166+
167+
/**
168+
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
169+
*
170+
* @param isAdmin
171+
* @param userId
172+
*/
173+
private static void userLoginIdentity(boolean isAdmin, Long userId) {
174+
Context context = Context.builder().loginUser(
175+
LoginUser.builder().admin(isAdmin).id(userId).build()
176+
).build();
177+
ContextUtils.setContext(context);
178+
Dbutils.setSession();
179+
}
180+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package ai.chat2db.server.start.test.core;
2+
3+
import ai.chat2db.server.domain.api.model.Config;
4+
import ai.chat2db.server.domain.api.param.SystemConfigParam;
5+
import ai.chat2db.server.domain.api.service.ConfigService;
6+
import ai.chat2db.server.domain.repository.Dbutils;
7+
import ai.chat2db.server.start.test.TestApplication;
8+
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
9+
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
10+
import ai.chat2db.server.tools.common.model.Context;
11+
import ai.chat2db.server.tools.common.model.LoginUser;
12+
import ai.chat2db.server.tools.common.util.ContextUtils;
13+
import org.junit.jupiter.api.Test;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
16+
import java.security.SecureRandom;
17+
import java.util.Optional;
18+
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
21+
public class ConfigServiceTest extends TestApplication {
22+
23+
@Autowired
24+
private ConfigService configService;
25+
26+
@Test
27+
public void testCreate() {
28+
try {
29+
userLoginIdentity(true, 1L);
30+
userLoginIdentity(false, 2L);
31+
} catch (Exception e) {
32+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
33+
}
34+
35+
SystemConfigParam systemConfigParam = new SystemConfigParam();
36+
Optional.ofNullable(systemConfigParam).ifPresent(param -> {
37+
param.setCode(RandomCodeGenerator.generateRandomCode(6));
38+
param.setContent(RandomCodeGenerator.generateRandomCode(6));
39+
param.setSummary(RandomCodeGenerator.generateRandomCode(6));
40+
});
41+
42+
ActionResult actionResult = configService.create(systemConfigParam);
43+
assertNotNull(actionResult);
44+
}
45+
46+
@Test
47+
public void testUpdate() {
48+
try {
49+
userLoginIdentity(true, 4L);
50+
userLoginIdentity(false, 5L);
51+
} catch (Exception e) {
52+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
53+
}
54+
55+
SystemConfigParam systemConfigParam = new SystemConfigParam();
56+
systemConfigParam.setCode(RandomCodeGenerator.generateRandomCode(6));
57+
systemConfigParam.setContent(RandomCodeGenerator.generateRandomCode(6));
58+
systemConfigParam.setSummary(RandomCodeGenerator.generateRandomCode(6));
59+
60+
ActionResult update = configService.update(systemConfigParam);
61+
assertNotNull(update);
62+
63+
}
64+
65+
@Test
66+
public void testCreateOrUpdate() {
67+
try {
68+
userLoginIdentity(true, 3L);
69+
userLoginIdentity(false, 6L);
70+
} catch (Exception e) {
71+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
72+
}
73+
74+
SystemConfigParam systemConfigParam = new SystemConfigParam();
75+
systemConfigParam.setCode(RandomCodeGenerator.generateRandomCode(6));
76+
systemConfigParam.setContent(RandomCodeGenerator.generateRandomCode(6));
77+
systemConfigParam.setSummary(RandomCodeGenerator.generateRandomCode(6));
78+
ActionResult orUpdate = configService.createOrUpdate(systemConfigParam);
79+
assertNotNull(orUpdate);
80+
81+
}
82+
83+
@Test
84+
public void testFind() {
85+
try {
86+
userLoginIdentity(true, 9L);
87+
userLoginIdentity(false, 4L);
88+
} catch (Exception e) {
89+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
90+
}
91+
92+
DataResult<Config> configDataResult = configService.find("4TxfzW");
93+
assertNotNull(configDataResult.getData());
94+
}
95+
96+
@Test
97+
public void testDelete() {
98+
try {
99+
userLoginIdentity(true, 11L);
100+
userLoginIdentity(false, 12L);
101+
} catch (Exception e) {
102+
throw new RuntimeException("An unexpected exception occurred during userLoginIdentity: " + e.getMessage());
103+
}
104+
105+
ActionResult result = configService.delete("4TxfzW");
106+
assertNotNull(result);
107+
}
108+
109+
/**
110+
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
111+
*
112+
* @param isAdmin
113+
* @param userId
114+
*/
115+
private static void userLoginIdentity(boolean isAdmin, Long userId) {
116+
Context context = Context.builder().loginUser(
117+
LoginUser.builder().admin(isAdmin).id(userId).build()
118+
).build();
119+
ContextUtils.setContext(context);
120+
Dbutils.setSession();
121+
}
122+
123+
public class RandomCodeGenerator {
124+
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
125+
private static final SecureRandom RANDOM = new SecureRandom();
126+
127+
public static String generateRandomCode(int length) {
128+
StringBuilder sb = new StringBuilder(length);
129+
for (int i = 0; i < length; i++) {
130+
sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
131+
}
132+
return sb.toString();
133+
}
134+
}
135+
}

chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/Function.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class Function implements Serializable {
2121
private static final long serialVersionUID = 1L;
2222
//FUNCTION_CAT String => function catalog (may be null)
23-
//FUNCTION_SCHEM String => function schema (may be null)
23+
//FUNCTION_SCHEME String => function schema (may be null)
2424
//FUNCTION_NAME String => function name. This is the name used to invoke the function
2525
//REMARKS String => explanatory comment on the function
2626
//FUNCTION_TYPE short => kind of function:

chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/Procedure.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class Procedure implements Serializable {
2121
private static final long serialVersionUID = 1L;
2222
//PROCEDURE_CAT String => procedure catalog (may be null)
23-
//PROCEDURE_SCHEM String => procedure schema (may be null)
23+
//PROCEDURE_SCHEME String => procedure schema (may be null)
2424
//PROCEDURE_NAME String => procedure name
2525
//REMARKS String => explanatory comment on the procedure
2626
//PROCEDURE_TYPE short => kind of procedure:

0 commit comments

Comments
 (0)