-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathquerybuilder_test.go
83 lines (63 loc) · 1.96 KB
/
querybuilder_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
package postgrest
import (
"encoding/json"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestNewClient(t *testing.T) {
assert.NotNil(t, NewClient("", "", nil))
}
func TestSelect(t *testing.T) {
assertions := assert.New(t)
c := createClient(t)
t.Run("ValidResult", func(t *testing.T) {
var got []map[string]interface{}
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
responder, _ := httpmock.NewJsonResponder(200, users)
httpmock.RegisterRegexpResponder("GET", mockPath, responder)
}
bs, count, err := c.From("users").Select("id, name, email", "", false).Execute()
assertions.NoError(err)
err = json.Unmarshal(bs, &got)
assertions.NoError(err)
assertions.EqualValues(users, got)
assertions.Equal(countType(0), count)
})
t.Run("WithCount", func(t *testing.T) {
var got []map[string]interface{}
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterRegexpResponder("GET", mockPath, func(req *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, users)
resp.Header.Add("Content-Range", "0-1/2")
return resp, nil
})
}
bs, count, err := c.From("users").Select("id, name, email", "exact", false).Execute()
assertions.NoError(err)
err = json.Unmarshal(bs, &got)
assertions.NoError(err)
assertions.EqualValues(users, got)
assertions.Equal(countType(2), count)
})
}
func TestFilter(t *testing.T) {
assertions := assert.New(t)
c := createClient(t)
t.Run("Eq", func(t *testing.T) {
want := "[{\"email\":\"[email protected]\"}]"
if mockResponses {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterRegexpResponder("GET", mockPath, httpmock.NewStringResponder(200, want))
}
got, _, err := c.From("users").Select("email", "", false).Eq("email", "[email protected]").ExecuteString()
assertions.NoError(err)
assertions.Equal(want, got)
})
}