Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Apr 24, 2024
1 parent 5f8fc3d commit 4f68a5a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
12 changes: 7 additions & 5 deletions authorutils/authorutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ func IsPersonalName(name string) bool {
}

// check for suffixes, e.g. "John Smith, MD"
suffix := strings.Split(name, ", ")[1]
suffixes := []string{"MD", "PhD", "BS"}
for _, s := range suffixes {
if suffix == s {
return true
suffix := strings.Split(name, ", ")
if len(suffix) > 1 {
suffixes := []string{"MD", "PhD", "BS"}
for _, s := range suffixes {
if suffix[1] == s {
return true
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions authorutils/authorutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package authorutils_test

import (
"testing"

"github.com/front-matter/commonmeta/authorutils"
)

func TestIsPersonalName(t *testing.T) {
t.Parallel()
type testCase struct {
input string
want bool
}
testCases := []testCase{
{input: "John Doe", want: false},
{input: "Harvard University", want: false},
{input: "LiberateScience", want: false},
{input: "Jane Smith, MD", want: true},
{input: "John", want: false},
}
for _, tc := range testCases {
got := authorutils.IsPersonalName(tc.input)
if tc.want != got {
t.Errorf("Is Personal Name(%v): want %v, got %v",
tc.input, tc.want, got)
}
}
}
2 changes: 1 addition & 1 deletion crossref/crossref.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ func QueryURL(number int, member string, _type string, sample bool, hasORCID boo
return u.String()
}

// Get the Crossref member name for a given member_id
// Get the Crossref member name for a given memberId
func GetMember(memberId string) (string, bool) {
type Response struct {
Message struct {
Expand Down
26 changes: 20 additions & 6 deletions crossref/crossref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestGetCrossref(t *testing.T) {
func TestGet(t *testing.T) {
t.Parallel()

type testCase struct {
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestFetch(t *testing.T) {
}
}

func TestCrossrefQueryUrl(t *testing.T) {
func TestQueryURL(t *testing.T) {
t.Parallel()

type testCase struct {
Expand All @@ -120,7 +120,14 @@ func TestCrossrefQueryUrl(t *testing.T) {
}
}

func TestGetCrossrefList(t *testing.T) {
func ExampleQueryURL() {
s := crossref.QueryURL(10, "340", "journal-article", false, false, false, false, false, false, false, false, false)
println(s)
// Output:
// https://api.crossref.org/works?filter=member%3A340%2Ctype%3Ajournal-article&order=desc&rows=10&sort=published
}

func TestGetList(t *testing.T) {
t.Parallel()

type testCase struct {
Expand All @@ -138,14 +145,14 @@ func TestGetCrossrefList(t *testing.T) {
for _, tc := range testCases {
got, err := crossref.GetList(tc.number, tc.member, tc._type, true, false, false, false, false, false, false, false, false)
if err != nil {
t.Errorf("GetCrossrefSample(%v): error %v", tc.number, err)
t.Errorf("GetList (%v): error %v", tc.number, err)
}
if diff := cmp.Diff(tc.number, len(got)); diff != "" {
t.Errorf("GetCrossrefList mismatch (-want +got):\n%s", diff)
t.Errorf("GetList mismatch (-want +got):\n%s", diff)
}
}
}
func TestGetCrossrefMember(t *testing.T) {
func TestGetMember(t *testing.T) {
t.Parallel()
type testCase struct {
input string
Expand All @@ -164,3 +171,10 @@ func TestGetCrossrefMember(t *testing.T) {
}
}
}

func ExampleGetMember() {
s, _ := crossref.GetMember("340")
println(s)
// Output:
// Public Library of Science (PLoS)
}
8 changes: 8 additions & 0 deletions dateutils/dateutils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dateutils_test

import (
"fmt"
"testing"

"github.com/front-matter/commonmeta/dateutils"
Expand Down Expand Up @@ -47,3 +48,10 @@ func TestGetDateFromUnixTimestamp(t *testing.T) {
}
}
}

func ExampleGetDateFromUnixTimestamp() {
s := dateutils.GetDateFromUnixTimestamp(1611312000)
fmt.Println(s)
// Output:
// 2021-01-22
}

0 comments on commit 4f68a5a

Please sign in to comment.