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:
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
type FundingRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
pool pgxPool
|
||||
}
|
||||
|
||||
func NewFundingRepo(pool *pgxpool.Pool) *FundingRepo {
|
||||
|
||||
109
internal/repo/persistent/postgres/funding_repo_test.go
Normal file
109
internal/repo/persistent/postgres/funding_repo_test.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"cryptoHermes/internal/entity"
|
||||
|
||||
"github.com/pashagolub/pgxmock/v4"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFundingRepo_UpsertMany_HappyPath(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO funding_rates").
|
||||
WithArgs("binance", "BTCUSDT", int64(1700000000000), "0.0001", "50000").
|
||||
WillReturnResult(pgxmock.NewResult("INSERT", 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &FundingRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.FundingRate{{
|
||||
Source: "binance", Symbol: "BTCUSDT",
|
||||
FundingTime: 1700000000000, FundingRate: "0.0001", MarkPrice: "50000",
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFundingRepo_UpsertMany_SkipsEmptyRate(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &FundingRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.FundingRate{
|
||||
{Symbol: "BTCUSDT", FundingTime: 1, FundingRate: ""}, // 空 rate 跳过
|
||||
{Symbol: "BTCUSDT", FundingTime: 0, FundingRate: "1"}, // 空时间戳跳过
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFundingRepo_UpsertMany_ExecError(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO funding_rates").
|
||||
WithArgs(pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg()).
|
||||
WillReturnError(errors.New("dup key"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &FundingRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.FundingRate{{
|
||||
Symbol: "BTCUSDT", FundingTime: 1, FundingRate: "0.0001",
|
||||
}})
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "upsert funding")
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFundingRepo_FindRecent_HappyPath(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
rows := mock.NewRows([]string{"source", "symbol", "funding_time", "funding_rate", "mark_price"}).
|
||||
AddRow("binance", "BTCUSDT", int64(2), "0.0002", "50100").
|
||||
AddRow("binance", "BTCUSDT", int64(1), "0.0001", "50000")
|
||||
|
||||
mock.ExpectQuery("SELECT source, symbol, funding_time").
|
||||
WithArgs("BTCUSDT", 100).
|
||||
WillReturnRows(rows)
|
||||
|
||||
r := &FundingRepo{pool: mock}
|
||||
got, err := r.FindRecent(context.Background(), "BTCUSDT", 0) // 0 → 100
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 2)
|
||||
// 反序后升序:[1,2]
|
||||
require.Equal(t, int64(1), got[0].FundingTime)
|
||||
require.Equal(t, "50100", got[1].MarkPrice)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFundingRepo_FindRecent_QueryError(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectQuery("SELECT source, symbol, funding_time").
|
||||
WithArgs("BTCUSDT", 50).
|
||||
WillReturnError(errors.New("conn closed"))
|
||||
|
||||
r := &FundingRepo{pool: mock}
|
||||
_, err = r.FindRecent(context.Background(), "BTCUSDT", 50)
|
||||
require.Error(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type KlineRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
pool pgxPool
|
||||
}
|
||||
|
||||
func NewKlineRepo(pool *pgxpool.Pool) *KlineRepo {
|
||||
|
||||
175
internal/repo/persistent/postgres/kline_repo_test.go
Normal file
175
internal/repo/persistent/postgres/kline_repo_test.go
Normal file
@@ -0,0 +1,175 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"cryptoHermes/internal/entity"
|
||||
|
||||
"github.com/pashagolub/pgxmock/v4"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestKlineRepo_UpsertMany_HappyPath(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO market_klines").
|
||||
WithArgs(
|
||||
"binance", "BTCUSDT", "1h",
|
||||
int64(1700000000000), int64(1700003599999),
|
||||
"50000", "50500", "49800", "50200",
|
||||
"123", "6190000",
|
||||
int64(789), "60", "3000000",
|
||||
).
|
||||
WillReturnResult(pgxmock.NewResult("INSERT", 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectRollback() // defer Rollback after Commit is a no-op but mock sees it
|
||||
|
||||
r := &KlineRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.Kline{{
|
||||
Source: "binance", Symbol: "BTCUSDT", Interval: "1h",
|
||||
OpenTime: 1700000000000, CloseTime: 1700003599999,
|
||||
Open: "50000", High: "50500", Low: "49800", Close: "50200",
|
||||
Volume: "123", QuoteVolume: "6190000",
|
||||
TradeCount: 789, TakerBuyBaseVolume: "60", TakerBuyQuoteVolume: "3000000",
|
||||
IsClosed: true,
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestKlineRepo_UpsertMany_EmptyShortCircuits(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
r := &KlineRepo{pool: mock}
|
||||
require.NoError(t, r.UpsertMany(context.Background(), nil))
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestKlineRepo_UpsertMany_SkipsUnclosed(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
// 第二根 K 线 IsClosed=false,应跳过;只 Exec 一次
|
||||
mock.ExpectExec("INSERT INTO market_klines").
|
||||
WithArgs(
|
||||
"binance", "BTCUSDT", "1h",
|
||||
int64(1), int64(2),
|
||||
"1", "1", "1", "1", "1", "1",
|
||||
int64(0), "", "",
|
||||
).
|
||||
WillReturnResult(pgxmock.NewResult("INSERT", 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &KlineRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.Kline{
|
||||
{Source: "binance", Symbol: "BTCUSDT", Interval: "1h", OpenTime: 1, CloseTime: 2,
|
||||
Open: "1", High: "1", Low: "1", Close: "1", Volume: "1", QuoteVolume: "1",
|
||||
IsClosed: true},
|
||||
{Source: "binance", Symbol: "BTCUSDT", Interval: "1h", OpenTime: 3, CloseTime: 4,
|
||||
IsClosed: false}, // 未收线,跳过
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestKlineRepo_UpsertMany_BeginError(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin().WillReturnError(errors.New("conn lost"))
|
||||
|
||||
r := &KlineRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.Kline{{IsClosed: true}})
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "begin")
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestKlineRepo_UpsertMany_ExecError(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO market_klines").
|
||||
WithArgs(
|
||||
pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(),
|
||||
pgxmock.AnyArg(), pgxmock.AnyArg(),
|
||||
pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(),
|
||||
pgxmock.AnyArg(), pgxmock.AnyArg(),
|
||||
pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(),
|
||||
).
|
||||
WillReturnError(errors.New("constraint violation"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &KlineRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.Kline{{
|
||||
Source: "binance", Symbol: "BTCUSDT", Interval: "1h",
|
||||
OpenTime: 1700000000000, IsClosed: true,
|
||||
}})
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "upsert kline")
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestKlineRepo_FindRecent_HappyPath(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
rows := mock.NewRows([]string{
|
||||
"source", "symbol", "interval", "open_time", "close_time",
|
||||
"open", "high", "low", "close",
|
||||
"volume", "quote_volume",
|
||||
"trade_count", "taker_buy_base_volume", "taker_buy_quote_volume",
|
||||
}).
|
||||
// DB 返回时间倒序:t=2 先,t=1 后
|
||||
AddRow("binance", "BTCUSDT", "1h", int64(2), int64(3),
|
||||
"50100", "50200", "50000", "50150",
|
||||
"10", "500000", int64(100), "5", "250000").
|
||||
AddRow("binance", "BTCUSDT", "1h", int64(1), int64(2),
|
||||
"50000", "50100", "49900", "50050",
|
||||
"9", "450000", int64(90), "4", "200000")
|
||||
|
||||
mock.ExpectQuery("SELECT source, symbol, interval").
|
||||
WithArgs("BTCUSDT", "1h", 300).
|
||||
WillReturnRows(rows)
|
||||
|
||||
r := &KlineRepo{pool: mock}
|
||||
got, err := r.FindRecent(context.Background(), "BTCUSDT", "1h", 0) // limit<=0 → 300
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 2)
|
||||
// 返回应已反序为升序:[1,2]
|
||||
require.Equal(t, int64(1), got[0].OpenTime)
|
||||
require.Equal(t, int64(2), got[1].OpenTime)
|
||||
require.True(t, got[0].IsClosed, "落库的都视为已收线")
|
||||
require.True(t, got[1].IsClosed)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestKlineRepo_FindRecent_QueryError(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectQuery("SELECT source, symbol, interval").
|
||||
WithArgs("BTCUSDT", "1h", 100).
|
||||
WillReturnError(errors.New("timeout"))
|
||||
|
||||
r := &KlineRepo{pool: mock}
|
||||
_, err = r.FindRecent(context.Background(), "BTCUSDT", "1h", 100)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "query")
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type LongShortRatioRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
pool pgxPool
|
||||
}
|
||||
|
||||
func NewLongShortRatioRepo(pool *pgxpool.Pool) *LongShortRatioRepo {
|
||||
|
||||
115
internal/repo/persistent/postgres/long_short_ratio_repo_test.go
Normal file
115
internal/repo/persistent/postgres/long_short_ratio_repo_test.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"cryptoHermes/internal/entity"
|
||||
|
||||
"github.com/pashagolub/pgxmock/v4"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLongShortRatioRepo_UpsertMany_HappyPath(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO long_short_ratio").
|
||||
WithArgs("binance", "BTCUSDT", "1h", entity.RatioTypeGlobalAccount, int64(1),
|
||||
"1.5", "0.6", "0.4").
|
||||
WillReturnResult(pgxmock.NewResult("INSERT", 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &LongShortRatioRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.LongShortRatio{{
|
||||
Source: "binance", Symbol: "BTCUSDT", Period: "1h",
|
||||
RatioType: entity.RatioTypeGlobalAccount, Timestamp: 1,
|
||||
LongShortRatio: "1.5", LongValue: "0.6", ShortValue: "0.4",
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestLongShortRatioRepo_UpsertMany_SkipsEmpty(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &LongShortRatioRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.LongShortRatio{
|
||||
{LongShortRatio: ""},
|
||||
{LongShortRatio: "1", Timestamp: 0},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestLongShortRatioRepo_UpsertMany_ExecError(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO long_short_ratio").
|
||||
WithArgs(pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(),
|
||||
pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg()).
|
||||
WillReturnError(errors.New("fk violation"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &LongShortRatioRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.LongShortRatio{{
|
||||
Symbol: "BTCUSDT", Period: "1h", RatioType: entity.RatioTypeGlobalAccount,
|
||||
Timestamp: 1, LongShortRatio: "1.5",
|
||||
}})
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "upsert ls")
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestLongShortRatioRepo_FindRecent_HappyPath(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
rows := mock.NewRows([]string{"source", "symbol", "period", "ratio_type", "timestamp",
|
||||
"long_short_ratio", "long_value", "short_value"}).
|
||||
AddRow("binance", "BTCUSDT", "1h", entity.RatioTypeGlobalAccount, int64(2),
|
||||
"1.6", "0.62", "0.38").
|
||||
AddRow("binance", "BTCUSDT", "1h", entity.RatioTypeGlobalAccount, int64(1),
|
||||
"1.5", "0.6", "0.4")
|
||||
|
||||
mock.ExpectQuery("SELECT source, symbol, period, ratio_type").
|
||||
WithArgs("BTCUSDT", "1h", entity.RatioTypeGlobalAccount, 200).
|
||||
WillReturnRows(rows)
|
||||
|
||||
r := &LongShortRatioRepo{pool: mock}
|
||||
got, err := r.FindRecent(context.Background(), "BTCUSDT", "1h", entity.RatioTypeGlobalAccount, 200)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 2)
|
||||
require.Equal(t, int64(1), got[0].Timestamp, "升序")
|
||||
require.Equal(t, "0.62", got[1].LongValue)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestLongShortRatioRepo_FindRecent_QueryError(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectQuery("SELECT source, symbol, period, ratio_type").
|
||||
WithArgs("BTCUSDT", "1h", entity.RatioTypeTopTraderPosition, 100).
|
||||
WillReturnError(errors.New("permission denied"))
|
||||
|
||||
r := &LongShortRatioRepo{pool: mock}
|
||||
_, err = r.FindRecent(context.Background(), "BTCUSDT", "1h", entity.RatioTypeTopTraderPosition, 0)
|
||||
require.Error(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type OpenInterestRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
pool pgxPool
|
||||
}
|
||||
|
||||
func NewOpenInterestRepo(pool *pgxpool.Pool) *OpenInterestRepo {
|
||||
|
||||
107
internal/repo/persistent/postgres/open_interest_repo_test.go
Normal file
107
internal/repo/persistent/postgres/open_interest_repo_test.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"cryptoHermes/internal/entity"
|
||||
|
||||
"github.com/pashagolub/pgxmock/v4"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestOpenInterestRepo_UpsertMany_HappyPath(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO open_interest").
|
||||
WithArgs("binance", "BTCUSDT", "1h", int64(1), "10000", "5000000").
|
||||
WillReturnResult(pgxmock.NewResult("INSERT", 1))
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &OpenInterestRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.OpenInterest{{
|
||||
Source: "binance", Symbol: "BTCUSDT", Period: "1h",
|
||||
Timestamp: 1, OpenInterest: "10000", OpenInterestValue: "5000000",
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestOpenInterestRepo_UpsertMany_SkipsEmpty(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectCommit()
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &OpenInterestRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.OpenInterest{
|
||||
{OpenInterest: ""}, // 空 OI 跳过
|
||||
{OpenInterest: "1", Timestamp: 0}, // 空时间戳跳过
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestOpenInterestRepo_UpsertMany_ExecError(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT INTO open_interest").
|
||||
WithArgs(pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg(), pgxmock.AnyArg()).
|
||||
WillReturnError(errors.New("disk full"))
|
||||
mock.ExpectRollback()
|
||||
|
||||
r := &OpenInterestRepo{pool: mock}
|
||||
err = r.UpsertMany(context.Background(), []entity.OpenInterest{{
|
||||
Symbol: "BTCUSDT", Period: "1h", Timestamp: 1, OpenInterest: "1",
|
||||
}})
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "upsert oi")
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestOpenInterestRepo_FindRecent_HappyPath(t *testing.T) {
|
||||
mock, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
defer mock.Close()
|
||||
|
||||
rows := mock.NewRows([]string{"source", "symbol", "period", "timestamp", "open_interest", "open_interest_value"}).
|
||||
AddRow("binance", "BTCUSDT", "1h", int64(2), "10100", "5050000").
|
||||
AddRow("binance", "BTCUSDT", "1h", int64(1), "10000", "5000000")
|
||||
|
||||
mock.ExpectQuery("SELECT source, symbol, period").
|
||||
WithArgs("BTCUSDT", "1h", 200).
|
||||
WillReturnRows(rows)
|
||||
|
||||
r := &OpenInterestRepo{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.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestOpenInterestRepo_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("timeout"))
|
||||
|
||||
r := &OpenInterestRepo{pool: mock}
|
||||
_, err = r.FindRecent(context.Background(), "BTCUSDT", "1h", 0)
|
||||
require.Error(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
21
internal/repo/persistent/postgres/pool.go
Normal file
21
internal/repo/persistent/postgres/pool.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
// pgxPool 是 5 个 repo 共享的最小连接池接口。
|
||||
//
|
||||
// 为什么需要这个接口:`*pgxpool.Pool` 是具体 struct,不能被 pgxmock 直接
|
||||
// 替换。把 repo 字段改成接口后,生产代码继续传 *pgxpool.Pool(满足接口),
|
||||
// 测试代码传 pgxmock.PgxPoolIface(同样满足)。
|
||||
//
|
||||
// 只列出 5 个 repo 实际用到的方法。如果未来用到 Acquire/Stat,在这里追加。
|
||||
type pgxPool interface {
|
||||
Begin(ctx context.Context) (pgx.Tx, error)
|
||||
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
|
||||
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type TakerVolumeRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
pool pgxPool
|
||||
}
|
||||
|
||||
func NewTakerVolumeRepo(pool *pgxpool.Pool) *TakerVolumeRepo {
|
||||
|
||||
109
internal/repo/persistent/postgres/taker_volume_repo_test.go
Normal file
109
internal/repo/persistent/postgres/taker_volume_repo_test.go
Normal file
@@ -0,0 +1,109 @@
|
||||
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())
|
||||
}
|
||||
Reference in New Issue
Block a user