-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_test.go
131 lines (108 loc) · 2.84 KB
/
query_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
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
package crossref
import (
"encoding/json"
"sync"
"testing"
)
func TestClient_QueryJSON(t *testing.T) {
client := NewClient("Crossref Go", "mail@example.com")
// Test a known existing entry
t.Run("Existing Entry", func(t *testing.T) {
want := "Slow Robots for Unobtrusive Posture Correction"
raw, err := client.QueryJSON(want)
if err != nil {
t.Error(err)
}
var data queryJSON
json.Unmarshal(raw, &data)
t.Log("\n\ttotal results =", data.Message.TotalResults)
if data.Message.TotalResults == 0 {
t.Fatal("known existing entry could not be found")
}
got := data.Message.Items[0].Titles[0]
if want != got {
t.Errorf("\ngot: \n\t%v\nwant: \n\t%v", got, want)
}
})
// Test a known non-existing entry
t.Run("Non-existing Entry", func(t *testing.T) {
search := "jtfiejfrlsadaksljablkjoifajebwoijffal"
raw, err := client.QueryJSON(search)
if err != nil {
t.Error(err)
}
var data queryJSON
json.Unmarshal(raw, &data)
got := data.Message.TotalResults
want := 0
if want != got {
t.Errorf("\ngot: \n\t%v\nwant: \n\t%v", got, want)
}
})
t.Run("Empty Query", func(t *testing.T) {
_, err := client.QueryJSON("")
if err != ErrEmptyQuery {
t.Error("failed to return an empty query error:", err)
}
})
t.Run("Concurrent", func(t *testing.T) {
queries := []string{
"Slow Robots for Unobtrusive Posture Correction",
"jtfiejfrlsadaksljablkjoifajebwoijffal",
"",
}
var wg sync.WaitGroup
for _, q := range queries {
wg.Add(1)
go func(q string) {
defer wg.Done()
raw, err := client.QueryJSON(q)
t.Logf("LOG: query: %q, got: %d bytes, error: %v", q, len(raw), err)
}(q)
}
wg.Wait()
})
}
func TestClient_Query(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"
works, err := client.Query(want)
if err != nil {
t.Fatal(err)
}
got := works[0].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.Query("jtfiejfrlsadaksljablkjoifajebwoijffal")
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.Query("")
if err != ErrEmptyQuery {
t.Error("failed to return an empty query error:", err)
}
})
t.Run("Concurrent", func(t *testing.T) {
queries := []string{
"Slow Robots for Unobtrusive Posture Correction",
"jtfiejfrlsadaksljablkjoifajebwoijffal",
"",
}
var wg sync.WaitGroup
for _, q := range queries {
wg.Add(1)
go func(q string) {
defer wg.Done()
works, err := client.Query(q)
t.Logf("LOG: query: %q, got: %d works, error: %v", q, len(works), err)
}(q)
}
wg.Wait()
})
}