-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget_documents_test.go
109 lines (93 loc) · 2.91 KB
/
get_documents_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
package sanity_test
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
sanity "github.com/sanity-io/client-go"
"github.com/sanity-io/client-go/api"
)
func TestGetDocuments(t *testing.T) {
docIDs := []string{"doc1", "doc2"}
now := time.Date(2020, 1, 2, 23, 01, 44, 0, time.UTC)
testDoc1 := &testDocument{
ID: "doc1",
Type: "doc",
CreatedAt: now,
UpdatedAt: now,
Value: "hello world",
}
testDoc2 := &testDocument{
ID: "doc2",
Type: "doc",
CreatedAt: now,
UpdatedAt: now,
Value: "hello world",
}
testDocuments := []api.Document{testDoc1.toMap(), testDoc2.toMap()}
t.Run("No document ID specified", func(t *testing.T) {
withSuite(t, func(s *Suite) {
resp, err := s.client.GetDocuments().Do(context.Background())
require.NoError(t, err)
require.Nil(t, resp.Documents)
})
})
t.Run("Empty document ID specified", func(t *testing.T) {
withSuite(t, func(s *Suite) {
s.mux.Get("/v1/data/doc/myDataset", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
_, err := s.client.GetDocuments([]string{""}...).Do(context.Background())
require.Error(t, err)
var reqErr *sanity.RequestError
require.True(t, errors.As(err, &reqErr))
})
})
t.Run("GET URL length exceeded", func(t *testing.T) {
withSuite(t, func(s *Suite) {
docID := make([]rune, 1024)
for i := range docID {
docID[i] = 'x'
}
_, err := s.client.GetDocuments(string(docID)).Do(context.Background())
require.Error(t, err)
})
})
t.Run("get 2 documents", func(t *testing.T) {
withSuite(t, func(s *Suite) {
s.mux.Get("/v1/data/doc/myDataset/doc1,doc2", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write(mustJSONBytes(&api.GetDocumentsResponse{
Documents: testDocuments,
}))
assert.NoError(t, err)
})
result, err := s.client.GetDocuments(docIDs...).Do(context.Background())
require.NoError(t, err)
assert.Equal(t, testDocuments, result.Documents)
})
})
t.Run("supports default tag", func(t *testing.T) {
withSuite(t, func(s *Suite) {
s.mux.Get("/v1/data/doc/myDataset", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "default", r.URL.Query().Get("tag"))
w.WriteHeader(http.StatusNotFound)
})
_, err := s.client.GetDocuments([]string{""}...).Do(context.Background())
require.Error(t, err)
}, sanity.WithTag("default"))
})
t.Run("supports overwriting tag", func(t *testing.T) {
withSuite(t, func(s *Suite) {
s.mux.Get("/v1/data/doc/myDataset", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "custom", r.URL.Query().Get("tag"))
w.WriteHeader(http.StatusNotFound)
})
_, err := s.client.GetDocuments([]string{""}...).Tag("custom").Do(context.Background())
require.Error(t, err)
}, sanity.WithTag("tag"))
})
}