-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk_test.go
More file actions
117 lines (104 loc) · 2.28 KB
/
Copy pathsdk_test.go
File metadata and controls
117 lines (104 loc) · 2.28 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
package database_sdk
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
type ServerTestSuite struct {
suite.Suite
server *httptest.Server
client *DBClient
res []ResponseData
req []RawData
tLoc *time.Location
}
func TestSetupServer(t *testing.T) {
suite.Run(t, new(ServerTestSuite))
}
func (s *ServerTestSuite) SetupSuite() {
t := time.Now().Round(1 * time.Nanosecond)
s.tLoc = t.Location()
s.res = []ResponseData{
{
Source: Source{
Date: t,
Title: "Test title",
},
Url: "http://testurl.com",
},
}
s.req = []RawData{{
Source: Source{
Date: t,
Title: "Test title",
},
Url: "http://testurl.com",
Data: "a b c d",
}}
b, err := json.Marshal(s.res)
if err != nil {
panic(err)
}
r := http.NewServeMux()
r.HandleFunc("/api/default/documents", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
if strings.Contains(r.FormValue("q"), "server error") {
http.Error(w, "test crash", http.StatusInternalServerError)
return
}
if _, err := fmt.Fprint(w, string(b)); err != nil {
panic(err)
}
return
}
if r.Method == http.MethodPost {
raw, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
if _, err := fmt.Fprint(w, string(raw)); err != nil {
panic(err)
}
return
}
panic("method not allowed")
})
r.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
if _, err := fmt.Fprint(w, "OK"); err != nil {
panic(err)
}
return
}
panic("method not allowed")
})
s.server = httptest.NewServer(r)
s.client, err = NewDBClient(s.server.URL)
if err != nil {
panic(err)
}
}
func (s *ServerTestSuite) TestClient_Get() {
d, err := s.client.GetData("default", "data1 data2", 10, 0)
s.NoError(err)
s.ElementsMatch(s.res, d)
}
func (s *ServerTestSuite) TestClient_Post() {
d, err := s.client.SaveData("default", Documents{Documents: s.req})
s.NoError(err)
s.ElementsMatch(s.req, d.Documents)
}
func (s *ServerTestSuite) TestClient_Error() {
_, err := s.client.GetData("default", "server error", 10, 0)
s.Error(err)
s.IsType(err, CustomError{})
}
func (s *ServerTestSuite) TearDownSuite() {
s.server.Close()
}