test: 测试数据改为按目录组织,不再放进 Flyway 迁移

This commit is contained in:
2026-10-22 21:59:53 +08:00
parent c59efa3676
commit 0618c3970a
5 changed files with 19 additions and 37 deletions
@@ -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
@@ -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;
* <p>每条用例用自己独有的时间窗口与任务类型取数,避免同库其他用例的数据串进来。</p>
*/
@SpringBootTest
@Sql(scripts = "/db/test-data/admin_user.sql")
class AiUsageAggregationIntegrationTest {
@Autowired
+2
View File
@@ -6,6 +6,8 @@ spring:
password:
flyway:
enabled: true
# 只加载正式迁移。测试专用数据走 @Sql(见 db/test-data/),不放进 Flyway 位置,
# 否则它会在「应用启动」时被跑到真实库上。
locations: classpath:db/migration
baseline-on-migrate: true
jackson:
@@ -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
);
@@ -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);