forked from outerbase/starbasedb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.test.ts
More file actions
229 lines (199 loc) · 8.65 KB
/
dump.test.ts
File metadata and controls
229 lines (199 loc) · 8.65 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { dumpDatabaseRoute } from './dump'
import { executeOperation } from '.'
import { createResponse } from '../utils'
import type { DataSource } from '../types'
import type { StarbaseDBConfiguration } from '../handler'
vi.mock('.', () => ({
executeOperation: vi.fn(),
}))
vi.mock('../utils', () => ({
createResponse: vi.fn(
(data, message, status) =>
new Response(JSON.stringify({ result: data, error: message }), {
status,
headers: { 'Content-Type': 'application/json' },
})
),
}))
let mockDataSource: DataSource
let mockConfig: StarbaseDBConfiguration
beforeEach(() => {
vi.clearAllMocks()
mockDataSource = {
source: 'external',
external: { dialect: 'sqlite' },
rpc: { executeQuery: vi.fn() },
} as any
mockConfig = {
outerbaseApiKey: 'mock-api-key',
role: 'admin',
features: { allowlist: true, rls: true, rest: true },
}
})
describe('Database Dump Module', () => {
it('should return a database dump when tables exist', async () => {
// Pagination logic: if rows.length < BATCH_SIZE (1000), stop looping.
// Both tables return 2 rows → each table needs only 1 batch call.
//
// Call order:
// 0: table list → ['users', 'orders']
// 1: users schema
// 2: users data batch (2 rows < 1000 → stops)
// 3: orders schema
// 4: orders data batch (2 rows < 1000 → stops)
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }, { name: 'orders' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE users (id INTEGER, name TEXT)' },
])
.mockResolvedValueOnce([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE orders (id INTEGER, total REAL)' },
])
.mockResolvedValueOnce([
{ id: 1, total: 99.99 },
{ id: 2, total: 49.5 },
])
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
expect(response).toBeInstanceOf(Response)
expect(response.headers.get('Content-Type')).toBe(
'application/x-sqlite3'
)
expect(response.headers.get('Content-Disposition')).toBe(
'attachment; filename="database_dump.sql"'
)
const dumpText = await response.text()
expect(dumpText).toContain('CREATE TABLE users (id INTEGER, name TEXT)')
expect(dumpText).toContain('INSERT INTO "users" VALUES (1, \'Alice\');')
expect(dumpText).toContain('INSERT INTO "users" VALUES (2, \'Bob\');')
expect(dumpText).toContain(
'CREATE TABLE orders (id INTEGER, total REAL)'
)
expect(dumpText).toContain('INSERT INTO "orders" VALUES (1, 99.99);')
expect(dumpText).toContain('INSERT INTO "orders" VALUES (2, 49.5);')
// dump must be wrapped in a transaction
expect(dumpText).toContain('BEGIN TRANSACTION;')
expect(dumpText).toContain('COMMIT;')
})
it('should handle empty databases (no tables)', async () => {
vi.mocked(executeOperation).mockResolvedValueOnce([])
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
expect(response).toBeInstanceOf(Response)
expect(response.headers.get('Content-Type')).toBe(
'application/x-sqlite3'
)
const dumpText = await response.text()
// Should still contain header comment and transaction markers
expect(dumpText).toContain('StarbaseDB SQL dump')
expect(dumpText).toContain('BEGIN TRANSACTION;')
expect(dumpText).toContain('COMMIT;')
})
it('should handle databases with tables but no data', async () => {
// data batch returns 0 rows → exits immediately
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE users (id INTEGER, name TEXT)' },
])
.mockResolvedValueOnce([])
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
expect(response).toBeInstanceOf(Response)
const dumpText = await response.text()
expect(dumpText).toContain('CREATE TABLE users (id INTEGER, name TEXT)')
expect(dumpText).not.toContain('INSERT INTO')
})
it('should escape single quotes properly in string values', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE users (id INTEGER, bio TEXT)' },
])
// 1 row → length 1 < 1000 → stops after first batch
.mockResolvedValueOnce([{ id: 1, bio: "Alice's adventure" }])
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
expect(response).toBeInstanceOf(Response)
const dumpText = await response.text()
expect(dumpText).toContain(
"INSERT INTO \"users\" VALUES (1, 'Alice''s adventure');"
)
})
it('should handle NULL values correctly', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE users (id INTEGER, name TEXT)' },
])
.mockResolvedValueOnce([{ id: 1, name: null }])
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
const dumpText = await response.text()
expect(dumpText).toContain('INSERT INTO "users" VALUES (1, NULL);')
})
it('should handle boolean values as 1/0', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'flags' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE flags (id INTEGER, active INTEGER)' },
])
.mockResolvedValueOnce([
{ id: 1, active: true },
{ id: 2, active: false },
])
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
const dumpText = await response.text()
expect(dumpText).toContain('INSERT INTO "flags" VALUES (1, 1);')
expect(dumpText).toContain('INSERT INTO "flags" VALUES (2, 0);')
})
it('should paginate large tables across multiple batches', async () => {
// Simulate a table with exactly BATCH_SIZE (1000) rows in first batch,
// then 500 rows in second batch (signals end-of-data since 500 < 1000).
const firstBatch = Array.from({ length: 1000 }, (_, i) => ({
id: i + 1,
val: `row${i + 1}`,
}))
const secondBatch = Array.from({ length: 500 }, (_, i) => ({
id: 1001 + i,
val: `row${1001 + i}`,
}))
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'big_table' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE big_table (id INTEGER, val TEXT)' },
])
// first batch: full 1000 rows → pagination continues
.mockResolvedValueOnce(firstBatch)
// second batch: 500 rows < 1000 → stops pagination
.mockResolvedValueOnce(secondBatch)
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
const dumpText = await response.text()
// Rows from both batches should appear
expect(dumpText).toContain(
'INSERT INTO "big_table" VALUES (1, \'row1\');'
)
expect(dumpText).toContain(
'INSERT INTO "big_table" VALUES (1000, \'row1000\');'
)
expect(dumpText).toContain(
'INSERT INTO "big_table" VALUES (1001, \'row1001\');'
)
expect(dumpText).toContain(
'INSERT INTO "big_table" VALUES (1500, \'row1500\');'
)
})
it('should return a 500 response when an error occurs during setup', async () => {
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
// Reject the very first call (table list) so the outer try-catch fires
vi.mocked(executeOperation).mockRejectedValue(
new Error('Database Error')
)
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
expect(response.status).toBe(500)
const jsonResponse: { error: string } = await response.json()
expect(jsonResponse.error).toBe('Failed to create database dump')
})
})