88 lines
3.2 KiB
Go
88 lines
3.2 KiB
Go
package directory
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type recordingWriter struct {
|
|
calls int
|
|
tenant string
|
|
profiles []Profile
|
|
err error
|
|
}
|
|
|
|
func (w *recordingWriter) UpsertDirectoryProfiles(_ context.Context, tenant string, profiles []Profile) error {
|
|
w.calls++
|
|
w.tenant = tenant
|
|
w.profiles = append([]Profile(nil), profiles...)
|
|
return w.err
|
|
}
|
|
|
|
func TestImportCSVAcceptsTestedSyncOutputAndExtendedFields(t *testing.T) {
|
|
csv := "\ufeffuser_id,union_id,name,nickname,title,department_ids,dept_name,main_department_name,region,employment_status,sync_time,mobile\n" +
|
|
"staff-1,union-1, 张三 ,小张,工程师,100/200,公司/技术支持部,技术支持部,APAC,on_job,2026-07-22 02:30:00,13800000000\n"
|
|
writer := &recordingWriter{}
|
|
count, err := ImportCSV(context.Background(), "tenant-a", strings.NewReader(csv), writer)
|
|
if err != nil {
|
|
t.Fatalf("ImportCSV: %v", err)
|
|
}
|
|
if count != 1 || writer.calls != 1 || writer.tenant != "tenant-a" || len(writer.profiles) != 1 {
|
|
t.Fatalf("count=%d calls=%d tenant=%q profiles=%d", count, writer.calls, writer.tenant, len(writer.profiles))
|
|
}
|
|
p := writer.profiles[0]
|
|
if p.StaffID != "staff-1" || p.Name != "张三" || p.DepartmentName != "公司/技术支持部" || p.MainDepartmentName != "技术支持部" || p.Region != "APAC" || p.EmploymentStatus != "on_job" || p.Source != SourceFullSync {
|
|
t.Fatalf("profile mismatch: %+v", p)
|
|
}
|
|
if len(p.DepartmentIDs) != 2 || p.DepartmentIDs[0] != "100" || p.DepartmentIDs[1] != "200" {
|
|
t.Fatalf("department ids: %v", p.DepartmentIDs)
|
|
}
|
|
// The domain type intentionally has no mobile/email/raw payload fields.
|
|
}
|
|
|
|
func TestImportCSVAcceptsCurrentSevenColumnOutput(t *testing.T) {
|
|
csv := "user_id,union_id,name,nickname,title,department_ids,dept_name\n" +
|
|
"staff-2,union-2,李四,,经理,300,公司/销售部\n"
|
|
writer := &recordingWriter{}
|
|
count, err := ImportCSV(context.Background(), "tenant-a", strings.NewReader(csv), writer)
|
|
if err != nil {
|
|
t.Fatalf("ImportCSV: %v", err)
|
|
}
|
|
if count != 1 || writer.profiles[0].Name != "李四" || writer.profiles[0].Region != "" {
|
|
t.Fatalf("count=%d profile=%+v", count, writer.profiles[0])
|
|
}
|
|
}
|
|
|
|
func TestImportCSVRejectsInvalidSnapshotBeforeWrite(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
csv string
|
|
}{
|
|
{name: "missing required header", csv: "user_id,dept_name\nstaff-1,技术支持部\n"},
|
|
{name: "empty snapshot", csv: "user_id,name,dept_name\n"},
|
|
{name: "empty staff id", csv: "user_id,name,dept_name\n,张三,技术支持部\n"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
writer := &recordingWriter{}
|
|
_, err := ImportCSV(context.Background(), "tenant-a", strings.NewReader(tt.csv), writer)
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if writer.calls != 0 {
|
|
t.Fatalf("writer called %d times before validation", writer.calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestImportCSVPropagatesWriterError(t *testing.T) {
|
|
writer := &recordingWriter{err: errors.New("db unavailable")}
|
|
_, err := ImportCSV(context.Background(), "tenant-a", strings.NewReader("user_id,name,dept_name\nstaff-1,张三,技术支持部\n"), writer)
|
|
if err == nil || writer.calls != 1 {
|
|
t.Fatalf("error=%v calls=%d", err, writer.calls)
|
|
}
|
|
}
|