/
testing.go
76 lines (70 loc) · 1.9 KB
/
testing.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
package client
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/cloudquery/plugin-sdk/plugins/source"
"github.com/cloudquery/plugin-sdk/schema"
"github.com/cloudquery/plugin-sdk/specs"
"github.com/plaid/plaid-go/v10/plaid"
"github.com/rs/zerolog"
)
func TestServer(t *testing.T, data any) *httptest.Server {
t.Helper()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
data, _ := json.Marshal(data)
_, _ = w.Write(data)
}))
return ts
}
func TestHelper(t *testing.T, table *schema.Table, ts *httptest.Server) {
version := "vDev"
table.IgnoreInTests = false
t.Helper()
l := zerolog.New(zerolog.NewTestWriter(t)).Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.StampMicro}).Level(zerolog.DebugLevel).With().Timestamp().Logger()
newTestExecutionClient := func(ctx context.Context, logger zerolog.Logger, spec specs.Source, opts source.Options) (schema.ClientMeta, error) {
configuration := plaid.NewConfiguration()
configuration.UseEnvironment(plaid.Development)
urlParts := strings.Split(ts.URL, "://")
configuration.Scheme = urlParts[0]
configuration.Host = urlParts[1]
client := plaid.NewAPIClient(configuration)
s := Spec{
ClientId: "test",
Secret: "test",
AccessToken: "test",
}
s.SetDefaults()
err := s.Validate()
if err != nil {
return nil, err
}
return &Client{
Logger: l,
Services: client,
ClientId: s.ClientId,
Secret: s.Secret,
}, nil
}
p := source.NewPlugin(
table.Name,
version,
[]*schema.Table{
table,
},
newTestExecutionClient)
p.SetLogger(l)
source.TestPluginSync(t, p, specs.Source{
Name: "dev",
Path: "cloudquery/dev",
Version: version,
Tables: []string{table.Name},
Destinations: []string{"mock-destination"},
})
}