From 0618c3970aa041858fa279d83e6d64d5659cf1e6 Mon Sep 17 00:00:00 2001 From: cat-shark <1716967236@qq.com> Date: Thu, 22 Oct 2026 21:59:53 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E6=8C=89=E7=9B=AE=E5=BD=95=E7=BB=84=E7=BB=87?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=86=8D=E6=94=BE=E8=BF=9B=20Flyway=20?= =?UTF-8?q?=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LoginControllerTest.java | 2 + .../AiUsageAggregationIntegrationTest.java | 2 + src/test/resources/application.yml | 2 + .../resources/db/migration/R__test_seed.sql | 37 ------------------- .../resources/db/test-data/admin_user.sql | 13 +++++++ 5 files changed, 19 insertions(+), 37 deletions(-) delete mode 100644 src/test/resources/db/migration/R__test_seed.sql create mode 100644 src/test/resources/db/test-data/admin_user.sql diff --git a/src/test/java/com/guo/learningprogresstracker/controller/LoginControllerTest.java b/src/test/java/com/guo/learningprogresstracker/controller/LoginControllerTest.java index 7fcc114..666595d 100644 --- a/src/test/java/com/guo/learningprogresstracker/controller/LoginControllerTest.java +++ b/src/test/java/com/guo/learningprogresstracker/controller/LoginControllerTest.java @@ -7,11 +7,13 @@ import com.guo.learningprogresstracker.exception.AppException; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.jdbc.Sql; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @SpringBootTest +@Sql(scripts = "/db/test-data/admin_user.sql") class LoginControllerTest { @Autowired diff --git a/src/test/java/com/guo/learningprogresstracker/service/impl/AiUsageAggregationIntegrationTest.java b/src/test/java/com/guo/learningprogresstracker/service/impl/AiUsageAggregationIntegrationTest.java index 853f66d..b6d11e3 100644 --- a/src/test/java/com/guo/learningprogresstracker/service/impl/AiUsageAggregationIntegrationTest.java +++ b/src/test/java/com/guo/learningprogresstracker/service/impl/AiUsageAggregationIntegrationTest.java @@ -15,6 +15,7 @@ import com.guo.learningprogresstracker.service.AiUsageService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.jdbc.Sql; import java.math.BigDecimal; import java.time.Instant; @@ -34,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; *
每条用例用自己独有的时间窗口与任务类型取数,避免同库其他用例的数据串进来。
*/ @SpringBootTest +@Sql(scripts = "/db/test-data/admin_user.sql") class AiUsageAggregationIntegrationTest { @Autowired diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 678c6b8..85344a3 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -6,6 +6,8 @@ spring: password: flyway: enabled: true + # 只加载正式迁移。测试专用数据走 @Sql(见 db/test-data/),不放进 Flyway 位置, + # 否则它会在「应用启动」时被跑到真实库上。 locations: classpath:db/migration baseline-on-migrate: true jackson: diff --git a/src/test/resources/db/migration/R__test_seed.sql b/src/test/resources/db/migration/R__test_seed.sql deleted file mode 100644 index 97799fa..0000000 --- a/src/test/resources/db/migration/R__test_seed.sql +++ /dev/null @@ -1,37 +0,0 @@ -insert into user (id, user_name, user_password, created_time, deleted) -values ('1', 'admin', '$2a$10$skdiSwut4R9pAvt2ZK5ct.6QNualPyJGiaxngFObdLrpAxRnzehBS', now(), 0); - -insert into tasks ( - id, - task_num, - task_name, - task_description, - urgency, - importance, - content_difficulty, - future_value, - subjective_priority, - calculated_priority, - material_url, - last_learning_status, - created_by, - created_time, - deleted -) -values ( - 1, - 'TASK_TEST', - '测试任务名称', - '测试任务描述', - 3, - 3, - 3, - 3, - 3, - 3, - 'http://example.com/material', - '未开始', - '1', - now(), - 0 -); diff --git a/src/test/resources/db/test-data/admin_user.sql b/src/test/resources/db/test-data/admin_user.sql new file mode 100644 index 0000000..37f1765 --- /dev/null +++ b/src/test/resources/db/test-data/admin_user.sql @@ -0,0 +1,13 @@ +-- 测试用的管理员账号:通过 @Sql 在测试里加载,**不是 Flyway 迁移**。 +-- +-- 为什么不放 db/migration:Flyway 是在「应用启动」时执行的,任何放在那里的种子 +-- 都会随应用一起跑到真实库上(带测试 classpath + local profile 启动就会中招)。 +-- 测试专用数据必须只在测试进程里加载,@Sql 只由 Spring TestContext 执行,应用启动永远碰不到它。 +-- +-- 幂等:先删后插,同一个测试类多个方法 / 多个测试类重复执行都不会主键冲突。 +-- 使用方:LoginControllerTest(登录)、AiUsageAggregationIntegrationTest(按用户分组的展示名)。 + +delete from user where id = '1' or user_name = 'admin'; + +insert into user (id, user_name, user_password, created_time, deleted) +values ('1', 'admin', '$2a$10$skdiSwut4R9pAvt2ZK5ct.6QNualPyJGiaxngFObdLrpAxRnzehBS', now(), 0);