test(server): mysql 集成测试对齐 codes 库新表(断言不变,环境搭建换 ApplyMigrations)(#codes-lib)
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
|||||||
tcredis "github.com/testcontainers/testcontainers-go/modules/redis"
|
tcredis "github.com/testcontainers/testcontainers-go/modules/redis"
|
||||||
|
|
||||||
"github.com/wangjia/pangolin/server/internal/codes"
|
"github.com/wangjia/pangolin/server/internal/codes"
|
||||||
|
"github.com/wangjia/pangolin/server/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@@ -102,28 +103,6 @@ func applySchema(db *sql.DB) error {
|
|||||||
ad_gate BOOLEAN NOT NULL DEFAULT FALSE
|
ad_gate BOOLEAN NOT NULL DEFAULT FALSE
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
||||||
|
|
||||||
`CREATE TABLE IF NOT EXISTS code_batches (
|
|
||||||
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
channel ENUM('store','tg','line','manual') NOT NULL,
|
|
||||||
created_by VARCHAR(64) NOT NULL,
|
|
||||||
note VARCHAR(255) NULL,
|
|
||||||
created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
|
||||||
|
|
||||||
`CREATE TABLE IF NOT EXISTS codes (
|
|
||||||
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
code_hash CHAR(64) NOT NULL UNIQUE,
|
|
||||||
plan_id BIGINT UNSIGNED NOT NULL,
|
|
||||||
duration_days INT NOT NULL,
|
|
||||||
batch_id BIGINT UNSIGNED NOT NULL,
|
|
||||||
status ENUM('unused','redeemed','void') NOT NULL DEFAULT 'unused',
|
|
||||||
redeemed_by BIGINT UNSIGNED NULL,
|
|
||||||
redeemed_at DATETIME(6) NULL,
|
|
||||||
FOREIGN KEY (plan_id) REFERENCES plans(id),
|
|
||||||
FOREIGN KEY (batch_id) REFERENCES code_batches(id),
|
|
||||||
INDEX idx_status (status)
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
|
||||||
|
|
||||||
`CREATE TABLE IF NOT EXISTS subscriptions (
|
`CREATE TABLE IF NOT EXISTS subscriptions (
|
||||||
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||||
user_id BIGINT UNSIGNED NOT NULL,
|
user_id BIGINT UNSIGNED NOT NULL,
|
||||||
@@ -154,10 +133,17 @@ func applySchema(db *sql.DB) error {
|
|||||||
return fmt.Errorf("schema exec: %w\nSQL: %s", err, stmt)
|
return fmt.Errorf("schema exec: %w\nSQL: %s", err, stmt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply the shared codes library migrations (creates codes_batches, codes, codes_audit_log).
|
||||||
|
if err := store.ApplyCodesLibMigrations(context.Background(), db, "mysql"); err != nil {
|
||||||
|
return fmt.Errorf("apply codes lib migrations: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// insertCode inserts a test code directly into the DB and returns it.
|
// insertCode inserts a test code directly into the new codes_batches and codes
|
||||||
|
// tables (library schema) for testing.
|
||||||
func insertCode(t *testing.T, db *sql.DB, plaintext string, plan codes.PlanCode, durationDays int) {
|
func insertCode(t *testing.T, db *sql.DB, plaintext string, plan codes.PlanCode, durationDays int) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
canonical, err := codes.Canonicalize(plaintext)
|
canonical, err := codes.Canonicalize(plaintext)
|
||||||
@@ -166,13 +152,14 @@ func insertCode(t *testing.T, db *sql.DB, plaintext string, plan codes.PlanCode,
|
|||||||
}
|
}
|
||||||
hash := codes.Hash(canonical)
|
hash := codes.Hash(canonical)
|
||||||
|
|
||||||
var planID int64
|
// Create the entitlement payload JSON for a duration entitlement.
|
||||||
if err := db.QueryRow(`SELECT id FROM plans WHERE code=?`, string(plan)).Scan(&planID); err != nil {
|
payload := fmt.Sprintf(`{"plan":"%s","days":%d}`, string(plan), durationDays)
|
||||||
t.Fatalf("plan lookup: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Insert batch into codes_batches.
|
||||||
_, err = db.Exec(
|
_, err = db.Exec(
|
||||||
`INSERT INTO code_batches (channel, created_by, created_at) VALUES ('manual','test',UTC_TIMESTAMP(6))`,
|
`INSERT INTO codes_batches (channel, entitlement_kind, entitlement_payload, created_by, created_at)
|
||||||
|
VALUES ('manual', 'duration', ?, 'test', UTC_TIMESTAMP(6))`,
|
||||||
|
payload,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("batch insert: %v", err)
|
t.Fatalf("batch insert: %v", err)
|
||||||
@@ -180,9 +167,11 @@ func insertCode(t *testing.T, db *sql.DB, plaintext string, plan codes.PlanCode,
|
|||||||
var batchID int64
|
var batchID int64
|
||||||
db.QueryRow(`SELECT LAST_INSERT_ID()`).Scan(&batchID)
|
db.QueryRow(`SELECT LAST_INSERT_ID()`).Scan(&batchID)
|
||||||
|
|
||||||
|
// Insert code into codes.
|
||||||
_, err = db.Exec(
|
_, err = db.Exec(
|
||||||
`INSERT INTO codes (code_hash, plan_id, duration_days, batch_id) VALUES (?,?,?,?)`,
|
`INSERT INTO codes (code_hash, batch_id, entitlement_kind, entitlement_payload, status, created_at)
|
||||||
hash, planID, durationDays, batchID,
|
VALUES (?, ?, 'duration', ?, 'unused', UTC_TIMESTAMP(6))`,
|
||||||
|
hash, batchID, payload,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("code insert: %v", err)
|
t.Fatalf("code insert: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user