blob: 196db26cde434eed8c53f02ea8a93202bf3e6614 [file] [log] [blame]
root33a7d952025-05-18 17:24:41 +00001// package databasetest;
2
3// import database.Database2;
4// import entity.*;
5// import org.junit.jupiter.api.*;
6
7// import javax.persistence.*;
8// import java.util.*;
9// import java.util.stream.Collectors;
10
11// public class database2Test {
12// private static EntityManagerFactory emf;
13// private static EntityManager em;
14// private static Database2 db;
15
16// @BeforeAll
17// static void setup() throws Exception {
18// // 强制加载 MySQL 驱动
19// Class.forName("com.mysql.cj.jdbc.Driver");
20// entity.config cfg = new entity.config();
21// Map<String, Object> props = new HashMap<>();
22// String jdbcUrl = String.format(
23// "jdbc:mysql://%s/%s?useSSL=false&serverTimezone=UTC",
24// cfg.SqlURL, cfg.TestDatabase);
25// props.put("javax.persistence.jdbc.url", jdbcUrl);
26// props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
27// props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
28// props.put("javax.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver");
29// emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
30// em = emf.createEntityManager();
31// // 修改此处:传入配置好的EntityManagerFactory给Database2
32// db = new Database2(emf);
33// }
34
35// @AfterAll
36// static void teardown() {
37// if (em != null && em.isOpen())
38// em.close();
39// if (emf != null && emf.isOpen())
40// emf.close();
41// }
42
43// @TestFactory
44// Collection<DynamicTest> testAddBegSeed() {
45// List<String> userIds = em.createQuery("select u.userid from User u",
46// String.class).getResultList();
47// if (userIds.isEmpty())
48// return Collections.emptyList();
49
50// String begId = "test_beg_" + UUID.randomUUID().toString();
51
52// BegInfo beg = new BegInfo();
53// beg.begid = begId;
54// beg.begnumbers = 1;
55// beg.magic = 100;
56// beg.endtime = new Date();
57// beg.hasseed = false;
58
59// return List.of(
60// DynamicTest.dynamicTest("AddBegSeed success", () -> {
61// int ret = db.AddBegSeed(beg);
62// Assertions.assertEquals(0, ret, "AddBegSeed应返回0");
63// BegInfo inserted = em.find(BegInfo.class, begId);
64// Assertions.assertNotNull(inserted, "求种信息应已插入");
65// Assertions.assertFalse(inserted.hasseed, "新求种默认未完成");
66// }),
67
68// DynamicTest.dynamicTest("AddBegSeed duplicate", () -> {
69// int ret = db.AddBegSeed(beg);
70// Assertions.assertEquals(1, ret, "重复插入应返回1");
71// }),
72
73// DynamicTest.dynamicTest("AddBegSeed invalid param", () -> {
74// Assertions.assertEquals(2, db.AddBegSeed(null));
75// BegInfo invalidBeg = new BegInfo();
76// invalidBeg.begid = "";
77// Assertions.assertEquals(2, db.AddBegSeed(invalidBeg));
78// }));
79// }
80
81// @TestFactory
82// Collection<DynamicTest> testUpdateBegSeed() {
83// List<String> userIds = em.createQuery("select u.userid from User u",
84// String.class).getResultList();
85// if (userIds.isEmpty())
86// return Collections.emptyList();
87
88// String begId = "test_beg_update_" + UUID.randomUUID().toString();
89
90// // 先插入一条求种记录
91// BegInfo beg = new BegInfo();
92// beg.begid = begId;
93// beg.begnumbers = 1;
94// beg.magic = 100;
95// beg.endtime = new Date();
96// db.AddBegSeed(beg);
97
98// return List.of(
99// DynamicTest.dynamicTest("UpdateBegSeed success", () -> {
100// BegInfo update = new BegInfo();
101// update.begid = begId;
102// update.begnumbers = 2;
103// update.magic = 200;
104
105// int ret = db.UpdateBegSeed(update);
106// Assertions.assertEquals(0, ret, "UpdateBegSeed应返回0");
107
108// BegInfo updated = em.find(BegInfo.class, begId);
109// Assertions.assertEquals(200, updated.magic, "魔力值应已更新");
110// Assertions.assertEquals(2, updated.begnumbers, "求种人数应已更新");
111// }),
112
113// DynamicTest.dynamicTest("UpdateBegSeed not exist", () -> {
114// BegInfo notExist = new BegInfo();
115// notExist.begid = "not_exist_beg";
116// notExist.begnumbers = 1;
117// int ret = db.UpdateBegSeed(notExist);
118// Assertions.assertEquals(1, ret, "不存在的求种应返回1");
119// }),
120
121// DynamicTest.dynamicTest("UpdateBegSeed invalid param", () -> {
122// Assertions.assertEquals(2, db.UpdateBegSeed(null));
123// BegInfo invalid = new BegInfo();
124// invalid.begid = "";
125// Assertions.assertEquals(2, db.UpdateBegSeed(invalid));
126// }));
127// }
128
129// @TestFactory
130// Collection<DynamicTest> testDeleteBegSeed() {
131// List<String> userIds = em.createQuery("select u.userid from User u",
132// String.class).getResultList();
133// if (userIds.isEmpty())
134// return Collections.emptyList();
135
136// String begId = "test_beg_delete_" + UUID.randomUUID().toString();
137
138// // 先插入一条求种记录
139// BegInfo beg = new BegInfo();
140// beg.begid = begId;
141// beg.begnumbers = 1;
142// beg.magic = 100;
143// beg.endtime = new Date();
144// beg.hasseed = false;
145// db.AddBegSeed(beg);
146
147// return List.of(
148// DynamicTest.dynamicTest("DeleteBegSeed success", () -> {
149// int ret = db.DeleteBegSeed(begId);
150// Assertions.assertEquals(0, ret, "DeleteBegSeed应返回0");
151// BegInfo deleted = em.find(BegInfo.class, begId);
152// Assertions.assertNull(deleted, "求种信息应已删除");
153// }),
154
155// DynamicTest.dynamicTest("DeleteBegSeed not exist", () -> {
156// int ret = db.DeleteBegSeed("not_exist_beg");
157// Assertions.assertEquals(1, ret, "不存在的求种应返回1");
158// }),
159
160// DynamicTest.dynamicTest("DeleteBegSeed invalid param", () -> {
161// Assertions.assertEquals(2, db.DeleteBegSeed(null));
162// Assertions.assertEquals(2, db.DeleteBegSeed(""));
163// }));
164// }
165
166// @TestFactory
167// Collection<DynamicTest> testVoteSeed() {
168// List<String> userIds = em.createQuery("select u.userid from User u",
169// String.class).getResultList();
170// if (userIds.isEmpty())
171// return Collections.emptyList();
172
173// String uid = userIds.get(0);
174// String begId = "test_beg_vote_" + UUID.randomUUID().toString();
175// String seedId = "test_seed_" + UUID.randomUUID().toString();
176
177// // 插入求种记录
178// BegInfo beg = new BegInfo();
179// beg.begid = begId;
180// beg.begnumbers = 1;
181// beg.magic = 100;
182// beg.endtime = new Date();
183// beg.hasseed = false;
184// db.AddBegSeed(beg);
185
186// // 插入种子记录
187// Seed seed = new Seed();
188// seed.seedid = seedId;
189// seed.seeduserid = uid;
190// seed.title = "测试种子";
191// em.getTransaction().begin();
192// em.persist(seed);
193// em.getTransaction().commit();
194
195// // 提交种子
196// db.SubmitSeed(begId, seed);
197
198// return List.of(
199// DynamicTest.dynamicTest("VoteSeed success", () -> {
200// int ret = db.VoteSeed(begId, seedId, uid);
201// Assertions.assertEquals(0, ret, "VoteSeed应返回0");
202// }),
203
204// DynamicTest.dynamicTest("VoteSeed duplicate", () -> {
205// int ret = db.VoteSeed(begId, seedId, uid);
206// Assertions.assertEquals(1, ret, "重复投票应返回1");
207// }),
208
209// DynamicTest.dynamicTest("VoteSeed invalid param", () -> {
210// Assertions.assertEquals(2, db.VoteSeed(null, seedId, uid));
211// Assertions.assertEquals(2, db.VoteSeed(begId, null, uid));
212// Assertions.assertEquals(2, db.VoteSeed(begId, seedId, null));
213// }));
214// }
215
216// @TestFactory
217// Collection<DynamicTest> testSubmitSeed() {
218// List<String> userIds = em.createQuery("select u.userid from User u",
219// String.class).getResultList();
220// if (userIds.isEmpty())
221// return Collections.emptyList();
222
223// String uid = userIds.get(0);
224// String begId = "test_beg_submit_" + UUID.randomUUID().toString();
225// String seedId = "test_seed_submit_" + UUID.randomUUID().toString();
226
227// // 插入求种记录
228// BegInfo beg = new BegInfo();
229// beg.begid = begId;
230// beg.begnumbers = 1;
231// beg.magic = 100;
232// beg.endtime = new Date();
233// beg.hasseed = false;
234// db.AddBegSeed(beg);
235
236// Seed seed = new Seed();
237// seed.seedid = seedId;
238// seed.seeduserid = uid;
239// seed.title = "测试种子";
240
241// return List.of(
242// DynamicTest.dynamicTest("SubmitSeed success", () -> {
243// int ret = db.SubmitSeed(begId, seed);
244// Assertions.assertEquals(0, ret, "SubmitSeed应返回0");
245// }),
246
247// DynamicTest.dynamicTest("SubmitSeed duplicate", () -> {
248// int ret = db.SubmitSeed(begId, seed);
249// Assertions.assertEquals(1, ret, "重复提交应返回1");
250// }),
251
252// DynamicTest.dynamicTest("SubmitSeed invalid param", () -> {
253// Assertions.assertEquals(2, db.SubmitSeed(null, seed));
254// Assertions.assertEquals(2, db.SubmitSeed(begId, null));
255// }));
256// }
257
258// @Test
259// void testSettleBeg() {
260// // 准备测试数据
261// String uid = em.createQuery("select u.userid from User u", String.class)
262// .setMaxResults(1)
263// .getSingleResult();
264
265// String begId = "test_beg_settle_" + UUID.randomUUID().toString();
266// String seedId = "test_seed_settle_" + UUID.randomUUID().toString();
267
268// // 创建求种记录(已过期)
269// BegInfo beg = new BegInfo();
270// beg.begid = begId;
271// beg.begnumbers = 1;
272// beg.magic = 100;
273// Calendar cal = Calendar.getInstance();
274// cal.add(Calendar.DAY_OF_MONTH, -15); // 15天前
275// beg.endtime = cal.getTime();
276// beg.hasseed = false;
277// db.AddBegSeed(beg);
278
279// // 创建种子并提交
280// Seed seed = new Seed();
281// seed.seedid = seedId;
282// seed.seeduserid = uid;
283// seed.title = "测试种子";
284// db.SubmitSeed(begId, seed);
285
286// // 投票
287// db.VoteSeed(begId, seedId, uid);
288
289// // 执行结算
290// db.SettleBeg();
291
292// // 验证结算结果
293// BegInfo settled = em.find(BegInfo.class, begId);
294// Assertions.assertTrue(settled.hasseed, "求种应已完成");
295
296// UserPT userPT = em.find(UserPT.class, uid);
297// Assertions.assertTrue(userPT.magic >= 100, "用户应获得魔力值奖励");
298// }
299
300// @TestFactory
301// Collection<DynamicTest> testAddPost() {
302// List<String> userIds = em.createQuery("select u.userid from User u",
303// String.class).getResultList();
304// if (userIds.isEmpty())
305// return Collections.emptyList();
306// String uid = userIds.get(0);
307// String postid = "test_post_" + UUID.randomUUID();
308// Post post = new Post();
309// post.postid = postid;
310// post.postuserid = uid;
311// post.posttitle = "单元测试帖子";
312// post.postcontent = "内容";
313// post.posttime = new Date();
314
315// return List.of(
316// DynamicTest.dynamicTest("AddPost success", () -> {
317// int ret = db.AddPost(post);
318// Assertions.assertEquals(0, ret, "AddPost 应返回0");
319// Post inserted = em.find(Post.class, postid);
320// Assertions.assertNotNull(inserted, "帖子应已插入");
321// }),
322// DynamicTest.dynamicTest("AddPost duplicate", () -> {
323// int ret = db.AddPost(post);
324// Assertions.assertEquals(1, ret, "重复插入应返回1");
325// }),
326// DynamicTest.dynamicTest("AddPost user not exist", () -> {
327// Post p2 = new Post();
328// p2.postid = "test_post_" + UUID.randomUUID();
329// p2.postuserid = "not_exist_user";
330// p2.posttitle = "无效用户";
331// p2.postcontent = "内容";
332// p2.posttime = new Date();
333// int ret = db.AddPost(p2);
334// Assertions.assertEquals(2, ret, "用户不存在应返回2");
335// }),
336// DynamicTest.dynamicTest("AddPost invalid param", () -> {
337// Assertions.assertEquals(2, db.AddPost(null));
338// Post p3 = new Post();
339// p3.postid = null;
340// p3.postuserid = uid;
341// Assertions.assertEquals(2, db.AddPost(p3));
342// Post p4 = new Post();
343// p4.postid = "";
344// p4.postuserid = uid;
345// Assertions.assertEquals(2, db.AddPost(p4));
346// }));
347// }
348
349// @TestFactory
350// Collection<DynamicTest> testUpdatePost() {
351// List<String> userIds = em.createQuery("select u.userid from User u",
352// String.class).getResultList();
353// if (userIds.isEmpty())
354// return Collections.emptyList();
355// String uid = userIds.get(0);
356// String postid = "test_post_update_" + UUID.randomUUID();
357// Post post = new Post();
358// post.postid = postid;
359// post.postuserid = uid;
360// post.posttitle = "原始标题";
361// post.postcontent = "原始内容";
362// post.posttime = new Date();
363// db.AddPost(post);
364
365// return List.of(
366// DynamicTest.dynamicTest("UpdatePost success", () -> {
367// Post update = new Post();
368// update.postid = postid;
369// update.postuserid = uid; // 实际实现会忽略此字段
370// update.posttitle = "新标题";
371// update.postcontent = "新内容";
372// update.posttime = post.posttime;
373// int ret = db.UpdatePost(update);
374// Assertions.assertEquals(0, ret, "UpdatePost 应返回0");
375// Post updated = em.find(Post.class, postid);
376// Assertions.assertEquals("新标题", updated.posttitle);
377// Assertions.assertEquals("新内容", updated.postcontent);
378// }),
379// DynamicTest.dynamicTest("UpdatePost not exist", () -> {
380// Post notExist = new Post();
381// notExist.postid = "not_exist_post";
382// notExist.postuserid = uid;
383// notExist.posttitle = "xxx";
384// notExist.postcontent = "xxx";
385// int ret = db.UpdatePost(notExist);
386// Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
387// }),
388// DynamicTest.dynamicTest("UpdatePost invalid param", () -> {
389// Assertions.assertEquals(2, db.UpdatePost(null));
390// Post p2 = new Post();
391// p2.postid = null;
392// Assertions.assertEquals(2, db.UpdatePost(p2));
393// Post p3 = new Post();
394// p3.postid = "";
395// Assertions.assertEquals(2, db.UpdatePost(p3));
396// }));
397// }
398
399// @TestFactory
400// Collection<DynamicTest> testDeletePost() {
401// List<String> userIds = em.createQuery("select u.userid from User u",
402// String.class).getResultList();
403// if (userIds.isEmpty())
404// return Collections.emptyList();
405// String uid = userIds.get(0);
406// String postid = "test_post_delete_" + UUID.randomUUID();
407// Post post = new Post();
408// post.postid = postid;
409// post.postuserid = uid;
410// post.posttitle = "待删除帖子";
411// post.postcontent = "内容";
412// post.posttime = new Date();
413// db.AddPost(post);
414
415// return List.of(
416// DynamicTest.dynamicTest("DeletePost success", () -> {
417// int ret = db.DeletePost(postid);
418// Assertions.assertEquals(0, ret, "DeletePost 应返回0");
419// Post deleted = em.find(Post.class, postid);
420// Assertions.assertNull(deleted, "帖子应已删除");
421// }),
422// DynamicTest.dynamicTest("DeletePost not exist", () -> {
423// int ret = db.DeletePost("not_exist_post");
424// Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
425// }),
426// DynamicTest.dynamicTest("DeletePost invalid param", () -> {
427// Assertions.assertEquals(2, db.DeletePost(null));
428// Assertions.assertEquals(2, db.DeletePost(""));
429// }));
430// }
431
432// @TestFactory
433// Collection<DynamicTest> testAddComment() {
434// List<String> userIds = em.createQuery("select u.userid from User u",
435// String.class).getResultList();
436// List<String> postIds = em.createQuery("select p.postid from Post p",
437// String.class).getResultList();
438// if (userIds.isEmpty() || postIds.isEmpty()) {
439// return Collections.emptyList();
440// }
441// String comment = "单元测试评论";
442// return userIds.stream().flatMap(uid -> postIds.stream()
443// .map(pid -> DynamicTest.dynamicTest("AddComment for user=" + uid + ", post="
444// + pid, () -> {
445// int ret = db.AddComment(pid, uid, comment);
446// Assertions.assertEquals(0, ret, "AddComment 应返回0");
447// // 验证评论已插入
448// List<PostReply> replies = em.createQuery(
449// "SELECT r FROM PostReply r WHERE r.postid = :pid AND r.authorid = :uid AND
450// r.content = :c",
451// PostReply.class)
452// .setParameter("pid", pid)
453// .setParameter("uid", uid)
454// .setParameter("c", comment)
455// .getResultList();
456// Assertions.assertFalse(replies.isEmpty(), "评论应已插入");
457// }))).collect(Collectors.toList());
458// }
459
460// @TestFactory
461// Collection<DynamicTest> testDeleteComment() {
462// List<String> userIds = em.createQuery("select u.userid from User u",
463// String.class).getResultList();
464// List<String> postIds = em.createQuery("select p.postid from Post p",
465// String.class).getResultList();
466// if (userIds.isEmpty() || postIds.isEmpty()) {
467// return Collections.emptyList();
468// }
469// String comment = "待删除评论";
470// List<DynamicTest> tests = new ArrayList<>();
471// for (String uid : userIds) {
472// for (String pid : postIds) {
473// // 插入评论
474// db.AddComment(pid, uid, comment);
475// // 查询评论id
476// List<PostReply> replies = em.createQuery(
477// "SELECT r FROM PostReply r WHERE r.postid = :pid AND r.authorid = :uid AND
478// r.content = :c",
479// PostReply.class)
480// .setParameter("pid", pid)
481// .setParameter("uid", uid)
482// .setParameter("c", comment)
483// .getResultList();
484// if (replies.isEmpty())
485// continue;
486// PostReply reply = replies.get(0);
487// String replyid = reply.replyid;
488
489// tests.add(DynamicTest.dynamicTest("DeleteComment for post=" + pid + ",
490// replyid=" + replyid, () -> {
491// int ret = db.DeleteComment(pid, replyid);
492// Assertions.assertEquals(0, ret, "DeleteComment 应返回0");
493// PostReply deleted = em.find(PostReply.class, replyid);
494// Assertions.assertNull(deleted, "评论应已删除");
495// }));
496// }
497// }
498// return tests;
499// }
500
501// @TestFactory
502// Collection<DynamicTest> testExchangeMagicToUpload() {
503// List<String> userIds = em.createQuery("select u.userid from User u",
504// String.class).getResultList();
505// if (userIds.isEmpty())
506// return Collections.emptyList();
507
508// String uid = userIds.get(0);
509// final UserPT testUserPT;
510
511// // 确保用户有足够的魔力值用于测试
512// EntityTransaction tx = em.getTransaction();
513// tx.begin();
514// UserPT tempUserPT = em.find(UserPT.class, uid);
515// if (tempUserPT == null) {
516// tempUserPT = new UserPT();
517// tempUserPT.userid = uid;
518// tempUserPT.magic = 1000;
519// tempUserPT.upload = 1000;
520// tempUserPT.download = 1000;
521// em.persist(tempUserPT);
522// } else {
523// tempUserPT.magic = 1000;
524// em.merge(tempUserPT);
525// }
526// testUserPT = tempUserPT;
527// tx.commit();
528
529// return List.of(
530// DynamicTest.dynamicTest("ExchangeMagicToUpload success", () -> {
531// int magic = 100;
532// long beforeUpload = testUserPT.upload;
533// int beforeMagic = testUserPT.magic;
534
535// boolean ret = db.ExchangeMagicToUpload(uid, magic);
536// Assertions.assertTrue(ret, "兑换上传量应成功");
537
538// em.clear();
539// UserPT after = em.find(UserPT.class, uid);
540// Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
541// Assertions.assertEquals(beforeUpload + magic, after.upload, "上传量应增加");
542// }),
543
544// DynamicTest.dynamicTest("ExchangeMagicToUpload insufficient magic", () -> {
545// boolean ret = db.ExchangeMagicToUpload(uid, 2000);
546// Assertions.assertFalse(ret, "魔力值不足时应返回false");
547// }),
548
549// DynamicTest.dynamicTest("ExchangeMagicToUpload invalid params", () -> {
550// Assertions.assertFalse(db.ExchangeMagicToUpload(null, 100));
551// Assertions.assertFalse(db.ExchangeMagicToUpload("", 100));
552// Assertions.assertFalse(db.ExchangeMagicToUpload(uid, 0));
553// Assertions.assertFalse(db.ExchangeMagicToUpload(uid, -1));
554// }));
555// }
556
557// @TestFactory
558// Collection<DynamicTest> testExchangeMagicToDownload() {
559// List<String> userIds = em.createQuery("select u.userid from User u",
560// String.class).getResultList();
561// if (userIds.isEmpty())
562// return Collections.emptyList();
563
564// String uid = userIds.get(0);
565// final UserPT testUserPT;
566
567// // 确保用户有足够的魔力值和下载量用于测试
568// EntityTransaction tx = em.getTransaction();
569// tx.begin();
570// UserPT tempUserPT = em.find(UserPT.class, uid);
571// if (tempUserPT == null) {
572// tempUserPT = new UserPT();
573// tempUserPT.userid = uid;
574// tempUserPT.magic = 1000;
575// tempUserPT.upload = 1000;
576// tempUserPT.download = 1000;
577// em.persist(tempUserPT);
578// } else {
579// tempUserPT.magic = 1000;
580// em.merge(tempUserPT);
581// }
582// testUserPT = tempUserPT;
583// tx.commit();
584
585// return List.of(
586// DynamicTest.dynamicTest("ExchangeMagicToDownload success", () -> {
587// int magic = 100;
588// long beforeDownload = testUserPT.download;
589// int beforeMagic = testUserPT.magic;
590
591// boolean ret = db.ExchangeMagicToDownload(uid, magic);
592// Assertions.assertTrue(ret, "兑换下载量应成功");
593
594// em.clear();
595// UserPT after = em.find(UserPT.class, uid);
596// Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
597// Assertions.assertEquals(beforeDownload - magic, after.download, "下载量应减少");
598// }),
599
600// DynamicTest.dynamicTest("ExchangeMagicToDownload insufficient magic", () -> {
601// boolean ret = db.ExchangeMagicToDownload(uid, 2000);
602// Assertions.assertFalse(ret, "魔力值不足时应返回false");
603// }),
604
605// DynamicTest.dynamicTest("ExchangeMagicToDownload invalid params", () -> {
606// Assertions.assertFalse(db.ExchangeMagicToDownload(null, 100));
607// Assertions.assertFalse(db.ExchangeMagicToDownload("", 100));
608// Assertions.assertFalse(db.ExchangeMagicToDownload(uid, 0));
609// Assertions.assertFalse(db.ExchangeMagicToDownload(uid, -1));
610// }));
611// }
612
613// @TestFactory
614// Collection<DynamicTest> testExchangeMagicToVip() {
615// List<String> userIds = em.createQuery("select u.userid from User u",
616// String.class).getResultList();
617// if (userIds.isEmpty())
618// return Collections.emptyList();
619
620// String uid = userIds.get(0);
621// final UserPT testUserPT;
622
623// // 确保用户有足够的魔力值用于测试
624// EntityTransaction tx = em.getTransaction();
625// tx.begin();
626// UserPT tempUserPT = em.find(UserPT.class, uid);
627// if (tempUserPT == null) {
628// tempUserPT = new UserPT();
629// tempUserPT.userid = uid;
630// tempUserPT.magic = 1000;
631// tempUserPT.upload = 1000;
632// tempUserPT.download = 1000;
633// em.persist(tempUserPT);
634// } else {
635// tempUserPT.magic = 1000;
636// em.merge(tempUserPT);
637// }
638// testUserPT = tempUserPT;
639// tx.commit();
640
641// return List.of(
642// DynamicTest.dynamicTest("ExchangeMagicToVip success", () -> {
643// int magic = 100;
644// int beforeVip = testUserPT.viptime;
645// int beforeMagic = testUserPT.magic;
646
647// boolean ret = db.ExchangeMagicToVip(uid, magic);
648// Assertions.assertTrue(ret, "兑换VIP次数应成功");
649
650// em.clear();
651// UserPT after = em.find(UserPT.class, uid);
652// Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
653// Assertions.assertEquals(beforeVip + magic, after.viptime, "VIP次数应增加");
654// }),
655
656// DynamicTest.dynamicTest("ExchangeMagicToVip insufficient magic", () -> {
657// boolean ret = db.ExchangeMagicToVip(uid, 2000);
658// Assertions.assertFalse(ret, "魔力值不足时应返回false");
659// }),
660
661// DynamicTest.dynamicTest("ExchangeMagicToVip invalid params", () -> {
662// Assertions.assertFalse(db.ExchangeMagicToVip(null, 100));
663// Assertions.assertFalse(db.ExchangeMagicToVip("", 100));
664// Assertions.assertFalse(db.ExchangeMagicToVip(uid, 0));
665// Assertions.assertFalse(db.ExchangeMagicToVip(uid, -1));
666// }));
667// }
668
669// @TestFactory
670// Collection<DynamicTest> testUploadTransmitProfile() {
671// List<String> userIds = em.createQuery("select u.userid from User u",
672// String.class).getResultList();
673// if (userIds.isEmpty())
674// return Collections.emptyList();
675
676// String uid = userIds.get(0);
677// String profileId = "test_profile_" + UUID.randomUUID();
678// Profile profile = new Profile();
679// profile.profileurl = profileId;
680// profile.userid = uid;
681// profile.magictogive = "100";
682// profile.uploadtogive = "1000";
683// profile.downloadtogive = "500";
684
685// return List.of(
686// DynamicTest.dynamicTest("UploadTransmitProfile success", () -> {
687// boolean ret = db.UploadTransmitProfile(profile);
688// Assertions.assertTrue(ret, "上传迁移信息应成功");
689
690// Profile inserted = em.find(Profile.class, profileId);
691// Assertions.assertNotNull(inserted, "迁移信息应已插入");
692// Assertions.assertFalse(inserted.exampass, "新迁移信息默认未审核");
693// Assertions.assertEquals("0", inserted.magicgived, "已发放魔力值应为0");
694// Assertions.assertEquals("0", inserted.uploadgived, "已发放上传量应为0");
695// }),
696
697// DynamicTest.dynamicTest("UploadTransmitProfile duplicate", () -> {
698// boolean ret = db.UploadTransmitProfile(profile);
699// Assertions.assertFalse(ret, "重复上传应返回false");
700// }),
701
702// DynamicTest.dynamicTest("UploadTransmitProfile invalid params", () -> {
703// Assertions.assertFalse(db.UploadTransmitProfile(null));
704// Profile invalidProfile = new Profile();
705// Assertions.assertFalse(db.UploadTransmitProfile(invalidProfile));
706// }));
707// }
708
709// @TestFactory
710// Collection<DynamicTest> testGetTransmitProfile() {
711// List<String> userIds = em.createQuery("select u.userid from User u",
712// String.class).getResultList();
713// if (userIds.isEmpty())
714// return Collections.emptyList();
715
716// String uid = userIds.get(0);
717// String profileId = "test_profile_get_" + UUID.randomUUID();
718// Profile profile = new Profile();
719// profile.profileurl = profileId;
720// profile.userid = uid;
721// profile.magictogive = "100";
722// profile.uploadtogive = "1000";
723// profile.exampass = false;
724
725// // 先插入测试数据
726// EntityTransaction tx = em.getTransaction();
727// tx.begin();
728// em.persist(profile);
729// tx.commit();
730
731// return List.of(
732// DynamicTest.dynamicTest("GetTransmitProfile success", () -> {
733// Profile ret = db.GetTransmitProfile(profileId);
734// Assertions.assertNotNull(ret, "应成功获取迁移信息");
735// Assertions.assertEquals(uid, ret.userid, "用户ID应匹配");
736// Assertions.assertEquals("100", ret.magictogive, "待发放魔力值应匹配");
737// Assertions.assertEquals("1000", ret.uploadtogive, "待发放上传量应匹配");
738// }),
739
740// DynamicTest.dynamicTest("GetTransmitProfile not exist", () -> {
741// Profile ret = db.GetTransmitProfile("not_exist_profile");
742// Assertions.assertNull(ret, "不存在的迁移信息应返回null");
743// }),
744
745// DynamicTest.dynamicTest("GetTransmitProfile invalid params", () -> {
746// Assertions.assertNull(db.GetTransmitProfile(null));
747// Assertions.assertNull(db.GetTransmitProfile(""));
748// }));
749// }
750
751// @TestFactory
752// Collection<DynamicTest> testExamTransmitProfile() {
753// List<String> userIds = em.createQuery("select u.userid from User u",
754// String.class).getResultList();
755// if (userIds.isEmpty())
756// return Collections.emptyList();
757
758// String uid = userIds.get(0);
759// String profileId = "test_profile_exam_" + UUID.randomUUID();
760// Profile profile = new Profile();
761// profile.profileurl = profileId;
762// profile.userid = uid;
763// profile.magictogive = "100";
764// profile.uploadtogive = "1000";
765// profile.exampass = false;
766
767// // 先插入测试数据
768// EntityTransaction tx = em.getTransaction();
769// tx.begin();
770// em.persist(profile);
771// UserPT userPT = em.find(UserPT.class, uid);
772// if (userPT == null) {
773// userPT = new UserPT();
774// userPT.userid = uid;
775// userPT.magic = 0;
776// userPT.upload = 0;
777// em.persist(userPT);
778// }
779// tx.commit();
780
781// return List.of(
782// DynamicTest.dynamicTest("ExamTransmitProfile approve", () -> {
783// boolean ret = db.ExamTransmitProfile(profileId, true);
784// Assertions.assertTrue(ret, "审核通过应成功");
785
786// em.clear();
787// Profile examined = em.find(Profile.class, profileId);
788// Assertions.assertTrue(examined.exampass, "迁移信息应已审核通过");
789// Assertions.assertEquals("100", examined.magicgived, "应记录已发放魔力值");
790// Assertions.assertEquals("1000", examined.uploadgived, "应记录已发放上传量");
791
792// UserPT afterPT = em.find(UserPT.class, uid);
793// Assertions.assertEquals(100, afterPT.magic, "用户魔力值应增加");
794// Assertions.assertEquals(1000, afterPT.upload, "用户上传量应增加");
795// }),
796
797// DynamicTest.dynamicTest("ExamTransmitProfile reject", () -> {
798// String rejectId = "test_profile_reject_" + UUID.randomUUID();
799// Profile rejectProfile = new Profile();
800// rejectProfile.profileurl = rejectId;
801// rejectProfile.userid = uid;
802// rejectProfile.magictogive = "100";
803// rejectProfile.uploadtogive = "1000";
804// rejectProfile.exampass = false;
805
806// tx.begin();
807// em.persist(rejectProfile);
808// tx.commit();
809
810// boolean ret = db.ExamTransmitProfile(rejectId, false);
811// Assertions.assertTrue(ret, "审核拒绝应成功");
812
813// em.clear();
814// Profile examined = em.find(Profile.class, rejectId);
815// Assertions.assertFalse(examined.exampass, "迁移信息应已审核拒绝");
816// Assertions.assertEquals("0", examined.magicgived, "不应发放魔力值");
817// Assertions.assertEquals("0", examined.uploadgived, "不应发放上传量");
818// }),
819
820// DynamicTest.dynamicTest("ExamTransmitProfile invalid params", () -> {
821// Assertions.assertFalse(db.ExamTransmitProfile(null, true));
822// Assertions.assertFalse(db.ExamTransmitProfile("", true));
823// Assertions.assertFalse(db.ExamTransmitProfile("not_exist_profile", true));
824// }));
825// }
826
827// @Test
828// void testGetTransmitProfileList() {
829// // 清空已有数据
830// em.getTransaction().begin();
831// em.createQuery("DELETE FROM Profile").executeUpdate();
832// em.getTransaction().commit();
833
834// // 插入测试数据
835// List<String> userIds = em.createQuery("select u.userid from User u",
836// String.class)
837// .setMaxResults(3)
838// .getResultList();
839// if (userIds.isEmpty()) {
840// return;
841// }
842
843// EntityTransaction tx = em.getTransaction();
844// tx.begin();
845// for (int i = 0; i < userIds.size(); i++) {
846// Profile profile = new Profile();
847// profile.profileurl = "test_profile_list_" + i;
848// profile.userid = userIds.get(i);
849// profile.magictogive = String.valueOf(100 * (i + 1));
850// profile.uploadtogive = String.valueOf(1000 * (i + 1));
851// profile.exampass = false;
852// em.persist(profile);
853// }
854// tx.commit();
855
856// // 测试获取列表
857// Profile[] profiles = db.GetTransmitProfileList();
858// Assertions.assertEquals(userIds.size(), profiles.length, "应返回所有迁移申请");
859// Arrays.stream(profiles)
860// .forEach(p ->
861// Assertions.assertTrue(p.profileurl.startsWith("test_profile_list_"),
862// "应为测试数据"));
863// }
864// }