aboutsummaryrefslogtreecommitdiffstats
path: root/skate/zippy_test.go
blob: 84c878fc640d61740c1bfdaae2465995c6fe1dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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 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)
		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)
		}
	}
}