diff --git a/test/test334.js b/test/test334.js index ce3b43e5a9..ce836d8c9c 100644 --- a/test/test334.js +++ b/test/test334.js @@ -10,31 +10,31 @@ if (typeof exports === 'object') { //http://stackoverflow.com/questions/18811265/sql-creating-temporary-variables // describe('Test 334 WITH CTE', function () { - it.skip('1. CREATE DATABASE', function (done) { + it('1. CREATE DATABASE', function (done) { alasql('CREATE DATABASE test334;USE test334'); done(); }); - it.skip('2. Create table', function (done) { + it('2. Create table', function (done) { var res = alasql(function () { /* CREATE TABLE grocery (name STRING, price MONEY, quantity INT); - INSERT INTO test VALUES ("Apples",10,10),("Melons",15,20),("Cucumbers",40,50); + INSERT INTO grocery VALUES ("Apples",10,10),("Melons",15,20),("Cucumbers",40,50); */ }); - assert.deepEqual(res, [1, 1]); + assert.deepEqual(res, [1, 3]); done(); }); - it.skip('3. WITH SELECT', function (done) { + it('3. WITH SELECT', function (done) { var res = alasql(function () { /* With Totals as ( select *, - price * quantity as [Total price], + price * quantity as [Total price] from grocery ) select * @@ -49,7 +49,6 @@ describe('Test 334 WITH CTE', function () { */ }); - console.log(res); assert.deepEqual(res, [ {tax: '0%', name: 'Apples', price: 10, quantity: 10, 'Total price': 100}, {tax: '3%', name: 'Melons', price: 15, quantity: 20, 'Total price': 300}, @@ -65,7 +64,7 @@ describe('Test 334 WITH CTE', function () { done(); }); - it.skip('99. DROP DATABASE', function (done) { + it('99. DROP DATABASE', function (done) { alasql('DROP DATABASE test334'); done(); }); diff --git a/test/test335.js b/test/test335.js index f8df9df236..2a8ced6dd2 100644 --- a/test/test335.js +++ b/test/test335.js @@ -10,25 +10,24 @@ if (typeof exports === 'object') { //http://stackoverflow.com/questions/18811265/sql-creating-temporary-variables // describe('Test 335 WITH RECURSIVE CTE', function () { - it.skip('1. CREATE DATABASE', function (done) { + it('1. CREATE DATABASE', function (done) { alasql('CREATE DATABASE test335;USE test335'); done(); }); - it.skip('2. Create table', function (done) { + it('2. Create table', function (done) { var res = alasql(function () { /* -- Create an Employee table. CREATE TABLE dbo.MyEmployees ( - EmployeeID smallint NOT NULL, + EmployeeID smallint NOT NULL PRIMARY KEY, FirstName nvarchar(30) NOT NULL, LastName nvarchar(40) NOT NULL, Title nvarchar(50) NOT NULL, DeptID smallint NOT NULL, - ManagerID int NULL, - CONSTRAINT PK_EmployeeID PRIMARY KEY CLUSTERED (EmployeeID ASC) + ManagerID ); -- Populate the table with values. INSERT INTO dbo.MyEmployees VALUES @@ -43,21 +42,21 @@ describe('Test 335 WITH RECURSIVE CTE', function () { ,(23, N'Mary', N'Gibson', N'Marketing Specialist', 4, 16); */ }); - assert.deepEqual(res, [1, 1]); + assert.deepEqual(res, [1, 9]); done(); }); - it.skip('3. WITH SELECT', function (done) { + it('3. WITH SELECT', function (done) { var res = alasql(function () { /* -WITH DirectReports(ManagerID, EmployeeID, Title, EmployeeLevel) AS +WITH RECURSIVE DirectReports(ManagerID, EmployeeID, Title, EmployeeLevel) AS ( SELECT ManagerID, EmployeeID, Title, 0 AS EmployeeLevel FROM dbo.MyEmployees WHERE ManagerID IS NULL UNION ALL - SELECT e.ManagerID, e.EmployeeID, e.Title, EmployeeLevel + 1 + SELECT e.ManagerID, e.EmployeeID, e.Title, d.EmployeeLevel + 1 FROM dbo.MyEmployees AS e INNER JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID @@ -68,13 +67,21 @@ ORDER BY ManagerID; */ }); - console.log(res); - assert.deepEqual(res, []); + // Verify all 9 employees are in the result + assert.equal(res.length, 9); + // Verify CEO is at level 0 + var ceo = res.find(r => r.EmployeeID === 1); + assert.equal(ceo.EmployeeLevel, 0); + assert.equal(ceo.ManagerID, undefined); + // Verify VPs report to CEO (level 1) + var vp = res.find(r => r.EmployeeID === 273); + assert.equal(vp.ManagerID, 1); + assert.equal(vp.EmployeeLevel, 1); done(); }); - it.skip('99. DROP DATABASE', function (done) { + it('99. DROP DATABASE', function (done) { alasql('DROP DATABASE test335'); done(); }); diff --git a/test/test355.js b/test/test355.js index 4b88b09b15..bb8f54ab57 100644 --- a/test/test355.js +++ b/test/test355.js @@ -9,13 +9,13 @@ if (typeof exports === 'object') { var data = [{a: 1}, {a: 2}]; describe('Test 355 PIVOT', function () { - it.skip('1. CREATE DATABASE', function (done) { + it('1. CREATE DATABASE', function (done) { alasql('CREATE DATABASE test355;USE test355'); done(); }); /* Source: http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/ */ - it.skip('2. Prepare Data', function (done) { + it('2. Prepare Data', function (done) { alasql('CREATE TABLE Product(Cust VARCHAR(25), Product VARCHAR(20), QTY INT)'); alasql(function () { @@ -40,69 +40,82 @@ describe('Test 355 PIVOT', function () { done(); }); - it.skip('3. Select Query', function (done) { - alasql(function () { + it('3. Select Query', function (done) { + var res = alasql(function () { /* SELECT * FROM Product */ }); + assert.equal(res.length, 7); done(); }); - it.skip('4. Pivot Table ordered by PRODUCT', function (done) { - alasql(function () { + it('4. Pivot Table ordered by PRODUCT', function (done) { + var res = alasql(function () { /* - SELECT PRODUCT, FRED, KATE - FROM ( - SELECT CUST, PRODUCT, QTY - FROM Product) up - PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt - ORDER BY PRODUCT + SELECT * FROM Product + PIVOT (SUM(QTY) FOR Cust IN (FRED, KATE)) + ORDER BY Product */ }); + assert.deepEqual(res, [ + {Product: 'BEER', FRED: 24, KATE: 12}, + {Product: 'MILK', FRED: 3, KATE: 1}, + {Product: 'SODA', KATE: 6}, + {Product: 'VEG', KATE: 5}, + ]); + done(); }); - it.skip('5. Pivot Table ordered by CUST', function (done) { - alasql(function () { + it('5. Pivot Table ordered by CUST', function (done) { + var res = alasql(function () { /* - SELECT CUST, VEG, SODA, MILK, BEER, CHIPS - FROM ( - SELECT CUST, PRODUCT, QTY - FROM Product) up - PIVOT (SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt - ORDER BY CUST + SELECT * FROM Product + PIVOT (SUM(QTY) FOR Product IN (VEG, SODA, MILK, BEER, CHIPS)) + ORDER BY Cust */ }); + assert.deepEqual(res, [ + {Cust: 'FRED', MILK: 3, BEER: 24}, + {Cust: 'KATE', VEG: 5, SODA: 6, MILK: 1, BEER: 12}, + ]); + done(); }); - it.skip('6. UnPivot Query', function (done) { - alasql(function () { + it('6. UnPivot Query', function (done) { + // First create a pivoted table + alasql('CREATE TABLE pivoted (Cust STRING, VEG INT, SODA INT, MILK INT, BEER INT, CHIPS INT)'); + alasql( + 'INSERT INTO pivoted SELECT * FROM Product PIVOT (SUM(QTY) FOR Product IN (VEG, SODA, MILK, BEER, CHIPS))' + ); + + var res = alasql(function () { /* - SELECT CUST, PRODUCT, QTY - FROM - ( - SELECT CUST, VEG, SODA, MILK, BEER, CHIPS - FROM ( - SELECT CUST, PRODUCT, QTY - FROM Product) up - PIVOT - ( SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt) p - UNPIVOT - (QTY FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS) - ) AS Unpvt + SELECT * + FROM pivoted + UNPIVOT (QTY FOR Product IN (VEG, SODA, MILK, BEER, CHIPS)) */ }); + // Should have 10 rows (2 custs * 5 products) + assert.equal(res.length, 10); + // Check that all rows have Cust, Product, and QTY + res.forEach(function (row) { + assert(row.Cust); + assert(row.Product); + assert(row.hasOwnProperty('QTY')); + }); + done(); }); - it.skip('99. DROP DATABASE', function (done) { + it('99. DROP DATABASE', function (done) { alasql.options.modifier = undefined; alasql('DROP DATABASE test355'); done(); diff --git a/test/test356.js b/test/test356.js index c4113b46fb..fb888ab50a 100644 --- a/test/test356.js +++ b/test/test356.js @@ -6,13 +6,13 @@ if (typeof exports === 'object') { } describe('Test 356 PIVOT', function () { - it.skip('1. CREATE DATABASE', function (done) { + it('1. CREATE DATABASE', function (done) { alasql('CREATE DATABASE test356;USE test356'); done(); }); /* Source: http://sqlfiddle.com/#!3/6f4a1/3 */ - it.skip('2. Prepare Data', function (done) { + it('2. Prepare Data', function (done) { alasql(function () { /* create table test @@ -41,87 +41,31 @@ describe('Test 356 PIVOT', function () { done(); }); - if (false) { - it.skip('3. Select Query', function (done) { - var cols = alasql('COLUMN OF SELECT DISTINCT subject from test'); - - alasql(function () { - /* - SELECT username,' + @cols + ' from - ( - select username, subject, score - from test - ) x - pivot - ( - avg(score) - for subject in(' + @cols + ') - ) p ' -*/ - }); - - done(); - }); - } - - it.skip('3. Select Query', function (done) { - alasql(function () { + it('3. Simple PIVOT by subject', function (done) { + var res = alasql(function () { /* - SELECT Score FROM Scores - GROUP BY Name - PIVOT BY Class + SELECT * FROM test + PIVOT (AVG(score) FOR subject IN (Chinese, Math, English, Biology)) */ }); - done(); - }); - it.skip('4. Select Query', function (done) { - alasql(function () { - /* - SELECT Name FROM Scores - GROUP BY Score - PIVOT BY Class - */ - }); + // Should have one row per username + assert.equal(res.length, 2); + // Check that pivot worked + assert.equal(res[0].Chinese, 80); + assert.equal(res[0].Math, 90); done(); }); - it.skip('5. Select Query', function (done) { - alasql(function () { - /* - SELECT Class FROM Scores - GROUP BY Name - PIVOT BY Score - */ - }); - done(); - }); - - it.skip('6. Select Query', function (done) { - alasql(function () { - /* - SELECT Score FROM Scores - GROUP BY Class - PIVOT BY Name - */ - }); - done(); - }); - - it.skip('7. Select Query', function (done) { - alasql(function () { - /* - SELECT Class FROM Scores - GROUP BY Score - PIVOT BY Name - */ - }); + it.skip('4. PIVOT BY syntax - not yet implemented', function (done) { + // PIVOT BY is a different syntax not currently supported + // This test is kept for reference but skipped done(); }); - it.skip('99. DROP DATABASE', function (done) { + it('99. DROP DATABASE', function (done) { alasql.options.modifier = undefined; - alasql('DROP DATABASE test355'); + alasql('DROP DATABASE test356'); done(); }); });