-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
212 lines (205 loc) · 4.58 KB
/
client_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
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
//go:build (linux || darwin) && amd64
package grobidclient
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"os/user"
"reflect"
"strings"
"testing"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestProcessPDF(t *testing.T) {
skipNoDocker(t)
if testing.Short() {
t.Skip("skipping testcontainer based tests in short mode")
}
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "grobid/grobid:0.8.1",
ExposedPorts: []string{"8070/tcp"},
WaitingFor: wait.ForListeningPort("8070/tcp"),
}
grobidC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
t.Fatalf("Could not start grobid: %s", err)
}
defer func() {
if err := grobidC.Terminate(ctx); err != nil {
t.Fatalf("Could not stop grobid: %s", err)
}
}()
ip, err := grobidC.Host(ctx)
if err != nil {
t.Fatalf("TC: count not get host: %v", err)
}
port, err := grobidC.MappedPort(ctx, "8070")
if err != nil {
t.Fatalf("TC: count not get port: %v", err)
}
hostPort := fmt.Sprintf("http://%s:%s", ip, port.Port())
t.Logf("starting e2e test, using grobid container running at %v", hostPort)
grobid := New(hostPort)
result, err := grobid.ProcessPDF(
"testdata/pdf/062RoisinAronAmericanNaturalist03.pdf",
"processFulltextDocument",
nil)
if err != nil {
t.Fatalf("expected successful parse, got %v", err)
}
if result.StatusCode != 200 {
t.Fatalf("expected HTTP 200, got %v: %v", result.StatusCode, string(result.Body))
}
}
func TestParseLines(t *testing.T) {
var cases = []struct {
about string
r io.Reader
result []string
err error
}{
{
about: "nothing to read",
r: strings.NewReader(``),
result: nil,
err: nil,
},
{
about: "single line",
r: strings.NewReader("1\n"),
result: []string{"1"},
err: nil,
},
{
about: "just an empty line",
r: strings.NewReader("\n"),
result: nil,
err: nil,
},
{
about: "just an empty line",
r: strings.NewReader("1\n2\n3 \n"),
result: []string{"1", "2", "3"},
err: nil,
},
}
for _, c := range cases {
lines, err := parseLines(c.r)
if err != c.err {
t.Fatalf("[%s] got %v, want %v", c.about, err, c.err)
}
if !reflect.DeepEqual(lines, c.result) {
t.Fatalf("[%s] got %v (%d), want %v (%d)", c.about, lines, len(lines), c.result, len(c.result))
}
}
}
func TestDefaultResultWriter(t *testing.T) {
var cases = []struct {
about string
result *Result
opts *Options
dst string // destination file
err error
}{
{
about: "nil",
result: nil,
opts: nil,
dst: "",
err: nil,
},
{
about: "empty result",
result: &Result{},
opts: nil,
dst: "",
err: nil,
},
{
about: "only 200",
result: &Result{
StatusCode: 200,
},
opts: nil,
dst: "_200.txt",
err: nil,
},
{
about: "only 200, zero body",
result: &Result{
Filename: "zerobody.jpg",
StatusCode: 200,
},
opts: nil,
dst: "zerobody_200.txt",
err: nil,
},
{
about: "only 200, 1 byte body",
result: &Result{
Filename: "1byte.txt",
StatusCode: 200,
Body: []byte{'1'},
},
opts: nil,
dst: "1byte.grobid.tei.xml",
err: nil,
},
}
for _, c := range cases {
err := DefaultResultWriter(c.result, c.opts)
if err != c.err {
t.Fatalf("got %v, want %v", err, c.err)
}
if c.dst != "" {
if _, err := os.Stat(c.dst); os.IsNotExist(err) {
t.Errorf("expected file %v as side effect", c.dst)
}
// TODO: rework result writer interface, so it a bit less awkward to test
if _, err := os.Stat(c.dst); err == nil {
t.Logf("cleanup: %v", c.dst)
os.Remove(c.dst)
}
}
}
}
func skipNoDocker(t *testing.T) {
noDocker := false
cmd := exec.Command("systemctl", "is-active", "docker")
b, err := cmd.CombinedOutput()
if err != nil {
noDocker = true
}
if strings.TrimSpace(string(b)) != "active" {
noDocker = true
}
if !noDocker {
// We found some docker.
return
}
// Otherwise, try podman.
_, err = exec.LookPath("podman")
if err == nil {
t.Logf("podman detected")
// DOCKER_HOST=unix:///run/user/$UID/podman/podman.sock
usr, err := user.Current()
if err != nil {
t.Logf("cannot get UID, set DOCKER_HOST manually")
} else {
sckt := fmt.Sprintf("unix:///run/user/%v/podman/podman.sock", usr.Uid)
os.Setenv("DOCKER_HOST", sckt)
t.Logf("set DOCKER_HOST to %v", sckt)
}
noDocker = false
}
if noDocker {
t.Skipf("docker not installed or not running")
}
}