为什么用 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 条守卫扫过无输出。
110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"cryptoHermes/internal/entity"
|
|
|
|
"github.com/pashagolub/pgxmock/v4"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTakerVolumeRepo_UpsertMany_HappyPath(t *testing.T) {
|
|
mock, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
defer mock.Close()
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectExec("INSERT INTO taker_buy_sell_volume").
|
|
WithArgs("binance", "BTCUSDT", "1h", int64(1), "1.2", "60", "50").
|
|
WillReturnResult(pgxmock.NewResult("INSERT", 1))
|
|
mock.ExpectCommit()
|
|
mock.ExpectRollback()
|
|
|
|
r := &TakerVolumeRepo{pool: mock}
|
|
err = r.UpsertMany(context.Background(), []entity.TakerBuySellVolume{{
|
|
Source: "binance", Symbol: "BTCUSDT", Period: "1h",
|
|
Timestamp: 1, BuySellRatio: "1.2", BuyVolume: "60", SellVolume: "50",
|
|
}})
|
|
require.NoError(t, err)
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
}
|
|
|
|
func TestTakerVolumeRepo_UpsertMany_SkipsEmptyTimestamp(t *testing.T) {
|
|
mock, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
defer mock.Close()
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectCommit()
|
|
mock.ExpectRollback()
|
|
|
|
r := &TakerVolumeRepo{pool: mock}
|
|
err = r.UpsertMany(context.Background(), []entity.TakerBuySellVolume{
|
|
{Timestamp: 0, BuyVolume: "60"},
|
|
})
|
|
require.NoError(t, err)
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
}
|
|
|
|
func TestTakerVolumeRepo_UpsertMany_ExecError(t *testing.T) {
|
|
mock, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
defer mock.Close()
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectExec("INSERT INTO taker_buy_sell_volume").
|
|
WithArgs(pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(),
|
|
pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg()).
|
|
WillReturnError(errors.New("readonly"))
|
|
mock.ExpectRollback()
|
|
|
|
r := &TakerVolumeRepo{pool: mock}
|
|
err = r.UpsertMany(context.Background(), []entity.TakerBuySellVolume{{
|
|
Symbol: "BTCUSDT", Period: "1h", Timestamp: 1,
|
|
}})
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "upsert taker")
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
}
|
|
|
|
func TestTakerVolumeRepo_FindRecent_HappyPath(t *testing.T) {
|
|
mock, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
defer mock.Close()
|
|
|
|
rows := mock.NewRows([]string{"source", "symbol", "period", "timestamp",
|
|
"buy_sell_ratio", "buy_volume", "sell_volume"}).
|
|
AddRow("binance", "BTCUSDT", "1h", int64(2), "1.3", "65", "50").
|
|
AddRow("binance", "BTCUSDT", "1h", int64(1), "1.2", "60", "50")
|
|
|
|
mock.ExpectQuery("SELECT source, symbol, period").
|
|
WithArgs("BTCUSDT", "1h", 200).
|
|
WillReturnRows(rows)
|
|
|
|
r := &TakerVolumeRepo{pool: mock}
|
|
got, err := r.FindRecent(context.Background(), "BTCUSDT", "1h", 200)
|
|
require.NoError(t, err)
|
|
require.Len(t, got, 2)
|
|
require.Equal(t, int64(1), got[0].Timestamp)
|
|
require.Equal(t, "65", got[1].BuyVolume)
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
}
|
|
|
|
func TestTakerVolumeRepo_FindRecent_QueryError(t *testing.T) {
|
|
mock, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
defer mock.Close()
|
|
|
|
mock.ExpectQuery("SELECT source, symbol, period").
|
|
WithArgs("BTCUSDT", "1h", 100).
|
|
WillReturnError(errors.New("deadlock"))
|
|
|
|
r := &TakerVolumeRepo{pool: mock}
|
|
_, err = r.FindRecent(context.Background(), "BTCUSDT", "1h", 0)
|
|
require.Error(t, err)
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
}
|