|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('Test 950 - CREATE VIEW with subquery', function () { |
| 7 | + const test = '950'; |
| 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) Create tables', function () { |
| 19 | + alasql('create table sub_table(id int)'); |
| 20 | + alasql('create table from_table(id int)'); |
| 21 | + alasql('insert into sub_table values (1), (2), (3)'); |
| 22 | + alasql('insert into from_table values (100)'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('B) Test the query works without view', function () { |
| 26 | + var result = alasql('select (select count(id) from sub_table) as amount from from_table'); |
| 27 | + assert.deepEqual(result, [{amount: 3}]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('C) Create view with subquery', function () { |
| 31 | + // This should not throw "Table does not exist: sub_table" error |
| 32 | + alasql( |
| 33 | + 'create view new_view as select (select count(id) from sub_table) as amount from from_table' |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('D) Query from view', function () { |
| 38 | + var result = alasql('select * from new_view'); |
| 39 | + assert.deepEqual(result, [{amount: 3}]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('E) View should reflect data changes', function () { |
| 43 | + alasql('insert into sub_table values (4), (5)'); |
| 44 | + var result = alasql('select * from new_view'); |
| 45 | + assert.deepEqual(result, [{amount: 5}]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('F) Drop view', function () { |
| 49 | + alasql('drop view new_view'); |
| 50 | + }); |
| 51 | +}); |
0 commit comments