This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
forked from sanity-io/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_test.go
102 lines (78 loc) · 1.94 KB
/
package_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
package sanity_test
import (
"encoding/json"
"errors"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/go-chi/chi"
"github.com/stretchr/testify/require"
sanity "github.com/sanity-io/client-go"
)
type Suite struct {
server *httptest.Server
mux chi.Router
client *sanity.Client
}
func withSuite(t *testing.T, f func(*Suite), opts ...sanity.Option) {
t.Helper()
mux := chi.NewRouter()
suite := &Suite{
mux: mux,
server: httptest.NewServer(mux),
}
url, err := url.Parse(suite.server.URL)
require.NoError(t, err)
url.Path = "/v1"
opts = append(opts, sanity.WithHTTPHost(url.Scheme, url.Host))
c, err := sanity.VersionV1.NewClient("myProject", "myDataset", opts...)
require.NoError(t, err)
suite.client = c
f(suite)
}
type testDocument struct {
ID string `json:"_id"`
Type string `json:"_type"`
CreatedAt time.Time `json:"_createdAt"`
UpdatedAt time.Time `json:"_updatedAt"`
Value string `json:"value"`
}
func (d testDocument) toMap() map[string]interface{} {
m, err := json.Marshal(d)
if err != nil {
panic(err)
}
var doc map[string]interface{}
if err := json.Unmarshal(m, &doc); err != nil {
panic(err)
}
return doc
}
type testDocumentWithCustomJSONMarshaler struct{}
func (testDocumentWithCustomJSONMarshaler) MarshalJSON() ([]byte, error) {
return []byte(`{"x":1}`), nil
}
type testDocumentWithJSONMarshalFailure struct{}
func (testDocumentWithJSONMarshalFailure) MarshalJSON() ([]byte, error) {
return nil, errMarshalFailure
}
type valueWithCustomJSONMarshaler float64
func (*valueWithCustomJSONMarshaler) MarshalJSON() ([]byte, error) {
return []byte("x"), nil
}
var errMarshalFailure = errors.New("failure")
func mustJSONMsg(val interface{}) *json.RawMessage {
b, err := json.Marshal(val)
if err != nil {
panic(err)
}
return (*json.RawMessage)(&b)
}
func mustJSONBytes(val interface{}) []byte {
b, err := json.Marshal(val)
if err != nil {
panic(err)
}
return b
}