Skip to content

Commit 4f68a5a

Browse files
committed
improve tests
1 parent 5f8fc3d commit 4f68a5a

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

authorutils/authorutils.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ func IsPersonalName(name string) bool {
5050
}
5151

5252
// check for suffixes, e.g. "John Smith, MD"
53-
suffix := strings.Split(name, ", ")[1]
54-
suffixes := []string{"MD", "PhD", "BS"}
55-
for _, s := range suffixes {
56-
if suffix == s {
57-
return true
53+
suffix := strings.Split(name, ", ")
54+
if len(suffix) > 1 {
55+
suffixes := []string{"MD", "PhD", "BS"}
56+
for _, s := range suffixes {
57+
if suffix[1] == s {
58+
return true
59+
}
5860
}
5961
}
6062

authorutils/authorutils_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package authorutils_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/front-matter/commonmeta/authorutils"
7+
)
8+
9+
func TestIsPersonalName(t *testing.T) {
10+
t.Parallel()
11+
type testCase struct {
12+
input string
13+
want bool
14+
}
15+
testCases := []testCase{
16+
{input: "John Doe", want: false},
17+
{input: "Harvard University", want: false},
18+
{input: "LiberateScience", want: false},
19+
{input: "Jane Smith, MD", want: true},
20+
{input: "John", want: false},
21+
}
22+
for _, tc := range testCases {
23+
got := authorutils.IsPersonalName(tc.input)
24+
if tc.want != got {
25+
t.Errorf("Is Personal Name(%v): want %v, got %v",
26+
tc.input, tc.want, got)
27+
}
28+
}
29+
}

crossref/crossref.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ func QueryURL(number int, member string, _type string, sample bool, hasORCID boo
751751
return u.String()
752752
}
753753

754-
// Get the Crossref member name for a given member_id
754+
// Get the Crossref member name for a given memberId
755755
func GetMember(memberId string) (string, bool) {
756756
type Response struct {
757757
Message struct {

crossref/crossref_test.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/google/go-cmp/cmp"
1515
)
1616

17-
func TestGetCrossref(t *testing.T) {
17+
func TestGet(t *testing.T) {
1818
t.Parallel()
1919

2020
type testCase struct {
@@ -95,7 +95,7 @@ func TestFetch(t *testing.T) {
9595
}
9696
}
9797

98-
func TestCrossrefQueryUrl(t *testing.T) {
98+
func TestQueryURL(t *testing.T) {
9999
t.Parallel()
100100

101101
type testCase struct {
@@ -120,7 +120,14 @@ func TestCrossrefQueryUrl(t *testing.T) {
120120
}
121121
}
122122

123-
func TestGetCrossrefList(t *testing.T) {
123+
func ExampleQueryURL() {
124+
s := crossref.QueryURL(10, "340", "journal-article", false, false, false, false, false, false, false, false, false)
125+
println(s)
126+
// Output:
127+
// https://api.crossref.org/works?filter=member%3A340%2Ctype%3Ajournal-article&order=desc&rows=10&sort=published
128+
}
129+
130+
func TestGetList(t *testing.T) {
124131
t.Parallel()
125132

126133
type testCase struct {
@@ -138,14 +145,14 @@ func TestGetCrossrefList(t *testing.T) {
138145
for _, tc := range testCases {
139146
got, err := crossref.GetList(tc.number, tc.member, tc._type, true, false, false, false, false, false, false, false, false)
140147
if err != nil {
141-
t.Errorf("GetCrossrefSample(%v): error %v", tc.number, err)
148+
t.Errorf("GetList (%v): error %v", tc.number, err)
142149
}
143150
if diff := cmp.Diff(tc.number, len(got)); diff != "" {
144-
t.Errorf("GetCrossrefList mismatch (-want +got):\n%s", diff)
151+
t.Errorf("GetList mismatch (-want +got):\n%s", diff)
145152
}
146153
}
147154
}
148-
func TestGetCrossrefMember(t *testing.T) {
155+
func TestGetMember(t *testing.T) {
149156
t.Parallel()
150157
type testCase struct {
151158
input string
@@ -164,3 +171,10 @@ func TestGetCrossrefMember(t *testing.T) {
164171
}
165172
}
166173
}
174+
175+
func ExampleGetMember() {
176+
s, _ := crossref.GetMember("340")
177+
println(s)
178+
// Output:
179+
// Public Library of Science (PLoS)
180+
}

dateutils/dateutils_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dateutils_test
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/front-matter/commonmeta/dateutils"
@@ -47,3 +48,10 @@ func TestGetDateFromUnixTimestamp(t *testing.T) {
4748
}
4849
}
4950
}
51+
52+
func ExampleGetDateFromUnixTimestamp() {
53+
s := dateutils.GetDateFromUnixTimestamp(1611312000)
54+
fmt.Println(s)
55+
// Output:
56+
// 2021-01-22
57+
}

0 commit comments

Comments
 (0)