|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('Test 79-B - GROUP BY with no matching rows', function () { |
| 7 | + const test = '79-B'; |
| 8 | + |
| 9 | + it('A) GROUP BY returns empty result when no rows meet condition', function () { |
| 10 | + // Original issue: SELECT with GROUP BY should return [] when WHERE clause matches no rows |
| 11 | + var res = alasql('SELECT Name FROM ? WHERE 1=0 GROUP BY Name', [[{Name: 'test'}]]); |
| 12 | + assert.deepEqual(res, []); |
| 13 | + }); |
| 14 | + |
| 15 | + it('B) GROUP BY with aggregate returns empty result when no rows meet condition', function () { |
| 16 | + var res = alasql('SELECT Name, COUNT(*) as cnt FROM ? WHERE 1=0 GROUP BY Name', [ |
| 17 | + [{Name: 'test'}], |
| 18 | + ]); |
| 19 | + assert.deepEqual(res, []); |
| 20 | + }); |
| 21 | + |
| 22 | + it('C) GROUP BY on empty dataset returns empty result', function () { |
| 23 | + var res = alasql('SELECT Name FROM ? GROUP BY Name', [[]]); |
| 24 | + assert.deepEqual(res, []); |
| 25 | + }); |
| 26 | + |
| 27 | + it('D) GROUP BY with multiple columns and no matches returns empty result', function () { |
| 28 | + var res = alasql('SELECT Name, Type FROM ? WHERE 1=0 GROUP BY Name, Type', [ |
| 29 | + [{Name: 'test', Type: 'A'}], |
| 30 | + ]); |
| 31 | + assert.deepEqual(res, []); |
| 32 | + }); |
| 33 | + |
| 34 | + it('E) Aggregate without GROUP BY returns one row with default values when no rows match', function () { |
| 35 | + // This is the correct SQL behavior - aggregates without GROUP BY return one row |
| 36 | + var res = alasql('SELECT COUNT(*) as cnt FROM ? WHERE 1=0', [[{Name: 'test'}]]); |
| 37 | + assert.deepEqual(res, [{cnt: 0}]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('F) Aggregate without GROUP BY on empty dataset returns one row', function () { |
| 41 | + // This is the correct SQL behavior - aggregates without GROUP BY return one row |
| 42 | + var res = alasql('SELECT COUNT(*) as cnt FROM ?', [[]]); |
| 43 | + assert.deepEqual(res, [{cnt: 0}]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('G) GROUP BY with valid data returns correct results', function () { |
| 47 | + // Sanity check that GROUP BY still works correctly with matching data |
| 48 | + var res = alasql('SELECT Name FROM ? WHERE 1=1 GROUP BY Name', [[{Name: 'test'}]]); |
| 49 | + assert.deepEqual(res, [{Name: 'test'}]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('H) GROUP BY with multiple groups returns correct results', function () { |
| 53 | + var data = [ |
| 54 | + {Name: 'Alice', Type: 'A'}, |
| 55 | + {Name: 'Bob', Type: 'B'}, |
| 56 | + {Name: 'Alice', Type: 'A'}, |
| 57 | + ]; |
| 58 | + var res = alasql('SELECT Name, COUNT(*) as cnt FROM ? GROUP BY Name ORDER BY Name', [data]); |
| 59 | + assert.deepEqual(res, [ |
| 60 | + {Name: 'Alice', cnt: 2}, |
| 61 | + {Name: 'Bob', cnt: 1}, |
| 62 | + ]); |
| 63 | + }); |
| 64 | +}); |
0 commit comments