blob: 4d8c1b672ce67ee6670b8f1693d2eb4c02791d5a [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() {
95630366963296f62025-06-02 21:16:32 +0800169 return List.of(
whtbe2bdc62025-06-08 23:14:26 +0800170 DynamicTest.dynamicTest("DeleteBegSeed success", () -> {
171 String begId = "test_beg_" + (System.currentTimeMillis() + 1);
172 BegInfo beg = new BegInfo();
173 beg.begid = begId;
174 beg.begnumbers = 0;
175 beg.magic = 1;
176 beg.endtime = new Date();
177 beg.hasseed = 0;
root33a7d952025-05-18 17:24:41 +0000178
whtbe2bdc62025-06-08 23:14:26 +0800179 int addRet = db.AddBegSeed(beg);
180 int delRet = db.DeleteBegSeed(begId);
181 Assertions.assertEquals(0, delRet, "DeleteBegSeed 应返回0");
182 }),
183 DynamicTest.dynamicTest("DeleteBegSeed not exist", () -> {
184 int ret = db.DeleteBegSeed("not_exist_beg");
185 Assertions.assertEquals(1, ret, "不存在的求种任务应返回1");
186 }),
187 DynamicTest.dynamicTest("DeleteBegSeed invalid param", () -> {
188 Assertions.assertEquals(2, db.DeleteBegSeed(null));
189 Assertions.assertEquals(2, db.DeleteBegSeed(""));
190 })
191 );
95630366963296f62025-06-02 21:16:32 +0800192 }
root33a7d952025-05-18 17:24:41 +0000193
rhj7125df42025-06-09 01:06:01 +0800194 // @TestFactory
195 // Collection<DynamicTest> testVoteSeed() {
196 // // 获取现有用户ID
197 // List<String> userIds = em.createQuery("select u.userid from User u", String.class)
198 // .setMaxResults(1)
199 // .getResultList();
200 // if (userIds.isEmpty())
201 // return Collections.emptyList();
202 // String uid = userIds.get(0);
root33a7d952025-05-18 17:24:41 +0000203
rhj7125df42025-06-09 01:06:01 +0800204 // // 获取现有的求种信息
205 // List<BegInfo> begs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class)
206 // .setMaxResults(1)
207 // .getResultList();
208 // if (begs.isEmpty())
209 // return Collections.emptyList();
210 // String begId = begs.get(0).begid;
root33a7d952025-05-18 17:24:41 +0000211
rhj7125df42025-06-09 01:06:01 +0800212 // // 获取现有的种子信息
213 // List<Seed> seeds = em.createQuery("SELECT s FROM Seed s", Seed.class)
214 // .setMaxResults(1)
215 // .getResultList();
216 // if (seeds.isEmpty())
217 // return Collections.emptyList();
218 // String seedId = seeds.get(0).seedid;
root33a7d952025-05-18 17:24:41 +0000219
rhj7125df42025-06-09 01:06:01 +0800220 // try {
221 // return List.of(
222 // DynamicTest.dynamicTest("VoteSeed success", () -> {
223 // int ret = db.VoteSeed(begId, seedId, uid);
224 // Assertions.assertEquals(0, ret, "VoteSeed应返回0");
225 // }),
root33a7d952025-05-18 17:24:41 +0000226
rhj7125df42025-06-09 01:06:01 +0800227 // DynamicTest.dynamicTest("VoteSeed duplicate", () -> {
228 // int ret = db.VoteSeed(begId, seedId, uid);
229 // Assertions.assertEquals(1, ret, "重复投票应返回1");
230 // }),
root33a7d952025-05-18 17:24:41 +0000231
rhj7125df42025-06-09 01:06:01 +0800232 // DynamicTest.dynamicTest("VoteSeed invalid param", () -> {
233 // Assertions.assertEquals(2, db.VoteSeed(null, seedId, uid));
234 // Assertions.assertEquals(2, db.VoteSeed(begId, null, uid));
235 // Assertions.assertEquals(2, db.VoteSeed(begId, seedId, null));
236 // }));
237 // } finally {
238 // EntityTransaction tx = em.getTransaction();
239 // tx.begin();
240 // try {
241 // em.createQuery(
242 // "DELETE FROM UserVotes v WHERE v.begid = :begid AND v.seedid = :seedid AND v.userid = :uid")
243 // .setParameter("begid", begId)
244 // .setParameter("seedid", seedId)
245 // .setParameter("uid", uid)
246 // .executeUpdate();
247 // } catch (Exception ignored) {
248 // }
249 // tx.commit();
250 // }
251 // }
root33a7d952025-05-18 17:24:41 +0000252
95630366963296f62025-06-02 21:16:32 +0800253 @TestFactory
254 Collection<DynamicTest> testSubmitSeed() {
255 // 获取现有的求种信息
256 List<BegInfo> begs = em.createQuery("SELECT b FROM BegInfo b WHERE b.hasseed = 0", BegInfo.class)
257 .setMaxResults(1)
258 .getResultList();
259 if (begs.isEmpty())
260 return Collections.emptyList();
261 String begId = begs.get(0).begid;
root33a7d952025-05-18 17:24:41 +0000262
95630366963296f62025-06-02 21:16:32 +0800263 // 获取现有的可用种子信息
264 List<Seed> existingSeeds = em.createQuery(
265 "SELECT s FROM Seed s WHERE s.seedid NOT IN " +
266 "(SELECT ss.seed.seedid FROM SubmitSeed ss)",
267 Seed.class)
268 .setMaxResults(1)
269 .getResultList();
270 if (existingSeeds.isEmpty())
271 return Collections.emptyList();
272 Seed seed = existingSeeds.get(0);
root33a7d952025-05-18 17:24:41 +0000273
95630366963296f62025-06-02 21:16:32 +0800274 try {
275 return List.of(
276 DynamicTest.dynamicTest("SubmitSeed success", () -> {
277 int ret = db.SubmitSeed(begId, seed);
278 Assertions.assertEquals(0, ret, "SubmitSeed应返回0");
279 }),
root33a7d952025-05-18 17:24:41 +0000280
95630366963296f62025-06-02 21:16:32 +0800281 DynamicTest.dynamicTest("SubmitSeed duplicate", () -> {
282 int ret = db.SubmitSeed(begId, seed);
283 Assertions.assertEquals(1, ret, "重复提交应返回1");
284 }),
root33a7d952025-05-18 17:24:41 +0000285
95630366963296f62025-06-02 21:16:32 +0800286 DynamicTest.dynamicTest("SubmitSeed invalid param", () -> {
287 Assertions.assertEquals(2, db.SubmitSeed(null, seed));
288 Assertions.assertEquals(2, db.SubmitSeed(begId, null));
289 }));
290 } finally {
291 // 清理测试数据
292 EntityTransaction tx = em.getTransaction();
293 tx.begin();
294 try {
295 // 恢复求种状态
296 BegInfo toRestore = em.find(BegInfo.class, begId);
297 if (toRestore != null) {
298 toRestore.hasseed = 0;
299 em.merge(toRestore);
300 }
root33a7d952025-05-18 17:24:41 +0000301
95630366963296f62025-06-02 21:16:32 +0800302 // 清理投票记录
303 em.createQuery("DELETE FROM Vote v WHERE v.begid = :begid AND v.seedid = :seedid")
304 .setParameter("begid", begId)
305 .setParameter("seedid", seed.seedid)
306 .executeUpdate();
307 } catch (Exception ignored) {
308 }
309 tx.commit();
310 }
311 }
root33a7d952025-05-18 17:24:41 +0000312
95630366963296f62025-06-02 21:16:32 +0800313 @Test
314 void testSettleBeg() {
315 String uid = em.createQuery("select u.userid from User u", String.class)
316 .setMaxResults(1)
317 .getSingleResult();
root33a7d952025-05-18 17:24:41 +0000318
95630366963296f62025-06-02 21:16:32 +0800319 String begId = "test_beg_settle_" + System.currentTimeMillis();
320 String seedId = "test_seed_settle_" + System.currentTimeMillis();
root33a7d952025-05-18 17:24:41 +0000321
95630366963296f62025-06-02 21:16:32 +0800322 BegInfo beg = new BegInfo();
323 beg.begid = begId;
324 beg.begnumbers = 1;
325 beg.magic = 100;
326 Calendar cal = Calendar.getInstance();
327 cal.add(Calendar.DAY_OF_MONTH, -15); // 15天前
328 beg.endtime = cal.getTime();
329 beg.hasseed = 0;
root33a7d952025-05-18 17:24:41 +0000330
95630366963296f62025-06-02 21:16:32 +0800331 Seed seed = new Seed();
332 seed.seedid = seedId;
333 seed.seeduserid = uid;
334 seed.title = "测试种子";
335 seed.seedsize = "1";
root33a7d952025-05-18 17:24:41 +0000336
95630366963296f62025-06-02 21:16:32 +0800337 try {
338 db.AddBegSeed(beg);
339 db.SubmitSeed(begId, seed);
340 db.VoteSeed(begId, seedId, uid);
root33a7d952025-05-18 17:24:41 +0000341
95630366963296f62025-06-02 21:16:32 +0800342 db.SettleBeg();
343 em.clear();
root33a7d952025-05-18 17:24:41 +0000344
95630366963296f62025-06-02 21:16:32 +0800345 BegInfo settled = em.find(BegInfo.class, begId);
346 Assertions.assertEquals(settled.hasseed, 1, "求种应已完成");
root33a7d952025-05-18 17:24:41 +0000347
95630366963296f62025-06-02 21:16:32 +0800348 UserPT userPT = em.find(UserPT.class, uid);
349 Assertions.assertTrue(userPT.magic >= 0, "用户应获得魔力值奖励");
350 } finally {
351 EntityTransaction tx = em.getTransaction();
352 tx.begin();
353 try {
354 // 删除投票记录
355 em.createQuery(
356 "DELETE FROM UserVotes v WHERE v.begid = :begid AND v.seedid = :seedid AND v.userid = :uid")
357 .setParameter("begid", begId)
358 .setParameter("seedid", seedId)
359 .setParameter("uid", uid)
360 .executeUpdate();
361 // 删除提交记录
362 em.createQuery("DELETE FROM SubmitSeed s WHERE s.begid = :begid AND s.seedid = :seedid")
363 .setParameter("begid", begId)
364 .setParameter("seedid", seedId)
365 .executeUpdate();
366 } catch (Exception ignored) {
367 }
368 // 删除种子和求种任务
369 Seed toDeleteSeed = em.find(Seed.class, seedId);
370 if (toDeleteSeed != null)
371 em.remove(toDeleteSeed);
372 BegInfo toDeleteBeg = em.find(BegInfo.class, begId);
373 if (toDeleteBeg != null)
374 em.remove(toDeleteBeg);
375 tx.commit();
376 }
377 }
root33a7d952025-05-18 17:24:41 +0000378
95630366963296f62025-06-02 21:16:32 +0800379 @TestFactory
380 Collection<DynamicTest> testAddPost() {
381 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
382 if (userIds.isEmpty())
383 return Collections.emptyList();
384 String uid = userIds.get(0);
385 String postid = "test_post_" + System.currentTimeMillis();
386 Post post = new Post();
387 post.postid = postid;
388 post.postuserid = uid;
389 post.posttitle = "单元测试帖子";
390 post.postcontent = "内容";
391 post.posttime = new Date();
root33a7d952025-05-18 17:24:41 +0000392
95630366963296f62025-06-02 21:16:32 +0800393 return List.of(
394 DynamicTest.dynamicTest("AddPost success", () -> {
395 try {
396 int ret = db.AddPost(post);
397 Assertions.assertEquals(0, ret, "AddPost 应返回0");
398 Post inserted = em.find(Post.class, postid);
399 Assertions.assertNotNull(inserted, "帖子应已插入");
400 } finally {
401 EntityTransaction tx = em.getTransaction();
402 tx.begin();
403 Post toDelete = em.find(Post.class, postid);
404 if (toDelete != null)
405 em.remove(toDelete);
406 tx.commit();
407 }
408 }),
409 DynamicTest.dynamicTest("AddPost duplicate", () -> {
410 try {
411 db.AddPost(post);
412 int ret = db.AddPost(post);
413 Assertions.assertEquals(1, ret, "重复插入应返回1");
414 } finally {
415 EntityTransaction tx = em.getTransaction();
416 tx.begin();
417 Post toDelete = em.find(Post.class, postid);
418 if (toDelete != null)
419 em.remove(toDelete);
420 tx.commit();
421 }
422 }),
423 DynamicTest.dynamicTest("AddPost user not exist", () -> {
424 Post p2 = new Post();
425 p2.postid = "test_post_" + (System.currentTimeMillis() + 1);
426 p2.postuserid = "not_exist_user";
427 p2.posttitle = "无效用户";
428 p2.postcontent = "内容";
429 p2.posttime = new Date();
430 int ret = db.AddPost(p2);
431 Assertions.assertEquals(2, ret, "用户不存在应返回2");
432 }),
433 DynamicTest.dynamicTest("AddPost invalid param", () -> {
434 Assertions.assertEquals(2, db.AddPost(null));
435 Post p3 = new Post();
436 p3.postid = null;
437 p3.postuserid = uid;
438 Assertions.assertEquals(2, db.AddPost(p3));
439 Post p4 = new Post();
440 p4.postid = "";
441 p4.postuserid = uid;
442 Assertions.assertEquals(2, db.AddPost(p4));
443 }));
444 }
root33a7d952025-05-18 17:24:41 +0000445
95630366963296f62025-06-02 21:16:32 +0800446 @TestFactory
447 Collection<DynamicTest> testUpdatePost() {
448 List<Post> existingPosts = em.createQuery("SELECT p FROM Post p", Post.class)
449 .setMaxResults(1)
450 .getResultList();
451 if (existingPosts.isEmpty()) {
452 return Collections.emptyList();
453 }
root33a7d952025-05-18 17:24:41 +0000454
95630366963296f62025-06-02 21:16:32 +0800455 Post originalPost = existingPosts.get(0);
456 String postId = originalPost.postid;
457 String originalTitle = originalPost.posttitle;
458 String originalContent = originalPost.postcontent;
459 String originalUserId = originalPost.postuserid;
460 Date originalTime = originalPost.posttime;
root33a7d952025-05-18 17:24:41 +0000461
95630366963296f62025-06-02 21:16:32 +0800462 return List.of(
463 DynamicTest.dynamicTest("UpdatePost success", () -> {
464 try {
465 Post update = new Post();
466 update.postid = postId;
467 update.postuserid = originalUserId;
468 update.posttitle = originalTitle + "_updated";
469 update.postcontent = originalContent + "_updated";
470 update.posttime = originalTime;
root33a7d952025-05-18 17:24:41 +0000471
95630366963296f62025-06-02 21:16:32 +0800472 int ret = db.UpdatePost(update);
473 Assertions.assertEquals(0, ret, "UpdatePost应返回0");
root33a7d952025-05-18 17:24:41 +0000474
95630366963296f62025-06-02 21:16:32 +0800475 em.clear();
476 Post updated = em.find(Post.class, postId);
477 Assertions.assertEquals(originalTitle + "_updated", updated.posttitle, "标题应已更新");
478 Assertions.assertEquals(originalContent + "_updated", updated.postcontent, "内容应已更新");
479 } finally {
480 // 恢复原始数据
481 EntityTransaction tx = em.getTransaction();
482 tx.begin();
483 Post toRestore = em.find(Post.class, postId);
484 if (toRestore != null) {
485 toRestore.posttitle = originalTitle;
486 toRestore.postcontent = originalContent;
487 toRestore.posttime = originalTime;
488 em.merge(toRestore);
489 }
490 tx.commit();
491 }
492 }),
493 DynamicTest.dynamicTest("UpdatePost not exist", () -> {
494 Post notExist = new Post();
495 notExist.postid = "not_exist_post";
496 notExist.postuserid = originalUserId;
497 notExist.posttitle = "不存在的帖子";
498 notExist.postcontent = "测试内容";
499 notExist.posttime = new Date();
root33a7d952025-05-18 17:24:41 +0000500
95630366963296f62025-06-02 21:16:32 +0800501 int ret = db.UpdatePost(notExist);
502 Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
503 }),
504 DynamicTest.dynamicTest("UpdatePost invalid param", () -> {
505 Assertions.assertEquals(2, db.UpdatePost(null));
506 Post invalid = new Post();
507 invalid.postid = "";
508 Assertions.assertEquals(2, db.UpdatePost(invalid));
root33a7d952025-05-18 17:24:41 +0000509
95630366963296f62025-06-02 21:16:32 +0800510 Post invalid2 = new Post();
511 invalid2.postid = null;
512 Assertions.assertEquals(2, db.UpdatePost(invalid2));
513 }));
514 }
root33a7d952025-05-18 17:24:41 +0000515
95630366963296f62025-06-02 21:16:32 +0800516 @TestFactory
517 Collection<DynamicTest> testDeletePost() {
wht85171c22025-06-08 01:38:55 +0800518 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
519 if (userIds.isEmpty()) {
95630366963296f62025-06-02 21:16:32 +0800520 return Collections.emptyList();
521 }
wht85171c22025-06-08 01:38:55 +0800522 String uid = userIds.get(0);
523 String postid = "test_post_" + System.currentTimeMillis();
524 Post post = new Post();
525 post.postid = postid;
526 post.postuserid = uid;
527 post.posttitle = "单元测试帖子";
528 post.postcontent = "内容";
529 post.posttime = new Date();
root33a7d952025-05-18 17:24:41 +0000530
95630366963296f62025-06-02 21:16:32 +0800531 return List.of(
wht85171c22025-06-08 01:38:55 +0800532 DynamicTest.dynamicTest("DeletePost success", () -> {
533 try {
534 int addRet = db.AddPost(post);
535 int delRet = db.DeletePost(postid);
536 Assertions.assertEquals(0, delRet, "DeletePost 应返回0");
537 } finally {
538 }
539 }),
540 DynamicTest.dynamicTest("DeletePost not exist", () -> {
541 int ret = db.DeletePost("not_exist_post");
542 Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
543 }),
544 DynamicTest.dynamicTest("DeletePost invalid param", () -> {
545 Assertions.assertEquals(2, db.DeletePost(null));
546 Assertions.assertEquals(2, db.DeletePost(""));
547 })
548 );
95630366963296f62025-06-02 21:16:32 +0800549 }
root33a7d952025-05-18 17:24:41 +0000550
95630366963296f62025-06-02 21:16:32 +0800551 @TestFactory
552 Collection<DynamicTest> testAddComment() {
553 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
554 List<String> postIds = em.createQuery("select p.postid from Post p", String.class).getResultList();
555 if (userIds.isEmpty() || postIds.isEmpty()) {
556 return Collections.emptyList();
557 }
558 String uid = userIds.get(0);
559 String pid = postIds.get(0);
560 String comment = "单元测试评论";
561 return List.of(
562 DynamicTest.dynamicTest("AddComment", () -> {
563 String replyid = null;
564 try {
565 int ret = db.AddComment(pid, uid, comment);
566 Assertions.assertEquals(0, ret, "AddComment 应返回0");
567 List<PostReply> replies = em.createQuery(
568 "SELECT r FROM PostReply r WHERE r.postid = :pid AND r.authorid = :uid AND r.content = :c",
569 PostReply.class)
570 .setParameter("pid", pid)
571 .setParameter("uid", uid)
572 .setParameter("c", comment)
573 .getResultList();
574 Assertions.assertFalse(replies.isEmpty(), "评论应已插入");
575 replyid = replies.get(0).replyid;
576 } finally {
577 if (replyid != null) {
578 EntityTransaction tx = em.getTransaction();
579 tx.begin();
580 PostReply toDelete = em.find(PostReply.class, replyid);
581 if (toDelete != null)
582 em.remove(toDelete);
583 tx.commit();
584 }
585 }
586 }));
587 }
root33a7d952025-05-18 17:24:41 +0000588
95630366963296f62025-06-02 21:16:32 +0800589 @TestFactory
590 Collection<DynamicTest> testDeleteComment() {
591 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
592 List<String> postIds = em.createQuery("select p.postid from Post p", String.class).getResultList();
593 if (userIds.isEmpty() || postIds.isEmpty()) {
594 return Collections.emptyList();
595 }
596 String uid = userIds.get(0);
597 String pid = postIds.get(0);
598 String comment = "待删除评论";
root33a7d952025-05-18 17:24:41 +0000599
95630366963296f62025-06-02 21:16:32 +0800600 return List.of(
601 DynamicTest.dynamicTest("DeleteComment", () -> {
602 String replyid = null;
603 try {
604 // 先确保评论存在
605 EntityTransaction tx = em.getTransaction();
606 tx.begin();
607 PostReply reply = new PostReply();
608 reply.replyid = "reply_" + System.currentTimeMillis();
609 reply.postid = pid;
610 reply.authorid = uid;
611 reply.content = comment;
612 reply.createdAt = new Date();
613 em.persist(reply);
614 tx.commit();
615 replyid = reply.replyid;
root33a7d952025-05-18 17:24:41 +0000616
95630366963296f62025-06-02 21:16:32 +0800617 // 执行删除测试
618 int ret = db.DeleteComment(pid, replyid);
619 Assertions.assertEquals(0, ret, "DeleteComment 应返回0");
root33a7d952025-05-18 17:24:41 +0000620
95630366963296f62025-06-02 21:16:32 +0800621 em.clear();
622 PostReply deleted = em.find(PostReply.class, replyid);
623 Assertions.assertNull(deleted, "评论应已删除");
624 } finally {
625 if (replyid != null) {
626 try {
627 EntityTransaction tx = em.getTransaction();
628 if (!tx.isActive()) {
629 tx.begin();
630 PostReply toDelete = em.find(PostReply.class, replyid);
631 if (toDelete != null) {
632 em.remove(toDelete);
633 }
634 tx.commit();
635 }
636 } catch (Exception e) {
637 }
638 }
639 }
640 }));
641 }
root33a7d952025-05-18 17:24:41 +0000642
95630366963296f62025-06-02 21:16:32 +0800643 @TestFactory
644 Collection<DynamicTest> testExchangeMagicToUpload() {
645 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
646 if (userIds.isEmpty())
647 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000648
95630366963296f62025-06-02 21:16:32 +0800649 String uid = userIds.get(0);
650 final UserPT testUserPT;
root33a7d952025-05-18 17:24:41 +0000651
95630366963296f62025-06-02 21:16:32 +0800652 EntityTransaction tx = em.getTransaction();
653 tx.begin();
654 UserPT tempUserPT = em.find(UserPT.class, uid);
root33a7d952025-05-18 17:24:41 +0000655
95630366963296f62025-06-02 21:16:32 +0800656 if (tempUserPT == null) {
657 tempUserPT = new UserPT();
658 tempUserPT.userid = uid;
659 tempUserPT.magic = 1000;
660 tempUserPT.upload = 1000;
661 tempUserPT.download = 1000;
662 em.persist(tempUserPT);
663 } else {
664 tempUserPT.magic = 1000;
665 tempUserPT.upload = 1000;
666 em.merge(tempUserPT);
667 }
668 testUserPT = tempUserPT;
669 tx.commit();
root33a7d952025-05-18 17:24:41 +0000670
95630366963296f62025-06-02 21:16:32 +0800671 return List.of(
672 DynamicTest.dynamicTest("ExchangeMagicToUpload success", () -> {
673 try {
674 int magic = 100;
675 long beforeUpload = testUserPT.upload;
676 int beforeMagic = testUserPT.magic;
root33a7d952025-05-18 17:24:41 +0000677
95630366963296f62025-06-02 21:16:32 +0800678 boolean ret = db.ExchangeMagicToUpload(uid, magic);
679 Assertions.assertTrue(ret, "兑换上传量应成功");
root33a7d952025-05-18 17:24:41 +0000680
95630366963296f62025-06-02 21:16:32 +0800681 em.clear();
682 UserPT after = em.find(UserPT.class, uid);
683 Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
684 Assertions.assertEquals(beforeUpload + magic, after.upload, "上传量应增加");
685 } finally {
686 EntityTransaction tx2 = em.getTransaction();
687 tx2.begin();
688 UserPT user = em.find(UserPT.class, uid);
689 if (user != null) {
690 user.magic = 0;
691 user.upload = 0;
692 em.merge(user);
693 }
694 tx2.commit();
695 }
696 }),
root33a7d952025-05-18 17:24:41 +0000697
95630366963296f62025-06-02 21:16:32 +0800698 DynamicTest.dynamicTest("ExchangeMagicToUpload insufficient magic", () -> {
699 boolean ret = db.ExchangeMagicToUpload(uid, 2000);
700 Assertions.assertFalse(ret, "魔力值不足时应返回false");
701 }),
root33a7d952025-05-18 17:24:41 +0000702
95630366963296f62025-06-02 21:16:32 +0800703 DynamicTest.dynamicTest("ExchangeMagicToUpload invalid params", () -> {
704 Assertions.assertFalse(db.ExchangeMagicToUpload(null, 100));
705 Assertions.assertFalse(db.ExchangeMagicToUpload("", 100));
706 Assertions.assertFalse(db.ExchangeMagicToUpload(uid, 0));
707 Assertions.assertFalse(db.ExchangeMagicToUpload(uid, -1));
708 }));
709 }
root33a7d952025-05-18 17:24:41 +0000710
95630366963296f62025-06-02 21:16:32 +0800711 @TestFactory
712 Collection<DynamicTest> testExchangeMagicToDownload() {
713 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
714 if (userIds.isEmpty())
715 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000716
95630366963296f62025-06-02 21:16:32 +0800717 String uid = userIds.get(0);
718 final UserPT testUserPT;
root33a7d952025-05-18 17:24:41 +0000719
95630366963296f62025-06-02 21:16:32 +0800720 EntityTransaction tx = em.getTransaction();
721 tx.begin();
722 UserPT tempUserPT = em.find(UserPT.class, uid);
723 if (tempUserPT == null) {
724 tempUserPT = new UserPT();
725 tempUserPT.userid = uid;
726 tempUserPT.magic = 1000;
727 tempUserPT.upload = 1000;
728 tempUserPT.download = 1000;
729 em.persist(tempUserPT);
730 } else {
731 tempUserPT.magic = 1000;
732 tempUserPT.download = 1000;
733 em.merge(tempUserPT);
734 }
735 testUserPT = tempUserPT;
736 tx.commit();
root33a7d952025-05-18 17:24:41 +0000737
95630366963296f62025-06-02 21:16:32 +0800738 return List.of(
739 DynamicTest.dynamicTest("ExchangeMagicToDownload success", () -> {
740 try {
741 int magic = 100;
742 long beforeDownload = testUserPT.download;
743 int beforeMagic = testUserPT.magic;
root33a7d952025-05-18 17:24:41 +0000744
95630366963296f62025-06-02 21:16:32 +0800745 boolean ret = db.ExchangeMagicToDownload(uid, magic);
746 Assertions.assertTrue(ret, "兑换下载量应成功");
root33a7d952025-05-18 17:24:41 +0000747
95630366963296f62025-06-02 21:16:32 +0800748 em.clear();
749 UserPT after = em.find(UserPT.class, uid);
750 Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
751 Assertions.assertEquals(beforeDownload - magic, after.download, "下载量应减少");
752 } finally {
753 EntityTransaction tx2 = em.getTransaction();
754 tx2.begin();
755 UserPT user = em.find(UserPT.class, uid);
756 if (user != null) {
757 user.magic = 0;
758 user.download = 0;
759 em.merge(user);
760 }
761 tx2.commit();
762 }
763 }),
root33a7d952025-05-18 17:24:41 +0000764
95630366963296f62025-06-02 21:16:32 +0800765 DynamicTest.dynamicTest("ExchangeMagicToDownload insufficient magic", () -> {
766 boolean ret = db.ExchangeMagicToDownload(uid, 2000);
767 Assertions.assertFalse(ret, "魔力值不足时应返回false");
768 }),
root33a7d952025-05-18 17:24:41 +0000769
95630366963296f62025-06-02 21:16:32 +0800770 DynamicTest.dynamicTest("ExchangeMagicToDownload invalid params", () -> {
771 Assertions.assertFalse(db.ExchangeMagicToDownload(null, 100));
772 Assertions.assertFalse(db.ExchangeMagicToDownload("", 100));
773 Assertions.assertFalse(db.ExchangeMagicToDownload(uid, 0));
774 Assertions.assertFalse(db.ExchangeMagicToDownload(uid, -1));
775 }));
776 }
root33a7d952025-05-18 17:24:41 +0000777
95630366963296f62025-06-02 21:16:32 +0800778 @TestFactory
779 Collection<DynamicTest> testExchangeMagicToVip() {
780 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
781 if (userIds.isEmpty())
782 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000783
95630366963296f62025-06-02 21:16:32 +0800784 String uid = userIds.get(0);
785 final UserPT testUserPT;
root33a7d952025-05-18 17:24:41 +0000786
95630366963296f62025-06-02 21:16:32 +0800787 EntityTransaction tx = em.getTransaction();
788 tx.begin();
789 UserPT tempUserPT = em.find(UserPT.class, uid);
790 if (tempUserPT == null) {
791 tempUserPT = new UserPT();
792 tempUserPT.userid = uid;
793 tempUserPT.magic = 1000;
794 tempUserPT.upload = 1000;
795 tempUserPT.download = 1000;
796 tempUserPT.viptime = 0;
797 em.persist(tempUserPT);
798 } else {
799 tempUserPT.magic = 1000;
800 tempUserPT.viptime = 0;
801 em.merge(tempUserPT);
802 }
803 testUserPT = tempUserPT;
804 tx.commit();
root33a7d952025-05-18 17:24:41 +0000805
95630366963296f62025-06-02 21:16:32 +0800806 return List.of(
807 DynamicTest.dynamicTest("ExchangeMagicToVip success", () -> {
808 try {
809 int magic = 100;
810 int beforeVip = testUserPT.viptime;
811 int beforeMagic = testUserPT.magic;
root33a7d952025-05-18 17:24:41 +0000812
95630366963296f62025-06-02 21:16:32 +0800813 boolean ret = db.ExchangeMagicToVip(uid, magic);
814 Assertions.assertTrue(ret, "兑换VIP次数应成功");
root33a7d952025-05-18 17:24:41 +0000815
95630366963296f62025-06-02 21:16:32 +0800816 em.clear();
817 UserPT after = em.find(UserPT.class, uid);
818 Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
819 Assertions.assertEquals(beforeVip + magic, after.viptime, "VIP次数应增加");
820 } finally {
821 EntityTransaction tx2 = em.getTransaction();
822 tx2.begin();
823 UserPT user = em.find(UserPT.class, uid);
824 if (user != null) {
825 user.magic = 0;
826 user.viptime = 0;
827 em.merge(user);
828 }
829 tx2.commit();
830 }
831 }),
root33a7d952025-05-18 17:24:41 +0000832
95630366963296f62025-06-02 21:16:32 +0800833 DynamicTest.dynamicTest("ExchangeMagicToVip insufficient magic", () -> {
834 boolean ret = db.ExchangeMagicToVip(uid, 2000);
835 Assertions.assertFalse(ret, "魔力值不足时应返回false");
836 }),
root33a7d952025-05-18 17:24:41 +0000837
95630366963296f62025-06-02 21:16:32 +0800838 DynamicTest.dynamicTest("ExchangeMagicToVip invalid params", () -> {
839 Assertions.assertFalse(db.ExchangeMagicToVip(null, 100));
840 Assertions.assertFalse(db.ExchangeMagicToVip("", 100));
841 Assertions.assertFalse(db.ExchangeMagicToVip(uid, 0));
842 Assertions.assertFalse(db.ExchangeMagicToVip(uid, -1));
843 }));
844 }
root33a7d952025-05-18 17:24:41 +0000845
95630366963296f62025-06-02 21:16:32 +0800846 @TestFactory
847 Collection<DynamicTest> testUploadTransmitProfile() {
848 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
849 if (userIds.isEmpty())
850 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000851
95630366963296f62025-06-02 21:16:32 +0800852 String uid = userIds.get(0);
853 String profileId = "test_profile_" + System.currentTimeMillis();
854 Profile profile = new Profile();
855 profile.profileurl = profileId;
856 profile.userid = uid;
857 profile.magictogive = "100";
858 profile.uploadtogive = "1000";
859 profile.downloadtogive = "500";
860 profile.exampass = false;
861 profile.magicgived = "0";
862 profile.uploadgived = "0";
863 profile.applicationurl = "http://example.com/apply";
root33a7d952025-05-18 17:24:41 +0000864
95630366963296f62025-06-02 21:16:32 +0800865 return List.of(
866 DynamicTest.dynamicTest("UploadTransmitProfile success", () -> {
867 try {
868 boolean ret = db.UploadTransmitProfile(profile);
869 Assertions.assertTrue(ret, "上传迁移信息应成功");
root33a7d952025-05-18 17:24:41 +0000870
95630366963296f62025-06-02 21:16:32 +0800871 Profile inserted = em.find(Profile.class, profileId);
872 Assertions.assertNotNull(inserted, "迁移信息应已插入");
873 Assertions.assertFalse(inserted.exampass, "新迁移信息默认未审核");
874 } finally {
875 EntityTransaction tx = em.getTransaction();
876 tx.begin();
877 Profile toDelete = em.find(Profile.class, profileId);
878 if (toDelete != null)
879 em.remove(toDelete);
880 tx.commit();
881 }
882 }),
root33a7d952025-05-18 17:24:41 +0000883
95630366963296f62025-06-02 21:16:32 +0800884 DynamicTest.dynamicTest("UploadTransmitProfile duplicate", () -> {
885 try {
886 db.UploadTransmitProfile(profile);
887 boolean ret = db.UploadTransmitProfile(profile);
888 Assertions.assertFalse(ret, "重复上传应返回false");
889 } finally {
890 EntityTransaction tx = em.getTransaction();
891 tx.begin();
892 Profile toDelete = em.find(Profile.class, profileId);
893 if (toDelete != null)
894 em.remove(toDelete);
895 tx.commit();
896 }
897 }),
root33a7d952025-05-18 17:24:41 +0000898
95630366963296f62025-06-02 21:16:32 +0800899 DynamicTest.dynamicTest("UploadTransmitProfile invalid params", () -> {
900 Assertions.assertFalse(db.UploadTransmitProfile(null));
901 Profile invalidProfile = new Profile();
902 Assertions.assertFalse(db.UploadTransmitProfile(invalidProfile));
903 }));
904 }
root33a7d952025-05-18 17:24:41 +0000905
95630366963296f62025-06-02 21:16:32 +0800906 @TestFactory
907 Collection<DynamicTest> testGetTransmitProfile() {
908 // 获取现有的迁移信息记录
909 List<Profile> existingProfiles = em.createQuery(
910 "SELECT p FROM Profile p", Profile.class)
911 .setMaxResults(1)
912 .getResultList();
913 if (existingProfiles.isEmpty())
914 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000915
95630366963296f62025-06-02 21:16:32 +0800916 Profile profile = existingProfiles.get(0);
917 String profileId = profile.profileurl;
root33a7d952025-05-18 17:24:41 +0000918
95630366963296f62025-06-02 21:16:32 +0800919 return List.of(
920 DynamicTest.dynamicTest("GetTransmitProfile success", () -> {
921 Profile ret = db.GetTransmitProfile(profileId);
922 Assertions.assertNotNull(ret, "应成功获取迁移信息");
923 }),
root33a7d952025-05-18 17:24:41 +0000924
95630366963296f62025-06-02 21:16:32 +0800925 DynamicTest.dynamicTest("GetTransmitProfile not exist", () -> {
926 Profile ret = db.GetTransmitProfile("not_exist_profile");
927 Assertions.assertNull(ret, "不存在的迁移信息应返回null");
928 }),
root33a7d952025-05-18 17:24:41 +0000929
95630366963296f62025-06-02 21:16:32 +0800930 DynamicTest.dynamicTest("GetTransmitProfile invalid params", () -> {
931 Assertions.assertNull(db.GetTransmitProfile(null));
932 Assertions.assertNull(db.GetTransmitProfile(""));
933 }));
934 }
root33a7d952025-05-18 17:24:41 +0000935
95630366963296f62025-06-02 21:16:32 +0800936 @TestFactory
937 Collection<DynamicTest> testExamTransmitProfile() {
938 List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
939 if (userIds.isEmpty())
940 return Collections.emptyList();
root33a7d952025-05-18 17:24:41 +0000941
95630366963296f62025-06-02 21:16:32 +0800942 String uid = userIds.get(0);
943 String profileId = "test_profile_exam_" + System.currentTimeMillis();
944 Profile profile = new Profile();
945 profile.profileurl = profileId;
946 profile.userid = uid;
947 profile.magictogive = "100";
948 profile.uploadtogive = "1000";
949 profile.exampass = false;
950 profile.magicgived = "0";
951 profile.uploadgived = "0";
952 profile.applicationurl = "http://example.com/apply";
root33a7d952025-05-18 17:24:41 +0000953
95630366963296f62025-06-02 21:16:32 +0800954 EntityTransaction tx = em.getTransaction();
955 tx.begin();
956 em.persist(profile);
957 UserPT userPT = em.find(UserPT.class, uid);
958 if (userPT == null) {
959 userPT = new UserPT();
960 userPT.userid = uid;
961 userPT.magic = 0;
962 userPT.upload = 0;
963 em.persist(userPT);
964 } else {
965 userPT.magic = 0;
966 userPT.upload = 0;
967 em.merge(userPT);
968 }
969 tx.commit();
root33a7d952025-05-18 17:24:41 +0000970
95630366963296f62025-06-02 21:16:32 +0800971 return List.of(
972 DynamicTest.dynamicTest("ExamTransmitProfile approve", () -> {
973 try {
974 boolean ret = db.ExamTransmitProfile(profileId, true);
975 Assertions.assertTrue(ret, "审核通过应成功");
976 } finally {
977 EntityTransaction tx2 = em.getTransaction();
978 tx2.begin();
979 UserPT user = em.find(UserPT.class, uid);
980 if (user != null) {
981 user.magic = 0;
982 user.upload = 0;
983 em.merge(user);
984 }
985 Profile toDelete = em.find(Profile.class, profileId);
986 if (toDelete != null)
987 em.remove(toDelete);
988 tx2.commit();
989 }
990 }),
root33a7d952025-05-18 17:24:41 +0000991
95630366963296f62025-06-02 21:16:32 +0800992 DynamicTest.dynamicTest("ExamTransmitProfile reject", () -> {
993 String rejectId = "test_profile_reject_" + System.currentTimeMillis();
994 Profile rejectProfile = new Profile();
995 rejectProfile.profileurl = rejectId;
996 rejectProfile.userid = uid;
997 rejectProfile.magictogive = "100";
998 rejectProfile.uploadtogive = "1000";
999 rejectProfile.magicgived = "0";
1000 rejectProfile.uploadgived = "0";
1001 rejectProfile.exampass = false;
1002 rejectProfile.applicationurl = "http://example.com/apply";
root33a7d952025-05-18 17:24:41 +00001003
95630366963296f62025-06-02 21:16:32 +08001004 EntityTransaction tx3 = em.getTransaction();
1005 tx3.begin();
1006 em.persist(rejectProfile);
1007 tx3.commit();
root33a7d952025-05-18 17:24:41 +00001008
95630366963296f62025-06-02 21:16:32 +08001009 try {
1010 boolean ret = db.ExamTransmitProfile(rejectId, false);
1011 Assertions.assertTrue(ret, "审核拒绝应成功");
1012 } finally {
1013 EntityTransaction tx4 = em.getTransaction();
1014 tx4.begin();
1015 Profile toDelete = em.find(Profile.class, rejectId);
1016 if (toDelete != null)
1017 em.remove(toDelete);
1018 tx4.commit();
1019 }
1020 }),
root33a7d952025-05-18 17:24:41 +00001021
95630366963296f62025-06-02 21:16:32 +08001022 DynamicTest.dynamicTest("ExamTransmitProfile invalid params", () -> {
1023 Assertions.assertFalse(db.ExamTransmitProfile(null, true));
1024 Assertions.assertFalse(db.ExamTransmitProfile("", true));
1025 Assertions.assertFalse(db.ExamTransmitProfile("not_exist_profile", true));
1026 }));
1027 }
root33a7d952025-05-18 17:24:41 +00001028
95630366963296f62025-06-02 21:16:32 +08001029 @Test
1030 void testGetTransmitProfileList() {
1031 em.getTransaction().begin();
1032 em.createQuery("DELETE FROM Profile").executeUpdate();
1033 em.getTransaction().commit();
root33a7d952025-05-18 17:24:41 +00001034
95630366963296f62025-06-02 21:16:32 +08001035 List<String> userIds = em.createQuery("select u.userid from User u", String.class)
1036 .setMaxResults(3)
1037 .getResultList();
1038 if (userIds.isEmpty()) {
1039 return;
1040 }
root33a7d952025-05-18 17:24:41 +00001041
95630366963296f62025-06-02 21:16:32 +08001042 EntityTransaction tx = em.getTransaction();
1043 tx.begin();
1044 for (int i = 0; i < userIds.size(); i++) {
1045 Profile profile = new Profile();
1046 profile.profileurl = "test_profile_list_" + i + "_" + System.currentTimeMillis();
1047 profile.userid = userIds.get(i);
1048 profile.magictogive = String.valueOf(100 * (i + 1));
1049 profile.uploadtogive = String.valueOf(1000 * (i + 1));
1050 profile.exampass = false;
1051 profile.magicgived = "0";
1052 profile.uploadgived = "0";
1053 profile.applicationurl = "http://example.com/apply";
1054 em.persist(profile);
1055 }
1056 tx.commit();
root33a7d952025-05-18 17:24:41 +00001057
95630366963296f62025-06-02 21:16:32 +08001058 try {
1059 Profile[] profiles = db.GetTransmitProfileList();
1060 Assertions.assertEquals(userIds.size(), profiles.length, "应返回所有迁移申请");
1061 Arrays.stream(profiles)
1062 .forEach(p -> Assertions.assertTrue(p.profileurl.startsWith("test_profile_list_"), "应为测试数据"));
1063 } finally {
1064 EntityTransaction tx2 = em.getTransaction();
1065 tx2.begin();
1066 em.createQuery("DELETE FROM Profile p WHERE p.profileurl LIKE 'test_profile_list_%'").executeUpdate();
1067 tx2.commit();
1068 }
1069 }
1070}