|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('Test 671 - UNION with ORDER BY', function () { |
| 7 | + const test = '671'; |
| 8 | + |
| 9 | + before(function () { |
| 10 | + alasql('create database test' + test); |
| 11 | + alasql('use test' + test); |
| 12 | + }); |
| 13 | + |
| 14 | + after(function () { |
| 15 | + alasql('drop database test' + test); |
| 16 | + }); |
| 17 | + |
| 18 | + it('A) UNION ALL with ORDER BY on alias', function () { |
| 19 | + var projects = [ |
| 20 | + {projectID: 1, Date1: '2023-01-01', Date2: '2023-02-01'}, |
| 21 | + {projectID: 2, Date1: '2023-03-01', Date2: '2023-01-15'}, |
| 22 | + {projectID: 3, Date1: '2023-02-01', Date2: '2023-04-01'}, |
| 23 | + ]; |
| 24 | + |
| 25 | + var res = alasql( |
| 26 | + 'SELECT projectID, Date1 as theDate FROM $0 UNION ALL SELECT projectID, Date2 as theDate FROM $0 ORDER BY theDate', |
| 27 | + [projects] |
| 28 | + ); |
| 29 | + |
| 30 | + assert.equal(res.length, 6); |
| 31 | + // Verify correct ascending order |
| 32 | + assert.equal(res[0].theDate, '2023-01-01'); |
| 33 | + assert.equal(res[1].theDate, '2023-01-15'); |
| 34 | + assert.equal(res[2].theDate, '2023-02-01'); |
| 35 | + assert.equal(res[3].theDate, '2023-02-01'); |
| 36 | + assert.equal(res[4].theDate, '2023-03-01'); |
| 37 | + assert.equal(res[5].theDate, '2023-04-01'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('B) UNION ALL with ORDER BY DESC', function () { |
| 41 | + var projects = [ |
| 42 | + {projectID: 1, Date1: '2023-01-01', Date2: '2023-02-01'}, |
| 43 | + {projectID: 2, Date1: '2023-03-01', Date2: '2023-01-15'}, |
| 44 | + ]; |
| 45 | + |
| 46 | + var res = alasql( |
| 47 | + 'SELECT projectID, Date1 as theDate FROM $0 UNION ALL SELECT projectID, Date2 as theDate FROM $0 ORDER BY theDate DESC', |
| 48 | + [projects] |
| 49 | + ); |
| 50 | + |
| 51 | + assert.equal(res.length, 4); |
| 52 | + // Verify correct descending order |
| 53 | + assert.equal(res[0].theDate, '2023-03-01'); |
| 54 | + assert.equal(res[1].theDate, '2023-02-01'); |
| 55 | + assert.equal(res[2].theDate, '2023-01-15'); |
| 56 | + assert.equal(res[3].theDate, '2023-01-01'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('C) UNION with ORDER BY (removes duplicates)', function () { |
| 60 | + var data = [ |
| 61 | + {a: 1, b: 'x'}, |
| 62 | + {a: 2, b: 'y'}, |
| 63 | + {a: 3, b: 'z'}, |
| 64 | + ]; |
| 65 | + |
| 66 | + var res = alasql( |
| 67 | + 'SELECT a FROM $0 WHERE a < 3 UNION SELECT a FROM $0 WHERE a > 1 ORDER BY a DESC', |
| 68 | + [data] |
| 69 | + ); |
| 70 | + |
| 71 | + assert.equal(res.length, 3); |
| 72 | + assert.equal(res[0].a, 3); |
| 73 | + assert.equal(res[1].a, 2); |
| 74 | + assert.equal(res[2].a, 1); |
| 75 | + }); |
| 76 | + |
| 77 | + it('D) UNION ALL with multiple ? parameters', function () { |
| 78 | + var data1 = [ |
| 79 | + {id: 1, value: 'a'}, |
| 80 | + {id: 2, value: 'b'}, |
| 81 | + ]; |
| 82 | + var data2 = [ |
| 83 | + {id: 3, value: 'c'}, |
| 84 | + {id: 4, value: 'd'}, |
| 85 | + ]; |
| 86 | + |
| 87 | + var res = alasql('SELECT id, value FROM ? UNION ALL SELECT id, value FROM ? ORDER BY id', [ |
| 88 | + data1, |
| 89 | + data2, |
| 90 | + ]); |
| 91 | + |
| 92 | + assert.equal(res.length, 4); |
| 93 | + assert.equal(res[0].id, 1); |
| 94 | + assert.equal(res[1].id, 2); |
| 95 | + assert.equal(res[2].id, 3); |
| 96 | + assert.equal(res[3].id, 4); |
| 97 | + }); |
| 98 | +}); |
0 commit comments