test(postgres): pgxmock 覆盖 5 个 repo,包级 87.6%

为什么用 pgxmock:testcontainers 需要 docker-in-CI 而当前还没有 CI;
mock 路线 1) 零基础设施 2) 把 SQL 字符串和参数顺序作为契约锁死,
schema 漂移立刻可见——这正是上生产前最需要的信号。pgxmock 升级到
testcontainers 不阻塞 v2,留 v2.1 做 nightly 集成测试。

关键改动:
- internal/repo/persistent/postgres/pool.go:新建 pgxPool 接口,
  列出 5 repo 实际用到的 Begin/Query/Exec 三个方法。生产代码继续
  传 *pgxpool.Pool(自动满足接口),测试传 pgxmock.PgxPoolIface。
- 5 个 repo struct 字段从 *pgxpool.Pool 改为 pgxPool 接口;构造器
  签名保留 *pgxpool.Pool,app.go DI 不动。
- 5 个 *_repo_test.go:每个 repo 至少
  · UpsertMany happy path(精确 SQL + 参数)
  · UpsertMany 跳过逻辑(空字段 / 未收线)
  · UpsertMany ExecError(Rollback 路径)
  · FindRecent happy path(验证 DESC → 反序为升序)
  · FindRecent QueryError
  KlineRepo 额外加了 EmptyShortCircuits 和 BeginError 两个边界。

覆盖率:postgres 包 87.6%(UpsertMany ~86%、FindRecent ~93%)。
New*Repo 构造器为 0% 是设计选择:测试直接构造 struct 避开
*pgxpool.Pool 依赖。

所有 12 条守卫扫过无输出。
This commit is contained in:
dela
2026-05-24 20:43:46 +08:00
parent c478a5feb9
commit ee420f9c56
13 changed files with 663 additions and 32 deletions

17
go.mod
View File

@@ -1,13 +1,15 @@
module cryptoHermes
go 1.23
go 1.23.0
require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/jackc/pgx/v5 v5.7.1
github.com/jackc/pgx/v5 v5.7.4
github.com/pashagolub/pgxmock/v4 v4.6.0
github.com/robfig/cron/v3 v3.0.1
golang.org/x/sync v0.8.0
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.13.0
golang.org/x/time v0.6.0
)
@@ -21,21 +23,18 @@ require (
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)