956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1 | package databasetest; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 2 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 3 | import database.Database2; |
| 4 | import entity.*; |
| 5 | import org.junit.jupiter.api.*; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 6 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 7 | import javax.persistence.*; |
| 8 | import java.util.*; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 9 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 10 | public class database2Test { |
| 11 | private static EntityManagerFactory emf; |
| 12 | private static EntityManager em; |
| 13 | private static Database2 db; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 14 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 15 | @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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 32 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 33 | @AfterAll |
| 34 | static void teardown() { |
| 35 | if (em != null && em.isOpen()) |
| 36 | em.close(); |
| 37 | if (emf != null && emf.isOpen()) |
| 38 | emf.close(); |
| 39 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 40 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 41 | @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); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 47 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 48 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 69 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 70 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 91 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 92 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 99 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 100 | @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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 108 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 109 | 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 115 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 116 | 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 125 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 126 | int ret = db.UpdateBegSeed(update); |
| 127 | Assertions.assertEquals(0, ret, "UpdateBegSeed应返回0"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 128 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 129 | 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 155 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 156 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 166 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 167 | @TestFactory |
| 168 | Collection<DynamicTest> testDeleteBegSeed() { |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 169 | return List.of( |
wht | be2bdc6 | 2025-06-08 23:14:26 +0800 | [diff] [blame] | 170 | 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 178 | |
wht | be2bdc6 | 2025-06-08 23:14:26 +0800 | [diff] [blame] | 179 | 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 | ); |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 192 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 193 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 194 | @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); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 203 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 204 | // 获取现有的求种信息 |
| 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 211 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 212 | // 获取现有的种子信息 |
| 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 219 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 220 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 226 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 227 | DynamicTest.dynamicTest("VoteSeed duplicate", () -> { |
| 228 | int ret = db.VoteSeed(begId, seedId, uid); |
| 229 | Assertions.assertEquals(1, ret, "重复投票应返回1"); |
| 230 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 231 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 232 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 252 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 253 | @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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 262 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 263 | // 获取现有的可用种子信息 |
| 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); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 273 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 274 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 280 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 281 | DynamicTest.dynamicTest("SubmitSeed duplicate", () -> { |
| 282 | int ret = db.SubmitSeed(begId, seed); |
| 283 | Assertions.assertEquals(1, ret, "重复提交应返回1"); |
| 284 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 285 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 286 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 301 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 302 | // 清理投票记录 |
| 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 312 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 313 | @Test |
| 314 | void testSettleBeg() { |
| 315 | String uid = em.createQuery("select u.userid from User u", String.class) |
| 316 | .setMaxResults(1) |
| 317 | .getSingleResult(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 318 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 319 | String begId = "test_beg_settle_" + System.currentTimeMillis(); |
| 320 | String seedId = "test_seed_settle_" + System.currentTimeMillis(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 321 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 322 | 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 330 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 331 | Seed seed = new Seed(); |
| 332 | seed.seedid = seedId; |
| 333 | seed.seeduserid = uid; |
| 334 | seed.title = "测试种子"; |
| 335 | seed.seedsize = "1"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 336 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 337 | try { |
| 338 | db.AddBegSeed(beg); |
| 339 | db.SubmitSeed(begId, seed); |
| 340 | db.VoteSeed(begId, seedId, uid); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 341 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 342 | db.SettleBeg(); |
| 343 | em.clear(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 344 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 345 | BegInfo settled = em.find(BegInfo.class, begId); |
| 346 | Assertions.assertEquals(settled.hasseed, 1, "求种应已完成"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 347 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 348 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 378 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 379 | @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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 392 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 393 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 445 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 446 | @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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 454 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 455 | 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 461 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 462 | 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 471 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 472 | int ret = db.UpdatePost(update); |
| 473 | Assertions.assertEquals(0, ret, "UpdatePost应返回0"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 474 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 475 | 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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 500 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 501 | 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)); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 509 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 510 | Post invalid2 = new Post(); |
| 511 | invalid2.postid = null; |
| 512 | Assertions.assertEquals(2, db.UpdatePost(invalid2)); |
| 513 | })); |
| 514 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 515 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 516 | @TestFactory |
| 517 | Collection<DynamicTest> testDeletePost() { |
wht | 85171c2 | 2025-06-08 01:38:55 +0800 | [diff] [blame] | 518 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 519 | if (userIds.isEmpty()) { |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 520 | return Collections.emptyList(); |
| 521 | } |
wht | 85171c2 | 2025-06-08 01:38:55 +0800 | [diff] [blame] | 522 | 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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 530 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 531 | return List.of( |
wht | 85171c2 | 2025-06-08 01:38:55 +0800 | [diff] [blame] | 532 | 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 | ); |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 549 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 550 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 551 | @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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 588 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 589 | @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 = "待删除评论"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 599 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 600 | 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; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 616 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 617 | // 执行删除测试 |
| 618 | int ret = db.DeleteComment(pid, replyid); |
| 619 | Assertions.assertEquals(0, ret, "DeleteComment 应返回0"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 620 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 621 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 642 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 643 | @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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 648 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 649 | String uid = userIds.get(0); |
| 650 | final UserPT testUserPT; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 651 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 652 | EntityTransaction tx = em.getTransaction(); |
| 653 | tx.begin(); |
| 654 | UserPT tempUserPT = em.find(UserPT.class, uid); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 655 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 656 | 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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 670 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 671 | return List.of( |
| 672 | DynamicTest.dynamicTest("ExchangeMagicToUpload success", () -> { |
| 673 | try { |
| 674 | int magic = 100; |
| 675 | long beforeUpload = testUserPT.upload; |
| 676 | int beforeMagic = testUserPT.magic; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 677 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 678 | boolean ret = db.ExchangeMagicToUpload(uid, magic); |
| 679 | Assertions.assertTrue(ret, "兑换上传量应成功"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 680 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 681 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 697 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 698 | DynamicTest.dynamicTest("ExchangeMagicToUpload insufficient magic", () -> { |
| 699 | boolean ret = db.ExchangeMagicToUpload(uid, 2000); |
| 700 | Assertions.assertFalse(ret, "魔力值不足时应返回false"); |
| 701 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 702 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 703 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 710 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 711 | @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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 716 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 717 | String uid = userIds.get(0); |
| 718 | final UserPT testUserPT; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 719 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 720 | 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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 737 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 738 | return List.of( |
| 739 | DynamicTest.dynamicTest("ExchangeMagicToDownload success", () -> { |
| 740 | try { |
| 741 | int magic = 100; |
| 742 | long beforeDownload = testUserPT.download; |
| 743 | int beforeMagic = testUserPT.magic; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 744 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 745 | boolean ret = db.ExchangeMagicToDownload(uid, magic); |
| 746 | Assertions.assertTrue(ret, "兑换下载量应成功"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 747 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 748 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 764 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 765 | DynamicTest.dynamicTest("ExchangeMagicToDownload insufficient magic", () -> { |
| 766 | boolean ret = db.ExchangeMagicToDownload(uid, 2000); |
| 767 | Assertions.assertFalse(ret, "魔力值不足时应返回false"); |
| 768 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 769 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 770 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 777 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 778 | @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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 783 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 784 | String uid = userIds.get(0); |
| 785 | final UserPT testUserPT; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 786 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 787 | 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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 805 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 806 | return List.of( |
| 807 | DynamicTest.dynamicTest("ExchangeMagicToVip success", () -> { |
| 808 | try { |
| 809 | int magic = 100; |
| 810 | int beforeVip = testUserPT.viptime; |
| 811 | int beforeMagic = testUserPT.magic; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 812 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 813 | boolean ret = db.ExchangeMagicToVip(uid, magic); |
| 814 | Assertions.assertTrue(ret, "兑换VIP次数应成功"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 815 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 816 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 832 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 833 | DynamicTest.dynamicTest("ExchangeMagicToVip insufficient magic", () -> { |
| 834 | boolean ret = db.ExchangeMagicToVip(uid, 2000); |
| 835 | Assertions.assertFalse(ret, "魔力值不足时应返回false"); |
| 836 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 837 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 838 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 845 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 846 | @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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 851 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 852 | 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"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 864 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 865 | return List.of( |
| 866 | DynamicTest.dynamicTest("UploadTransmitProfile success", () -> { |
| 867 | try { |
| 868 | boolean ret = db.UploadTransmitProfile(profile); |
| 869 | Assertions.assertTrue(ret, "上传迁移信息应成功"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 870 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 871 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 883 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 884 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 898 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 899 | DynamicTest.dynamicTest("UploadTransmitProfile invalid params", () -> { |
| 900 | Assertions.assertFalse(db.UploadTransmitProfile(null)); |
| 901 | Profile invalidProfile = new Profile(); |
| 902 | Assertions.assertFalse(db.UploadTransmitProfile(invalidProfile)); |
| 903 | })); |
| 904 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 905 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 906 | @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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 915 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 916 | Profile profile = existingProfiles.get(0); |
| 917 | String profileId = profile.profileurl; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 918 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 919 | return List.of( |
| 920 | DynamicTest.dynamicTest("GetTransmitProfile success", () -> { |
| 921 | Profile ret = db.GetTransmitProfile(profileId); |
| 922 | Assertions.assertNotNull(ret, "应成功获取迁移信息"); |
| 923 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 924 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 925 | DynamicTest.dynamicTest("GetTransmitProfile not exist", () -> { |
| 926 | Profile ret = db.GetTransmitProfile("not_exist_profile"); |
| 927 | Assertions.assertNull(ret, "不存在的迁移信息应返回null"); |
| 928 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 929 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 930 | DynamicTest.dynamicTest("GetTransmitProfile invalid params", () -> { |
| 931 | Assertions.assertNull(db.GetTransmitProfile(null)); |
| 932 | Assertions.assertNull(db.GetTransmitProfile("")); |
| 933 | })); |
| 934 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 935 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 936 | @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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 941 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 942 | 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"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 953 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 954 | 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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 970 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 971 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 991 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 992 | 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"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1003 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1004 | EntityTransaction tx3 = em.getTransaction(); |
| 1005 | tx3.begin(); |
| 1006 | em.persist(rejectProfile); |
| 1007 | tx3.commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1008 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1009 | 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 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1021 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1022 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1028 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1029 | @Test |
| 1030 | void testGetTransmitProfileList() { |
| 1031 | em.getTransaction().begin(); |
| 1032 | em.createQuery("DELETE FROM Profile").executeUpdate(); |
| 1033 | em.getTransaction().commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1034 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1035 | 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 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1041 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1042 | 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(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1057 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1058 | 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 | } |