forked from supabase/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_test.go
More file actions
144 lines (125 loc) · 3.17 KB
/
db_test.go
File metadata and controls
144 lines (125 loc) · 3.17 KB
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
142
143
144
package integration
// Basic imports
import (
"context"
"io"
"net/http"
"os"
"path/filepath"
"sync"
"testing"
"github.com/gin-gonic/gin"
gonanoid "github.com/matoous/go-nanoid/v2"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
clicmd "github.com/supabase/cli/cmd"
"github.com/supabase/cli/test/mocks/docker"
)
type DBTestSuite struct {
suite.Suite
cmd *cobra.Command
tempDir string
ids []string
bodies []string
params []gin.Params
mtx sync.RWMutex
}
// test functions
// add tests here <-
// hooks
func (suite *DBTestSuite) SetupTest() {
suite.tempDir = NewTempDir(Logger, TempDir)
suite.mtx.Lock()
suite.ids = []string{}
suite.bodies = []string{}
suite.params = []gin.Params{}
suite.mtx.Unlock()
// add docker mock handlers
DockerMock.ExecCreateHandler = func(c *gin.Context) {
suite.addParams(c.Copy())
body, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "error reading body",
})
return
}
suite.addBody(body)
id := gonanoid.MustGenerate(docker.IDAlphabet, docker.IDLength)
c.JSON(http.StatusCreated, gin.H{
"Id": id,
})
suite.addID(id)
}
DockerMock.ExecStartHandler = func(c *gin.Context) {
suite.addParams(c.Copy())
body, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "error reading body",
})
return
}
suite.addBody(body)
docker.HijackedResponse(c, "0")
}
DockerMock.ContainerInspectHandler = func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{})
}
// create supabase dir
suite.cmd = clicmd.GetRootCmd()
init, _, err := suite.cmd.Find([]string{"init"})
require.NoError(suite.T(), err)
init.SetContext(context.Background())
err = init.RunE(init, []string{})
require.NoError(suite.T(), err)
err = os.Mkdir("supabase/.branches", os.FileMode(0755))
require.NoError(suite.T(), err)
}
func (suite *DBTestSuite) TeardownTest() {
require.NoError(suite.T(), os.Chdir(TempDir))
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestDBTestSuite(t *testing.T) {
suite.Run(t, new(DBTestSuite))
}
// helper functions
func (suite *DBTestSuite) addParams(c *gin.Context) {
suite.mtx.Lock()
defer suite.mtx.Unlock()
suite.params = append(suite.params, c.Params)
}
func (suite *DBTestSuite) addBody(body []byte) {
suite.mtx.Lock()
defer suite.mtx.Unlock()
suite.bodies = append(suite.bodies, string(body))
}
func (suite *DBTestSuite) addID(id string) {
suite.mtx.Lock()
defer suite.mtx.Unlock()
suite.ids = append(suite.ids, id)
}
func (suite *DBTestSuite) constructParams() []gin.Params {
ids := []gin.Params{}
// for each exec docker call we have to calls to docker api:
// one to create exec, one to start exec
for _, id := range suite.ids {
// this one represents call to create exec
ids = append(ids, gin.Params{
gin.Param{
Key: "id",
Value: "supabase_db_" + filepath.Base(suite.tempDir),
},
})
// this one represents call to start exec
ids = append(ids, gin.Params{
gin.Param{
Key: "id",
Value: id,
},
})
}
return ids
}