// Package mysqltest provides real wallet-service MySQL repositories for tests. package mysqltest import ( "context" "testing" "time" "hyapp/internal/testutil/mysqlschema" mysqlstorage "hyapp/services/wallet-service/internal/storage/mysql" ) const testCurrency = "coin" // Repository wraps the production MySQL wallet repository with test seed helpers. type Repository struct { *mysqlstorage.Repository t testing.TB schema *mysqlschema.Schema } // NewRepository creates an isolated wallet schema and opens the production repository. func NewRepository(t testing.TB) *Repository { t.Helper() schema := mysqlschema.New(t, mysqlschema.Config{ EnvVar: "WALLET_SERVICE_MYSQL_TEST_DSN", InitDBPath: initDBPath(t), DatabasePrefix: "hy_wallet_test", }) repository, err := mysqlstorage.Open(context.Background(), schema.DSN, testCurrency, 3600) if err != nil { t.Fatalf("open wallet mysql repository failed: %v", err) } wrapped := &Repository{Repository: repository, t: t, schema: schema} t.Cleanup(wrapped.Close) return wrapped } // Close shuts down the repository; mysqlschema owns schema cleanup. func (r *Repository) Close() { r.t.Helper() if r.Repository != nil { _ = r.Repository.Close() } } // SetBalance prepares an account row using the same currency as the test repository. func (r *Repository) SetBalance(userID int64, balance int64) { r.t.Helper() nowMs := time.Now().UnixMilli() _, err := r.schema.DB.ExecContext(context.Background(), ` INSERT INTO wallet_accounts (user_id, currency, balance, version, created_at_ms, updated_at_ms) VALUES (?, ?, ?, 1, ?, ?) ON DUPLICATE KEY UPDATE balance = VALUES(balance), version = version + 1, updated_at_ms = VALUES(updated_at_ms) `, userID, testCurrency, balance, nowMs, nowMs) if err != nil { r.t.Fatalf("seed wallet balance failed: %v", err) } } func initDBPath(t testing.TB) string { t.Helper() return mysqlschema.InitDBPath(t, mysqlschema.CallerFile(t, 1), "..", "..", "..", "deploy", "mysql", "initdb", "001_wallet_service.sql", ) }