Repository navigation
Expand file tree
/
Copy pathdataciteapi_test.go
More file actions
228 lines (213 loc) · 5.46 KB
/
Copy pathdataciteapi_test.go
File metadata and controls
228 lines (213 loc) · 5.46 KB
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
package dataciteapi
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"testing"
)
var (
MailTo string
)
func TestClientWorks(t *testing.T) {
api, err := NewDataCiteClient("datacite_test.go", MailTo)
if err != nil {
t.Error(err)
t.FailNow()
}
if api.RateLimitLimit != 0 {
t.Errorf("expected 0, got %d", api.RateLimitLimit)
}
if api.RateLimitInterval != 0 {
t.Errorf("expected 0, got %d", api.RateLimitInterval)
}
if fmt.Sprintf("%s", api.LastRequest) == "0001-01-01 00:00:00 +0000" {
t.Errorf("expected 0001-01-01 00:00:00 +0000, got %s", api.LastRequest)
}
// test low level getJSON, 10.22002/D1.868 is
// a Caltech Library DataCite DOI
src, err := api.getJSON("/works/10.22002/D1.868")
if err != nil {
t.Error(err)
t.FailNow()
}
if api.StatusCode != 200 {
t.Errorf("Expected StatusCode 200, got %d -> %s", api.StatusCode, api.Status)
}
if len(src) == 0 {
t.Errorf("expected a response body from /works/10.22002/D1.868")
}
if api.RateLimitLimit == 0 {
t.Errorf("expected greater than zero, got %d", api.RateLimitLimit)
}
if api.RateLimitInterval == 0 {
t.Errorf("expected greater than zero, got %d", api.RateLimitInterval)
}
if fmt.Sprintf("%s", api.LastRequest) == "0001-01-01 00:00:00 +0000" {
t.Errorf("expected not equal to 0001-01-01 00:00:00 +0000, got %s", api.LastRequest)
}
// Now test WorksJSON()
doi := "10.22002/D1.868"
doi_url := "https://dx.doi.org/10.22002/D1.868"
src, err = api.WorksJSON(doi)
if err != nil {
t.Errorf("expected a JSON response, got %s", err)
t.FailNow()
}
if api.StatusCode != 200 {
t.Errorf("expected status code 200, got %d -> %q", api.StatusCode, api.Status)
t.FailNow()
}
obj1 := make(Object)
err = json.Unmarshal(src, &obj1)
if err != nil {
t.Error(err)
t.FailNow()
}
if obj1 == nil {
t.Errorf("expected unmarshaled object, got nil")
t.FailNow()
}
obj2, err := api.Works(doi_url)
if obj2 == nil {
t.Errorf("expected an non-nil Object from Types(), got nil but no error")
t.FailNow()
}
if len(obj1) != len(obj2) {
t.Errorf("expected equal lengths for obj1, obj2 ->\n%+v, \n%+v", obj1, obj2)
t.FailNow()
}
}
func TestClientDois(t *testing.T) {
api, err := NewDataCiteClient("datacite_test.go", MailTo)
if err != nil {
t.Error(err)
t.FailNow()
}
if api.RateLimitLimit != 0 {
t.Errorf("expected 0, got %d", api.RateLimitLimit)
}
if api.RateLimitInterval != 0 {
t.Errorf("expected 0, got %d", api.RateLimitInterval)
}
if fmt.Sprintf("%s", api.LastRequest) == "0001-01-01 00:00:00 +0000" {
t.Errorf("expected 0001-01-01 00:00:00 +0000, got %s", api.LastRequest)
}
// test low level getJSON, 10.22002/D1.868 is
// a Caltech Library DataCite DOI
src, err := api.getJSON("/dois/10.22002/D1.868")
if err != nil {
t.Error(err)
t.FailNow()
}
if api.StatusCode != 200 {
t.Errorf("Expected StatusCode 200, got %d -> %s", api.StatusCode, api.Status)
}
if len(src) == 0 {
t.Errorf("expected a response body from /dois/10.22002/D1.868")
}
if api.RateLimitLimit == 0 {
t.Errorf("expected greater than zero, got %d", api.RateLimitLimit)
}
if api.RateLimitInterval == 0 {
t.Errorf("expected greater than zero, got %d", api.RateLimitInterval)
}
if fmt.Sprintf("%s", api.LastRequest) == "0001-01-01 00:00:00 +0000" {
t.Errorf("expected not equal to 0001-01-01 00:00:00 +0000, got %s", api.LastRequest)
}
// Now test DoisJSON()
doi := "10.22002/D1.868"
doi_url := "https://dx.doi.org/10.22002/D1.868"
src, err = api.DoisJSON(doi)
if err != nil {
t.Errorf("expected a JSON response, got %s", err)
t.FailNow()
}
if api.StatusCode != 200 {
t.Errorf("expected status code 200, got %d -> %q", api.StatusCode, api.Status)
t.FailNow()
}
obj1 := make(Object)
err = json.Unmarshal(src, &obj1)
if err != nil {
t.Error(err)
t.FailNow()
}
if obj1 == nil {
t.Errorf("expected unmarshaled object, got nil")
t.FailNow()
}
obj2, err := api.Dois(doi_url)
if obj2 == nil {
t.Errorf("expected an non-nil Object from Types(), got nil but no error")
t.FailNow()
}
if len(obj1) != len(obj2) {
t.Errorf("expected equal lengths for obj1, obj2 ->\n%+v, \n%+v", obj1, obj2)
t.FailNow()
}
}
func TestArXivLookup(t *testing.T) {
api, err := NewDataCiteClient("datacite_test.go", MailTo)
if err != nil {
t.Error(err)
t.FailNow()
}
ids := []string{
"10.22002/D1.868",
"arXiv:2402.12335v1",
"arXiv:2401.12460v1",
"arXiv:2204.13532v2",
"arXiv:2312.07215",
"arXiv:2305.06519",
"arXiv:2312.03791",
"arXiv:2305.19279",
"arXiv:2305.05315",
"arXiv:2305.07673",
"arXiv:2111.03606",
"arXiv:2112.06016",
}
for _, id := range ids {
_, err = api.WorksJSON(id)
if err != nil {
t.Errorf("Run api.WorksJSON(%q), got unexpected error, %s", id, err)
}
}
}
func TestDoisLookup(t *testing.T) {
api, err := NewDataCiteClient("datacite_test.go", MailTo)
if err != nil {
t.Error(err)
t.FailNow()
}
ids := []string{
"10.22002/D1.868",
"arXiv:2402.12335v1",
"arXiv:2401.12460v1",
"arXiv:2204.13532v2",
"arXiv:2312.07215",
"arXiv:2305.06519",
"arXiv:2312.03791",
"arXiv:2305.19279",
"arXiv:2305.05315",
"arXiv:2305.07673",
"arXiv:2111.03606",
"arXiv:2112.06016",
}
for _, id := range ids {
_, err = api.DoisJSON(id)
if err != nil {
t.Errorf("Run api.DoisJSON(%q), got unexpected error, %s", id, err)
}
}
}
func TestMain(m *testing.M) {
flag.StringVar(&MailTo, "mailto", "", "set the mailto for testing")
flag.Parse()
if MailTo == "" {
MailTo = "test@example.library.edu"
}
log.Printf("mailto: %q", MailTo)
os.Exit(m.Run())
}