package douyin import ( "context" "testing" "bindbox-game/internal/pkg/logger" "bindbox-game/internal/repository/mysql" "bindbox-game/internal/repository/mysql/model" ) func TestResolveActivityAttribution_ChannelCodePriority(t *testing.T) { repo, err := mysql.NewSQLiteRepoForTest() if err != nil { t.Fatal(err) } ddls := []string{ `CREATE TABLE livestream_activities ( id INTEGER PRIMARY KEY, channel_id INTEGER, channel_code TEXT )`, `CREATE TABLE channels ( id INTEGER PRIMARY KEY, code TEXT )`, } for _, ddl := range ddls { if err := repo.GetDbW().Exec(ddl).Error; err != nil { t.Fatal(err) } } seeds := []string{ `INSERT INTO channels (id, code) VALUES (11, 'A001')`, `INSERT INTO channels (id, code) VALUES (12, 'OLD12')`, `INSERT INTO channels (id, code) VALUES (19, 'B009')`, `INSERT INTO channels (id, code) VALUES (21, 'IDONLY')`, `INSERT INTO livestream_activities (id, channel_id, channel_code) VALUES (1, 0, 'A001')`, `INSERT INTO livestream_activities (id, channel_id, channel_code) VALUES (2, 12, '')`, `INSERT INTO livestream_activities (id, channel_id, channel_code) VALUES (3, 12, 'B009')`, `INSERT INTO livestream_activities (id, channel_id, channel_code) VALUES (4, 0, ' A001 ')`, `INSERT INTO livestream_activities (id, channel_id, channel_code) VALUES (5, 12, 'NOT_EXIST')`, `INSERT INTO livestream_activities (id, channel_id, channel_code) VALUES (6, 21, NULL)`, } for _, seed := range seeds { if err := repo.GetDbW().Exec(seed).Error; err != nil { t.Fatal(err) } } lg, err := logger.NewCustomLogger(logger.WithOutputInConsole()) if err != nil { t.Fatal(err) } svc := New(lg, repo, nil, nil, nil, nil).(*service) logs := []model.LivestreamDrawLogs{ {ActivityID: 1}, {ActivityID: 2}, {ActivityID: 3}, {ActivityID: 4}, {ActivityID: 5}, {ActivityID: 6}, {ActivityID: 0}, // should be ignored } got := svc.resolveActivityAttribution(context.Background(), logs) assertAttribution(t, got, 1, 11, "A001") assertAttribution(t, got, 2, 12, "OLD12") assertAttribution(t, got, 3, 19, "B009") assertAttribution(t, got, 4, 11, "A001") assertAttribution(t, got, 5, 12, "NOT_EXIST") assertAttribution(t, got, 6, 21, "IDONLY") } func assertAttribution(t *testing.T, got map[int64]activityAttribution, activityID, wantChannelID int64, wantChannelCode string) { t.Helper() item, ok := got[activityID] if !ok { t.Fatalf("activity %d attribution not found", activityID) } if item.channelID != wantChannelID || item.channelCode != wantChannelCode { t.Fatalf( "activity %d attribution mismatch: got{id=%d,code=%q} want{id=%d,code=%q}", activityID, item.channelID, item.channelCode, wantChannelID, wantChannelCode, ) } }