Skip to content

Commit 203bf66

Browse files
refact mock
1 parent 07ac163 commit 203bf66

8 files changed

Lines changed: 101 additions & 44 deletions

File tree

example/request/go.mod

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
module github.com/the-hotels-network/go-tinybird/example/request
22

3-
go 1.19
3+
go 1.24
44

5-
replace github.com/the-hotels-network/go-tinybird => ../..
5+
toolchain go1.24.2
66

77
require (
8-
github.com/stretchr/testify v1.8.0
8+
github.com/stretchr/testify v1.8.4
99
github.com/the-hotels-network/go-tinybird v0.0.0-00010101000000-000000000000
1010
)
1111

1212
require (
1313
github.com/davecgh/go-spew v1.1.1 // indirect
1414
github.com/olivere/ndjson v1.0.1 // indirect
1515
github.com/pmezard/go-difflib v1.0.0 // indirect
16-
github.com/sirupsen/logrus v1.9.0 // indirect
17-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
16+
github.com/sirupsen/logrus v1.9.3 // indirect
17+
golang.org/x/sys v0.9.0 // indirect
1818
gopkg.in/yaml.v3 v3.0.1 // indirect
1919
)
20+
21+
replace github.com/the-hotels-network/go-tinybird => /Users/nicola/Documents/go-tinybird/

example/request/go.sum

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ github.com/olivere/ndjson v1.0.1 h1:q+rEa/MOpElAGj7W4IHmpY6VG7baHAugfs0MGx8DNA8=
77
github.com/olivere/ndjson v1.0.1/go.mod h1:y3NXfLEBYQd+QGVQxzIFYnbRhdVKVBaLSCGAoyialk4=
88
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
99
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10-
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
11-
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
10+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
11+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
1212
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
1413
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
15-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
16-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
17-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
18-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
14+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
15+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
1916
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
18+
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2019
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2120
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2221
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

example/request/main.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"net/http"
66
"net/url"
7-
"os"
87

98
"github.com/the-hotels-network/go-tinybird"
109
)
@@ -13,33 +12,36 @@ func main() {
1312
params := url.Values{}
1413
params.Add("start_date", "2023-05-01")
1514
params.Add("end_date", "2023-05-29")
16-
params.Add("property_id", "1011163")
17-
params.Add("property_id", "1011832")
18-
params.Add("property_id", "1011846")
19-
params.Add("property_id", "1011847")
15+
params.Add("user_id", "1011")
16+
params.Add("user_id", "1012")
2017

2118
req := tinybird.Request{
2219
Method: http.MethodGet,
2320
Pipe: tinybird.Pipe{
24-
Name: "ep_quantum_disparities",
21+
Name: "ep_users",
2522
Parameters: params,
2623
Workspace: tinybird.Workspace{
27-
Name: "quantum",
28-
Token: os.Getenv("TB_TOKEN"),
24+
Name: "example",
25+
Token: "testoken",
2926
},
3027
},
3128
}
3229

33-
err := req.Execute()
34-
if err != nil {
35-
fmt.Println(err)
36-
os.Exit(1)
37-
}
30+
tinybird.MockResponseFunc(
31+
http.StatusOK,
32+
func(url string) string {
33+
return `{"id": "01H3EHWCP9TF5GSHYBVFXSZX9M", "type": "user", "min": 0, "currency": "EUR"}`
34+
},
35+
nil,
36+
)
3837

38+
req.Execute()
3939
res := req.Response
40+
4041
fmt.Println("Status code:", res.Status)
4142
fmt.Println("Elapsed time:", req.Elapsed)
4243
fmt.Println("Error:", res.Error)
43-
fmt.Println("Data:", res.Data)
44-
fmt.Println("URL:", req.URI())
44+
fmt.Println("Body:", res.Body)
45+
fmt.Println("URL:", req.URL())
46+
fmt.Println("URI:", req.URI())
4547
}

