-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathdump.test.ts
More file actions
211 lines (180 loc) · 7.51 KB
/
dump.test.ts
File metadata and controls
211 lines (180 loc) · 7.51 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
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 () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }, { name: 'orders' }])
// users schema
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE users (id INTEGER, name TEXT);' },
])
// users count
.mockResolvedValueOnce([{ count: 2 }])
// users data batch
.mockResolvedValueOnce([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
])
// orders schema
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE orders (id INTEGER, total REAL);' },
])
// orders count
.mockResolvedValueOnce([{ count: 2 }])
// orders data batch
.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);')
})
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()
expect(dumpText).toBe('SQLite format 3\0')
})
it('should handle databases with tables but no data', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE users (id INTEGER, name TEXT);' },
])
// count returns 0
.mockResolvedValueOnce([{ count: 0 }])
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 users VALUES')
})
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);' },
])
.mockResolvedValueOnce([{ count: 1 }])
.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 return a 500 response when an error occurs', async () => {
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
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')
})
it('should stream data in batches for large tables', async () => {
// Simulate a table with more rows than BATCH_SIZE (5000)
const largeBatch = Array.from({ length: 5000 }, (_, i) => ({
id: i + 1,
name: `User${i + 1}`,
}))
const smallBatch = Array.from({ length: 500 }, (_, i) => ({
id: 5001 + i,
name: `User${5001 + i}`,
}))
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE users (id INTEGER, name TEXT);' },
])
.mockResolvedValueOnce([{ count: 5500 }])
// First batch of 5000
.mockResolvedValueOnce(largeBatch)
// Second batch of 500
.mockResolvedValueOnce(smallBatch)
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
expect(response).toBeInstanceOf(Response)
const dumpText = await response.text()
// Verify first and last rows from first batch
expect(dumpText).toContain("INSERT INTO users VALUES (1, 'User1');")
expect(dumpText).toContain(
"INSERT INTO users VALUES (5000, 'User5000');"
)
// Verify rows from second batch
expect(dumpText).toContain(
"INSERT INTO users VALUES (5001, 'User5001');"
)
expect(dumpText).toContain(
"INSERT INTO users VALUES (5500, 'User5500');"
)
// Verify executeOperation was called with LIMIT/OFFSET queries
const calls = vi.mocked(executeOperation).mock.calls
// Call 4 (index 3): first batch with OFFSET 0
expect(calls[3][0][0].sql).toContain('LIMIT 5000 OFFSET 0')
// Call 5 (index 4): second batch with OFFSET 5000
expect(calls[4][0][0].sql).toContain('LIMIT 5000 OFFSET 5000')
})
it('should use Transfer-Encoding chunked header for streaming', async () => {
vi.mocked(executeOperation).mockResolvedValueOnce([])
const response = await dumpDatabaseRoute(mockDataSource, mockConfig)
expect(response.headers.get('Transfer-Encoding')).toBe('chunked')
})
})