|
| 1 | +import { jest } from '@jest/globals'; |
| 2 | +import { |
| 3 | + BigQuerySchemaService, |
| 4 | + getBigQueryTableSchemaSchema, |
| 5 | + listBigQueryDatasetsSchema, |
| 6 | + listBigQueryTablesSchema, |
| 7 | + type SchemaDiscoveryClient, |
| 8 | +} from '../bigQuerySchema.js'; |
| 9 | +import type { RedashQueryResult } from '../redashClient.js'; |
| 10 | + |
| 11 | +function queryResult(rows: Array<Record<string, unknown>>): RedashQueryResult { |
| 12 | + return { |
| 13 | + data: { |
| 14 | + columns: [], |
| 15 | + rows, |
| 16 | + }, |
| 17 | + } as unknown as RedashQueryResult; |
| 18 | +} |
| 19 | + |
| 20 | +function createMockClient() { |
| 21 | + return { |
| 22 | + getDataSource: jest.fn<any>(), |
| 23 | + getDataSources: jest.fn<any>(), |
| 24 | + executeAdhocQuery: jest.fn<any>(), |
| 25 | + getSchema: jest.fn<any>(), |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +describe('BigQuerySchemaService', () => { |
| 30 | + let client: ReturnType<typeof createMockClient>; |
| 31 | + let service: BigQuerySchemaService; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + client = createMockClient(); |
| 35 | + client.getDataSource.mockResolvedValue({ |
| 36 | + id: 4, |
| 37 | + name: 'BigQuery', |
| 38 | + type: 'bigquery', |
| 39 | + options: { |
| 40 | + projectId: 'kanabell-prod', |
| 41 | + location: 'asia-northeast1', |
| 42 | + }, |
| 43 | + }); |
| 44 | + client.getDataSources.mockResolvedValue([]); |
| 45 | + service = new BigQuerySchemaService(client as unknown as SchemaDiscoveryClient); |
| 46 | + }); |
| 47 | + |
| 48 | + it('blocks the unbounded Redash schema endpoint for BigQuery', async () => { |
| 49 | + await expect(service.getSchema(4)).rejects.toThrow( |
| 50 | + 'Use list_bigquery_datasets, then list_bigquery_tables, and finally get_bigquery_table_schema' |
| 51 | + ); |
| 52 | + expect(client.getSchema).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('keeps the existing schema endpoint for non-BigQuery data sources', async () => { |
| 56 | + client.getDataSource.mockResolvedValue({ id: 2, name: 'PostgreSQL', type: 'pg' }); |
| 57 | + client.getSchema.mockResolvedValue({ schema: [] }); |
| 58 | + |
| 59 | + await expect(service.getSchema(2)).resolves.toEqual({ schema: [] }); |
| 60 | + expect(client.getSchema).toHaveBeenCalledWith(2); |
| 61 | + }); |
| 62 | + |
| 63 | + it('falls back to the data source list when details omit the type', async () => { |
| 64 | + client.getDataSource.mockResolvedValue({ view_only: true }); |
| 65 | + client.getDataSources.mockResolvedValue([ |
| 66 | + { id: 4, name: 'BigQuery', type: 'bigquery_gce', view_only: true }, |
| 67 | + ]); |
| 68 | + |
| 69 | + await expect(service.getSchema(4)).rejects.toThrow('Unbounded BigQuery schema retrieval is disabled'); |
| 70 | + expect(client.getSchema).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('fails closed when the data source type cannot be determined', async () => { |
| 74 | + client.getDataSource.mockResolvedValue({ view_only: true }); |
| 75 | + client.getDataSources.mockResolvedValue([]); |
| 76 | + |
| 77 | + await expect(service.getSchema(4)).rejects.toThrow( |
| 78 | + 'Unable to determine data source 4 type; refusing schema discovery for safety' |
| 79 | + ); |
| 80 | + expect(client.getSchema).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('lists one bounded page of datasets using Redash data source configuration', async () => { |
| 84 | + client.executeAdhocQuery.mockResolvedValue(queryResult([ |
| 85 | + { catalog_name: 'kanabell-prod', schema_name: 'analytics', location: 'asia-northeast1' }, |
| 86 | + { catalog_name: 'kanabell-prod', schema_name: 'raw', location: 'asia-northeast1' }, |
| 87 | + { catalog_name: 'kanabell-prod', schema_name: 'staging', location: 'asia-northeast1' }, |
| 88 | + ])); |
| 89 | + |
| 90 | + const input = listBigQueryDatasetsSchema.parse({ dataSourceId: '4', pageSize: '2' }); |
| 91 | + const result = await service.listDatasets(input); |
| 92 | + |
| 93 | + expect(client.executeAdhocQuery).toHaveBeenCalledWith( |
| 94 | + expect.stringContaining('FROM `kanabell-prod`.`region-asia-northeast1`.INFORMATION_SCHEMA.SCHEMATA'), |
| 95 | + 4 |
| 96 | + ); |
| 97 | + const query = client.executeAdhocQuery.mock.calls[0][0] as string; |
| 98 | + expect(query).toContain('LIMIT 3\nOFFSET 0'); |
| 99 | + expect(result).toEqual({ |
| 100 | + page: 1, |
| 101 | + pageSize: 2, |
| 102 | + hasMore: true, |
| 103 | + nextPage: 2, |
| 104 | + datasets: [ |
| 105 | + { projectId: 'kanabell-prod', dataset: 'analytics', location: 'asia-northeast1' }, |
| 106 | + { projectId: 'kanabell-prod', dataset: 'raw', location: 'asia-northeast1' }, |
| 107 | + ], |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('lists tables for only the requested dataset and page', async () => { |
| 112 | + client.executeAdhocQuery.mockResolvedValue(queryResult([ |
| 113 | + { |
| 114 | + table_catalog: 'reporting-prod', |
| 115 | + table_schema: 'analytics', |
| 116 | + table_name: 'orders', |
| 117 | + table_type: 'BASE TABLE', |
| 118 | + creation_time: '2026-07-21T00:00:00Z', |
| 119 | + }, |
| 120 | + ])); |
| 121 | + |
| 122 | + const input = listBigQueryTablesSchema.parse({ |
| 123 | + dataSourceId: 4, |
| 124 | + projectId: 'reporting-prod', |
| 125 | + dataset: 'analytics', |
| 126 | + page: 2, |
| 127 | + pageSize: 2, |
| 128 | + }); |
| 129 | + const result = await service.listTables(input); |
| 130 | + |
| 131 | + const query = client.executeAdhocQuery.mock.calls[0][0] as string; |
| 132 | + expect(query).toContain('FROM `reporting-prod`.`analytics`.INFORMATION_SCHEMA.TABLES'); |
| 133 | + expect(query).toContain('LIMIT 3\nOFFSET 2'); |
| 134 | + expect(result.hasMore).toBe(false); |
| 135 | + expect(result.nextPage).toBeNull(); |
| 136 | + expect(result.tables[0]).toMatchObject({ name: 'orders', type: 'BASE TABLE' }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('accepts Redash query_result wrappers and defaults an empty configured location to US', async () => { |
| 140 | + client.getDataSource.mockResolvedValue({ |
| 141 | + id: 4, |
| 142 | + name: 'BigQuery', |
| 143 | + type: 'bigquery', |
| 144 | + options: { projectId: 'kanabell-prod', location: '' }, |
| 145 | + }); |
| 146 | + client.executeAdhocQuery.mockResolvedValue({ |
| 147 | + query_result: queryResult([ |
| 148 | + { catalog_name: 'kanabell-prod', schema_name: 'analytics', location: 'US' }, |
| 149 | + ]), |
| 150 | + }); |
| 151 | + |
| 152 | + const result = await service.listDatasets(listBigQueryDatasetsSchema.parse({ dataSourceId: 4 })); |
| 153 | + |
| 154 | + const query = client.executeAdhocQuery.mock.calls[0][0] as string; |
| 155 | + expect(query).toContain('FROM `kanabell-prod`.`region-us`.INFORMATION_SCHEMA.SCHEMATA'); |
| 156 | + expect(result.datasets).toEqual([ |
| 157 | + { projectId: 'kanabell-prod', dataset: 'analytics', location: 'US' }, |
| 158 | + ]); |
| 159 | + }); |
| 160 | + |
| 161 | + it('gets bounded column metadata for one table and escapes its name', async () => { |
| 162 | + client.executeAdhocQuery.mockResolvedValue(queryResult([ |
| 163 | + { |
| 164 | + column_name: 'order_id', |
| 165 | + ordinal_position: 1, |
| 166 | + data_type: 'STRING', |
| 167 | + is_nullable: 'NO', |
| 168 | + is_partitioning_column: 'YES', |
| 169 | + clustering_ordinal_position: null, |
| 170 | + }, |
| 171 | + ])); |
| 172 | + |
| 173 | + const input = getBigQueryTableSchemaSchema.parse({ |
| 174 | + dataSourceId: 4, |
| 175 | + dataset: 'analytics', |
| 176 | + table: "order's", |
| 177 | + pageSize: 100, |
| 178 | + }); |
| 179 | + const result = await service.getTableSchema(input); |
| 180 | + |
| 181 | + const query = client.executeAdhocQuery.mock.calls[0][0] as string; |
| 182 | + expect(query).toContain('FROM `kanabell-prod`.`analytics`.INFORMATION_SCHEMA.COLUMNS'); |
| 183 | + expect(query).toContain("WHERE table_name = 'order\\'s'"); |
| 184 | + expect(query).toContain('LIMIT 101\nOFFSET 0'); |
| 185 | + expect(result.columns).toEqual([ |
| 186 | + { |
| 187 | + name: 'order_id', |
| 188 | + position: 1, |
| 189 | + type: 'STRING', |
| 190 | + nullable: false, |
| 191 | + isPartitioningColumn: true, |
| 192 | + clusteringPosition: null, |
| 193 | + }, |
| 194 | + ]); |
| 195 | + }); |
| 196 | + |
| 197 | + it('rejects BigQuery-specific discovery for a different data source type', async () => { |
| 198 | + client.getDataSource.mockResolvedValue({ id: 2, name: 'MySQL', type: 'mysql' }); |
| 199 | + const input = listBigQueryTablesSchema.parse({ |
| 200 | + dataSourceId: 2, |
| 201 | + dataset: 'analytics', |
| 202 | + }); |
| 203 | + |
| 204 | + await expect(service.listTables(input)).rejects.toThrow('is not a BigQuery data source'); |
| 205 | + expect(client.executeAdhocQuery).not.toHaveBeenCalled(); |
| 206 | + }); |
| 207 | + |
| 208 | + it('rejects unsafe datasets and oversized pages before executing a query', () => { |
| 209 | + expect(() => listBigQueryTablesSchema.parse({ |
| 210 | + dataSourceId: 4, |
| 211 | + dataset: 'analytics` UNION ALL SELECT secret', |
| 212 | + })).toThrow(); |
| 213 | + expect(() => listBigQueryDatasetsSchema.parse({ |
| 214 | + dataSourceId: 4, |
| 215 | + pageSize: 101, |
| 216 | + })).toThrow(); |
| 217 | + }); |
| 218 | +}); |
0 commit comments