|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('Test 167B - SEARCH DISTINCT and SELECT DISTINCT functions', function () { |
| 7 | + it('1. Basic DISTINCT comparison', () => { |
| 8 | + var data = [{a: 1}, {a: 2}, {a: 1}]; |
| 9 | + |
| 10 | + var res1 = alasql('SELECT * FROM ?', [data]); |
| 11 | + var res2 = alasql('SELECT DISTINCT * FROM ?', [data]); |
| 12 | + var res3 = alasql('SELECT COLUMN DISTINCT _ FROM ?', [data]); |
| 13 | + var res4 = alasql('SEARCH DISTINCT(/) FROM ?', [data]); |
| 14 | + |
| 15 | + console.log('res1 (SELECT *):', JSON.stringify(res1)); |
| 16 | + console.log('res2 (SELECT DISTINCT *):', JSON.stringify(res2)); |
| 17 | + console.log('res3 (SELECT COLUMN DISTINCT _):', JSON.stringify(res3)); |
| 18 | + console.log('res4 (SEARCH DISTINCT(/)):', JSON.stringify(res4)); |
| 19 | + |
| 20 | + // res1 should have all 3 items |
| 21 | + assert.equal(res1.length, 3); |
| 22 | + |
| 23 | + // res2, res3, res4 should all have 2 distinct items |
| 24 | + assert.equal(res2.length, 2, 'SELECT DISTINCT * should return 2 items'); |
| 25 | + assert.equal(res3.length, 2, 'SELECT COLUMN DISTINCT _ should return 2 items'); |
| 26 | + assert.equal(res4.length, 2, 'SEARCH DISTINCT(/) should return 2 items'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('2. DISTINCT with different middle value (issue b)', () => { |
| 30 | + var data = [{a: 1}, {b: 2}, {a: 1}]; |
| 31 | + |
| 32 | + var res3 = alasql('SELECT COLUMN DISTINCT _ FROM ?', [data]); |
| 33 | + var res4 = alasql('SEARCH DISTINCT(/) FROM ?', [data]); |
| 34 | + |
| 35 | + console.log('res3 (SELECT COLUMN DISTINCT _) with mixed data:', JSON.stringify(res3)); |
| 36 | + console.log('res4 (SEARCH DISTINCT(/)) with mixed data:', JSON.stringify(res4)); |
| 37 | + |
| 38 | + // Should return 2 distinct objects: {a:1} and {b:2} |
| 39 | + assert.equal(res3.length, 2, 'SELECT COLUMN DISTINCT _ should return 2 items'); |
| 40 | + assert.equal(res4.length, 2, 'SEARCH DISTINCT(/) should return 2 items'); |
| 41 | + |
| 42 | + // Check that we have both distinct objects |
| 43 | + var hasA1 = res3.some(function (item) { |
| 44 | + return item.a === 1 && !item.b; |
| 45 | + }); |
| 46 | + var hasB2 = res3.some(function (item) { |
| 47 | + return item.b === 2 && !item.a; |
| 48 | + }); |
| 49 | + |
| 50 | + assert(hasA1, 'Result should contain {a:1}'); |
| 51 | + assert(hasB2, 'Result should contain {b:2}'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('3. SEARCH DISTINCT vs SELECT DISTINCT consistency', () => { |
| 55 | + var data = [{a: 1}, {b: 2}, {a: 1}]; |
| 56 | + |
| 57 | + var res2 = alasql('SELECT DISTINCT * FROM ?', [data]); |
| 58 | + var res4 = alasql('SEARCH DISTINCT(/) FROM ?', [data]); |
| 59 | + |
| 60 | + console.log('res2 (SELECT DISTINCT *):', JSON.stringify(res2)); |
| 61 | + console.log('res4 (SEARCH DISTINCT(/)):', JSON.stringify(res4)); |
| 62 | + |
| 63 | + // Results should be the same |
| 64 | + assert.deepEqual( |
| 65 | + res2, |
| 66 | + res4, |
| 67 | + 'SELECT DISTINCT * and SEARCH DISTINCT(/) should produce same results' |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('4. SELECT COLUMN DISTINCT _ with arrays', () => { |
| 72 | + var data = [1, 2, 1, 3, 2]; |
| 73 | + |
| 74 | + var res = alasql('SELECT COLUMN DISTINCT _ FROM ?', [data]); |
| 75 | + console.log('SELECT COLUMN DISTINCT _ from array:', JSON.stringify(res)); |
| 76 | + |
| 77 | + // Should return distinct values: [1, 2, 3] |
| 78 | + assert.equal(res.length, 3, 'Should return 3 distinct values'); |
| 79 | + assert(res.includes(1), 'Should include 1'); |
| 80 | + assert(res.includes(2), 'Should include 2'); |
| 81 | + assert(res.includes(3), 'Should include 3'); |
| 82 | + }); |
| 83 | +}); |
0 commit comments