-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_test.go
141 lines (117 loc) · 2.76 KB
/
handler_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package gdgberlin
import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"testing"
"github.com/chromedp/chromedp"
"github.com/fortytw2/dockertest"
)
type testFn struct {
name string
do func(*sql.DB) func(t *testing.T)
}
type e2eTestFn struct {
name string
do func(*sql.DB, context.Context) func(t *testing.T)
}
const pgVersion = "postgres:11-alpine"
func TestDemoApp(t *testing.T) {
container, err := dockertest.RunContainer(pgVersion, "5432", func(addr string) error {
db, err := sql.Open("postgres", "postgres://postgres:postgres@"+addr+"?sslmode=disable")
if err != nil {
log.Println(err)
return err
}
return db.Ping()
}, "--tmpfs", "/var/lib/postgresql")
if err != nil {
t.Fatal(err)
}
defer container.Shutdown()
db, err := NewDB("postgres://postgres:postgres@" + container.Addr + "?sslmode=disable")
if err != nil {
t.Fatal(err)
}
testCases := map[string][]testFn{
"fish": fishCases,
}
for name, cases := range testCases {
t.Run(name, func(t *testing.T) {
for _, c := range cases {
t.Run(c.name, c.do(db))
TruncateTables(db, t.Fatal)
}
})
}
// end to end cases
e2eCases := map[string][]e2eTestFn{
// "fish": fishE2ECases,
"root": rootE2ECases,
}
handler := NewHandler(db)
localPort := freePort()
go http.ListenAndServe(fmt.Sprintf(":%d", localPort), handler)
dir, err := ioutil.TempDir("", "test-chromedp")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
opts := []chromedp.ExecAllocatorOption{
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
// chromedp.Headless,
chromedp.DisableGPU,
chromedp.UserDataDir(dir),
}
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
// also set up a custom logger
ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(t.Logf))
defer cancel()
for name, cases := range e2eCases {
t.Run(name, func(t *testing.T) {
for _, c := range cases {
err = chromedp.Run(ctx, chromedp.Navigate(fmt.Sprintf("http://localhost:%d", localPort)))
if err != nil {
t.Fatal(err)
}
t.Run(c.name, c.do(db, ctx))
TruncateTables(db, t.Fatal)
}
})
}
}
func TruncateTables(db *sql.DB, f func(...interface{})) {
// https://stackoverflow.com/a/12082038
_, err := db.Exec(`
DO
$func$
BEGIN
EXECUTE (SELECT 'TRUNCATE TABLE ' || string_agg(oid::regclass::text, ', ') || ' CASCADE'
FROM pg_class
WHERE relkind = 'r' -- only tables
AND relnamespace = 'public'::regnamespace);
END
$func$;`)
if err != nil {
f(err)
}
}
func freePort() int {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
panic(err)
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(err)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}