package skate import ( "reflect" "testing" "github.com/kr/pretty" ) func TestLineColumn(t *testing.T) { var cases = []struct { line string sep string column int result string }{ {"", "", 2, ""}, {"1 2 3", " ", 1, "1"}, {"1 2 3", " ", 2, "2"}, {"1 2 3", " ", 3, "3"}, {"1 2 3", " ", 4, ""}, {"1 2 3", "\t", 1, "1 2 3"}, } for _, c := range cases { result := CutSep(c.line, c.sep, c.column) if result != c.result { t.Fatalf("got %v, want %v", result, c.result) } } } func TestCutBatch(t *testing.T) { var cases = []struct { lines []string column int result []string }{ { []string{}, 1, nil, }, { []string{}, 9, nil, }, { []string{"1\t2\n", "3\t4\n"}, 2, []string{"2", "4"}, }, } for _, c := range cases { result := CutBatch(c.lines, c.column) if !reflect.DeepEqual(result, c.result) { t.Fatalf("got %v (%d), want %v (%d)", result, len(result), c.result, len(c.result)) } } } func TestUniqueMatches(t *testing.T) { var cases = []struct { about string docs []string result []*BiblioRef err error }{ { about: "missing fields are ignored", docs: []string{`{}`}, result: []*BiblioRef{&BiblioRef{}}, err: nil, }, { about: "a single doc is passed on", docs: []string{`{ "source_release_ident": "s1", "target_release_ident": "t1"}`}, result: []*BiblioRef{&BiblioRef{ SourceReleaseIdent: "s1", TargetReleaseIdent: "t1", }}, err: nil, }, { about: "we want to keep the exact match, if available", docs: []string{` {"source_release_ident": "s1", "target_release_ident": "t1", "match_status": "fuzzy"}`, `{"source_release_ident": "s1", "target_release_ident": "t1", "match_status": "exact"}`, }, result: []*BiblioRef{&BiblioRef{ SourceReleaseIdent: "s1", TargetReleaseIdent: "t1", MatchStatus: "exact", }}, err: nil, }, { about: "if both are exact, we just take (any) one", docs: []string{` {"source_release_ident": "s1", "target_release_ident": "t1", "match_status": "exact", "match_reason": "a"}`, `{"source_release_ident": "s1", "target_release_ident": "t1", "match_status": "exact", "match_reason": "b"}`, }, result: []*BiblioRef{&BiblioRef{ SourceReleaseIdent: "s1", TargetReleaseIdent: "t1", MatchStatus: "exact", MatchReason: "b", }}, err: nil, }, } for _, c := range cases { result, err := uniqueMatches(c.docs, &statsAugment{}) if err != c.err { t.Fatalf("got %v, want %v (%s)", err, c.err, c.about) } if !reflect.DeepEqual(result, c.result) { t.Fatalf("got %#v, want %#v (%s)", pretty.Sprint(result), pretty.Sprint(c.result), c.about) } } } func TestMatchedRefsExtend(t *testing.T) { var cases = []struct { matched []*BiblioRef refs []*Ref result []*BiblioRef }{ { matched: []*BiblioRef{}, refs: []*Ref{}, result: []*BiblioRef{}, }, { matched: []*BiblioRef{ &BiblioRef{ RefIndex: 2, RefKey: "K2", }, }, refs: []*Ref{}, result: []*BiblioRef{ &BiblioRef{ RefIndex: 2, RefKey: "K2", }, }, }, { matched: []*BiblioRef{ &BiblioRef{ SourceReleaseIdent: "pud5shsflfgrth77lmlernavjm", RefIndex: 2, RefKey: "K2", }, }, refs: []*Ref{ &Ref{ ReleaseIdent: "0000", Biblio: Biblio{ Title: "Title", }, Index: 3, Key: "K3", }, }, result: []*BiblioRef{ &BiblioRef{ SourceReleaseIdent: "pud5shsflfgrth77lmlernavjm", RefIndex: 2, RefKey: "K2", }, &BiblioRef{ Key: "0000_3", SourceReleaseIdent: "0000", RefIndex: 3, RefKey: "K3", MatchStatus: StatusUnmatched.Short(), MatchReason: ReasonUnknown.Short(), SourceYear: "0", // Extra: struct { // Ref Ref `json:"ref"` // }{ // Ref: Ref{ // ReleaseIdent: "0000", // Biblio: Biblio{ // Title: "Title", // }, // Index: 3, // Key: "K3", // }, // }, }, }, }, { matched: []*BiblioRef{ &BiblioRef{ SourceReleaseIdent: "pud5shsflfgrth77lmlernavjm", RefIndex: 2, RefKey: "K2", }, }, refs: []*Ref{ &Ref{ ReleaseIdent: "0000", Biblio: Biblio{ Title: "Title", }, Index: 2, Key: "K2", }, }, result: []*BiblioRef{ &BiblioRef{ SourceReleaseIdent: "pud5shsflfgrth77lmlernavjm", RefIndex: 2, RefKey: "K2", }, }, }, } for i, c := range cases { result := matchedRefsExtend(c.matched, c.refs, &statsAugment{}) if !reflect.DeepEqual(result, c.result) { t.Fatalf("[%d]: got %v, want %v (%v)", i+1, result, c.result, pretty.Diff(result, c.result)) } } }