107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/topfans/backend/pkg/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func setupTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
assert.NoError(t, err)
|
|
err = db.AutoMigrate(&models.AppDownloadConfig{})
|
|
assert.NoError(t, err)
|
|
return db
|
|
}
|
|
|
|
func TestFindByType_NativeApp(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewAppDownloadRepository(db)
|
|
|
|
// seed: 一条 native_app + 一条 wgt
|
|
db.Create(&models.AppDownloadConfig{
|
|
Platform: "android", Type: "native_app", DownloadURL: "https://cdn.example.com/app.apk", Version: "1.0.5",
|
|
})
|
|
db.Create(&models.AppDownloadConfig{
|
|
Platform: "android", Type: "wgt", DownloadURL: "https://cdn.example.com/wgt.wgt", Version: "1.0.5",
|
|
})
|
|
|
|
configs, err := repo.FindByType(context.Background(), models.AppPackageTypeNativeApp)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, configs, 1)
|
|
assert.Equal(t, "native_app", configs[0].Type)
|
|
}
|
|
|
|
func TestFindByType_Wgt(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewAppDownloadRepository(db)
|
|
|
|
db.Create(&models.AppDownloadConfig{
|
|
Platform: "android", Type: "wgt", DownloadURL: "https://cdn.example.com/wgt.wgt", Version: "1.0.5",
|
|
})
|
|
|
|
configs, err := repo.FindByType(context.Background(), models.AppPackageTypeWgt)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, configs, 1)
|
|
assert.Equal(t, "wgt", configs[0].Type)
|
|
}
|
|
|
|
func TestUpsertAll_Insert(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewAppDownloadRepository(db)
|
|
|
|
err := repo.UpsertAll(context.Background(), []models.AppDownloadConfig{
|
|
{Platform: "android", Type: "native_app", DownloadURL: "https://v1.apk", Version: "1.0.0"},
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
var count int64
|
|
db.Model(&models.AppDownloadConfig{}).Count(&count)
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
var cfg models.AppDownloadConfig
|
|
db.First(&cfg)
|
|
assert.Equal(t, "https://v1.apk", cfg.DownloadURL)
|
|
}
|
|
|
|
func TestUpsertAll_Update(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewAppDownloadRepository(db)
|
|
|
|
// 首次插入
|
|
err := repo.UpsertAll(context.Background(), []models.AppDownloadConfig{
|
|
{Platform: "android", Type: "native_app", DownloadURL: "https://v1.apk", Version: "1.0.0"},
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
// 更新同一条
|
|
err = repo.UpsertAll(context.Background(), []models.AppDownloadConfig{
|
|
{Platform: "android", Type: "native_app", DownloadURL: "https://v2.apk", Version: "2.0.0"},
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
// 验证只有 1 条且已更新
|
|
var count int64
|
|
db.Model(&models.AppDownloadConfig{}).Count(&count)
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
var cfg models.AppDownloadConfig
|
|
db.First(&cfg)
|
|
assert.Equal(t, "https://v2.apk", cfg.DownloadURL)
|
|
assert.Equal(t, "2.0.0", cfg.Version)
|
|
}
|
|
|
|
func TestFindByType_Empty(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewAppDownloadRepository(db)
|
|
|
|
configs, err := repo.FindByType(context.Background(), models.AppPackageTypeNativeApp)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, configs, 0)
|
|
}
|