104 lines
3.9 KiB
Go
104 lines
3.9 KiB
Go
package data
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/conf"
|
|
)
|
|
|
|
func newTransactionTestData(t *testing.T) *Data {
|
|
t.Helper()
|
|
name := strings.NewReplacer("/", "_", " ", "_").Replace(t.Name())
|
|
db, err := openWithDriver("sqlite", "file:"+name+"?mode=memory&cache=shared")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = migrateAll(db); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data := &Data{gormDB: newReloadableDB(db, nil), redis: newReloadableRedis(nil), runtime: conf.NewRuntime(&conf.Data{Database: &conf.Data_Database{Driver: "sqlite"}}, &conf.AdminBackend{})}
|
|
t.Cleanup(func() { data.gormDB.close() })
|
|
return data
|
|
}
|
|
|
|
func TestUserAuthorityWritesAreAtomic(t *testing.T) {
|
|
data := newTransactionTestData(t)
|
|
ctx := context.Background()
|
|
if err := data.gormDB.WithContext(ctx).Create(&[]authorityPO{{AuthorityID: 888, AuthorityName: "admin"}, {AuthorityID: 999, AuthorityName: "operator"}}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := &userRepo{data: data}
|
|
user := &biz.User{Username: "atomic", Password: "hash", NickName: "before", AuthorityID: 888, Enable: 1}
|
|
created, err := repo.CreateUserWithAuthorities(ctx, user, []uint{888, 999})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var links int64
|
|
if err = data.gormDB.WithContext(ctx).Model(&userAuthorityPO{}).Where("sys_user_id = ?", created.ID).Count(&links).Error; err != nil || links != 2 {
|
|
t.Fatalf("authority links = %d, err = %v", links, err)
|
|
}
|
|
|
|
created.NickName = "must rollback"
|
|
if err = repo.UpdateUserWithAuthorities(ctx, created, []uint{888, 123456}); err == nil {
|
|
t.Fatal("expected invalid authority update to fail")
|
|
}
|
|
var persisted userPO
|
|
if err = data.gormDB.WithContext(ctx).First(&persisted, created.ID).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if persisted.NickName != "before" {
|
|
t.Fatalf("user update was not rolled back: %q", persisted.NickName)
|
|
}
|
|
|
|
invalid := &biz.User{Username: "invalid", Password: "hash", AuthorityID: 888, Enable: 1}
|
|
if _, err = repo.CreateUserWithAuthorities(ctx, invalid, []uint{888, 123456}); err == nil {
|
|
t.Fatal("expected invalid authority create to fail")
|
|
}
|
|
var users int64
|
|
if err = data.gormDB.WithContext(ctx).Model(&userPO{}).Where("username = ?", "invalid").Count(&users).Error; err != nil || users != 0 {
|
|
t.Fatalf("partial user persisted: count=%d err=%v", users, err)
|
|
}
|
|
}
|
|
|
|
func TestSetUserAuthoritiesRejectsMissingUser(t *testing.T) {
|
|
data := newTransactionTestData(t)
|
|
ctx := context.Background()
|
|
if err := data.gormDB.WithContext(ctx).Create(&authorityPO{AuthorityID: 888, AuthorityName: "admin"}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := &userRepo{data: data}
|
|
if err := repo.SetUserAuthorities(ctx, 999999, []uint{888}); err == nil || err.Error() != "查询用户数据失败" {
|
|
t.Fatalf("expected the compatible missing-user error, got %v", err)
|
|
}
|
|
var links int64
|
|
if err := data.gormDB.WithContext(ctx).Model(&userAuthorityPO{}).Where("sys_user_id = ?", 999999).Count(&links).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if links != 0 {
|
|
t.Fatalf("unexpected authority links for missing user: %d", links)
|
|
}
|
|
}
|
|
|
|
func TestDictionaryImportKeepsHierarchyInOneTransaction(t *testing.T) {
|
|
data := newTransactionTestData(t)
|
|
ctx := context.Background()
|
|
repo := &dictionaryRepo{data: data}
|
|
parentID := uint(10)
|
|
active := true
|
|
dictionary := &biz.Dictionary{Name: "status", Type: "status", Status: &active}
|
|
details := []*biz.DictionaryDetail{{ID: 10, Label: "parent", Value: "1", Status: &active}, {ID: 11, Label: "child", Value: "2", ParentID: &parentID, Status: &active}}
|
|
if err := repo.ImportDictionary(ctx, dictionary, details); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var persisted []dictionaryDetailPO
|
|
if err := data.gormDB.WithContext(ctx).Where("sys_dictionary_id = ?", dictionary.ID).Order("id").Find(&persisted).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(persisted) != 2 || persisted[1].ParentID == nil || *persisted[1].ParentID != persisted[0].ID {
|
|
t.Fatalf("unexpected imported hierarchy: %+v", persisted)
|
|
}
|
|
}
|