mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-01 06:36:38 +08:00
fb6a3edb89
- Physically purged all legacy internal/ packages, centralized pkg/model/ and pkg/repository/ - Migrated domain models and database repositories into self-contained owner plugins (user, auth, message_gateway, admin, upload, risk_control) - Decoupled cross-plugin interactions via pure core/contracts and typed EventBus - Ensured 100% test coverage pass, zero data races (-race clean), and 0 lint issues in make code-check
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package migrator
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/Rain-kl/Wavelet/pkg/config"
|
|
db "github.com/Rain-kl/Wavelet/pkg/persistence"
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/glebarez/sqlite"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/redis/go-redis/v9/maintnotifications"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const expectedMigratedSystemConfigCount = 35
|
|
|
|
func TestMigrateInitializesSQLiteDatabase(t *testing.T) {
|
|
sqliteDB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
|
|
DisableForeignKeyConstraintWhenMigrating: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("gorm.Open(sqlite) error = %v", err)
|
|
}
|
|
|
|
mr, err := miniredis.Run()
|
|
if err != nil {
|
|
t.Fatalf("miniredis.Run() error = %v", err)
|
|
}
|
|
redisClient := redis.NewClient(&redis.Options{
|
|
Addr: mr.Addr(),
|
|
MaintNotificationsConfig: &maintnotifications.Config{
|
|
Mode: maintnotifications.ModeDisabled,
|
|
},
|
|
})
|
|
|
|
previousDBEnabled := config.Config.Database.Enabled
|
|
config.Config.Database.Enabled = false
|
|
db.SetDB(sqliteDB)
|
|
t.Cleanup(func() {
|
|
config.Config.Database.Enabled = previousDBEnabled
|
|
db.SetDB(nil)
|
|
_ = redisClient.Close()
|
|
mr.Close()
|
|
})
|
|
|
|
Migrate()
|
|
|
|
var systemConfigCount int64
|
|
if err := sqliteDB.Table("w_system_configs").Count(&systemConfigCount).Error; err != nil {
|
|
t.Fatalf("Migrate() count w_system_configs error = %v", err)
|
|
}
|
|
if systemConfigCount != expectedMigratedSystemConfigCount {
|
|
t.Errorf("Migrate() w_system_configs count = %d, want %d", systemConfigCount, expectedMigratedSystemConfigCount)
|
|
}
|
|
|
|
var adminCount int64
|
|
if err := sqliteDB.Table("w_users").Where("username = ?", "admin").Count(&adminCount).Error; err != nil {
|
|
t.Fatalf("Migrate() count admin user error = %v", err)
|
|
}
|
|
if adminCount != 1 {
|
|
t.Errorf("Migrate() admin user count = %d, want %d", adminCount, 1)
|
|
}
|
|
|
|
var templateCount int64
|
|
if err := sqliteDB.Table("w_templates").Count(&templateCount).Error; err != nil {
|
|
t.Fatalf("Migrate() count templates error = %v", err)
|
|
}
|
|
if templateCount != 2 {
|
|
t.Errorf("Migrate() templates count = %d, want %d", templateCount, 2)
|
|
}
|
|
}
|