|
| 1 | +import { PostgresQuery } from '../../src/adapter/PostgresQuery'; |
| 2 | +import { prepareCompiler } from './PrepareCompiler'; |
| 3 | + |
| 4 | +describe('JavaScript SQL Comments Preservation', () => { |
| 5 | + it('preserves SQL comments in JS models', async () => { |
| 6 | + const { compiler, joinGraph, cubeEvaluator } = prepareCompiler([ |
| 7 | + { |
| 8 | + fileName: 'test.js', |
| 9 | + content: ` |
| 10 | + cube('JSTestCube', { |
| 11 | + sql: \` |
| 12 | + SELECT |
| 13 | + r.id as record_id, |
| 14 | + r.created_at as record_created_at, |
| 15 | + -- Extract target_record_id from workspace association JSON |
| 16 | + JSON_EXTRACT_SCALAR(workspace.value, '$[0].target_record_id') as workspace_target_record_id, |
| 17 | + -- Get actual workspace name by joining with workspace record |
| 18 | + CASE |
| 19 | + WHEN workspace_name.value IS NOT NULL |
| 20 | + THEN JSON_EXTRACT_SCALAR(JSON_EXTRACT_ARRAY(workspace_name.value)[OFFSET(0)], '$.value') |
| 21 | + ELSE NULL |
| 22 | + END as workspace_name |
| 23 | + FROM \\\`table\\\`.\\\`record\\\` r |
| 24 | + JOIN \\\`table\\\`.\\\`object\\\` o ON r.object_id = o.id |
| 25 | + -- Get company name |
| 26 | + LEFT JOIN \\\`table\\\`.\\\`record_value\\\` company_name ON r.id = company_name.record_id |
| 27 | + AND company_name.name = 'name' |
| 28 | + WHERE r._fivetran_deleted = FALSE |
| 29 | + AND o.singular_noun = 'Company' |
| 30 | + \`, |
| 31 | +
|
| 32 | + dimensions: { |
| 33 | + record_id: { |
| 34 | + sql: 'record_id', |
| 35 | + type: 'string', |
| 36 | + primaryKey: true |
| 37 | + } |
| 38 | + }, |
| 39 | +
|
| 40 | + measures: { |
| 41 | + count: { |
| 42 | + type: 'count' |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + ` |
| 47 | + } |
| 48 | + ]); |
| 49 | + |
| 50 | + await compiler.compile(); |
| 51 | + |
| 52 | + // Build a simple query to extract the actual SQL |
| 53 | + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, { |
| 54 | + measures: ['JSTestCube.count'], |
| 55 | + dimensions: ['JSTestCube.record_id'], |
| 56 | + timezone: 'UTC' |
| 57 | + }); |
| 58 | + |
| 59 | + const [sql] = query.buildSqlAndParams(); |
| 60 | + |
| 61 | + // Verify that SQL comments are preserved on separate lines |
| 62 | + expect(sql).toContain('-- Extract target_record_id from workspace association JSON'); |
| 63 | + expect(sql).toContain('-- Get actual workspace name by joining with workspace record'); |
| 64 | + expect(sql).toContain('-- Get company name'); |
| 65 | + |
| 66 | + // Ensure comments are on separate lines in JS models |
| 67 | + const lines = sql.split('\n'); |
| 68 | + const commentLine = lines.find(line => line.trim() === '-- Get company name'); |
| 69 | + expect(commentLine).toBeDefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('handles edge cases in JS SQL strings', async () => { |
| 73 | + const { compiler, joinGraph, cubeEvaluator } = prepareCompiler([ |
| 74 | + { |
| 75 | + fileName: 'edge-cases.js', |
| 76 | + content: ` |
| 77 | + cube('EdgeCasesTest', { |
| 78 | + sql: \` |
| 79 | + SELECT |
| 80 | + id, |
| 81 | + -- Comment with 'quotes' and "double quotes" |
| 82 | + name, |
| 83 | + -- Comment with special chars: !@#$%^&*() |
| 84 | + email, |
| 85 | + created_at |
| 86 | + FROM users |
| 87 | + -- SQL string in comment: SELECT * FROM table |
| 88 | + WHERE active = true |
| 89 | + \`, |
| 90 | +
|
| 91 | + dimensions: { |
| 92 | + id: { |
| 93 | + sql: 'id', |
| 94 | + type: 'string', |
| 95 | + primaryKey: true |
| 96 | + } |
| 97 | + }, |
| 98 | +
|
| 99 | + measures: { |
| 100 | + count: { |
| 101 | + type: 'count' |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + ` |
| 106 | + } |
| 107 | + ]); |
| 108 | + |
| 109 | + await compiler.compile(); |
| 110 | + |
| 111 | + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, { |
| 112 | + measures: ['EdgeCasesTest.count'], |
| 113 | + dimensions: ['EdgeCasesTest.id'], |
| 114 | + timezone: 'UTC' |
| 115 | + }); |
| 116 | + |
| 117 | + const [sql] = query.buildSqlAndParams(); |
| 118 | + |
| 119 | + const testLines = [ |
| 120 | + '-- Comment with \'quotes\' and "double quotes"', |
| 121 | + '-- Comment with special chars: !@#$%^&*()', |
| 122 | + '-- SQL string in comment: SELECT * FROM table', |
| 123 | + ]; |
| 124 | + |
| 125 | + // Ensure all comments are properly preserved |
| 126 | + const lines = sql.split('\n').map(l => l.trim()); |
| 127 | + for (const testLine of testLines) { |
| 128 | + expect(lines.includes(testLine)).toBeTruthy(); |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments