-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathcsv.test.ts
More file actions
218 lines (191 loc) · 6.7 KB
/
Copy pathcsv.test.ts
File metadata and controls
218 lines (191 loc) · 6.7 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
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { exportTableToCsvRoute } from './csv'
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('CSV Export Module', () => {
it('should stream a CSV file when table data exists', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce([
{ name: 'id' },
{ name: 'name' },
{ name: 'age' },
])
.mockResolvedValueOnce([
{
sql: 'CREATE TABLE users (id INTEGER, name TEXT, age INTEGER);',
},
])
.mockResolvedValueOnce([
{ id: 1, name: 'Alice', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
])
const response = await exportTableToCsvRoute(
'users',
mockDataSource,
mockConfig
)
expect(response.headers.get('Content-Type')).toBe('text/csv')
expect(response.headers.get('Content-Disposition')).toBe(
'attachment; filename="users_export.csv"'
)
await expect(response.text()).resolves.toBe(
'id,name,age\n1,Alice,30\n2,Bob,25\n'
)
})
it('should return 404 if table does not exist', async () => {
vi.mocked(executeOperation).mockResolvedValueOnce([])
const response = await exportTableToCsvRoute(
'non_existent_table',
mockDataSource,
mockConfig
)
expect(response.status).toBe(404)
const jsonResponse: { error: string } = await response.json()
expect(jsonResponse.error).toBe(
"Table 'non_existent_table' does not exist."
)
})
it('should include headers for empty tables', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'empty_table' }])
.mockResolvedValueOnce([{ name: 'id' }, { name: 'name' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE empty_table (id INTEGER, name TEXT);' },
])
.mockResolvedValueOnce([])
const response = await exportTableToCsvRoute(
'empty_table',
mockDataSource,
mockConfig
)
await expect(response.text()).resolves.toBe('id,name\n')
})
it('should escape commas, quotes, and newlines in CSV values', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'special_chars' }])
.mockResolvedValueOnce([
{ name: 'id' },
{ name: 'name' },
{ name: 'bio' },
])
.mockResolvedValueOnce([
{
sql: 'CREATE TABLE special_chars (id INTEGER, name TEXT, bio TEXT);',
},
])
.mockResolvedValueOnce([
{
id: 1,
name: 'Sahithi, is',
bio: 'my forever "penguin"\nline',
},
])
const response = await exportTableToCsvRoute(
'special_chars',
mockDataSource,
mockConfig
)
await expect(response.text()).resolves.toBe(
'id,name,bio\n1,"Sahithi, is","my forever ""penguin""\nline"\n'
)
})
it('should page table data instead of loading the full table', async () => {
const firstPage = Array.from({ length: 1000 }, (_, index) => ({
__starbasedb_export_cursor_rowid: index + 1,
id: index + 1,
name: `User ${index + 1}`,
}))
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce([{ name: 'id' }, { name: 'name' }])
.mockResolvedValueOnce([
{ sql: 'CREATE TABLE users (id INTEGER, name TEXT);' },
])
.mockResolvedValueOnce(firstPage)
.mockResolvedValueOnce([
{
__starbasedb_export_cursor_rowid: 1001,
id: 1001,
name: 'Last User',
},
])
const response = await exportTableToCsvRoute(
'users',
mockDataSource,
mockConfig
)
const csv = await response.text()
expect(csv).toContain('1001,Last User\n')
expect(executeOperation).toHaveBeenNthCalledWith(
4,
[
{
sql: 'SELECT rowid AS "__starbasedb_export_cursor_rowid", "id", "name" FROM "users" ORDER BY rowid LIMIT ?;',
params: [1000],
},
],
mockDataSource,
mockConfig
)
expect(executeOperation).toHaveBeenNthCalledWith(
5,
[
{
sql: 'SELECT rowid AS "__starbasedb_export_cursor_rowid", "id", "name" FROM "users" WHERE rowid > ? ORDER BY rowid LIMIT ?;',
params: [1000, 1000],
},
],
mockDataSource,
mockConfig
)
})
it('should return 500 on an unexpected error before streaming starts', async () => {
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
vi.mocked(executeOperation).mockRejectedValue(
new Error('Database Error')
)
const response = await exportTableToCsvRoute(
'users',
mockDataSource,
mockConfig
)
expect(response.status).toBe(500)
const jsonResponse: { error: string } = await response.json()
expect(jsonResponse.error).toBe('Failed to export table to CSV')
consoleErrorMock.mockRestore()
})
})