perf: keep default ledger queries on platform time indexes
This commit is contained in:
@@ -3,12 +3,54 @@ package business
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"kra/internal/biz/business"
|
||||
)
|
||||
|
||||
func TestUnfilteredLedgerKeepsEveryStoredPlatform(t *testing.T) {
|
||||
db, repo, _, filter := prepareLedgerData(t)
|
||||
for _, query := range []string{
|
||||
"INSERT INTO tab_user(id,source_db,register_time,promote_id,puid) VALUES(90,'historical','1500',0,0)",
|
||||
"INSERT INTO tab_spend(id,source_db,user_id,pay_amount,pay_time,pay_status,promote_id) VALUES(990,'historical',90,12.34,1500,1,0)",
|
||||
} {
|
||||
if err := db.Exec(query).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
scope := business.LedgerScope{Viewer: business.UserScope{All: true}, Unrestricted: true, IncludeSub: true}
|
||||
filter.Sources = nil
|
||||
registrations, err := repo.Registrations(context.Background(), scope, filter, business.LedgerListOptions{Limit: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spends, err := repo.Spends(context.Background(), scope, filter, business.LedgerListOptions{Limit: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
filter.Sources = []string{"lx", "hw", "historical"}
|
||||
expectedRegistrations, err := repo.Registrations(context.Background(), scope, filter, business.LedgerListOptions{Limit: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedSpends, err := repo.Spends(context.Background(), scope, filter, business.LedgerListOptions{Limit: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(registrations, expectedRegistrations) || !reflect.DeepEqual(spends, expectedSpends) {
|
||||
t.Fatal("omitted platform filter lost historical, official or orphan rows")
|
||||
}
|
||||
if err := db.Exec("INSERT INTO tab_user(id,source_db,register_time,promote_id,puid) VALUES(91,NULL,'1500',0,0)").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
all := business.LedgerFilter{}
|
||||
if err := resolveLedgerSources(db, "tab_user", &all); err != nil || len(all.Sources) != 0 {
|
||||
t.Fatalf("NULL platform must retain unfiltered semantics: %+v %v", all, err)
|
||||
}
|
||||
}
|
||||
|
||||
func prepareLedgerData(t *testing.T) (*gorm.DB, business.LedgerRepo, business.LedgerScope, business.LedgerFilter) {
|
||||
t.Helper()
|
||||
db, _ := prepareChannels(t)
|
||||
|
||||
Reference in New Issue
Block a user