example/request/main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestRequest(t *testing.T) {
2323
tinybird.MockResponse(
2424
http.StatusOK,
2525
`{"data":[{"Col1": "1", "Col2": 2}],"rows":1,"statistics":{"elapsed":0.00091042,"rows_read": 4,"bytes_read": 296}}`,
26+
nil,
2627
)
2728

2829
req.Execute()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/the-hotels-network/go-tinybird
22

3-
go 1.19
3+
go 1.24
44

55
require (
66
github.com/olivere/ndjson v1.0.1

mock.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,43 @@ import (
66
"net/http"
77
)
88

9-
type MockRoundTripper func(r *http.Request) *http.Response
10-
11-
func InjectHTTPClient(httpClient *http.Client) {
12-
Client = httpClient
13-
}
9+
type MockRoundTripper func(r *http.Request) (*http.Response, error)
1410

1511
func (f MockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
16-
return f(r), nil
12+
return f(r)
1713
}
1814

19-
func MockResponse(statusCode int, body string) {
20-
MockResponseWithRequestInspect(statusCode, body, nil)
15+
func MockResponse(statusCode int, body string, requestInspect func(r *http.Request)) {
16+
Client = &http.Client{
17+
Transport: MockRoundTripper(func(r *http.Request) (*http.Response, error) {
18+
if requestInspect != nil {
19+
requestInspect(r)
20+
}
21+
22+
return &http.Response{
23+
StatusCode: statusCode,
24+
Body: io.NopCloser(bytes.NewReader([]byte(body))),
25+
ContentLength: int64(len([]byte(body))),
26+
Request: r,
27+
}, nil
28+
}),
29+
}
2130
}
2231

23-
func MockResponseWithRequestInspect(statusCode int, body string, requestInspect func(r *http.Request)) {
24-
InjectHTTPClient(&http.Client{
25-
Transport: MockRoundTripper(func(r *http.Request) *http.Response {
32+
func MockResponseFunc(statusCode int, bodyFn func(string) string, requestInspect func(r *http.Request)) {
33+
Client = &http.Client{
34+
Transport: MockRoundTripper(func(r *http.Request) (*http.Response, error) {
2635
if requestInspect != nil {
2736
requestInspect(r)
2837
}
2938

39+
body := bodyFn(r.URL.String())
3040
return &http.Response{
31-
StatusCode: statusCode,
32-
Body: io.NopCloser(bytes.NewReader([]byte(body))),
33-
}
41+
StatusCode: statusCode,
42+
Body: io.NopCloser(bytes.NewReader([]byte(body))),
43+
ContentLength: int64(len([]byte(body))),
44+
Request: r,
45+
}, nil
3446
}),
35-
})
47+
}
3648
}

mock_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tinybird_test
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/the-hotels-network/go-tinybird"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestMock(t *testing.T) {
13+
req := tinybird.Request{
14+
Method: http.MethodGet,
15+
Pipe: tinybird.Pipe{
16+
Name: "test",
17+
Workspace: tinybird.Workspace{
18+
Name: "test",
19+
Token: "testoken",
20+
},
21+
},
22+
}
23+
24+
tinybird.MockResponse(
25+
http.StatusOK,
26+
`ok`,
27+
func(r *http.Request) {
28+
assert.Equal(t, r.URL.String(), "https://api.tinybird.co/v0/pipes/test.json")
29+
},
30+
)
31+
32+
req.Execute()
33+
res := req.Response
34+
35+
assert.Nil(t, req.Error)
36+
assert.Equal(t, res.Status, http.StatusOK)
37+
assert.Equal(t, res.Body, "ok")
38+
}

request_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestRequest(t *testing.T) {
2626
tinybird.MockResponse(
2727
http.StatusOK,
2828
`{"data":[{"Col1": "1", "Col2": 2}],"rows":1,"statistics":{"elapsed":0.00091042,"rows_read": 4,"bytes_read": 296}}`,
29+
nil,
2930
)
3031

3132
req.Execute()
@@ -56,6 +57,7 @@ func TestRequestWithCustomURL(t *testing.T) {
5657
tinybird.MockResponse(
5758
http.StatusOK,
5859
`{"data":[{"Col1": "1", "Col2": 2}],"rows":1,"statistics":{"elapsed":0.00091042,"rows_read": 4,"bytes_read": 296}}`,
60+
nil,
5961
)
6062

6163
req.Execute()
@@ -88,7 +90,7 @@ func TestRequestWithRequestParamInspect(t *testing.T) {
8890
},
8991
}
9092

91-
tinybird.MockResponseWithRequestInspect(
93+
tinybird.MockResponse(
9294
http.StatusOK,
9395
`{"data":[{"Col1": "1", "Col2": 2}],"rows":1,"statistics":{"elapsed":0.00091042,"rows_read": 4,"bytes_read": 296}}`,
9496
func(r *http.Request) {
@@ -194,6 +196,7 @@ func TestRequestEvent(t *testing.T) {
194196
tinybird.MockResponse(
195197
http.StatusOK,
196198
`{"successful_rows":1,"quarantined_rows":0}`,
199+
nil,
197200
)
198201

199202
req.Execute()

0 commit comments

Comments
 (0)