-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathpull_test.go
More file actions
174 lines (163 loc) · 5.69 KB
/
Copy pathpull_test.go
File metadata and controls
174 lines (163 loc) · 5.69 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package pull
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"github.com/h2non/gock"
"github.com/jackc/pgconn"
"github.com/jackc/pgerrcode"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supabase/cli/internal/testing/apitest"
"github.com/supabase/cli/internal/testing/fstest"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/migration"
"github.com/supabase/cli/pkg/pgtest"
)
var dbConfig = pgconn.Config{
Host: "db.supabase.co",
Port: 5432,
User: "admin",
Password: "password",
Database: "postgres",
}
func TestPullCommand(t *testing.T) {
t.Run("throws error on connect failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := Run(context.Background(), nil, pgconn.Config{}, "", fsys)
// Check error
assert.ErrorContains(t, err, "invalid port (outside range)")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on sync failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
ReplyError(pgerrcode.InvalidCatalogName, `database "postgres" does not exist`)
// Run test
err := Run(context.Background(), nil, dbConfig, "", fsys, conn.Intercept)
// Check error
assert.ErrorContains(t, err, `ERROR: database "postgres" does not exist (SQLSTATE 3D000)`)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}
func TestPullSchema(t *testing.T) {
t.Run("dumps remote schema", func(t *testing.T) {
errNetwork := errors.New("network error")
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), "test-db")
require.NoError(t, apitest.MockDockerLogs(utils.Docker, "test-db", "test"))
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/images/" + utils.GetRegistryImageUrl(utils.Config.Db.Image) + "/json").
ReplyError(errNetwork)
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
Reply("SELECT 0")
// Run test
err := run(context.Background(), nil, "0_test.sql", conn.MockClient(t), fsys)
// Check error
assert.ErrorIs(t, err, errNetwork)
assert.Empty(t, apitest.ListUnmatchedRequests())
contents, err := afero.ReadFile(fsys, "0_test.sql")
assert.NoError(t, err)
assert.Equal(t, []byte("test"), contents)
})
t.Run("throws error on diff failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
path := filepath.Join(utils.MigrationsDir, "0_test.sql")
require.NoError(t, afero.WriteFile(fsys, path, []byte(""), 0644))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/images/" + utils.GetRegistryImageUrl(utils.Config.Db.Image) + "/json").
ReplyError(errors.New("network error"))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
Reply("SELECT 1", []any{"0"})
// Run test
err := run(context.Background(), []string{"public"}, "", conn.MockClient(t), fsys)
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}
func TestSyncRemote(t *testing.T) {
t.Run("throws error on permission denied", func(t *testing.T) {
// Setup in-memory fs
fsys := &fstest.OpenErrorFs{DenyPath: utils.MigrationsDir}
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
Reply("SELECT 0")
// Run test
err := assertRemoteInSync(context.Background(), conn.MockClient(t), fsys)
// Check error
assert.ErrorIs(t, err, os.ErrPermission)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on mismatched length", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
path := filepath.Join(utils.MigrationsDir, "0_test.sql")
require.NoError(t, afero.WriteFile(fsys, path, []byte(""), 0644))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
Reply("SELECT 0")
// Run test
err := assertRemoteInSync(context.Background(), conn.MockClient(t), fsys)
// Check error
assert.ErrorIs(t, err, errConflict)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on mismatched migration", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
path := filepath.Join(utils.MigrationsDir, "0_test.sql")
require.NoError(t, afero.WriteFile(fsys, path, []byte(""), 0644))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
Reply("SELECT 1", []any{"20220727064247"})
// Run test
err := assertRemoteInSync(context.Background(), conn.MockClient(t), fsys)
// Check error
assert.ErrorIs(t, err, errConflict)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on missing migration", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.LIST_MIGRATION_VERSION).
Reply("SELECT 0")
// Run test
err := assertRemoteInSync(context.Background(), conn.MockClient(t), fsys)
// Check error
assert.ErrorIs(t, err, errMissing)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}