-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathcsv.test.ts
More file actions
149 lines (126 loc) · 4.49 KB
/
Copy pathcsv.test.ts
File metadata and controls
149 lines (126 loc) · 4.49 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
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { exportTableToCsvRoute } from './csv'
import { executeOperation } from './index'
import { createResponse } from '../utils'
import type { DataSource } from '../types'
import type { StarbaseDBConfiguration } from '../handler'
vi.mock('./index', () => ({
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
const tableColumns = (names: string[]) =>
names.map((name, index) => ({
cid: index,
name,
type: '',
notnull: 0,
dflt_value: null,
pk: name === 'id' ? 1 : 0,
}))
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 return a CSV file when table data exists', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'users' }])
.mockResolvedValueOnce(tableColumns(['id', 'name', 'age']))
.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 handle empty table (return only headers)', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'empty_table' }])
.mockResolvedValueOnce(tableColumns(['id', 'name']))
.mockResolvedValueOnce([])
const response = await exportTableToCsvRoute(
'empty_table',
mockDataSource,
mockConfig
)
expect(response.headers.get('Content-Type')).toBe('text/csv')
await expect(response.text()).resolves.toBe('id,name\n')
})
it('should escape commas and quotes in CSV values', async () => {
vi.mocked(executeOperation)
.mockResolvedValueOnce([{ name: 'special_chars' }])
.mockResolvedValueOnce(tableColumns(['id', 'name', 'bio']))
.mockResolvedValueOnce([
{ id: 1, name: 'Sahithi, is', bio: 'my forever "penguin"' },
])
const response = await exportTableToCsvRoute(
'special_chars',
mockDataSource,
mockConfig
)
await expect(response.text()).resolves.toBe(
'id,name,bio\n1,"Sahithi, is","my forever ""penguin"""\n'
)
expect(response.headers.get('Content-Type')).toBe('text/csv')
})
it('should return 500 on an unexpected error', 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')
})
})