|
| 1 | +import { TableSchema } from '../types/cube-types'; |
1 | 2 | import { meerkatPlaceholderReplacer } from './meerkat-placeholder-replacer';
|
2 | 3 |
|
3 |
| -describe("meerkatPlaceholderReplacer", () => { |
4 |
| - it("should not replace placeholders with tableName if placeholder pattern doesnt end in .", () => { |
5 |
| - const sql = "SELECT * FROM {MEERKAT}fieldName"; |
6 |
| - const tableName = "customers"; |
7 |
| - expect(meerkatPlaceholderReplacer(sql, tableName)).toEqual("SELECT * FROM {MEERKAT}fieldName"); |
8 |
| - }); |
9 |
| - |
10 |
| - it("should replace multiple placeholders in the SQL query", () => { |
11 |
| - const sql = "SELECT {MEERKAT}.a, {MEERKAT}.b FROM orders"; |
12 |
| - const tableName = "orders"; |
13 |
| - expect(meerkatPlaceholderReplacer(sql, tableName)).toEqual('SELECT orders__a, orders__b FROM orders'); |
14 |
| - }); |
15 |
| - |
16 |
| - it("should be case sensitive", () => { |
17 |
| - const sql = "SELECT {meerkat}.a FROM orders"; |
18 |
| - const tableName = "orders"; |
19 |
| - expect(meerkatPlaceholderReplacer(sql, tableName)).toEqual('SELECT {meerkat}.a FROM orders'); |
20 |
| - }); |
21 |
| - |
22 |
| - it("should replace the correct match", () => { |
23 |
| - const sql = "SELECT {MEERKAT.{MEERKAT}.}.a FROM customers"; |
24 |
| - const tableName = "customers"; |
25 |
| - expect(meerkatPlaceholderReplacer(sql, tableName)).toEqual("SELECT {MEERKAT.customers__}.a FROM customers"); |
26 |
| - }); |
27 |
| - |
28 |
| - it("should handle empty SQL queries", () => { |
29 |
| - const sql = ""; |
30 |
| - const tableName = "customers"; |
31 |
| - expect(meerkatPlaceholderReplacer(sql, tableName)).toEqual(''); |
32 |
| - }); |
33 |
| - |
34 |
| - it("should handle SQL queries without placeholders", () => { |
35 |
| - const sql = "SELECT * FROM customers."; |
36 |
| - const tableName = "orders"; |
37 |
| - expect(meerkatPlaceholderReplacer(sql, tableName)).toEqual('SELECT * FROM customers.'); |
38 |
| - }); |
39 |
| - |
40 |
| - |
| 4 | +describe('meerkatPlaceholderReplacer', () => { |
| 5 | + let tableSchema: TableSchema; |
| 6 | + beforeEach(() => { |
| 7 | + tableSchema = { |
| 8 | + name: 'test', |
| 9 | + sql: 'test', |
| 10 | + measures: [ |
| 11 | + { name: 'measure1', sql: 'COUNT(*)', type: 'number', alias: 'alias1' }, |
| 12 | + ], |
| 13 | + dimensions: [], |
| 14 | + }; |
| 15 | + }); |
| 16 | + |
| 17 | + it('should not replace placeholders with tableName if placeholder pattern doesnt end in .', () => { |
| 18 | + const sql = 'SELECT * FROM {MEERKAT}fieldName'; |
| 19 | + const tableName = 'customers'; |
| 20 | + expect(meerkatPlaceholderReplacer(sql, tableName, tableSchema)).toEqual( |
| 21 | + 'SELECT * FROM {MEERKAT}fieldName' |
| 22 | + ); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should replace multiple placeholders in the SQL query', () => { |
| 26 | + const sql = 'SELECT {MEERKAT}.a, {MEERKAT}.b FROM orders'; |
| 27 | + const tableName = 'orders'; |
| 28 | + expect(meerkatPlaceholderReplacer(sql, tableName, tableSchema)).toEqual( |
| 29 | + 'SELECT orders__a, orders__b FROM orders' |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should be case sensitive', () => { |
| 34 | + const sql = 'SELECT {meerkat}.a FROM orders'; |
| 35 | + const tableName = 'orders'; |
| 36 | + expect(meerkatPlaceholderReplacer(sql, tableName, tableSchema)).toEqual( |
| 37 | + 'SELECT {meerkat}.a FROM orders' |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should replace the correct match', () => { |
| 42 | + const sql = 'SELECT {MEERKAT.{MEERKAT}.a}.a FROM customers'; |
| 43 | + const tableName = 'customers'; |
| 44 | + expect(meerkatPlaceholderReplacer(sql, tableName, tableSchema)).toEqual( |
| 45 | + 'SELECT {MEERKAT.customers__a}.a FROM customers' |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should handle empty SQL queries', () => { |
| 50 | + const sql = ''; |
| 51 | + const tableName = 'customers'; |
| 52 | + expect(meerkatPlaceholderReplacer(sql, tableName, tableSchema)).toEqual(''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle SQL queries without placeholders', () => { |
| 56 | + const sql = 'SELECT * FROM customers.'; |
| 57 | + const tableName = 'orders'; |
| 58 | + expect(meerkatPlaceholderReplacer(sql, tableName, tableSchema)).toEqual( |
| 59 | + 'SELECT * FROM customers.' |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should replace placeholders with alias if provided', () => { |
| 64 | + const sql = 'SELECT {MEERKAT}.measure1 FROM orders'; |
| 65 | + const tableName = 'orders'; |
| 66 | + expect(meerkatPlaceholderReplacer(sql, tableName, tableSchema)).toEqual( |
| 67 | + 'SELECT "alias1" FROM orders' |
| 68 | + ); |
| 69 | + }); |
41 | 70 | });
|
0 commit comments