diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-07-14 22:59:18 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-07-14 22:59:18 +0200 |
commit | e99f54c2e653f0662ba0823fa3ac4bccc475dce1 (patch) | |
tree | 7435d0c0d0a15f7709449b7252fdbc603aed6042 | |
parent | 26235bca235c9785752856ab7ebbdf7bf8806be8 (diff) | |
download | refcat-e99f54c2e653f0662ba0823fa3ac4bccc475dce1.tar.gz refcat-e99f54c2e653f0662ba0823fa3ac4bccc475dce1.zip |
reduce: add test
-rw-r--r-- | skate/reduce.go | 39 | ||||
-rw-r--r-- | skate/reduce_test.go | 20 |
2 files changed, 41 insertions, 18 deletions
diff --git a/skate/reduce.go b/skate/reduce.go index ade6a02..d093f5a 100644 --- a/skate/reduce.go +++ b/skate/reduce.go @@ -303,23 +303,8 @@ func ZippyVerifyRefsOpenLibraryTable(olr, refs io.Reader, w io.Writer) error { // release) and writes biblioref. func ZippyVerifyRefsOpenLibrary(olr, refs io.Reader, w io.Writer) error { var ( - enc = json.NewEncoder(xio.NewSingleWriter(w)) - keyer = makeKeyFunc("\t", 1) - cleanIdentifier = func(s string) string { - // Turn ids like /books/OL31189321M into OL31189321M - if s = strings.TrimSpace(s); len(s) == 0 { - return "" - } - var ( - parts = strings.Split(s, "/") - last = parts[len(parts)-1] - ) - if strings.HasPrefix(last, "OL") { - return last - } - log.Printf("warning: unexpected OL id: %s", s) - return "" - } + enc = json.NewEncoder(xio.NewSingleWriter(w)) + keyer = makeKeyFunc("\t", 1) grouper = func(g *zipkey.Group) error { // TODO: For openlibrary and wayback matches, pass through either // unstructured ref string, or CSL JSON @@ -344,7 +329,7 @@ func ZippyVerifyRefsOpenLibrary(olr, refs io.Reader, w io.Writer) error { result := Verify(pivot, ref) switch result.Status { case StatusExact, StatusStrong: - openLibraryWorkID := cleanIdentifier(pivot.WorkID) + openLibraryWorkID := cleanOpenLibraryIdentifier(pivot.WorkID) if openLibraryWorkID == "" { continue } @@ -681,3 +666,21 @@ func parseBiblioref(s string) (r *BiblioRef, err error) { err = json.Unmarshal([]byte(s), &r) return } + +// cleanOpenLibraryIdentifier turns OL ids like /books/OL31189321M into OL31189321M. +func cleanOpenLibraryIdentifier(s string) string { + // XXX: This can be made faster; iterate from the end of the string + // and use a slice. + if s = strings.TrimSpace(s); len(s) == 0 { + return "" + } + var ( + parts = strings.Split(s, "/") + last = parts[len(parts)-1] + ) + if strings.HasPrefix(last, "OL") { + return last + } + log.Printf("warning: unexpected OL id: %s", s) + return "" +} diff --git a/skate/reduce_test.go b/skate/reduce_test.go index ddbca08..9c134f8 100644 --- a/skate/reduce_test.go +++ b/skate/reduce_test.go @@ -510,3 +510,23 @@ func TestCutSep(t *testing.T) { } } } + +func TestCleanOpenLibraryIdentifier(t *testing.T) { + var cases = []struct { + s string + result string + }{ + {"", ""}, + {"/books/OL31189321M", "OL31189321M"}, + {"/b/OL31189321M", "OL31189321M"}, + {"OL31189321M", "OL31189321M"}, + {"OL123", "OL123"}, + {"123", ""}, + } + for _, c := range cases { + result := cleanOpenLibraryIdentifier(c.s) + if result != c.result { + t.Fatalf("got %v, want %v", result, c.result) + } + } +} |