Files
tiennm99bot/internal/modules/stats/startup_mongo_test.go
T
tiennm99 5e96490254 refactor: remove completed one-time startup migrations
Production data is migrated and every marker is recorded in the system
collection, so the stock dividend-history, sticker legacy-pack and stats
stock_dividend migrations no longer do anything. Stats keeps its startup
index creation and still hides retired stock_dividend rows.
2026-10-08 10:35:21 +07:00

112 lines
3.2 KiB
Go

package stats
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/tiennm99/tiennm99bot/internal/storage"
"github.com/tiennm99/tiennm99bot/internal/testutil/mongotest"
)
var mongoTests mongotest.Manager
func TestMain(m *testing.M) {
os.Exit(mongoTests.Run(m))
}
func TestInitStore_MongoCreatesIndexes(t *testing.T) {
ctx, statsColl := setupMongoStatsTest(t)
rawStatsColl, ok := storage.MongoCollection(statsColl)
if !ok {
t.Fatal("stats collection is not Mongo-backed")
}
docs := storage.Typed[usageEntry](statsColl)
if err := docs.Put(ctx, "stock_dividend", usageEntry{Cmd: "stock_dividend", N: 9, Deleted: true}); err != nil {
t.Fatalf("seed anonymous stats: %v", err)
}
if err := docs.Put(ctx, "stock_dividend:7", usageEntry{Cmd: "stock_dividend", UserID: 7, Username: "alice", N: 4, Deleted: true}); err != nil {
t.Fatalf("seed user stats: %v", err)
}
if err := docs.Put(ctx, "stock_dividend_extra", usageEntry{Cmd: "stock_dividend_extra", N: 3}); err != nil {
t.Fatalf("seed prefix stats: %v", err)
}
if err := InitStore(ctx, statsColl); err != nil {
t.Fatalf("InitStore: %v", err)
}
if err := InitStore(ctx, statsColl); err != nil {
t.Fatalf("InitStore second run: %v", err)
}
store := newUsageStore(statsColl)
if rows, err := store.TopCommands(ctx, 10); err != nil || len(rows) != 1 || rows[0].display != "/stock_dividend_extra" || rows[0].n != 3 {
t.Fatalf("top commands with deleted rows = %+v, err=%v", rows, err)
}
if rows, err := store.TopUsers(ctx, 10); err != nil || len(rows) != 0 {
t.Fatalf("deleted top users = %+v, err=%v", rows, err)
}
if rows, err := store.UsersByCommand(ctx, "stock_dividend", 10); err != nil || len(rows) != 0 {
t.Fatalf("deleted users = %+v, err=%v", rows, err)
}
cur, err := rawStatsColl.Indexes().List(ctx)
if err != nil {
t.Fatalf("list indexes: %v", err)
}
defer func() { _ = cur.Close(ctx) }()
found := map[string]bool{}
for cur.Next(ctx) {
var doc struct {
Name string `bson:"name"`
}
if err := cur.Decode(&doc); err != nil {
t.Fatalf("decode index: %v", err)
}
found[doc.Name] = true
}
if err := cur.Err(); err != nil {
t.Fatalf("index cursor: %v", err)
}
for _, name := range []string{statsCommandUsersIndexName, statsUserCommandsIndexName, statsUsernameLookupIndexName} {
if !found[name] {
t.Fatalf("missing index %s; indexes=%v", name, found)
}
}
}
func setupMongoStatsTest(t *testing.T) (context.Context, storage.Collection) {
t.Helper()
uri := mongoTests.URI(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
t.Cleanup(cancel)
client, err := storage.NewMongoClient(ctx, uri)
if err != nil {
t.Fatalf("NewMongoClient: %v", err)
}
dbName := fmt.Sprintf("tiennm99bot_stats_test_%d", time.Now().UnixNano())
db := client.Database(dbName)
t.Cleanup(func() {
cleanupCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = db.Drop(cleanupCtx)
_ = client.Disconnect(cleanupCtx)
})
provider := storage.NewMongoProvider(db)
return ctx, provider.Collection("stats")
}
func TestInc_MongoUsernameMoveClearsOldHolder(t *testing.T) {
_, statsColl := setupMongoStatsTest(t)
assertUsernameMoveClearsOldHolder(t, statsColl)
}