aboutsummaryrefslogtreecommitdiffstats
path: root/skate/zippy_test.go
blob: f1e8822f0a326005bc9667f07ccb3c22721770b3 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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{`{
			    "_id": "s1_0",
				"source_release_ident": "s1",
			    "target_release_ident": "t1"}`},
			result: []*BiblioRef{&BiblioRef{
				Key:                "s1_0",
				SourceReleaseIdent: "s1",
				TargetReleaseIdent: "t1",
			}},
			err: nil,
		},
		{
			about: "we want to keep the exact match, if available",
			docs: []string{`
			{"_id": "s1_0",
			 "source_release_ident": "s1",
			 "target_release_ident": "t1",
		     "match_status": "fuzzy"}`,
				`{"_id": "s1_1",
				  "source_release_ident": "s1",
			      "target_release_ident": "t1",
			      "match_status": "exact"}`,
			},
			result: []*BiblioRef{&BiblioRef{
				Key:                "s1_1",
				SourceReleaseIdent: "s1",
				TargetReleaseIdent: "t1",
				MatchStatus:        "exact",
			}},
			err: nil,
		},
		{
			about: "if both are exact, we just take (any) one",
			docs: []string{`
			{"_id": "s1_0",
			 "source_release_ident": "s1",
			 "target_release_ident": "t1",
		     "match_status": "exact",
			 "match_reason": "a"}`,
				`{"_id": "s1_1",
				  "source_release_ident": "s1",
			      "target_release_ident": "t1",
			      "match_status": "exact",
			      "match_reason": "b"}`,
			},
			result: []*BiblioRef{&BiblioRef{
				Key:                "s1_1",
				SourceReleaseIdent: "s1",
				TargetReleaseIdent: "t1",
				MatchStatus:        "exact",
				MatchReason:        "b",
			}},
			err: nil,
		},
		{
			about: "regression; a buggy sort?",
			docs: []string{`
			{"_id": "s1_0",
			 "source_release_ident": "s1",
			 "target_release_ident": "t1",
		     "match_status": "exact",
			 "match_reason": "a"}`,
				`{"_id": "s1_1",
				  "source_release_ident": "s1",
			      "target_release_ident": "t1",
			      "match_status": "fuzzy",
			      "match_reason": "b"}`,
			},
			result: []*BiblioRef{&BiblioRef{
				Key:                "s1_0",
				SourceReleaseIdent: "s1",
				TargetReleaseIdent: "t1",
				MatchStatus:        "exact",
				MatchReason:        "a",
			}},
			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{})
		for _, v := range result {
			v.IndexedTs = "" // we do not want to mock out time, now
		}
		if !reflect.DeepEqual(result, c.result) {
			t.Fatalf("[%d]: got %v, want %v (%v)",
				i+1, result, c.result, pretty.Diff(result, c.result))
		}
	}
}