-
Notifications
You must be signed in to change notification settings - Fork 1
/
doi_test.go
85 lines (67 loc) · 1.87 KB
/
doi_test.go
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
package crossref
import (
"encoding/json"
"testing"
)
func TestClient_DOIJSON(t *testing.T) {
client := NewClient("Crossref Go", "mail@example.com")
t.Run("Existing Entry", func(t *testing.T) {
want := "Slow Robots for Unobtrusive Posture Correction"
raw, err := client.DOIJSON("10.1145/3290605.3300843")
if err != nil {
t.Fatal(err)
}
var data doiJSON
json.Unmarshal(raw, &data)
if data.Work == nil {
t.Fatal("could not unmarshal json to work")
}
got := data.Work.Titles[0]
if want != got {
t.Errorf("\ngot: \n\t%v\nwant: \n\t%v", got, want)
}
})
t.Run("Non-existing Entry", func(t *testing.T) {
raw, err := client.DOIJSON("10.1145/3290605.330084") // deleted the last character from DOI
if err != nil {
t.Fatal(err)
}
var data doiJSON
json.Unmarshal(raw, &data)
if data.Status == "ok" {
t.Error("unexpected entry found for non-existing entry")
}
})
t.Run("Empty Query", func(t *testing.T) {
_, err := client.DOIJSON("")
if err != ErrEmptyQuery {
t.Error("failed to return an empty query error:", err)
}
})
}
func TestClient_DOI(t *testing.T) {
client := NewClient("Crossref Go", "mail@example.com")
t.Run("Existing Entry", func(t *testing.T) {
want := "Slow Robots for Unobtrusive Posture Correction"
work, err := client.DOI("10.1145/3290605.3300843")
if err != nil {
t.Fatal(err)
}
got := work.Title
if want != got {
t.Errorf("\ngot: \n\t%v\nwant: \n\t%v", got, want)
}
})
t.Run("Non-existing Entry", func(t *testing.T) {
_, err := client.DOI("10.1145/3290605.330084") // deleted the last character from DOI
if err != ErrZeroWorks {
t.Error("returned an error other than ErrZeroWorks for a non-existing entry:", err)
}
})
t.Run("Empty Query", func(t *testing.T) {
_, err := client.DOI("")
if err != ErrEmptyQuery {
t.Error("failed to return an empty query error:", err)
}
})
}