package httpapi_test import ( "database/sql" "fmt" "net/http" "strings" "testing" "git.yeij.top/AskaEth/Scoreboard/server/internal/httpapi" ) // 单位增删改必须写 change_log,否则 broadcastChanges 推空、大屏单位名不刷新。 func TestTeamWritesWriteChangeLogAndBroadcast(t *testing.T) { fake := &fakeRealtime{} env := newTestEnvWith(t, func(o *httpapi.Options) { o.Realtime = fake }) fake.db = env.ev env.createUser("recorder", "recorder-pass", "recorder") client, _ := env.client() env.login(client, "recorder", "recorder-pass") status, env2 := env.do(client, http.MethodPost, "/api/teams", map[string]any{ "code": "C", "name": "第三分公司", "active": true, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("建单位应 200,得到 %d %+v", status, env2.Error) } if got := changeLogCount(t, env, "teams", "upsert"); got != 1 { t.Fatalf("建单位应写 1 条 teams change_log,得到 %d", got) } created := decodeData[map[string]any](t, env2) teamID := int64(created["id"].(float64)) found := false for _, ch := range fake.firstBatch(t) { if ch.Entity == "teams" && ch.Action == "upsert" && ch.ID == teamID { found = true } } if !found { t.Fatalf("建单位后应广播 teams 变更") } if status, env2 = env.do(client, http.MethodDelete, fmt.Sprintf("/api/teams/%d", teamID), nil, true); status != http.StatusOK { t.Fatalf("删单位应 200,得到 %d %+v", status, env2.Error) } if got := changeLogCount(t, env, "teams", "void"); got != 1 { t.Fatalf("删单位应写 1 条 teams void 变更,得到 %d", got) } } // 项目、组别、通道的写操作同样要推 change_log,大屏才会刷新。 func TestEventWritesWriteChangeLog(t *testing.T) { env := newTestEnv(t) ids := env.seed(t) env.createUser("recorder", "recorder-pass", "recorder") client, _ := env.client() env.login(client, "recorder", "recorder-pass") status, env2 := env.do(client, http.MethodPost, "/api/events", map[string]any{ "sportId": ids.sport, "code": "M400", "name": "400米跑", "eventType": "individual", "resultType": "time", "compare": "asc", "scored": true, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("建项目应 200,得到 %d %+v", status, env2.Error) } if got := changeLogCount(t, env, "events", "upsert"); got != 1 { t.Fatalf("建项目应写 1 条 events change_log,得到 %d", got) } created := decodeData[map[string]any](t, env2) eventID := int64(created["id"].(float64)) status, env2 = env.do(client, http.MethodPut, fmt.Sprintf("/api/events/%d/categories", eventID), map[string]any{ "categories": []map[string]any{{"name": "男子", "multiplier": 1}}, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("替换组别应 200,得到 %d %+v", status, env2.Error) } if got := changeLogCount(t, env, "events", "upsert"); got != 2 { t.Fatalf("替换组别应再写 1 条 events change_log,得到 %d", got) } if status, env2 = env.do(client, http.MethodDelete, fmt.Sprintf("/api/events/%d", eventID), nil, true); status != http.StatusOK { t.Fatalf("删项目应 200,得到 %d %+v", status, env2.Error) } if got := changeLogCount(t, env, "events", "void"); got != 1 { t.Fatalf("删项目应写 1 条 events void 变更,得到 %d", got) } } // 小项类型新增「个人+团体」:REST 写接口要能存下来并原样返回,不被当成未知类型。 func TestEventAcceptsIndividualTeamType(t *testing.T) { env := newTestEnv(t) ids := env.seed(t) env.createUser("recorder", "recorder-pass", "recorder") client, _ := env.client() env.login(client, "recorder", "recorder-pass") status, env2 := env.do(client, http.MethodPost, "/api/events", map[string]any{ "sportId": ids.sport, "code": "TEAM_PULLUP", "name": "团体引体向上", "eventType": "individual_team", "resultType": "count", "compare": "desc", "scored": true, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("建「个人+团体」项目应 200,得到 %d %+v", status, env2.Error) } created := decodeData[map[string]any](t, env2) if created["eventType"] != "individual_team" { t.Fatalf("建回的 eventType = %v,期望 individual_team", created["eventType"]) } eventID := int64(created["id"].(float64)) status, env2 = env.do(client, http.MethodGet, fmt.Sprintf("/api/events/%d", eventID), nil, true) if status != http.StatusOK || !env2.OK { t.Fatalf("读项目应 200,得到 %d %+v", status, env2.Error) } if got := decodeData[map[string]any](t, env2); got["eventType"] != "individual_team" { t.Fatalf("读回的 eventType = %v,期望 individual_team", got["eventType"]) } status, env2 = env.do(client, http.MethodPut, fmt.Sprintf("/api/events/%d", eventID), map[string]any{ "sportId": ids.sport, "code": "TEAM_PULLUP", "name": "团体引体向上", "eventType": "team", "resultType": "count", "compare": "desc", "scored": true, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("改回团体应 200,得到 %d %+v", status, env2.Error) } if got := decodeData[map[string]any](t, env2); got["eventType"] != "team" { t.Fatalf("改回的 eventType = %v,期望 team", got["eventType"]) } } // 证件号是人员的唯一标识:可读写、重复 409、空值不互斥(部分唯一索引)。 func TestAthleteIDCardUnique(t *testing.T) { env := newTestEnv(t) ids := env.seed(t) env.createUser("recorder", "recorder-pass", "recorder") client, _ := env.client() env.login(client, "recorder", "recorder-pass") status, env2 := env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "张三", "gender": "男", "teamId": ids.teamA, "idCard": "110101199001011234", "active": true, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("建人员应 200,得到 %d %+v", status, env2.Error) } created := decodeData[map[string]any](t, env2) if created["idCard"] != "110101199001011234" { t.Fatalf("建人员应返回 idCard: %+v", created) } athleteID := int64(created["id"].(float64)) status, env2 = env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "李四", "gender": "男", "teamId": ids.teamA, "idCard": "110101199001011234", "active": true, }, true) if status != http.StatusConflict || env2.Error == nil || env2.Error.Code != "CONFLICT" { t.Fatalf("重复证件号应 409 CONFLICT,得到 %d %+v", status, env2.Error) } status, env2 = env.do(client, http.MethodPut, fmt.Sprintf("/api/athletes/%d", ids.athB), map[string]any{ "name": "李强", "gender": "男", "teamId": ids.teamB, "idCard": "110101199001011234", "active": true, }, true) if status != http.StatusConflict || env2.Error == nil || env2.Error.Code != "CONFLICT" { t.Fatalf("改人员到重复证件号应 409,得到 %d %+v", status, env2.Error) } // 空证件号是合法的(历史数据),多条空值不能互相冲突。 status, env2 = env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "王五", "gender": "女", "teamId": ids.teamA, "active": true, }, true) if status != http.StatusOK { t.Fatalf("空证件号应可建,得到 %d %+v", status, env2.Error) } env.exec(t, `INSERT INTO athletes (name, gender, team_id, id_card, active) VALUES ('甲', '男', ?, '', 1)`, ids.teamA) env.exec(t, `INSERT INTO athletes (name, gender, team_id, id_card, active) VALUES ('乙', '男', ?, '', 1)`, ids.teamA) if _, err := env.ev.Exec( `INSERT INTO athletes (name, team_id, id_card, active) VALUES ('丙', ?, '110101199001011234', 1)`, ids.teamA); err == nil { t.Fatalf("唯一索引应拒绝重复证件号") } status, env2 = env.do(client, http.MethodGet, fmt.Sprintf("/api/athletes/%d", athleteID), nil, false) got := decodeData[map[string]any](t, env2) if status != http.StatusOK || got["idCard"] != "110101199001011234" { t.Fatalf("读人员应带 idCard,得到 %d %+v", status, got) } } // 号码布与证件号同为唯一标识:可读写、重复 409(文案区分字段)、空值不互斥。 func TestAthleteBibUnique(t *testing.T) { env := newTestEnv(t) ids := env.seed(t) env.createUser("recorder", "recorder-pass", "recorder") client, _ := env.client() env.login(client, "recorder", "recorder-pass") status, env2 := env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "张三", "gender": "男", "teamId": ids.teamA, "bib": "A001", "active": true, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("仅给号码布应能建人员,得到 %d %+v", status, env2.Error) } created := decodeData[map[string]any](t, env2) if created["bib"] != "A001" { t.Fatalf("建人员应返回 bib: %+v", created) } athleteID := int64(created["id"].(float64)) status, env2 = env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "李四", "gender": "男", "teamId": ids.teamA, "bib": "A001", "active": true, }, true) if status != http.StatusConflict || env2.Error == nil || env2.Error.Code != "CONFLICT" { t.Fatalf("重复号码布应 409 CONFLICT,得到 %d %+v", status, env2.Error) } if !strings.Contains(env2.Error.Message, "号码布") { t.Fatalf("409 文案应点明号码布,得到 %+v", env2.Error) } status, env2 = env.do(client, http.MethodPut, fmt.Sprintf("/api/athletes/%d", ids.athB), map[string]any{ "name": "李强", "gender": "男", "teamId": ids.teamB, "bib": "A001", "active": true, }, true) if status != http.StatusConflict || env2.Error == nil || env2.Error.Code != "CONFLICT" { t.Fatalf("改人员到重复号码布应 409,得到 %d %+v", status, env2.Error) } // 空号码布是合法的,多条空值不能互相冲突;唯一索引兜底重复写入。 status, env2 = env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "王五", "gender": "女", "teamId": ids.teamA, "active": true, }, true) if status != http.StatusOK { t.Fatalf("空号码布应可建,得到 %d %+v", status, env2.Error) } env.exec(t, `INSERT INTO athletes (name, gender, team_id, bib, active) VALUES ('甲', '男', ?, '', 1)`, ids.teamA) env.exec(t, `INSERT INTO athletes (name, gender, team_id, bib, active) VALUES ('乙', '男', ?, '', 1)`, ids.teamA) if _, err := env.ev.Exec( `INSERT INTO athletes (name, team_id, bib, active) VALUES ('丙', ?, 'A001', 1)`, ids.teamA); err == nil { t.Fatalf("唯一索引应拒绝重复号码布") } status, env2 = env.do(client, http.MethodGet, fmt.Sprintf("/api/athletes/%d", athleteID), nil, false) got := decodeData[map[string]any](t, env2) if status != http.StatusOK || got["bib"] != "A001" { t.Fatalf("读人员应带 bib,得到 %d %+v", status, got) } } // 年龄可读可写;越界或非整数返回 400,不填时为空。 func TestAthleteAgeReadWriteAndRange(t *testing.T) { env := newTestEnv(t) ids := env.seed(t) env.createUser("recorder", "recorder-pass", "recorder") client, _ := env.client() env.login(client, "recorder", "recorder-pass") status, env2 := env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "张三", "gender": "男", "teamId": ids.teamA, "age": 30, "active": true, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("建人员应 200,得到 %d %+v", status, env2.Error) } created := decodeData[map[string]any](t, env2) if created["age"] != float64(30) { t.Fatalf("建人员应返回 age: %+v", created) } athleteID := int64(created["id"].(float64)) status, env2 = env.do(client, http.MethodGet, fmt.Sprintf("/api/athletes/%d", athleteID), nil, false) if got := decodeData[map[string]any](t, env2); status != http.StatusOK || got["age"] != float64(30) { t.Fatalf("读人员应带 age,得到 %d %+v", status, got) } // 边界值 0 与 120 合法。 status, env2 = env.do(client, http.MethodPut, fmt.Sprintf("/api/athletes/%d", athleteID), map[string]any{ "name": "张三", "gender": "男", "teamId": ids.teamA, "age": 0, "active": true, }, true) if status != http.StatusOK || decodeData[map[string]any](t, env2)["age"] != float64(0) { t.Fatalf("age=0 应可保存,得到 %d %+v", status, env2.Error) } status, env2 = env.do(client, http.MethodPut, fmt.Sprintf("/api/athletes/%d", athleteID), map[string]any{ "name": "张三", "gender": "男", "teamId": ids.teamA, "age": 120, "active": true, }, true) if status != http.StatusOK || decodeData[map[string]any](t, env2)["age"] != float64(120) { t.Fatalf("age=120 应可保存,得到 %d %+v", status, env2.Error) } for _, age := range []int{-1, 121} { status, env2 = env.do(client, http.MethodPut, fmt.Sprintf("/api/athletes/%d", athleteID), map[string]any{ "name": "张三", "gender": "男", "teamId": ids.teamA, "age": age, "active": true, }, true) if status != http.StatusBadRequest || env2.Error == nil || env2.Error.Code != "VALIDATION_FAILED" { t.Fatalf("age=%d 应 400,得到 %d %+v", age, status, env2.Error) } } status, env2 = env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "李四", "gender": "男", "teamId": ids.teamA, "age": 121, "active": true, }, true) if status != http.StatusBadRequest { t.Fatalf("新建年龄越界应 400,得到 %d %+v", status, env2.Error) } status, env2 = env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "赵六", "gender": "男", "teamId": ids.teamA, "age": "abc", "active": true, }, true) if status != http.StatusBadRequest { t.Fatalf("年龄非整数应 400,得到 %d %+v", status, env2.Error) } // 不填年龄是合法的,返回空值。 status, env2 = env.do(client, http.MethodPost, "/api/athletes", map[string]any{ "name": "王五", "gender": "女", "teamId": ids.teamA, "active": true, }, true) if status != http.StatusOK { t.Fatalf("不填年龄应可建,得到 %d %+v", status, env2.Error) } if created = decodeData[map[string]any](t, env2); created["age"] != nil { t.Fatalf("没填年龄应返回 null: %+v", created) } var age sql.NullInt64 if err := env.ev.QueryRow(`SELECT age FROM athletes WHERE id = ?`, athleteID).Scan(&age); err != nil { t.Fatalf("查询人员年龄: %v", err) } if !age.Valid || age.Int64 != 120 { t.Fatalf("越界的 400 不该改库中年龄,得到 %+v", age) } }