blob: e40e661a5feb7b400fe9959fa8043adcb5a696b5 [file] [log] [blame]
95630366963296f62025-06-02 21:16:32 +08001package databasetest;
root33a7d952025-05-18 17:24:41 +00002
95630366963296f62025-06-02 21:16:32 +08003import database.Database2;
4import entity.*;
5import org.junit.jupiter.api.*;
root33a7d952025-05-18 17:24:41 +00006
95630366963296f62025-06-02 21:16:32 +08007import javax.persistence.*;
8import java.util.*;
root33a7d952025-05-18 17:24:41 +00009
95630366963296f62025-06-02 21:16:32 +080010public class database2Test {
11 private static EntityManagerFactory emf;
12 private static EntityManager em;
13 private static Database2 db;
root33a7d952025-05-18 17:24:41 +000014
95630366963296f62025-06-02 21:16:32 +080015 @BeforeAll
16 static void setup() throws Exception {
17 // 强制加载 MySQL 驱动
18 Class.forName("com.mysql.cj.jdbc.Driver");
19 entity.config cfg = new entity.config();
20 Map<String, Object> props = new HashMap<>();
21 String jdbcUrl = String.format(
22 "jdbc:mysql://%s/%s?useSSL=false&serverTimezone=UTC",
23 cfg.SqlURL, cfg.TestDatabase);
24 props.put("javax.persistence.jdbc.url", jdbcUrl);
25 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
26 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
27 props.put("javax.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver");
28 emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
29 em = emf.createEntityManager();
30 db = new Database2(emf);
31 }
root33a7d952025-05-18 17:24:41 +000032
95630366963296f62025-06-02 21:16:32 +080033 @AfterAll
34 static void teardown() {
35 if (em != null && em.isOpen())
36 em.close();
37 if (emf != null && emf.isOpen())
38 emf.close();
39 }
root33a7d952025-05-18 17:24:41 +000040
95630366963296f62025-06-02 21:16:32 +080041 @TestFactory
42 Collection<DynamicTest> testAddBegSeed() {
43 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
44 if (userIds.isEmpty())
45 return Collections.emptyList();
46 String uid = userIds.get(0);
root33a7d952025-05-18 17:24:41 +000047
95630366963296f62025-06-02 21:16:32 +080048 return List.of(
49 DynamicTest.dynamicTest("AddBegSeed success", () -> {
50 String begId = "test_beg_" + System.currentTimeMillis();
51 BegInfo beg = new BegInfo();
52 beg.begid = begId;
53 beg.begnumbers = 1;
54 beg.magic = 100;
55 beg.endtime = new Date();
56 beg.hasseed = 0;
57 try {
58 int ret = db.AddBegSeed(beg);
59 Assertions.assertEquals(0, ret, "AddBegSeed应返回0");
60 } finally {
61 EntityTransaction tx = em.getTransaction();
62 tx.begin();
63 BegInfo toDelete = em.find(BegInfo.class, begId);
64 if (toDelete != null)
65 em.remove(toDelete);
66 tx.commit();
67 }
68 }),
root33a7d952025-05-18 17:24:41 +000069
95630366963296f62025-06-02 21:16:32 +080070 DynamicTest.dynamicTest("AddBegSeed duplicate", () -> {
71 String begId = "test_beg_" + (System.currentTimeMillis() + 1);
72 BegInfo beg = new BegInfo();
73 beg.begid = begId;
74 beg.begnumbers = 1;
75 beg.magic = 100;
76 beg.endtime = new Date();
77 beg.hasseed = 0;
78 try {
79 db.AddBegSeed(beg);
80 int ret = db.AddBegSeed(beg);
81 Assertions.assertEquals(1, ret, "重复插入应返回1");
82 } finally {
83 EntityTransaction tx = em.getTransaction();
84 tx.begin();
85 BegInfo toDelete = em.find(BegInfo.class, begId);
86 if (toDelete != null)
87 em.remove(toDelete);
88 tx.commit();
89 }
90 }),
root33a7d952025-05-18 17:24:41 +000091
95630366963296f62025-06-02 21:16:32 +080092 DynamicTest.dynamicTest("AddBegSeed invalid param", () -> {
93 Assertions.assertEquals(2, db.AddBegSeed(null));
94 BegInfo invalidBeg = new BegInfo();
95 invalidBeg.begid = "";
96 Assertions.assertEquals(2, db.AddBegSeed(invalidBeg));
97 }));
98 }
root33a7d952025-05-18 17:24:41 +000099
95630366963296f62025-06-02 21:16:32 +0800100 @TestFactory
101 Collection<DynamicTest> testUpdateBegSeed() {
102 List<BegInfo> existingBegs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class)
103 .setMaxResults(1)
104 .getResultList();
105 if (existingBegs.isEmpty()) {
106 return Collections.emptyList();
107 }
root33a7d952025-05-18 17:24:41 +0000108
95630366963296f62025-06-02 21:16:32 +0800109 BegInfo originalBeg = existingBegs.get(0);
110 String begId = originalBeg.begid;
111 int originalMagic = originalBeg.magic;
112 int originalBegNumbers = originalBeg.begnumbers;
113 int originalHasSeed = originalBeg.hasseed;
114 Date originalEndTime = originalBeg.endtime;
root33a7d952025-05-18 17:24:41 +0000115
95630366963296f62025-06-02 21:16:32 +0800116 return List.of(
117 DynamicTest.dynamicTest("UpdateBegSeed success", () -> {
118 try {
119 BegInfo update = new BegInfo();
120 update.begid = begId;
121 update.begnumbers = originalBegNumbers + 1;
122 update.magic = originalMagic + 100;
123 update.endtime = originalEndTime;
124 update.hasseed = originalHasSeed;
root33a7d952025-05-18 17:24:41 +0000125
95630366963296f62025-06-02 21:16:32 +0800126 int ret = db.UpdateBegSeed(update);
127 Assertions.assertEquals(0, ret, "UpdateBegSeed应返回0");
root33a7d952025-05-18 17:24:41 +0000128
95630366963296f62025-06-02 21:16:32 +0800129 em.clear();
130 BegInfo updated = em.find(BegInfo.class, begId);
131 Assertions.assertEquals(originalMagic + 100, updated.magic, "魔力值应已更新");
132 Assertions.assertEquals(originalBegNumbers + 1, updated.begnumbers, "求种人数应已更新");
133 } finally {
134 // 恢复原始数据
135 EntityTransaction tx = em.getTransaction();
136 tx.begin();
137 BegInfo toRestore = em.find(BegInfo.class, begId);
138 if (toRestore != null) {
139 toRestore.magic = originalMagic;
140 toRestore.begnumbers = originalBegNumbers;
141 toRestore.hasseed = originalHasSeed;
142 toRestore.endtime = originalEndTime;
143 em.merge(toRestore);
144 }
145 tx.commit();
146 }
147 }),
148 DynamicTest.dynamicTest("UpdateBegSeed not exist", () -> {
149 BegInfo notExist = new BegInfo();
150 notExist.begid = "not_exist_beg";
151 notExist.begnumbers = 1;
152 notExist.magic = 100;
153 notExist.endtime = new Date();
154 notExist.hasseed = 0;
root33a7d952025-05-18 17:24:41 +0000155
95630366963296f62025-06-02 21:16:32 +0800156 int ret = db.UpdateBegSeed(notExist);
157 Assertions.assertEquals(1, ret, "不存在的求种应返回1");
158 }),
159 DynamicTest.dynamicTest("UpdateBegSeed invalid param", () -> {
160 Assertions.assertEquals(2, db.UpdateBegSeed(null));
161 BegInfo invalid = new BegInfo();
162 invalid.begid = "";
163 Assertions.assertEquals(2, db.UpdateBegSeed(invalid));
164 }));
165 }
root33a7d952025-05-18 17:24:41 +0000166
95630366963296f62025-06-02 21:16:32 +0800167 @TestFactory
168 Collection<DynamicTest> testDeleteBegSeed() {
169 List<BegInfo> existingBegs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class)
170 .setMaxResults(1)
171 .getResultList();
172 if (existingBegs.isEmpty()) {
173 return Collections.emptyList();
174 }
root33a7d952025-05-18 17:24:41 +0000175
95630366963296f62025-06-02 21:16:32 +0800176 BegInfo begToDelete = existingBegs.get(0);
177 String begId = begToDelete.begid;
root33a7d952025-05-18 17:24:41 +0000178
95630366963296f62025-06-02 21:16:32 +0800179 return List.of(
180 DynamicTest.dynamicTest("DeleteBegSeed success", () -> {
181 EntityTransaction tx = em.getTransaction();
182 try {
183 tx.begin();
184 int ret = db.DeleteBegSeed(begId);
185 Assertions.assertEquals(0, ret, "DeleteBegSeed 应返回0");
186 } finally {
187 tx.rollback();
188 }
189 }),
root33a7d952025-05-18 17:24:41 +0000190
95630366963296f62025-06-02 21:16:32 +0800191 DynamicTest.dynamicTest("DeleteBegSeed not exist", () -> {
192 int ret = db.DeleteBegSeed("not_exist_beg");
193 Assertions.assertEquals(1, ret, "不存在的求种任务应返回1");
194 }),
root33a7d952025-05-18 17:24:41 +0000195
95630366963296f62025-06-02 21:16:32 +0800196 DynamicTest.dynamicTest("DeleteBegSeed invalid param", () -> {
197 Assertions.assertEquals(2, db.DeleteBegSeed(null));
198 Assertions.assertEquals(2, db.DeleteBegSeed(""));
199 }));
200 }
root33a7d952025-05-18 17:24:41 +0000201
95630366963296f62025-06-02 21:16:32 +0800202 @TestFactory
203 Collection<DynamicTest> testVoteSeed() {
204 // 获取现有用户ID
205 List<String> userIds = em.createQuery("select u.userid from User u", String.class)
206 .setMaxResults(1)
207 .getResultList();
208 if (userIds.isEmpty())
209 return Collections.emptyList();
210 String uid = userIds.get(0);
root33a7d952025-05-18 17:24:41 +0000211
95630366963296f62025-06-02 21:16:32 +0800212 // 获取现有的求种信息
213 List<BegInfo> begs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class)
214 .setMaxResults(1)
215 .getResultList();
216 if (begs.isEmpty())
217 return Collections.emptyList();
218 String begId = begs.get(0).begid;
root33a7d952025-05-18 17:24:41 +0000219
95630366963296f62025-06-02 21:16:32 +0800220 // 获取现有的种子信息
221 List<Seed> seeds = em.createQuery("SELECT s FROM Seed s", Seed.class)
222 .setMaxResults(1)
223 .getResultList();
224 if (seeds.isEmpty())
225 return Collections.emptyList();
226 String seedId = seeds.get(0).seedid;
root33a7d952025-05-18 17:24:41 +0000227
95630366963296f62025-06-02 21:16:32 +0800228 try {
229 return List.of(
230 DynamicTest.dynamicTest("VoteSeed success", () -> {
231 int ret = db.VoteSeed(begId, seedId, uid);
232 Assertions.assertEquals(0, ret, "VoteSeed应返回0");
233 }),
root33a7d952025-05-18 17:24:41 +0000234
95630366963296f62025-06-02 21:16:32 +0800235 DynamicTest.dynamicTest("VoteSeed duplicate", () -> {
236 int ret = db.VoteSeed(begId, seedId, uid);
237 Assertions.assertEquals(1, ret, "重复投票应返回1");
238 }),
root33a7d952025-05-18 17:24:41 +0000239
95630366963296f62025-06-02 21:16:32 +0800240 DynamicTest.dynamicTest("VoteSeed invalid param", () -> {
241 Assertions.assertEquals(2, db.VoteSeed(null, seedId, uid));
242 Assertions.assertEquals(2, db.VoteSeed(begId, null, uid));
243 Assertions.assertEquals(2, db.VoteSeed(begId, seedId, null));
244 }));
245 } finally {
246 EntityTransaction tx = em.getTransaction();
247 tx.begin();
248 try {
249 em.createQuery(
250 "DELETE FROM UserVotes v WHERE v.begid = :begid AND v.seedid = :seedid AND v.userid = :uid")
251 .setParameter("begid", begId)
252 .setParameter("seedid", seedId)
253 .setParameter("uid", uid)
254 .executeUpdate();
255 } catch (Exception ignored) {
256 }
257 tx.commit();
258 }
259 }
root33a7d952025-05-18 17:24:41 +0000260
95630366963296f62025-06-02 21:16:32 +0800261 @TestFactory
262 Collection<DynamicTest> testSubmitSeed() {
263 // 获取现有的求种信息
264 List<BegInfo> begs = em.createQuery("SELECT b FROM BegInfo b WHERE b.hasseed = 0", BegInfo.class)
265 .setMaxResults(1)
266 .getResultList();
267 if (begs.isEmpty())
268 return Collections.emptyList();
269 String begId = begs.get(0).begid;
root33a7d952025-05-18 17:24:41 +0000270
95630366963296f62025-06-02 21:16:32 +0800271 // 获取现有的可用种子信息
272 List<Seed> existingSeeds = em.createQuery(
273 "SELECT s FROM Seed s WHERE s.seedid NOT IN " +
274 "(SELECT ss.seed.seedid FROM SubmitSeed ss)",
275 Seed.class)
276 .setMaxResults(1)
277 .getResultList();
278 if (existingSeeds.isEmpty())
279 return Collections.emptyList();
280 Seed seed = existingSeeds.get(0);
root33a7d952025-05-18 17:24:41 +0000281
95630366963296f62025-06-02 21:16:32 +0800282 try {
283 return List.of(
284 DynamicTest.dynamicTest("SubmitSeed success", () -> {
285 int ret = db.SubmitSeed(begId, seed);
286 Assertions.assertEquals(0, ret, "SubmitSeed应返回0");
287 }),
root33a7d952025-05-18 17:24:41 +0000288
95630366963296f62025-06-02 21:16:32 +0800289 DynamicTest.dynamicTest("SubmitSeed duplicate", () -> {
290 int ret = db.SubmitSeed(begId, seed);
291 Assertions.assertEquals(1, ret, "重复提交应返回1");
292 }),
root33a7d952025-05-18 17:24:41 +0000293
95630366963296f62025-06-02 21:16:32 +0800294 DynamicTest.dynamicTest("SubmitSeed invalid param", () -> {
295 Assertions.assertEquals(2, db.SubmitSeed(null, seed));
296 Assertions.assertEquals(2, db.SubmitSeed(begId, null));
297 }));
298 } finally {
299 // 清理测试数据
300 EntityTransaction tx = em.getTransaction();
301 tx.begin();
302 try {
303 // 恢复求种状态
304 BegInfo toRestore = em.find(BegInfo.class, begId);
305 if (toRestore != null) {
306 toRestore.hasseed = 0;
307 em.merge(toRestore);
308 }
root33a7d952025-05-18 17:24:41 +0000309
95630366963296f62025-06-02 21:16:32 +0800310 // 清理投票记录
311 em.createQuery("DELETE FROM Vote v WHERE v.begid = :begid AND v.seedid = :seedid")
312 .setParameter("begid", begId)
313 .setParameter("seedid", seed.seedid)
314 .executeUpdate();
315 } catch (Exception ignored) {
316 }
317 tx.commit();
318 }
319 }
root33a7d952025-05-18 17:24:41 +0000320
95630366963296f62025-06-02 21:16:32 +0800321 @Test
322 void testSettleBeg() {
323 String uid = em.createQuery("select u.userid from User u", String.class)
324 .setMaxResults(1)
325 .getSingleResult();
root33a7d952025-05-18 17:24:41 +0000326
95630366963296f62025-06-02 21:16:32 +0800327 String begId = "test_beg_settle_" + System.currentTimeMillis();
328 String seedId = "test_seed_settle_" + System.currentTimeMillis();
root33a7d952025-05-18 17:24:41 +0000329
95630366963296f62025-06-02 21:16:32 +0800330 BegInfo beg = new BegInfo();
331 beg.begid = begId;
332 beg.begnumbers = 1;
333 beg.magic = 100;
334 Calendar cal = Calendar.getInstance();
335 cal.add(Calendar.DAY_OF_MONTH, -15); // 15天前
336 beg.endtime = cal.getTime();
337 beg.hasseed = 0;
root33a7d952025-05-18 17:24:41 +0000338
95630366963296f62025-06-02 21:16:32 +0800339 Seed seed = new Seed();
340 seed.seedid = seedId;
341 seed.seeduserid = uid;
342 seed.title = "测试种子";
343 seed.seedsize = "1";
root33a7d952025-05-18 17:24:41 +0000344
95630366963296f62025-06-02 21:16:32 +0800345 try {
346 db.AddBegSeed(beg);
347 db.SubmitSeed(begId, seed);
348 db.VoteSeed(begId, seedId, uid);
root33a7d952025-05-18 17:24:41 +0000349
95630366963296f62025-06-02 21:16:32 +0800350 db.SettleBeg();
351 em.clear();
root33a7d952025-05-18 17:24:41 +0000352
95630366963296f62025-06-02 21:16:32 +0800353 BegInfo settled = em.find(BegInfo.class, begId);
354 Assertions.assertEquals(settled.hasseed, 1, "求种应已完成");
root33a7d952025-05-18 17:24:41 +0000355
95630366963296f62025-06-02 21:16:32 +0800356 UserPT userPT = em.find(UserPT.class, uid);
357 Assertions.assertTrue(userPT.magic >= 0, "用户应获得魔力值奖励");
358 } finally {
359 EntityTransaction tx = em.getTransaction();
360 tx.begin();
361 try {
362 // 删除投票记录
363 em.createQuery(
364 "DELETE FROM UserVotes v WHERE v.begid = :begid AND v.seedid = :seedid AND v.userid = :uid")
365 .setParameter("begid", begId)
366 .setParameter("seedid", seedId)
367 .setParameter("uid", uid)
368 .executeUpdate();
369 // 删除提交记录
370 em.createQuery("DELETE FROM SubmitSeed s WHERE s.begid = :begid AND s.seedid = :seedid")
371 .setParameter("begid", begId)
372 .setParameter("seedid", seedId)
373 .executeUpdate();
374 } catch (Exception ignored) {
375 }
376 // 删除种子和求种任务
377 Seed toDeleteSeed = em.find(Seed.class, seedId);
378 if (toDeleteSeed != null)
379 em.remove(toDeleteSeed);
380 BegInfo toDeleteBeg = em.find(BegInfo.class, begId);
381 if (toDeleteBeg != null)
382 em.remove(toDeleteBeg);
383 tx.commit();
384 }
385 }
root33a7d952025-05-18 17:24:41 +0000386
95630366963296f62025-06-02 21:16:32 +0800387 @TestFactory
388 Collection<DynamicTest> testAddPost() {
389 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
390 if (userIds.isEmpty())
391 return Collections.emptyList();
392 String uid = userIds.get(0);
393 String postid = "test_post_" + System.currentTimeMillis();
394 Post post = new Post();
395 post.postid = postid;
396 post.postuserid = uid;
397 post.posttitle = "单元测试帖子";
398 post.postcontent = "内容";
399 post.posttime = new Date();
root33a7d952025-05-18 17:24:41 +0000400
95630366963296f62025-06-02 21:16:32 +0800401 return List.of(
402 DynamicTest.dynamicTest("AddPost success", () -> {
403 try {
404 int ret = db.AddPost(post);
405 Assertions.assertEquals(0, ret, "AddPost 应返回0");
406 Post inserted = em.find(Post.class, postid);
407 Assertions.assertNotNull(inserted, "帖子应已插入");
408 } finally {
409 EntityTransaction tx = em.getTransaction();
410 tx.begin();
411 Post toDelete = em.find(Post.class, postid);
412 if (toDelete != null)
413 em.remove(toDelete);
414 tx.commit();
415 }
416 }),
417 DynamicTest.dynamicTest("AddPost duplicate", () -> {
418 try {
419 db.AddPost(post);
420 int ret = db.AddPost(post);
421 Assertions.assertEquals(1, ret, "重复插入应返回1");
422 } finally {
423 EntityTransaction tx = em.getTransaction();
424 tx.begin();
425 Post toDelete = em.find(Post.class, postid);
426 if (toDelete != null)
427 em.remove(toDelete);
428 tx.commit();
429 }
430 }),
431 DynamicTest.dynamicTest("AddPost user not exist", () -> {
432 Post p2 = new Post();
433 p2.postid = "test_post_" + (System.currentTimeMillis() + 1);
434 p2.postuserid = "not_exist_user";
435 p2.posttitle = "无效用户";
436 p2.postcontent = "内容";
437 p2.posttime = new Date();
438 int ret = db.AddPost(p2);
439 Assertions.assertEquals(2, ret, "用户不存在应返回2");
440 }),
441 DynamicTest.dynamicTest("AddPost invalid param", () -> {
442 Assertions.assertEquals(2, db.AddPost(null));
443 Post p3 = new Post();
444 p3.postid = null;
445 p3.postuserid = uid;
446 Assertions.assertEquals(2, db.AddPost(p3));
447 Post p4 = new Post();
448 p4.postid = "";
449 p4.postuserid = uid;
450 Assertions.assertEquals(2, db.AddPost(p4));
451 }));
452 }
root33a7d952025-05-18 17:24:41 +0000453
95630366963296f62025-06-02 21:16:32 +0800454 @TestFactory
455 Collection<DynamicTest> testUpdatePost() {
456 List<Post> existingPosts = em.createQuery("SELECT p FROM Post p", Post.class)
457 .setMaxResults(1)
458 .getResultList();
459 if (existingPosts.isEmpty()) {
460 return Collections.emptyList();
461 }
root33a7d952025-05-18 17:24:41 +0000462
95630366963296f62025-06-02 21:16:32 +0800463 Post originalPost = existingPosts.get(0);
464 String postId = originalPost.postid;
465 String originalTitle = originalPost.posttitle;
466 String originalContent = originalPost.postcontent;
467 String originalUserId = originalPost.postuserid;
468 Date originalTime = originalPost.posttime;
root33a7d952025-05-18 17:24:41 +0000469
95630366963296f62025-06-02 21:16:32 +0800470 return List.of(
471 DynamicTest.dynamicTest("UpdatePost success", () -> {
472 try {
473 Post update = new Post();
474 update.postid = postId;
475 update.postuserid = originalUserId;
476 update.posttitle = originalTitle + "_updated";
477 update.postcontent = originalContent + "_updated";
478 update.posttime = originalTime;
root33a7d952025-05-18 17:24:41 +0000479
95630366963296f62025-06-02 21:16:32 +0800480 int ret = db.UpdatePost(update);
481 Assertions.assertEquals(0, ret, "UpdatePost应返回0");
root33a7d952025-05-18 17:24:41 +0000482
95630366963296f62025-06-02 21:16:32 +0800483 em.clear();
484 Post updated = em.find(Post.class, postId);
485 Assertions.assertEquals(originalTitle + "_updated", updated.posttitle, "标题应已更新");
486 Assertions.assertEquals(originalContent + "_updated", updated.postcontent, "内容应已更新");
487 } finally {
488 // 恢复原始数据
489 EntityTransaction tx = em.getTransaction();
490 tx.begin();
491 Post toRestore = em.find(Post.class, postId);
492 if (toRestore != null) {
493 toRestore.posttitle = originalTitle;
494 toRestore.postcontent = originalContent;
495 toRestore.posttime = originalTime;
496 em.merge(toRestore);
497 }
498 tx.commit();
499 }
500 }),
501 DynamicTest.dynamicTest("UpdatePost not exist", () -> {
502 Post notExist = new Post();
503 notExist.postid = "not_exist_post";
504 notExist.postuserid = originalUserId;
505 notExist.posttitle = "不存在的帖子";
506 notExist.postcontent = "测试内容";
507 notExist.posttime = new Date();
root33a7d952025-05-18 17:24:41 +0000508
95630366963296f62025-06-02 21:16:32 +0800509 int ret = db.UpdatePost(notExist);
510 Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
511 }),
512 DynamicTest.dynamicTest("UpdatePost invalid param", () -> {
513 Assertions.assertEquals(2, db.UpdatePost(null));
514 Post invalid = new Post();
515 invalid.postid = "";
516 Assertions.assertEquals(2, db.UpdatePost(invalid));
root33a7d952025-05-18 17:24:41 +0000517
95630366963296f62025-06-02 21:16:32 +0800518 Post invalid2 = new Post();
519 invalid2.postid = null;
520 Assertions.assertEquals(2, db.UpdatePost(invalid2));
521 }));
522 }
root33a7d952025-05-18 17:24:41 +0000523
95630366963296f62025-06-02 21:16:32 +0800524 @TestFactory
525 Collection<DynamicTest> testDeletePost() {
wht85171c22025-06-08 01:38:55 +0800526 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
527 if (userIds.isEmpty()) {
95630366963296f62025-06-02 21:16:32 +0800528 return Collections.emptyList();
529 }
wht85171c22025-06-08 01:38:55 +0800530 String uid = userIds.get(0);
531 String postid = "test_post_" + System.currentTimeMillis();
532 Post post = new Post();
533 post.postid = postid;
534 post.postuserid = uid;
535 post.posttitle = "单元测试帖子";
536 post.postcontent = "内容";
537 post.posttime = new Date();
root33a7d952025-05-18 17:24:41 +0000538
95630366963296f62025-06-02 21:16:32 +0800539 return List.of(
wht85171c22025-06-08 01:38:55 +0800540 DynamicTest.dynamicTest("DeletePost success", () -> {
541 try {
542 int addRet = db.AddPost(post);
543 int delRet = db.DeletePost(postid);
544 Assertions.assertEquals(0, delRet, "DeletePost 应返回0");
545 } finally {
546 }
547 }),
548 DynamicTest.dynamicTest("DeletePost not exist", () -> {
549 int ret = db.DeletePost("not_exist_post");
550 Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
551 }),
552 DynamicTest.dynamicTest("DeletePost invalid param", () -> {
553 Assertions.assertEquals(2, db.DeletePost(null));
554 Assertions.assertEquals(2, db.DeletePost(""));
555 })
556 );
95630366963296f62025-06-02 21:16:32 +0800557 }
root33a7d952025-05-18 17:24:41 +0000558
95630366963296f62025-06-02 21:16:32 +0800559 @TestFactory
560 Collection<DynamicTest> testAddComment() {
561 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
562 List<String> postIds = em.createQuery("select p.postid from Post p", String.class).getResultList();
563 if (userIds.isEmpty() || postIds.isEmpty()) {
564 return Collections.emptyList();
565 }
566 String uid = userIds.get(0);
567 String pid = postIds.get(0);
568 String comment = "单元测试评论";
569 return List.of(
570 DynamicTest.dynamicTest("AddComment", () -> {
571 String replyid = null;
572 try {
573 int ret = db.AddComment(pid, uid, comment);
574 Assertions.assertEquals(0, ret, "AddComment 应返回0");
575 List<PostReply> replies = em.createQuery(
576 "SELECT r FROM PostReply r WHERE r.postid = :pid AND r.authorid = :uid AND r.content = :c",
577 PostReply.class)
578 .setParameter("pid", pid)
579 .setParameter("uid", uid)
580 .setParameter("c", comment)
581 .getResultList();
582 Assertions.assertFalse(replies.isEmpty(), "评论应已插入");
583 replyid = replies.get(0).replyid;
584 } finally {
585 if (replyid != null) {
586 EntityTransaction tx = em.getTransaction();
587 tx.begin();
588 PostReply toDelete = em.find(PostReply.class, replyid);
589 if (toDelete != null)
590 em.remove(toDelete);
591 tx.commit();
592 }
593 }
594 }));
595 }
root33a7d952025-05-18 17:24:41 +0000596
95630366963296f62025-06-02 21:16:32 +0800597 @TestFactory
598 Collection<DynamicTest> testDeleteComment() {
599 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
600 List<String> postIds = em.createQuery("select p.postid from Post p", String.class).getResultList();
601 if (userIds.isEmpty() || postIds.isEmpty()) {
602 return Collections.emptyList();
603 }
604 String uid = userIds.get(0);
605 String pid = postIds.get(0);
606 String comment = "待删除评论";
root33a7d952025-05-18 17:24:41 +0000607
95630366963296f62025-06-02 21:16:32 +0800608 return List.of(
609 DynamicTest.dynamicTest("DeleteComment", () -> {
610 String replyid = null;
611 try {
612 // 先确保评论存在
613 EntityTransaction tx = em.getTransaction();
614 tx.begin();
615 PostReply reply = new PostReply();
616 reply.replyid = "reply_" + System.currentTimeMillis();
617 reply.postid = pid;
618 reply.authorid = uid;
619 reply.content = comment;
620 reply.createdAt = new Date();
621 em.persist(reply);
622 tx.commit();
623 replyid = reply.replyid;
root33a7d952025-05-18 17:24:41 +0000624
95630366963296f62025-06-02 21:16:32 +0800625 // 执行删除测试
626 int ret = db.DeleteComment(pid, replyid);
627 Assertions.assertEquals(0, ret, "DeleteComment 应返回0");
root33a7d952025-05-18 17:24:41 +0000628
95630366963296f62025-06-02 21:16:32 +0800629 em.clear();
630 PostReply deleted = em.find(PostReply.class, replyid);
631 Assertions.assertNull(deleted, "评论应已删除");
632 } finally {
633 if (replyid != null) {
634 try {
635 EntityTransaction tx = em.getTransaction();
636 if (!tx.isActive()) {
637 tx.begin();
638 PostReply toDelete = em.find(PostReply.class, replyid);
639 if (toDelete != null) {
640 em.remove(toDelete);
641 }
642 tx.commit();
643 }
644 } catch (Exception e) {
645 }
646 }
647 }
648 }));
649 }
root33a7d952025-05-18 17:24:41 +0000650
95630366963296f62025-06-02 21:16:32 +0800651 @TestFactory
652 Collection<DynamicTest> testExchangeMagicToUpload() {
653 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
654 if (userIds.isEmpty())
655 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000656
95630366963296f62025-06-02 21:16:32 +0800657 String uid = userIds.get(0);
658 final UserPT testUserPT;
root33a7d952025-05-18 17:24:41 +0000659
95630366963296f62025-06-02 21:16:32 +0800660 EntityTransaction tx = em.getTransaction();
661 tx.begin();
662 UserPT tempUserPT = em.find(UserPT.class, uid);
root33a7d952025-05-18 17:24:41 +0000663
95630366963296f62025-06-02 21:16:32 +0800664 if (tempUserPT == null) {
665 tempUserPT = new UserPT();
666 tempUserPT.userid = uid;
667 tempUserPT.magic = 1000;
668 tempUserPT.upload = 1000;
669 tempUserPT.download = 1000;
670 em.persist(tempUserPT);
671 } else {
672 tempUserPT.magic = 1000;
673 tempUserPT.upload = 1000;
674 em.merge(tempUserPT);
675 }
676 testUserPT = tempUserPT;
677 tx.commit();
root33a7d952025-05-18 17:24:41 +0000678
95630366963296f62025-06-02 21:16:32 +0800679 return List.of(
680 DynamicTest.dynamicTest("ExchangeMagicToUpload success", () -> {
681 try {
682 int magic = 100;
683 long beforeUpload = testUserPT.upload;
684 int beforeMagic = testUserPT.magic;
root33a7d952025-05-18 17:24:41 +0000685
95630366963296f62025-06-02 21:16:32 +0800686 boolean ret = db.ExchangeMagicToUpload(uid, magic);
687 Assertions.assertTrue(ret, "兑换上传量应成功");
root33a7d952025-05-18 17:24:41 +0000688
95630366963296f62025-06-02 21:16:32 +0800689 em.clear();
690 UserPT after = em.find(UserPT.class, uid);
691 Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
692 Assertions.assertEquals(beforeUpload + magic, after.upload, "上传量应增加");
693 } finally {
694 EntityTransaction tx2 = em.getTransaction();
695 tx2.begin();
696 UserPT user = em.find(UserPT.class, uid);
697 if (user != null) {
698 user.magic = 0;
699 user.upload = 0;
700 em.merge(user);
701 }
702 tx2.commit();
703 }
704 }),
root33a7d952025-05-18 17:24:41 +0000705
95630366963296f62025-06-02 21:16:32 +0800706 DynamicTest.dynamicTest("ExchangeMagicToUpload insufficient magic", () -> {
707 boolean ret = db.ExchangeMagicToUpload(uid, 2000);
708 Assertions.assertFalse(ret, "魔力值不足时应返回false");
709 }),
root33a7d952025-05-18 17:24:41 +0000710
95630366963296f62025-06-02 21:16:32 +0800711 DynamicTest.dynamicTest("ExchangeMagicToUpload invalid params", () -> {
712 Assertions.assertFalse(db.ExchangeMagicToUpload(null, 100));
713 Assertions.assertFalse(db.ExchangeMagicToUpload("", 100));
714 Assertions.assertFalse(db.ExchangeMagicToUpload(uid, 0));
715 Assertions.assertFalse(db.ExchangeMagicToUpload(uid, -1));
716 }));
717 }
root33a7d952025-05-18 17:24:41 +0000718
95630366963296f62025-06-02 21:16:32 +0800719 @TestFactory
720 Collection<DynamicTest> testExchangeMagicToDownload() {
721 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
722 if (userIds.isEmpty())
723 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000724
95630366963296f62025-06-02 21:16:32 +0800725 String uid = userIds.get(0);
726 final UserPT testUserPT;
root33a7d952025-05-18 17:24:41 +0000727
95630366963296f62025-06-02 21:16:32 +0800728 EntityTransaction tx = em.getTransaction();
729 tx.begin();
730 UserPT tempUserPT = em.find(UserPT.class, uid);
731 if (tempUserPT == null) {
732 tempUserPT = new UserPT();
733 tempUserPT.userid = uid;
734 tempUserPT.magic = 1000;
735 tempUserPT.upload = 1000;
736 tempUserPT.download = 1000;
737 em.persist(tempUserPT);
738 } else {
739 tempUserPT.magic = 1000;
740 tempUserPT.download = 1000;
741 em.merge(tempUserPT);
742 }
743 testUserPT = tempUserPT;
744 tx.commit();
root33a7d952025-05-18 17:24:41 +0000745
95630366963296f62025-06-02 21:16:32 +0800746 return List.of(
747 DynamicTest.dynamicTest("ExchangeMagicToDownload success", () -> {
748 try {
749 int magic = 100;
750 long beforeDownload = testUserPT.download;
751 int beforeMagic = testUserPT.magic;
root33a7d952025-05-18 17:24:41 +0000752
95630366963296f62025-06-02 21:16:32 +0800753 boolean ret = db.ExchangeMagicToDownload(uid, magic);
754 Assertions.assertTrue(ret, "兑换下载量应成功");
root33a7d952025-05-18 17:24:41 +0000755
95630366963296f62025-06-02 21:16:32 +0800756 em.clear();
757 UserPT after = em.find(UserPT.class, uid);
758 Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
759 Assertions.assertEquals(beforeDownload - magic, after.download, "下载量应减少");
760 } finally {
761 EntityTransaction tx2 = em.getTransaction();
762 tx2.begin();
763 UserPT user = em.find(UserPT.class, uid);
764 if (user != null) {
765 user.magic = 0;
766 user.download = 0;
767 em.merge(user);
768 }
769 tx2.commit();
770 }
771 }),
root33a7d952025-05-18 17:24:41 +0000772
95630366963296f62025-06-02 21:16:32 +0800773 DynamicTest.dynamicTest("ExchangeMagicToDownload insufficient magic", () -> {
774 boolean ret = db.ExchangeMagicToDownload(uid, 2000);
775 Assertions.assertFalse(ret, "魔力值不足时应返回false");
776 }),
root33a7d952025-05-18 17:24:41 +0000777
95630366963296f62025-06-02 21:16:32 +0800778 DynamicTest.dynamicTest("ExchangeMagicToDownload invalid params", () -> {
779 Assertions.assertFalse(db.ExchangeMagicToDownload(null, 100));
780 Assertions.assertFalse(db.ExchangeMagicToDownload("", 100));
781 Assertions.assertFalse(db.ExchangeMagicToDownload(uid, 0));
782 Assertions.assertFalse(db.ExchangeMagicToDownload(uid, -1));
783 }));
784 }
root33a7d952025-05-18 17:24:41 +0000785
95630366963296f62025-06-02 21:16:32 +0800786 @TestFactory
787 Collection<DynamicTest> testExchangeMagicToVip() {
788 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
789 if (userIds.isEmpty())
790 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000791
95630366963296f62025-06-02 21:16:32 +0800792 String uid = userIds.get(0);
793 final UserPT testUserPT;
root33a7d952025-05-18 17:24:41 +0000794
95630366963296f62025-06-02 21:16:32 +0800795 EntityTransaction tx = em.getTransaction();
796 tx.begin();
797 UserPT tempUserPT = em.find(UserPT.class, uid);
798 if (tempUserPT == null) {
799 tempUserPT = new UserPT();
800 tempUserPT.userid = uid;
801 tempUserPT.magic = 1000;
802 tempUserPT.upload = 1000;
803 tempUserPT.download = 1000;
804 tempUserPT.viptime = 0;
805 em.persist(tempUserPT);
806 } else {
807 tempUserPT.magic = 1000;
808 tempUserPT.viptime = 0;
809 em.merge(tempUserPT);
810 }
811 testUserPT = tempUserPT;
812 tx.commit();
root33a7d952025-05-18 17:24:41 +0000813
95630366963296f62025-06-02 21:16:32 +0800814 return List.of(
815 DynamicTest.dynamicTest("ExchangeMagicToVip success", () -> {
816 try {
817 int magic = 100;
818 int beforeVip = testUserPT.viptime;
819 int beforeMagic = testUserPT.magic;
root33a7d952025-05-18 17:24:41 +0000820
95630366963296f62025-06-02 21:16:32 +0800821 boolean ret = db.ExchangeMagicToVip(uid, magic);
822 Assertions.assertTrue(ret, "兑换VIP次数应成功");
root33a7d952025-05-18 17:24:41 +0000823
95630366963296f62025-06-02 21:16:32 +0800824 em.clear();
825 UserPT after = em.find(UserPT.class, uid);
826 Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
827 Assertions.assertEquals(beforeVip + magic, after.viptime, "VIP次数应增加");
828 } finally {
829 EntityTransaction tx2 = em.getTransaction();
830 tx2.begin();
831 UserPT user = em.find(UserPT.class, uid);
832 if (user != null) {
833 user.magic = 0;
834 user.viptime = 0;
835 em.merge(user);
836 }
837 tx2.commit();
838 }
839 }),
root33a7d952025-05-18 17:24:41 +0000840
95630366963296f62025-06-02 21:16:32 +0800841 DynamicTest.dynamicTest("ExchangeMagicToVip insufficient magic", () -> {
842 boolean ret = db.ExchangeMagicToVip(uid, 2000);
843 Assertions.assertFalse(ret, "魔力值不足时应返回false");
844 }),
root33a7d952025-05-18 17:24:41 +0000845
95630366963296f62025-06-02 21:16:32 +0800846 DynamicTest.dynamicTest("ExchangeMagicToVip invalid params", () -> {
847 Assertions.assertFalse(db.ExchangeMagicToVip(null, 100));
848 Assertions.assertFalse(db.ExchangeMagicToVip("", 100));
849 Assertions.assertFalse(db.ExchangeMagicToVip(uid, 0));
850 Assertions.assertFalse(db.ExchangeMagicToVip(uid, -1));
851 }));
852 }
root33a7d952025-05-18 17:24:41 +0000853
95630366963296f62025-06-02 21:16:32 +0800854 @TestFactory
855 Collection<DynamicTest> testUploadTransmitProfile() {
856 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
857 if (userIds.isEmpty())
858 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000859
95630366963296f62025-06-02 21:16:32 +0800860 String uid = userIds.get(0);
861 String profileId = "test_profile_" + System.currentTimeMillis();
862 Profile profile = new Profile();
863 profile.profileurl = profileId;
864 profile.userid = uid;
865 profile.magictogive = "100";
866 profile.uploadtogive = "1000";
867 profile.downloadtogive = "500";
868 profile.exampass = false;
869 profile.magicgived = "0";
870 profile.uploadgived = "0";
871 profile.applicationurl = "http://example.com/apply";
root33a7d952025-05-18 17:24:41 +0000872
95630366963296f62025-06-02 21:16:32 +0800873 return List.of(
874 DynamicTest.dynamicTest("UploadTransmitProfile success", () -> {
875 try {
876 boolean ret = db.UploadTransmitProfile(profile);
877 Assertions.assertTrue(ret, "上传迁移信息应成功");
root33a7d952025-05-18 17:24:41 +0000878
95630366963296f62025-06-02 21:16:32 +0800879 Profile inserted = em.find(Profile.class, profileId);
880 Assertions.assertNotNull(inserted, "迁移信息应已插入");
881 Assertions.assertFalse(inserted.exampass, "新迁移信息默认未审核");
882 } finally {
883 EntityTransaction tx = em.getTransaction();
884 tx.begin();
885 Profile toDelete = em.find(Profile.class, profileId);
886 if (toDelete != null)
887 em.remove(toDelete);
888 tx.commit();
889 }
890 }),
root33a7d952025-05-18 17:24:41 +0000891
95630366963296f62025-06-02 21:16:32 +0800892 DynamicTest.dynamicTest("UploadTransmitProfile duplicate", () -> {
893 try {
894 db.UploadTransmitProfile(profile);
895 boolean ret = db.UploadTransmitProfile(profile);
896 Assertions.assertFalse(ret, "重复上传应返回false");
897 } finally {
898 EntityTransaction tx = em.getTransaction();
899 tx.begin();
900 Profile toDelete = em.find(Profile.class, profileId);
901 if (toDelete != null)
902 em.remove(toDelete);
903 tx.commit();
904 }
905 }),
root33a7d952025-05-18 17:24:41 +0000906
95630366963296f62025-06-02 21:16:32 +0800907 DynamicTest.dynamicTest("UploadTransmitProfile invalid params", () -> {
908 Assertions.assertFalse(db.UploadTransmitProfile(null));
909 Profile invalidProfile = new Profile();
910 Assertions.assertFalse(db.UploadTransmitProfile(invalidProfile));
911 }));
912 }
root33a7d952025-05-18 17:24:41 +0000913
95630366963296f62025-06-02 21:16:32 +0800914 @TestFactory
915 Collection<DynamicTest> testGetTransmitProfile() {
916 // 获取现有的迁移信息记录
917 List<Profile> existingProfiles = em.createQuery(
918 "SELECT p FROM Profile p", Profile.class)
919 .setMaxResults(1)
920 .getResultList();
921 if (existingProfiles.isEmpty())
922 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000923
95630366963296f62025-06-02 21:16:32 +0800924 Profile profile = existingProfiles.get(0);
925 String profileId = profile.profileurl;
root33a7d952025-05-18 17:24:41 +0000926
95630366963296f62025-06-02 21:16:32 +0800927 return List.of(
928 DynamicTest.dynamicTest("GetTransmitProfile success", () -> {
929 Profile ret = db.GetTransmitProfile(profileId);
930 Assertions.assertNotNull(ret, "应成功获取迁移信息");
931 }),
root33a7d952025-05-18 17:24:41 +0000932
95630366963296f62025-06-02 21:16:32 +0800933 DynamicTest.dynamicTest("GetTransmitProfile not exist", () -> {
934 Profile ret = db.GetTransmitProfile("not_exist_profile");
935 Assertions.assertNull(ret, "不存在的迁移信息应返回null");
936 }),
root33a7d952025-05-18 17:24:41 +0000937
95630366963296f62025-06-02 21:16:32 +0800938 DynamicTest.dynamicTest("GetTransmitProfile invalid params", () -> {
939 Assertions.assertNull(db.GetTransmitProfile(null));
940 Assertions.assertNull(db.GetTransmitProfile(""));
941 }));
942 }
root33a7d952025-05-18 17:24:41 +0000943
95630366963296f62025-06-02 21:16:32 +0800944 @TestFactory
945 Collection<DynamicTest> testExamTransmitProfile() {
946 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
947 if (userIds.isEmpty())
948 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000949
95630366963296f62025-06-02 21:16:32 +0800950 String uid = userIds.get(0);
951 String profileId = "test_profile_exam_" + System.currentTimeMillis();
952 Profile profile = new Profile();
953 profile.profileurl = profileId;
954 profile.userid = uid;
955 profile.magictogive = "100";
956 profile.uploadtogive = "1000";
957 profile.exampass = false;
958 profile.magicgived = "0";
959 profile.uploadgived = "0";
960 profile.applicationurl = "http://example.com/apply";
root33a7d952025-05-18 17:24:41 +0000961
95630366963296f62025-06-02 21:16:32 +0800962 EntityTransaction tx = em.getTransaction();
963 tx.begin();
964 em.persist(profile);
965 UserPT userPT = em.find(UserPT.class, uid);
966 if (userPT == null) {
967 userPT = new UserPT();
968 userPT.userid = uid;
969 userPT.magic = 0;
970 userPT.upload = 0;
971 em.persist(userPT);
972 } else {
973 userPT.magic = 0;
974 userPT.upload = 0;
975 em.merge(userPT);
976 }
977 tx.commit();
root33a7d952025-05-18 17:24:41 +0000978
95630366963296f62025-06-02 21:16:32 +0800979 return List.of(
980 DynamicTest.dynamicTest("ExamTransmitProfile approve", () -> {
981 try {
982 boolean ret = db.ExamTransmitProfile(profileId, true);
983 Assertions.assertTrue(ret, "审核通过应成功");
984 } finally {
985 EntityTransaction tx2 = em.getTransaction();
986 tx2.begin();
987 UserPT user = em.find(UserPT.class, uid);
988 if (user != null) {
989 user.magic = 0;
990 user.upload = 0;
991 em.merge(user);
992 }
993 Profile toDelete = em.find(Profile.class, profileId);
994 if (toDelete != null)
995 em.remove(toDelete);
996 tx2.commit();
997 }
998 }),
root33a7d952025-05-18 17:24:41 +0000999
95630366963296f62025-06-02 21:16:32 +08001000 DynamicTest.dynamicTest("ExamTransmitProfile reject", () -> {
1001 String rejectId = "test_profile_reject_" + System.currentTimeMillis();
1002 Profile rejectProfile = new Profile();
1003 rejectProfile.profileurl = rejectId;
1004 rejectProfile.userid = uid;
1005 rejectProfile.magictogive = "100";
1006 rejectProfile.uploadtogive = "1000";
1007 rejectProfile.magicgived = "0";
1008 rejectProfile.uploadgived = "0";
1009 rejectProfile.exampass = false;
1010 rejectProfile.applicationurl = "http://example.com/apply";
root33a7d952025-05-18 17:24:41 +00001011
95630366963296f62025-06-02 21:16:32 +08001012 EntityTransaction tx3 = em.getTransaction();
1013 tx3.begin();
1014 em.persist(rejectProfile);
1015 tx3.commit();
root33a7d952025-05-18 17:24:41 +00001016
95630366963296f62025-06-02 21:16:32 +08001017 try {
1018 boolean ret = db.ExamTransmitProfile(rejectId, false);
1019 Assertions.assertTrue(ret, "审核拒绝应成功");
1020 } finally {
1021 EntityTransaction tx4 = em.getTransaction();
1022 tx4.begin();
1023 Profile toDelete = em.find(Profile.class, rejectId);
1024 if (toDelete != null)
1025 em.remove(toDelete);
1026 tx4.commit();
1027 }
1028 }),
root33a7d952025-05-18 17:24:41 +00001029
95630366963296f62025-06-02 21:16:32 +08001030 DynamicTest.dynamicTest("ExamTransmitProfile invalid params", () -> {
1031 Assertions.assertFalse(db.ExamTransmitProfile(null, true));
1032 Assertions.assertFalse(db.ExamTransmitProfile("", true));
1033 Assertions.assertFalse(db.ExamTransmitProfile("not_exist_profile", true));
1034 }));
1035 }
root33a7d952025-05-18 17:24:41 +00001036
95630366963296f62025-06-02 21:16:32 +08001037 @Test
1038 void testGetTransmitProfileList() {
1039 em.getTransaction().begin();
1040 em.createQuery("DELETE FROM Profile").executeUpdate();
1041 em.getTransaction().commit();
root33a7d952025-05-18 17:24:41 +00001042
95630366963296f62025-06-02 21:16:32 +08001043 List<String> userIds = em.createQuery("select u.userid from User u", String.class)
1044 .setMaxResults(3)
1045 .getResultList();
1046 if (userIds.isEmpty()) {
1047 return;
1048 }
root33a7d952025-05-18 17:24:41 +00001049
95630366963296f62025-06-02 21:16:32 +08001050 EntityTransaction tx = em.getTransaction();
1051 tx.begin();
1052 for (int i = 0; i < userIds.size(); i++) {
1053 Profile profile = new Profile();
1054 profile.profileurl = "test_profile_list_" + i + "_" + System.currentTimeMillis();
1055 profile.userid = userIds.get(i);
1056 profile.magictogive = String.valueOf(100 * (i + 1));
1057 profile.uploadtogive = String.valueOf(1000 * (i + 1));
1058 profile.exampass = false;
1059 profile.magicgived = "0";
1060 profile.uploadgived = "0";
1061 profile.applicationurl = "http://example.com/apply";
1062 em.persist(profile);
1063 }
1064 tx.commit();
root33a7d952025-05-18 17:24:41 +00001065
95630366963296f62025-06-02 21:16:32 +08001066 try {
1067 Profile[] profiles = db.GetTransmitProfileList();
1068 Assertions.assertEquals(userIds.size(), profiles.length, "应返回所有迁移申请");
1069 Arrays.stream(profiles)
1070 .forEach(p -> Assertions.assertTrue(p.profileurl.startsWith("test_profile_list_"), "应为测试数据"));
1071 } finally {
1072 EntityTransaction tx2 = em.getTransaction();
1073 tx2.begin();
1074 em.createQuery("DELETE FROM Profile p WHERE p.profileurl LIKE 'test_profile_list_%'").executeUpdate();
1075 tx2.commit();
1076 }
1077 }
1078}