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() { |
| 169 | List<BegInfo> existingBegs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class) |
| 170 | .setMaxResults(1) |
| 171 | .getResultList(); |
| 172 | if (existingBegs.isEmpty()) { |
| 173 | return Collections.emptyList(); |
| 174 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 175 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 176 | BegInfo begToDelete = existingBegs.get(0); |
| 177 | String begId = begToDelete.begid; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 178 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 179 | return List.of( |
| 180 | DynamicTest.dynamicTest("DeleteBegSeed success", () -> { |
| 181 | EntityTransaction tx = em.getTransaction(); |
| 182 | try { |
| 183 | tx.begin(); |
| 184 | int ret = db.DeleteBegSeed(begId); |
| 185 | Assertions.assertEquals(0, ret, "DeleteBegSeed 应返回0"); |
| 186 | } finally { |
| 187 | tx.rollback(); |
| 188 | } |
| 189 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 190 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 191 | DynamicTest.dynamicTest("DeleteBegSeed not exist", () -> { |
| 192 | int ret = db.DeleteBegSeed("not_exist_beg"); |
| 193 | Assertions.assertEquals(1, ret, "不存在的求种任务应返回1"); |
| 194 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 195 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 196 | DynamicTest.dynamicTest("DeleteBegSeed invalid param", () -> { |
| 197 | Assertions.assertEquals(2, db.DeleteBegSeed(null)); |
| 198 | Assertions.assertEquals(2, db.DeleteBegSeed("")); |
| 199 | })); |
| 200 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 201 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 202 | @TestFactory |
| 203 | Collection<DynamicTest> testVoteSeed() { |
| 204 | // 获取现有用户ID |
| 205 | List<String> userIds = em.createQuery("select u.userid from User u", String.class) |
| 206 | .setMaxResults(1) |
| 207 | .getResultList(); |
| 208 | if (userIds.isEmpty()) |
| 209 | return Collections.emptyList(); |
| 210 | String uid = userIds.get(0); |
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<BegInfo> begs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class) |
| 214 | .setMaxResults(1) |
| 215 | .getResultList(); |
| 216 | if (begs.isEmpty()) |
| 217 | return Collections.emptyList(); |
| 218 | String begId = begs.get(0).begid; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 219 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 220 | // 获取现有的种子信息 |
| 221 | List<Seed> seeds = em.createQuery("SELECT s FROM Seed s", Seed.class) |
| 222 | .setMaxResults(1) |
| 223 | .getResultList(); |
| 224 | if (seeds.isEmpty()) |
| 225 | return Collections.emptyList(); |
| 226 | String seedId = seeds.get(0).seedid; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 227 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 228 | try { |
| 229 | return List.of( |
| 230 | DynamicTest.dynamicTest("VoteSeed success", () -> { |
| 231 | int ret = db.VoteSeed(begId, seedId, uid); |
| 232 | Assertions.assertEquals(0, ret, "VoteSeed应返回0"); |
| 233 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 234 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 235 | DynamicTest.dynamicTest("VoteSeed duplicate", () -> { |
| 236 | int ret = db.VoteSeed(begId, seedId, uid); |
| 237 | Assertions.assertEquals(1, ret, "重复投票应返回1"); |
| 238 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 239 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 240 | DynamicTest.dynamicTest("VoteSeed invalid param", () -> { |
| 241 | Assertions.assertEquals(2, db.VoteSeed(null, seedId, uid)); |
| 242 | Assertions.assertEquals(2, db.VoteSeed(begId, null, uid)); |
| 243 | Assertions.assertEquals(2, db.VoteSeed(begId, seedId, null)); |
| 244 | })); |
| 245 | } finally { |
| 246 | EntityTransaction tx = em.getTransaction(); |
| 247 | tx.begin(); |
| 248 | try { |
| 249 | em.createQuery( |
| 250 | "DELETE FROM UserVotes v WHERE v.begid = :begid AND v.seedid = :seedid AND v.userid = :uid") |
| 251 | .setParameter("begid", begId) |
| 252 | .setParameter("seedid", seedId) |
| 253 | .setParameter("uid", uid) |
| 254 | .executeUpdate(); |
| 255 | } catch (Exception ignored) { |
| 256 | } |
| 257 | tx.commit(); |
| 258 | } |
| 259 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 260 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 261 | @TestFactory |
| 262 | Collection<DynamicTest> testSubmitSeed() { |
| 263 | // 获取现有的求种信息 |
| 264 | List<BegInfo> begs = em.createQuery("SELECT b FROM BegInfo b WHERE b.hasseed = 0", BegInfo.class) |
| 265 | .setMaxResults(1) |
| 266 | .getResultList(); |
| 267 | if (begs.isEmpty()) |
| 268 | return Collections.emptyList(); |
| 269 | String begId = begs.get(0).begid; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 270 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 271 | // 获取现有的可用种子信息 |
| 272 | List<Seed> existingSeeds = em.createQuery( |
| 273 | "SELECT s FROM Seed s WHERE s.seedid NOT IN " + |
| 274 | "(SELECT ss.seed.seedid FROM SubmitSeed ss)", |
| 275 | Seed.class) |
| 276 | .setMaxResults(1) |
| 277 | .getResultList(); |
| 278 | if (existingSeeds.isEmpty()) |
| 279 | return Collections.emptyList(); |
| 280 | Seed seed = existingSeeds.get(0); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 281 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 282 | try { |
| 283 | return List.of( |
| 284 | DynamicTest.dynamicTest("SubmitSeed success", () -> { |
| 285 | int ret = db.SubmitSeed(begId, seed); |
| 286 | Assertions.assertEquals(0, ret, "SubmitSeed应返回0"); |
| 287 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 288 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 289 | DynamicTest.dynamicTest("SubmitSeed duplicate", () -> { |
| 290 | int ret = db.SubmitSeed(begId, seed); |
| 291 | Assertions.assertEquals(1, ret, "重复提交应返回1"); |
| 292 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 293 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 294 | DynamicTest.dynamicTest("SubmitSeed invalid param", () -> { |
| 295 | Assertions.assertEquals(2, db.SubmitSeed(null, seed)); |
| 296 | Assertions.assertEquals(2, db.SubmitSeed(begId, null)); |
| 297 | })); |
| 298 | } finally { |
| 299 | // 清理测试数据 |
| 300 | EntityTransaction tx = em.getTransaction(); |
| 301 | tx.begin(); |
| 302 | try { |
| 303 | // 恢复求种状态 |
| 304 | BegInfo toRestore = em.find(BegInfo.class, begId); |
| 305 | if (toRestore != null) { |
| 306 | toRestore.hasseed = 0; |
| 307 | em.merge(toRestore); |
| 308 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 309 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 310 | // 清理投票记录 |
| 311 | em.createQuery("DELETE FROM Vote v WHERE v.begid = :begid AND v.seedid = :seedid") |
| 312 | .setParameter("begid", begId) |
| 313 | .setParameter("seedid", seed.seedid) |
| 314 | .executeUpdate(); |
| 315 | } catch (Exception ignored) { |
| 316 | } |
| 317 | tx.commit(); |
| 318 | } |
| 319 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 320 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 321 | @Test |
| 322 | void testSettleBeg() { |
| 323 | String uid = em.createQuery("select u.userid from User u", String.class) |
| 324 | .setMaxResults(1) |
| 325 | .getSingleResult(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 326 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 327 | String begId = "test_beg_settle_" + System.currentTimeMillis(); |
| 328 | String seedId = "test_seed_settle_" + System.currentTimeMillis(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 329 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 330 | BegInfo beg = new BegInfo(); |
| 331 | beg.begid = begId; |
| 332 | beg.begnumbers = 1; |
| 333 | beg.magic = 100; |
| 334 | Calendar cal = Calendar.getInstance(); |
| 335 | cal.add(Calendar.DAY_OF_MONTH, -15); // 15天前 |
| 336 | beg.endtime = cal.getTime(); |
| 337 | beg.hasseed = 0; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 338 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 339 | Seed seed = new Seed(); |
| 340 | seed.seedid = seedId; |
| 341 | seed.seeduserid = uid; |
| 342 | seed.title = "测试种子"; |
| 343 | seed.seedsize = "1"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 344 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 345 | try { |
| 346 | db.AddBegSeed(beg); |
| 347 | db.SubmitSeed(begId, seed); |
| 348 | db.VoteSeed(begId, seedId, uid); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 349 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 350 | db.SettleBeg(); |
| 351 | em.clear(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 352 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 353 | BegInfo settled = em.find(BegInfo.class, begId); |
| 354 | Assertions.assertEquals(settled.hasseed, 1, "求种应已完成"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 355 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 356 | UserPT userPT = em.find(UserPT.class, uid); |
| 357 | Assertions.assertTrue(userPT.magic >= 0, "用户应获得魔力值奖励"); |
| 358 | } finally { |
| 359 | EntityTransaction tx = em.getTransaction(); |
| 360 | tx.begin(); |
| 361 | try { |
| 362 | // 删除投票记录 |
| 363 | em.createQuery( |
| 364 | "DELETE FROM UserVotes v WHERE v.begid = :begid AND v.seedid = :seedid AND v.userid = :uid") |
| 365 | .setParameter("begid", begId) |
| 366 | .setParameter("seedid", seedId) |
| 367 | .setParameter("uid", uid) |
| 368 | .executeUpdate(); |
| 369 | // 删除提交记录 |
| 370 | em.createQuery("DELETE FROM SubmitSeed s WHERE s.begid = :begid AND s.seedid = :seedid") |
| 371 | .setParameter("begid", begId) |
| 372 | .setParameter("seedid", seedId) |
| 373 | .executeUpdate(); |
| 374 | } catch (Exception ignored) { |
| 375 | } |
| 376 | // 删除种子和求种任务 |
| 377 | Seed toDeleteSeed = em.find(Seed.class, seedId); |
| 378 | if (toDeleteSeed != null) |
| 379 | em.remove(toDeleteSeed); |
| 380 | BegInfo toDeleteBeg = em.find(BegInfo.class, begId); |
| 381 | if (toDeleteBeg != null) |
| 382 | em.remove(toDeleteBeg); |
| 383 | tx.commit(); |
| 384 | } |
| 385 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 386 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 387 | @TestFactory |
| 388 | Collection<DynamicTest> testAddPost() { |
| 389 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 390 | if (userIds.isEmpty()) |
| 391 | return Collections.emptyList(); |
| 392 | String uid = userIds.get(0); |
| 393 | String postid = "test_post_" + System.currentTimeMillis(); |
| 394 | Post post = new Post(); |
| 395 | post.postid = postid; |
| 396 | post.postuserid = uid; |
| 397 | post.posttitle = "单元测试帖子"; |
| 398 | post.postcontent = "内容"; |
| 399 | post.posttime = new Date(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 400 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 401 | return List.of( |
| 402 | DynamicTest.dynamicTest("AddPost success", () -> { |
| 403 | try { |
| 404 | int ret = db.AddPost(post); |
| 405 | Assertions.assertEquals(0, ret, "AddPost 应返回0"); |
| 406 | Post inserted = em.find(Post.class, postid); |
| 407 | Assertions.assertNotNull(inserted, "帖子应已插入"); |
| 408 | } finally { |
| 409 | EntityTransaction tx = em.getTransaction(); |
| 410 | tx.begin(); |
| 411 | Post toDelete = em.find(Post.class, postid); |
| 412 | if (toDelete != null) |
| 413 | em.remove(toDelete); |
| 414 | tx.commit(); |
| 415 | } |
| 416 | }), |
| 417 | DynamicTest.dynamicTest("AddPost duplicate", () -> { |
| 418 | try { |
| 419 | db.AddPost(post); |
| 420 | int ret = db.AddPost(post); |
| 421 | Assertions.assertEquals(1, ret, "重复插入应返回1"); |
| 422 | } finally { |
| 423 | EntityTransaction tx = em.getTransaction(); |
| 424 | tx.begin(); |
| 425 | Post toDelete = em.find(Post.class, postid); |
| 426 | if (toDelete != null) |
| 427 | em.remove(toDelete); |
| 428 | tx.commit(); |
| 429 | } |
| 430 | }), |
| 431 | DynamicTest.dynamicTest("AddPost user not exist", () -> { |
| 432 | Post p2 = new Post(); |
| 433 | p2.postid = "test_post_" + (System.currentTimeMillis() + 1); |
| 434 | p2.postuserid = "not_exist_user"; |
| 435 | p2.posttitle = "无效用户"; |
| 436 | p2.postcontent = "内容"; |
| 437 | p2.posttime = new Date(); |
| 438 | int ret = db.AddPost(p2); |
| 439 | Assertions.assertEquals(2, ret, "用户不存在应返回2"); |
| 440 | }), |
| 441 | DynamicTest.dynamicTest("AddPost invalid param", () -> { |
| 442 | Assertions.assertEquals(2, db.AddPost(null)); |
| 443 | Post p3 = new Post(); |
| 444 | p3.postid = null; |
| 445 | p3.postuserid = uid; |
| 446 | Assertions.assertEquals(2, db.AddPost(p3)); |
| 447 | Post p4 = new Post(); |
| 448 | p4.postid = ""; |
| 449 | p4.postuserid = uid; |
| 450 | Assertions.assertEquals(2, db.AddPost(p4)); |
| 451 | })); |
| 452 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 453 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 454 | @TestFactory |
| 455 | Collection<DynamicTest> testUpdatePost() { |
| 456 | List<Post> existingPosts = em.createQuery("SELECT p FROM Post p", Post.class) |
| 457 | .setMaxResults(1) |
| 458 | .getResultList(); |
| 459 | if (existingPosts.isEmpty()) { |
| 460 | return Collections.emptyList(); |
| 461 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 462 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 463 | Post originalPost = existingPosts.get(0); |
| 464 | String postId = originalPost.postid; |
| 465 | String originalTitle = originalPost.posttitle; |
| 466 | String originalContent = originalPost.postcontent; |
| 467 | String originalUserId = originalPost.postuserid; |
| 468 | Date originalTime = originalPost.posttime; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 469 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 470 | return List.of( |
| 471 | DynamicTest.dynamicTest("UpdatePost success", () -> { |
| 472 | try { |
| 473 | Post update = new Post(); |
| 474 | update.postid = postId; |
| 475 | update.postuserid = originalUserId; |
| 476 | update.posttitle = originalTitle + "_updated"; |
| 477 | update.postcontent = originalContent + "_updated"; |
| 478 | update.posttime = originalTime; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 479 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 480 | int ret = db.UpdatePost(update); |
| 481 | Assertions.assertEquals(0, ret, "UpdatePost应返回0"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 482 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 483 | em.clear(); |
| 484 | Post updated = em.find(Post.class, postId); |
| 485 | Assertions.assertEquals(originalTitle + "_updated", updated.posttitle, "标题应已更新"); |
| 486 | Assertions.assertEquals(originalContent + "_updated", updated.postcontent, "内容应已更新"); |
| 487 | } finally { |
| 488 | // 恢复原始数据 |
| 489 | EntityTransaction tx = em.getTransaction(); |
| 490 | tx.begin(); |
| 491 | Post toRestore = em.find(Post.class, postId); |
| 492 | if (toRestore != null) { |
| 493 | toRestore.posttitle = originalTitle; |
| 494 | toRestore.postcontent = originalContent; |
| 495 | toRestore.posttime = originalTime; |
| 496 | em.merge(toRestore); |
| 497 | } |
| 498 | tx.commit(); |
| 499 | } |
| 500 | }), |
| 501 | DynamicTest.dynamicTest("UpdatePost not exist", () -> { |
| 502 | Post notExist = new Post(); |
| 503 | notExist.postid = "not_exist_post"; |
| 504 | notExist.postuserid = originalUserId; |
| 505 | notExist.posttitle = "不存在的帖子"; |
| 506 | notExist.postcontent = "测试内容"; |
| 507 | notExist.posttime = new Date(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 508 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 509 | int ret = db.UpdatePost(notExist); |
| 510 | Assertions.assertEquals(1, ret, "不存在的帖子应返回1"); |
| 511 | }), |
| 512 | DynamicTest.dynamicTest("UpdatePost invalid param", () -> { |
| 513 | Assertions.assertEquals(2, db.UpdatePost(null)); |
| 514 | Post invalid = new Post(); |
| 515 | invalid.postid = ""; |
| 516 | Assertions.assertEquals(2, db.UpdatePost(invalid)); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 517 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 518 | Post invalid2 = new Post(); |
| 519 | invalid2.postid = null; |
| 520 | Assertions.assertEquals(2, db.UpdatePost(invalid2)); |
| 521 | })); |
| 522 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 523 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 524 | @TestFactory |
| 525 | Collection<DynamicTest> testDeletePost() { |
| 526 | List<Post> existingPosts = em.createQuery("SELECT p FROM Post p", Post.class) |
| 527 | .setMaxResults(1) |
| 528 | .getResultList(); |
| 529 | if (existingPosts.isEmpty()) { |
| 530 | return Collections.emptyList(); |
| 531 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 532 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 533 | Post postToDelete = existingPosts.get(0); |
| 534 | String postId = postToDelete.postid; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 535 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 536 | return List.of( |
| 537 | DynamicTest.dynamicTest("DeletePost success", () -> { |
| 538 | EntityTransaction tx = em.getTransaction(); |
| 539 | try { |
| 540 | tx.begin(); |
| 541 | int ret = db.DeletePost(postId); |
| 542 | Assertions.assertEquals(0, ret, "DeletePost 应返回0"); |
| 543 | } finally { |
| 544 | tx.rollback(); |
| 545 | } |
| 546 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 547 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 548 | DynamicTest.dynamicTest("DeletePost not exist", () -> { |
| 549 | int ret = db.DeletePost("not_exist_post"); |
| 550 | Assertions.assertEquals(1, ret, "不存在的帖子应返回1"); |
| 551 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 552 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 553 | DynamicTest.dynamicTest("DeletePost invalid param", () -> { |
| 554 | Assertions.assertEquals(2, db.DeletePost(null)); |
| 555 | Assertions.assertEquals(2, db.DeletePost("")); |
| 556 | })); |
| 557 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 558 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 559 | @TestFactory |
| 560 | Collection<DynamicTest> testAddComment() { |
| 561 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 562 | List<String> postIds = em.createQuery("select p.postid from Post p", String.class).getResultList(); |
| 563 | if (userIds.isEmpty() || postIds.isEmpty()) { |
| 564 | return Collections.emptyList(); |
| 565 | } |
| 566 | String uid = userIds.get(0); |
| 567 | String pid = postIds.get(0); |
| 568 | String comment = "单元测试评论"; |
| 569 | return List.of( |
| 570 | DynamicTest.dynamicTest("AddComment", () -> { |
| 571 | String replyid = null; |
| 572 | try { |
| 573 | int ret = db.AddComment(pid, uid, comment); |
| 574 | Assertions.assertEquals(0, ret, "AddComment 应返回0"); |
| 575 | List<PostReply> replies = em.createQuery( |
| 576 | "SELECT r FROM PostReply r WHERE r.postid = :pid AND r.authorid = :uid AND r.content = :c", |
| 577 | PostReply.class) |
| 578 | .setParameter("pid", pid) |
| 579 | .setParameter("uid", uid) |
| 580 | .setParameter("c", comment) |
| 581 | .getResultList(); |
| 582 | Assertions.assertFalse(replies.isEmpty(), "评论应已插入"); |
| 583 | replyid = replies.get(0).replyid; |
| 584 | } finally { |
| 585 | if (replyid != null) { |
| 586 | EntityTransaction tx = em.getTransaction(); |
| 587 | tx.begin(); |
| 588 | PostReply toDelete = em.find(PostReply.class, replyid); |
| 589 | if (toDelete != null) |
| 590 | em.remove(toDelete); |
| 591 | tx.commit(); |
| 592 | } |
| 593 | } |
| 594 | })); |
| 595 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 596 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 597 | @TestFactory |
| 598 | Collection<DynamicTest> testDeleteComment() { |
| 599 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 600 | List<String> postIds = em.createQuery("select p.postid from Post p", String.class).getResultList(); |
| 601 | if (userIds.isEmpty() || postIds.isEmpty()) { |
| 602 | return Collections.emptyList(); |
| 603 | } |
| 604 | String uid = userIds.get(0); |
| 605 | String pid = postIds.get(0); |
| 606 | String comment = "待删除评论"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 607 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 608 | return List.of( |
| 609 | DynamicTest.dynamicTest("DeleteComment", () -> { |
| 610 | String replyid = null; |
| 611 | try { |
| 612 | // 先确保评论存在 |
| 613 | EntityTransaction tx = em.getTransaction(); |
| 614 | tx.begin(); |
| 615 | PostReply reply = new PostReply(); |
| 616 | reply.replyid = "reply_" + System.currentTimeMillis(); |
| 617 | reply.postid = pid; |
| 618 | reply.authorid = uid; |
| 619 | reply.content = comment; |
| 620 | reply.createdAt = new Date(); |
| 621 | em.persist(reply); |
| 622 | tx.commit(); |
| 623 | replyid = reply.replyid; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 624 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 625 | // 执行删除测试 |
| 626 | int ret = db.DeleteComment(pid, replyid); |
| 627 | Assertions.assertEquals(0, ret, "DeleteComment 应返回0"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 628 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 629 | em.clear(); |
| 630 | PostReply deleted = em.find(PostReply.class, replyid); |
| 631 | Assertions.assertNull(deleted, "评论应已删除"); |
| 632 | } finally { |
| 633 | if (replyid != null) { |
| 634 | try { |
| 635 | EntityTransaction tx = em.getTransaction(); |
| 636 | if (!tx.isActive()) { |
| 637 | tx.begin(); |
| 638 | PostReply toDelete = em.find(PostReply.class, replyid); |
| 639 | if (toDelete != null) { |
| 640 | em.remove(toDelete); |
| 641 | } |
| 642 | tx.commit(); |
| 643 | } |
| 644 | } catch (Exception e) { |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | })); |
| 649 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 650 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 651 | @TestFactory |
| 652 | Collection<DynamicTest> testExchangeMagicToUpload() { |
| 653 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 654 | if (userIds.isEmpty()) |
| 655 | return Collections.emptyList(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 656 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 657 | String uid = userIds.get(0); |
| 658 | final UserPT testUserPT; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 659 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 660 | EntityTransaction tx = em.getTransaction(); |
| 661 | tx.begin(); |
| 662 | UserPT tempUserPT = em.find(UserPT.class, uid); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 663 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 664 | if (tempUserPT == null) { |
| 665 | tempUserPT = new UserPT(); |
| 666 | tempUserPT.userid = uid; |
| 667 | tempUserPT.magic = 1000; |
| 668 | tempUserPT.upload = 1000; |
| 669 | tempUserPT.download = 1000; |
| 670 | em.persist(tempUserPT); |
| 671 | } else { |
| 672 | tempUserPT.magic = 1000; |
| 673 | tempUserPT.upload = 1000; |
| 674 | em.merge(tempUserPT); |
| 675 | } |
| 676 | testUserPT = tempUserPT; |
| 677 | tx.commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 678 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 679 | return List.of( |
| 680 | DynamicTest.dynamicTest("ExchangeMagicToUpload success", () -> { |
| 681 | try { |
| 682 | int magic = 100; |
| 683 | long beforeUpload = testUserPT.upload; |
| 684 | int beforeMagic = testUserPT.magic; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 685 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 686 | boolean ret = db.ExchangeMagicToUpload(uid, magic); |
| 687 | Assertions.assertTrue(ret, "兑换上传量应成功"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 688 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 689 | em.clear(); |
| 690 | UserPT after = em.find(UserPT.class, uid); |
| 691 | Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少"); |
| 692 | Assertions.assertEquals(beforeUpload + magic, after.upload, "上传量应增加"); |
| 693 | } finally { |
| 694 | EntityTransaction tx2 = em.getTransaction(); |
| 695 | tx2.begin(); |
| 696 | UserPT user = em.find(UserPT.class, uid); |
| 697 | if (user != null) { |
| 698 | user.magic = 0; |
| 699 | user.upload = 0; |
| 700 | em.merge(user); |
| 701 | } |
| 702 | tx2.commit(); |
| 703 | } |
| 704 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 705 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 706 | DynamicTest.dynamicTest("ExchangeMagicToUpload insufficient magic", () -> { |
| 707 | boolean ret = db.ExchangeMagicToUpload(uid, 2000); |
| 708 | Assertions.assertFalse(ret, "魔力值不足时应返回false"); |
| 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 | DynamicTest.dynamicTest("ExchangeMagicToUpload invalid params", () -> { |
| 712 | Assertions.assertFalse(db.ExchangeMagicToUpload(null, 100)); |
| 713 | Assertions.assertFalse(db.ExchangeMagicToUpload("", 100)); |
| 714 | Assertions.assertFalse(db.ExchangeMagicToUpload(uid, 0)); |
| 715 | Assertions.assertFalse(db.ExchangeMagicToUpload(uid, -1)); |
| 716 | })); |
| 717 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 718 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 719 | @TestFactory |
| 720 | Collection<DynamicTest> testExchangeMagicToDownload() { |
| 721 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 722 | if (userIds.isEmpty()) |
| 723 | return Collections.emptyList(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 724 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 725 | String uid = userIds.get(0); |
| 726 | final UserPT testUserPT; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 727 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 728 | EntityTransaction tx = em.getTransaction(); |
| 729 | tx.begin(); |
| 730 | UserPT tempUserPT = em.find(UserPT.class, uid); |
| 731 | if (tempUserPT == null) { |
| 732 | tempUserPT = new UserPT(); |
| 733 | tempUserPT.userid = uid; |
| 734 | tempUserPT.magic = 1000; |
| 735 | tempUserPT.upload = 1000; |
| 736 | tempUserPT.download = 1000; |
| 737 | em.persist(tempUserPT); |
| 738 | } else { |
| 739 | tempUserPT.magic = 1000; |
| 740 | tempUserPT.download = 1000; |
| 741 | em.merge(tempUserPT); |
| 742 | } |
| 743 | testUserPT = tempUserPT; |
| 744 | tx.commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 745 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 746 | return List.of( |
| 747 | DynamicTest.dynamicTest("ExchangeMagicToDownload success", () -> { |
| 748 | try { |
| 749 | int magic = 100; |
| 750 | long beforeDownload = testUserPT.download; |
| 751 | int beforeMagic = testUserPT.magic; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 752 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 753 | boolean ret = db.ExchangeMagicToDownload(uid, magic); |
| 754 | Assertions.assertTrue(ret, "兑换下载量应成功"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 755 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 756 | em.clear(); |
| 757 | UserPT after = em.find(UserPT.class, uid); |
| 758 | Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少"); |
| 759 | Assertions.assertEquals(beforeDownload - magic, after.download, "下载量应减少"); |
| 760 | } finally { |
| 761 | EntityTransaction tx2 = em.getTransaction(); |
| 762 | tx2.begin(); |
| 763 | UserPT user = em.find(UserPT.class, uid); |
| 764 | if (user != null) { |
| 765 | user.magic = 0; |
| 766 | user.download = 0; |
| 767 | em.merge(user); |
| 768 | } |
| 769 | tx2.commit(); |
| 770 | } |
| 771 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 772 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 773 | DynamicTest.dynamicTest("ExchangeMagicToDownload insufficient magic", () -> { |
| 774 | boolean ret = db.ExchangeMagicToDownload(uid, 2000); |
| 775 | Assertions.assertFalse(ret, "魔力值不足时应返回false"); |
| 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 | DynamicTest.dynamicTest("ExchangeMagicToDownload invalid params", () -> { |
| 779 | Assertions.assertFalse(db.ExchangeMagicToDownload(null, 100)); |
| 780 | Assertions.assertFalse(db.ExchangeMagicToDownload("", 100)); |
| 781 | Assertions.assertFalse(db.ExchangeMagicToDownload(uid, 0)); |
| 782 | Assertions.assertFalse(db.ExchangeMagicToDownload(uid, -1)); |
| 783 | })); |
| 784 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 785 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 786 | @TestFactory |
| 787 | Collection<DynamicTest> testExchangeMagicToVip() { |
| 788 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 789 | if (userIds.isEmpty()) |
| 790 | return Collections.emptyList(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 791 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 792 | String uid = userIds.get(0); |
| 793 | final UserPT testUserPT; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 794 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 795 | EntityTransaction tx = em.getTransaction(); |
| 796 | tx.begin(); |
| 797 | UserPT tempUserPT = em.find(UserPT.class, uid); |
| 798 | if (tempUserPT == null) { |
| 799 | tempUserPT = new UserPT(); |
| 800 | tempUserPT.userid = uid; |
| 801 | tempUserPT.magic = 1000; |
| 802 | tempUserPT.upload = 1000; |
| 803 | tempUserPT.download = 1000; |
| 804 | tempUserPT.viptime = 0; |
| 805 | em.persist(tempUserPT); |
| 806 | } else { |
| 807 | tempUserPT.magic = 1000; |
| 808 | tempUserPT.viptime = 0; |
| 809 | em.merge(tempUserPT); |
| 810 | } |
| 811 | testUserPT = tempUserPT; |
| 812 | tx.commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 813 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 814 | return List.of( |
| 815 | DynamicTest.dynamicTest("ExchangeMagicToVip success", () -> { |
| 816 | try { |
| 817 | int magic = 100; |
| 818 | int beforeVip = testUserPT.viptime; |
| 819 | int beforeMagic = testUserPT.magic; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 820 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 821 | boolean ret = db.ExchangeMagicToVip(uid, magic); |
| 822 | Assertions.assertTrue(ret, "兑换VIP次数应成功"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 823 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 824 | em.clear(); |
| 825 | UserPT after = em.find(UserPT.class, uid); |
| 826 | Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少"); |
| 827 | Assertions.assertEquals(beforeVip + magic, after.viptime, "VIP次数应增加"); |
| 828 | } finally { |
| 829 | EntityTransaction tx2 = em.getTransaction(); |
| 830 | tx2.begin(); |
| 831 | UserPT user = em.find(UserPT.class, uid); |
| 832 | if (user != null) { |
| 833 | user.magic = 0; |
| 834 | user.viptime = 0; |
| 835 | em.merge(user); |
| 836 | } |
| 837 | tx2.commit(); |
| 838 | } |
| 839 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 840 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 841 | DynamicTest.dynamicTest("ExchangeMagicToVip insufficient magic", () -> { |
| 842 | boolean ret = db.ExchangeMagicToVip(uid, 2000); |
| 843 | Assertions.assertFalse(ret, "魔力值不足时应返回false"); |
| 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 | DynamicTest.dynamicTest("ExchangeMagicToVip invalid params", () -> { |
| 847 | Assertions.assertFalse(db.ExchangeMagicToVip(null, 100)); |
| 848 | Assertions.assertFalse(db.ExchangeMagicToVip("", 100)); |
| 849 | Assertions.assertFalse(db.ExchangeMagicToVip(uid, 0)); |
| 850 | Assertions.assertFalse(db.ExchangeMagicToVip(uid, -1)); |
| 851 | })); |
| 852 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 853 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 854 | @TestFactory |
| 855 | Collection<DynamicTest> testUploadTransmitProfile() { |
| 856 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 857 | if (userIds.isEmpty()) |
| 858 | return Collections.emptyList(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 859 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 860 | String uid = userIds.get(0); |
| 861 | String profileId = "test_profile_" + System.currentTimeMillis(); |
| 862 | Profile profile = new Profile(); |
| 863 | profile.profileurl = profileId; |
| 864 | profile.userid = uid; |
| 865 | profile.magictogive = "100"; |
| 866 | profile.uploadtogive = "1000"; |
| 867 | profile.downloadtogive = "500"; |
| 868 | profile.exampass = false; |
| 869 | profile.magicgived = "0"; |
| 870 | profile.uploadgived = "0"; |
| 871 | profile.applicationurl = "http://example.com/apply"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 872 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 873 | return List.of( |
| 874 | DynamicTest.dynamicTest("UploadTransmitProfile success", () -> { |
| 875 | try { |
| 876 | boolean ret = db.UploadTransmitProfile(profile); |
| 877 | Assertions.assertTrue(ret, "上传迁移信息应成功"); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 878 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 879 | Profile inserted = em.find(Profile.class, profileId); |
| 880 | Assertions.assertNotNull(inserted, "迁移信息应已插入"); |
| 881 | Assertions.assertFalse(inserted.exampass, "新迁移信息默认未审核"); |
| 882 | } finally { |
| 883 | EntityTransaction tx = em.getTransaction(); |
| 884 | tx.begin(); |
| 885 | Profile toDelete = em.find(Profile.class, profileId); |
| 886 | if (toDelete != null) |
| 887 | em.remove(toDelete); |
| 888 | tx.commit(); |
| 889 | } |
| 890 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 891 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 892 | DynamicTest.dynamicTest("UploadTransmitProfile duplicate", () -> { |
| 893 | try { |
| 894 | db.UploadTransmitProfile(profile); |
| 895 | boolean ret = db.UploadTransmitProfile(profile); |
| 896 | Assertions.assertFalse(ret, "重复上传应返回false"); |
| 897 | } finally { |
| 898 | EntityTransaction tx = em.getTransaction(); |
| 899 | tx.begin(); |
| 900 | Profile toDelete = em.find(Profile.class, profileId); |
| 901 | if (toDelete != null) |
| 902 | em.remove(toDelete); |
| 903 | tx.commit(); |
| 904 | } |
| 905 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 906 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 907 | DynamicTest.dynamicTest("UploadTransmitProfile invalid params", () -> { |
| 908 | Assertions.assertFalse(db.UploadTransmitProfile(null)); |
| 909 | Profile invalidProfile = new Profile(); |
| 910 | Assertions.assertFalse(db.UploadTransmitProfile(invalidProfile)); |
| 911 | })); |
| 912 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 913 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 914 | @TestFactory |
| 915 | Collection<DynamicTest> testGetTransmitProfile() { |
| 916 | // 获取现有的迁移信息记录 |
| 917 | List<Profile> existingProfiles = em.createQuery( |
| 918 | "SELECT p FROM Profile p", Profile.class) |
| 919 | .setMaxResults(1) |
| 920 | .getResultList(); |
| 921 | if (existingProfiles.isEmpty()) |
| 922 | return Collections.emptyList(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 923 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 924 | Profile profile = existingProfiles.get(0); |
| 925 | String profileId = profile.profileurl; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 926 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 927 | return List.of( |
| 928 | DynamicTest.dynamicTest("GetTransmitProfile success", () -> { |
| 929 | Profile ret = db.GetTransmitProfile(profileId); |
| 930 | Assertions.assertNotNull(ret, "应成功获取迁移信息"); |
| 931 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 932 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 933 | DynamicTest.dynamicTest("GetTransmitProfile not exist", () -> { |
| 934 | Profile ret = db.GetTransmitProfile("not_exist_profile"); |
| 935 | Assertions.assertNull(ret, "不存在的迁移信息应返回null"); |
| 936 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 937 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 938 | DynamicTest.dynamicTest("GetTransmitProfile invalid params", () -> { |
| 939 | Assertions.assertNull(db.GetTransmitProfile(null)); |
| 940 | Assertions.assertNull(db.GetTransmitProfile("")); |
| 941 | })); |
| 942 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 943 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 944 | @TestFactory |
| 945 | Collection<DynamicTest> testExamTransmitProfile() { |
| 946 | List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList(); |
| 947 | if (userIds.isEmpty()) |
| 948 | return Collections.emptyList(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 949 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 950 | String uid = userIds.get(0); |
| 951 | String profileId = "test_profile_exam_" + System.currentTimeMillis(); |
| 952 | Profile profile = new Profile(); |
| 953 | profile.profileurl = profileId; |
| 954 | profile.userid = uid; |
| 955 | profile.magictogive = "100"; |
| 956 | profile.uploadtogive = "1000"; |
| 957 | profile.exampass = false; |
| 958 | profile.magicgived = "0"; |
| 959 | profile.uploadgived = "0"; |
| 960 | profile.applicationurl = "http://example.com/apply"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 961 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 962 | EntityTransaction tx = em.getTransaction(); |
| 963 | tx.begin(); |
| 964 | em.persist(profile); |
| 965 | UserPT userPT = em.find(UserPT.class, uid); |
| 966 | if (userPT == null) { |
| 967 | userPT = new UserPT(); |
| 968 | userPT.userid = uid; |
| 969 | userPT.magic = 0; |
| 970 | userPT.upload = 0; |
| 971 | em.persist(userPT); |
| 972 | } else { |
| 973 | userPT.magic = 0; |
| 974 | userPT.upload = 0; |
| 975 | em.merge(userPT); |
| 976 | } |
| 977 | tx.commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 978 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 979 | return List.of( |
| 980 | DynamicTest.dynamicTest("ExamTransmitProfile approve", () -> { |
| 981 | try { |
| 982 | boolean ret = db.ExamTransmitProfile(profileId, true); |
| 983 | Assertions.assertTrue(ret, "审核通过应成功"); |
| 984 | } finally { |
| 985 | EntityTransaction tx2 = em.getTransaction(); |
| 986 | tx2.begin(); |
| 987 | UserPT user = em.find(UserPT.class, uid); |
| 988 | if (user != null) { |
| 989 | user.magic = 0; |
| 990 | user.upload = 0; |
| 991 | em.merge(user); |
| 992 | } |
| 993 | Profile toDelete = em.find(Profile.class, profileId); |
| 994 | if (toDelete != null) |
| 995 | em.remove(toDelete); |
| 996 | tx2.commit(); |
| 997 | } |
| 998 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 999 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1000 | DynamicTest.dynamicTest("ExamTransmitProfile reject", () -> { |
| 1001 | String rejectId = "test_profile_reject_" + System.currentTimeMillis(); |
| 1002 | Profile rejectProfile = new Profile(); |
| 1003 | rejectProfile.profileurl = rejectId; |
| 1004 | rejectProfile.userid = uid; |
| 1005 | rejectProfile.magictogive = "100"; |
| 1006 | rejectProfile.uploadtogive = "1000"; |
| 1007 | rejectProfile.magicgived = "0"; |
| 1008 | rejectProfile.uploadgived = "0"; |
| 1009 | rejectProfile.exampass = false; |
| 1010 | rejectProfile.applicationurl = "http://example.com/apply"; |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1011 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1012 | EntityTransaction tx3 = em.getTransaction(); |
| 1013 | tx3.begin(); |
| 1014 | em.persist(rejectProfile); |
| 1015 | tx3.commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1016 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1017 | try { |
| 1018 | boolean ret = db.ExamTransmitProfile(rejectId, false); |
| 1019 | Assertions.assertTrue(ret, "审核拒绝应成功"); |
| 1020 | } finally { |
| 1021 | EntityTransaction tx4 = em.getTransaction(); |
| 1022 | tx4.begin(); |
| 1023 | Profile toDelete = em.find(Profile.class, rejectId); |
| 1024 | if (toDelete != null) |
| 1025 | em.remove(toDelete); |
| 1026 | tx4.commit(); |
| 1027 | } |
| 1028 | }), |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1029 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1030 | DynamicTest.dynamicTest("ExamTransmitProfile invalid params", () -> { |
| 1031 | Assertions.assertFalse(db.ExamTransmitProfile(null, true)); |
| 1032 | Assertions.assertFalse(db.ExamTransmitProfile("", true)); |
| 1033 | Assertions.assertFalse(db.ExamTransmitProfile("not_exist_profile", true)); |
| 1034 | })); |
| 1035 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1036 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1037 | @Test |
| 1038 | void testGetTransmitProfileList() { |
| 1039 | em.getTransaction().begin(); |
| 1040 | em.createQuery("DELETE FROM Profile").executeUpdate(); |
| 1041 | em.getTransaction().commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1042 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1043 | List<String> userIds = em.createQuery("select u.userid from User u", String.class) |
| 1044 | .setMaxResults(3) |
| 1045 | .getResultList(); |
| 1046 | if (userIds.isEmpty()) { |
| 1047 | return; |
| 1048 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1049 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1050 | EntityTransaction tx = em.getTransaction(); |
| 1051 | tx.begin(); |
| 1052 | for (int i = 0; i < userIds.size(); i++) { |
| 1053 | Profile profile = new Profile(); |
| 1054 | profile.profileurl = "test_profile_list_" + i + "_" + System.currentTimeMillis(); |
| 1055 | profile.userid = userIds.get(i); |
| 1056 | profile.magictogive = String.valueOf(100 * (i + 1)); |
| 1057 | profile.uploadtogive = String.valueOf(1000 * (i + 1)); |
| 1058 | profile.exampass = false; |
| 1059 | profile.magicgived = "0"; |
| 1060 | profile.uploadgived = "0"; |
| 1061 | profile.applicationurl = "http://example.com/apply"; |
| 1062 | em.persist(profile); |
| 1063 | } |
| 1064 | tx.commit(); |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 1065 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1066 | try { |
| 1067 | Profile[] profiles = db.GetTransmitProfileList(); |
| 1068 | Assertions.assertEquals(userIds.size(), profiles.length, "应返回所有迁移申请"); |
| 1069 | Arrays.stream(profiles) |
| 1070 | .forEach(p -> Assertions.assertTrue(p.profileurl.startsWith("test_profile_list_"), "应为测试数据")); |
| 1071 | } finally { |
| 1072 | EntityTransaction tx2 = em.getTransaction(); |
| 1073 | tx2.begin(); |
| 1074 | em.createQuery("DELETE FROM Profile p WHERE p.profileurl LIKE 'test_profile_list_%'").executeUpdate(); |
| 1075 | tx2.commit(); |
| 1076 | } |
| 1077 | } |
| 1078 | } |