71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"hyapp-admin-server/internal/model"
|
|
)
|
|
|
|
func TestNormalizeUserTeamUsesTeamID(t *testing.T) {
|
|
store, mock, closeStore := newRepositorySQLMock(t)
|
|
defer closeStore()
|
|
|
|
mock.ExpectQuery("SELECT \\* FROM `admin_teams` WHERE `admin_teams`.`id` = \\? ORDER BY `admin_teams`.`id` LIMIT \\?").
|
|
WithArgs(model.TeamOperationsID, 1).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "description", "sort", "created_at_ms", "updated_at_ms"}).
|
|
AddRow(model.TeamOperationsID, "运营团队", "运营后台用户", 2, int64(1700000000000), int64(1700000000000)))
|
|
|
|
inputTeamID := model.TeamOperationsID
|
|
teamID, teamName, err := store.NormalizeUserTeam(&inputTeamID, "平台运营")
|
|
if err != nil {
|
|
t.Fatalf("normalize team by id failed: %v", err)
|
|
}
|
|
if teamID == nil || *teamID != model.TeamOperationsID || teamName != "运营团队" {
|
|
t.Fatalf("normalized team mismatch: id=%v name=%q", teamID, teamName)
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatalf("sql expectations mismatch: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeUserTeamKeepsLegacyNameWhenTeamMissing(t *testing.T) {
|
|
store, mock, closeStore := newRepositorySQLMock(t)
|
|
defer closeStore()
|
|
|
|
mock.ExpectQuery("SELECT \\* FROM `admin_teams` WHERE name = \\? ORDER BY `admin_teams`.`id` LIMIT \\?").
|
|
WithArgs("外包团队", 1).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "description", "sort", "created_at_ms", "updated_at_ms"}))
|
|
|
|
teamID, teamName, err := store.NormalizeUserTeam(nil, " 外包团队 ")
|
|
if err != nil {
|
|
t.Fatalf("normalize legacy team failed: %v", err)
|
|
}
|
|
if teamID != nil || teamName != "外包团队" {
|
|
t.Fatalf("legacy team should be preserved, got id=%v name=%q", teamID, teamName)
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatalf("sql expectations mismatch: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTeamAndChildIDsReturnsParentAndChildren(t *testing.T) {
|
|
store, mock, closeStore := newRepositorySQLMock(t)
|
|
defer closeStore()
|
|
|
|
mock.ExpectQuery("SELECT `id` FROM `admin_teams` WHERE parent_id = \\? ORDER BY sort ASC, id ASC").
|
|
WithArgs(model.TeamOperationsID).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(11).AddRow(12))
|
|
|
|
ids, err := store.TeamAndChildIDs(model.TeamOperationsID)
|
|
if err != nil {
|
|
t.Fatalf("load team ids failed: %v", err)
|
|
}
|
|
if len(ids) != 3 || ids[0] != model.TeamOperationsID || ids[1] != 11 || ids[2] != 12 {
|
|
t.Fatalf("team ids mismatch: %#v", ids)
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatalf("sql expectations mismatch: %v", err)
|
|
}
|
|
}
|