|
| 1 | +import { getTestCompiler } from '@vulcan-sql/test-utility'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +it('Should replace with table name of dbt model with type table', async () => { |
| 5 | + // Arrange |
| 6 | + const { compileAndLoad, execute, getExecutedQueries } = await getTestCompiler( |
| 7 | + { |
| 8 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 9 | + config: { |
| 10 | + dbt: { |
| 11 | + modelFiles: [path.join(__dirname, 'test-artifact.json')], |
| 12 | + }, |
| 13 | + }, |
| 14 | + } |
| 15 | + ); |
| 16 | + |
| 17 | + // Act. |
| 18 | + await compileAndLoad(`select * from {% dbt "model.test.1_table" %}`); |
| 19 | + await execute({}); |
| 20 | + |
| 21 | + // Assert |
| 22 | + const queries = await getExecutedQueries(); |
| 23 | + expect(queries[0]).toBe('select * from "postgres"."public"."1_table"'); |
| 24 | +}); |
| 25 | + |
| 26 | +it('Should replace with table name of dbt model with type view', async () => { |
| 27 | + // Arrange |
| 28 | + const { compileAndLoad, execute, getExecutedQueries } = await getTestCompiler( |
| 29 | + { |
| 30 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 31 | + config: { |
| 32 | + dbt: { |
| 33 | + modelFiles: [path.join(__dirname, 'test-artifact.json')], |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + ); |
| 38 | + |
| 39 | + // Act. |
| 40 | + await compileAndLoad(`select * from {% dbt "model.test.2_view" %}`); |
| 41 | + await execute({}); |
| 42 | + |
| 43 | + // Assert |
| 44 | + const queries = await getExecutedQueries(); |
| 45 | + expect(queries[0]).toBe('select * from "postgres"."public"."2_view"'); |
| 46 | +}); |
| 47 | + |
| 48 | +it('Should replace with sub-query of dbt model with type ephemeral', async () => { |
| 49 | + // Arrange |
| 50 | + const { compileAndLoad, execute, getExecutedQueries } = await getTestCompiler( |
| 51 | + { |
| 52 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 53 | + config: { |
| 54 | + dbt: { |
| 55 | + modelFiles: [path.join(__dirname, 'test-artifact.json')], |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + // Act. |
| 62 | + await compileAndLoad( |
| 63 | + `select sub.* from {% dbt "model.test.3_ephemeral" %} as sub` |
| 64 | + ); |
| 65 | + await execute({}); |
| 66 | + |
| 67 | + // Assert |
| 68 | + const queries = await getExecutedQueries(); |
| 69 | + expect(queries[0]).toBe(`select sub.* from ( |
| 70 | +select * |
| 71 | +from "postgres"."public"."1_table" |
| 72 | +where age <= 18) as sub`); |
| 73 | +}); |
| 74 | + |
| 75 | +it('Should replace with table name of dbt model with type incremental', async () => { |
| 76 | + // Arrange |
| 77 | + const { compileAndLoad, execute, getExecutedQueries } = await getTestCompiler( |
| 78 | + { |
| 79 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 80 | + config: { |
| 81 | + dbt: { |
| 82 | + modelFiles: [path.join(__dirname, 'test-artifact.json')], |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + // Act. |
| 89 | + await compileAndLoad(`select * from {% dbt "model.test.4_incremental" %}`); |
| 90 | + await execute({}); |
| 91 | + |
| 92 | + // Assert |
| 93 | + const queries = await getExecutedQueries(); |
| 94 | + expect(queries[0]).toBe('select * from "postgres"."public"."4_incremental"'); |
| 95 | +}); |
| 96 | + |
| 97 | +it('Should merge multiple artifacts', async () => { |
| 98 | + // Arrange |
| 99 | + const { compileAndLoad, execute, getExecutedQueries } = await getTestCompiler( |
| 100 | + { |
| 101 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 102 | + config: { |
| 103 | + dbt: { |
| 104 | + modelFiles: [ |
| 105 | + path.join(__dirname, 'test-artifact.json'), |
| 106 | + path.join(__dirname, 'test-artifact-2.json'), |
| 107 | + ], |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + // Act. |
| 114 | + await compileAndLoad( |
| 115 | + `{% dbt "model.test.4_incremental" %}{% dbt "model.test.5_model_from_artifact_2" %}` |
| 116 | + ); |
| 117 | + await execute({}); |
| 118 | + |
| 119 | + // Assert |
| 120 | + const queries = await getExecutedQueries(); |
| 121 | + expect(queries[0]).toBe( |
| 122 | + '"postgres"."public"."4_incremental""postgres"."public"."1_table"' |
| 123 | + ); |
| 124 | +}); |
| 125 | + |
| 126 | +it('Should throw error when models are unambiguous', async () => { |
| 127 | + // Arrange |
| 128 | + const { compileAndLoad } = await getTestCompiler({ |
| 129 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 130 | + config: { |
| 131 | + dbt: { |
| 132 | + modelFiles: [ |
| 133 | + path.join(__dirname, 'test-artifact-2.json'), |
| 134 | + path.join(__dirname, 'test-artifact-2.json'), |
| 135 | + ], |
| 136 | + }, |
| 137 | + }, |
| 138 | + }); |
| 139 | + |
| 140 | + // Act. Assert |
| 141 | + await expect(compileAndLoad(`Some sql`)).rejects.toThrow( |
| 142 | + `Model name model.test.5_model_from_artifact_2 is unambiguous` |
| 143 | + ); |
| 144 | +}); |
| 145 | + |
| 146 | +it('Should throw error when model name not found', async () => { |
| 147 | + // Arrange |
| 148 | + const { compileAndLoad } = await getTestCompiler({ |
| 149 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 150 | + config: { |
| 151 | + dbt: { |
| 152 | + modelFiles: [path.join(__dirname, 'test-artifact.json')], |
| 153 | + }, |
| 154 | + }, |
| 155 | + }); |
| 156 | + |
| 157 | + // Act. Assert |
| 158 | + await expect(compileAndLoad(`{% dbt "not.found.model" %}`)).rejects.toThrow( |
| 159 | + `Model not.found.model is not found in modelFiles` |
| 160 | + ); |
| 161 | +}); |
| 162 | + |
| 163 | +it('Should throw error when argument type is not correct', async () => { |
| 164 | + // Arrange |
| 165 | + const { compileAndLoad } = await getTestCompiler({ |
| 166 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 167 | + config: { |
| 168 | + dbt: { |
| 169 | + modelFiles: [path.join(__dirname, 'test-artifact.json')], |
| 170 | + }, |
| 171 | + }, |
| 172 | + }); |
| 173 | + |
| 174 | + // Act. Assert |
| 175 | + await expect(compileAndLoad(`{% dbt model.test.1_table %}`)).rejects.toThrow( |
| 176 | + `Expect model name as string, but got symbol` |
| 177 | + ); |
| 178 | +}); |
| 179 | + |
| 180 | +it('Should throw error when there are too many arguments', async () => { |
| 181 | + // Arrange |
| 182 | + const { compileAndLoad } = await getTestCompiler({ |
| 183 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 184 | + config: { |
| 185 | + dbt: { |
| 186 | + modelFiles: [path.join(__dirname, 'test-artifact.json')], |
| 187 | + }, |
| 188 | + }, |
| 189 | + }); |
| 190 | + |
| 191 | + // Act. Assert |
| 192 | + await expect( |
| 193 | + compileAndLoad(`{% dbt "model.test.1_table" extra arg %}`) |
| 194 | + ).rejects.toThrow(`Expect block end %}, but got symbol`); |
| 195 | +}); |
| 196 | + |
| 197 | +it('Should not throw error even if there is no dbt config', async () => { |
| 198 | + // Arrange |
| 199 | + const { compileAndLoad, execute, getExecutedQueries } = await getTestCompiler( |
| 200 | + { |
| 201 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 202 | + config: {}, |
| 203 | + } |
| 204 | + ); |
| 205 | + |
| 206 | + // Act. |
| 207 | + await compileAndLoad(`some query`); |
| 208 | + await execute({}); |
| 209 | + |
| 210 | + // Assert |
| 211 | + const queries = await getExecutedQueries(); |
| 212 | + expect(queries[0]).toBe('some query'); |
| 213 | +}); |
| 214 | + |
| 215 | +it('Should not throw error even if the artifact file is empty', async () => { |
| 216 | + // Arrange |
| 217 | + const { compileAndLoad, execute, getExecutedQueries } = await getTestCompiler( |
| 218 | + { |
| 219 | + extensionNames: [path.join(__dirname, '..', 'src')], |
| 220 | + config: { |
| 221 | + dbt: { |
| 222 | + modelFiles: [path.join(__dirname, 'empty-artifact.json')], |
| 223 | + }, |
| 224 | + }, |
| 225 | + } |
| 226 | + ); |
| 227 | + |
| 228 | + // Act. |
| 229 | + await compileAndLoad(`some query`); |
| 230 | + await execute({}); |
| 231 | + |
| 232 | + // Assert |
| 233 | + const queries = await getExecutedQueries(); |
| 234 | + expect(queries[0]).toBe('some query'); |
| 235 | +}); |
0 commit comments