|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('Test 485 - UNION (ALL) with HTML tables', function () { |
| 7 | + const test = '485'; |
| 8 | + |
| 9 | + // This test documents the fix for issue #485 |
| 10 | + // The bug was in src/84from.js line 44: if (!sel && sel.tagName !== 'TABLE') |
| 11 | + // Changed to: if (!sel || sel.tagName !== 'TABLE') |
| 12 | + // |
| 13 | + // The original logic used && (AND) which would never properly validate: |
| 14 | + // - If !sel is true, sel.tagName would error |
| 15 | + // - The condition should check if EITHER sel is null OR it's not a TABLE |
| 16 | + // |
| 17 | + // This caused UNION operations on HTML tables to fail silently, |
| 18 | + // resulting in empty objects for rows from the second table. |
| 19 | + |
| 20 | + it('A) Documents the bug fix for HTML table validation', function () { |
| 21 | + // This test verifies that the logic fix is correct |
| 22 | + // The actual HTML table functionality requires a DOM which isn't available in Node.js tests |
| 23 | + |
| 24 | + // Simulate the old buggy logic |
| 25 | + var sel = null; |
| 26 | + var oldLogic = !sel && (sel ? sel.tagName !== 'TABLE' : false); |
| 27 | + // With old logic (!sel && ...), this evaluates to false when sel is null |
| 28 | + assert.equal(oldLogic, false, 'Old logic fails to detect null selector'); |
| 29 | + |
| 30 | + // Simulate the fixed logic |
| 31 | + var newLogic = !sel || (sel ? sel.tagName !== 'TABLE' : false); |
| 32 | + // With new logic (!sel || ...), this correctly evaluates to true when sel is null |
| 33 | + assert.equal(newLogic, true, 'New logic correctly detects null selector'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('B) UNION ALL works with regular tables', function () { |
| 37 | + // This verifies UNION ALL functionality with regular in-memory tables |
| 38 | + // which is the same operation that would be performed on HTML tables |
| 39 | + |
| 40 | + var res = alasql('SELECT 1 as ID, "John" as Name UNION ALL SELECT 2 as ID, "Jane" as Name'); |
| 41 | + |
| 42 | + assert.equal(res.length, 2, 'UNION ALL should return 2 rows'); |
| 43 | + assert.deepEqual( |
| 44 | + res, |
| 45 | + [ |
| 46 | + {ID: 1, Name: 'John'}, |
| 47 | + {ID: 2, Name: 'Jane'}, |
| 48 | + ], |
| 49 | + 'Both rows should have data' |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('C) UNION works with regular tables', function () { |
| 54 | + var res = alasql( |
| 55 | + 'SELECT 1 as ID, "John" as Name UNION SELECT 2 as ID, "Jane" as Name ORDER BY ID' |
| 56 | + ); |
| 57 | + |
| 58 | + assert.equal(res.length, 2, 'UNION should return 2 rows'); |
| 59 | + assert.deepEqual( |
| 60 | + res, |
| 61 | + [ |
| 62 | + {ID: 1, Name: 'John'}, |
| 63 | + {ID: 2, Name: 'Jane'}, |
| 64 | + ], |
| 65 | + 'Both rows should have data' |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('D) UNION ALL removes duplicates correctly', function () { |
| 70 | + var res = alasql('SELECT 1 as ID UNION SELECT 1 as ID'); |
| 71 | + |
| 72 | + assert.equal(res.length, 1, 'UNION should remove duplicates'); |
| 73 | + assert.deepEqual(res, [{ID: 1}], 'Only one row should remain'); |
| 74 | + }); |
| 75 | +}); |
0 commit comments