fix(setup): 数据库有用户时跳过管理员引导
This commit is contained in:
51
backend/internal/setup/setup_test.go
Normal file
51
backend/internal/setup/setup_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package setup
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDecideAdminBootstrap(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
totalUsers int64
|
||||
adminUsers int64
|
||||
should bool
|
||||
reason string
|
||||
}{
|
||||
{
|
||||
name: "empty database should create admin",
|
||||
totalUsers: 0,
|
||||
adminUsers: 0,
|
||||
should: true,
|
||||
reason: adminBootstrapReasonEmptyDatabase,
|
||||
},
|
||||
{
|
||||
name: "admin exists should skip",
|
||||
totalUsers: 10,
|
||||
adminUsers: 1,
|
||||
should: false,
|
||||
reason: adminBootstrapReasonAdminExists,
|
||||
},
|
||||
{
|
||||
name: "users exist without admin should skip",
|
||||
totalUsers: 5,
|
||||
adminUsers: 0,
|
||||
should: false,
|
||||
reason: adminBootstrapReasonUsersExistWithoutAdmin,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got := decideAdminBootstrap(tc.totalUsers, tc.adminUsers)
|
||||
if got.shouldCreate != tc.should {
|
||||
t.Fatalf("shouldCreate=%v, want %v", got.shouldCreate, tc.should)
|
||||
}
|
||||
if got.reason != tc.reason {
|
||||
t.Fatalf("reason=%q, want %q", got.reason, tc.reason)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user