84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package faqtable
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type dispatcherRepoStub struct {
|
|
items []PendingExport
|
|
exported map[string]string
|
|
failed map[string]string
|
|
}
|
|
|
|
func (s *dispatcherRepoStub) Claim(context.Context, int, int, time.Time) ([]PendingExport, error) {
|
|
items := s.items
|
|
s.items = nil
|
|
return items, nil
|
|
}
|
|
func (s *dispatcherRepoStub) MarkExported(_ context.Context, id, recordID string) error {
|
|
if s.exported == nil {
|
|
s.exported = map[string]string{}
|
|
}
|
|
s.exported[id] = recordID
|
|
return nil
|
|
}
|
|
func (s *dispatcherRepoStub) MarkFailed(_ context.Context, id, code string) error {
|
|
if s.failed == nil {
|
|
s.failed = map[string]string{}
|
|
}
|
|
s.failed[id] = code
|
|
return nil
|
|
}
|
|
|
|
type fieldsInserterStub struct {
|
|
fields map[string]interface{}
|
|
id string
|
|
err error
|
|
}
|
|
|
|
func (s *fieldsInserterStub) InsertFields(_ context.Context, fields map[string]interface{}) (string, error) {
|
|
s.fields = fields
|
|
return s.id, s.err
|
|
}
|
|
|
|
func TestDispatcherExportsFAQAndPersistsRecordID(t *testing.T) {
|
|
repo := &dispatcherRepoStub{items: []PendingExport{{
|
|
CaseID: "case-1",
|
|
Row: Row{StandardQuestion: "中文问题", OriginalQuestion: "中文问题", AskerName: "张三", AskerUnionID: "union-1"},
|
|
}}}
|
|
inserter := &fieldsInserterStub{id: "record-1"}
|
|
d, err := NewDispatcher(repo, inserter, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stats, err := d.RunOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stats.Claimed != 1 || stats.Exported != 1 || stats.Failed != 0 {
|
|
t.Fatalf("stats=%+v", stats)
|
|
}
|
|
if repo.exported["case-1"] != "record-1" || inserter.fields[FieldLanguage] != "Chinese" {
|
|
t.Fatalf("exported=%#v fields=%#v", repo.exported, inserter.fields)
|
|
}
|
|
}
|
|
|
|
func TestDispatcherPersistsOnlyStableFAQFailureCode(t *testing.T) {
|
|
repo := &dispatcherRepoStub{items: []PendingExport{{CaseID: "case-2", Row: Row{StandardQuestion: "secret question", OriginalQuestion: "secret question"}}}}
|
|
inserter := &fieldsInserterStub{err: errors.New("token=secret raw question=secret question")}
|
|
d, err := NewDispatcher(repo, inserter, &DispatcherOptions{SendTimeout: time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stats, err := d.RunOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stats.Failed != 1 || repo.failed["case-2"] != FailureCodeRemoteInsertFailed {
|
|
t.Fatalf("stats=%+v failed=%#v", stats, repo.failed)
|
|
}
|
|
}
|