|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('Test 2318 - Update table with PK and direct data assignment', function () { |
| 7 | + const test = '2318'; |
| 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) Update with no PK and direct assignment works', function () { |
| 19 | + alasql('create table nopk (id int, name string)'); |
| 20 | + var db = alasql.databases['test' + test]; |
| 21 | + db.tables.nopk.data = [ |
| 22 | + {id: 1, name: 'Alice'}, |
| 23 | + {id: 2, name: 'Bob'}, |
| 24 | + ]; |
| 25 | + |
| 26 | + var res = alasql('update nopk set name = ? where id = ?', ['Charlie', 1]); |
| 27 | + assert.equal(res, 1); |
| 28 | + |
| 29 | + var data = alasql('select * from nopk order by id'); |
| 30 | + assert.deepEqual(data, [ |
| 31 | + {id: 1, name: 'Charlie'}, |
| 32 | + {id: 2, name: 'Bob'}, |
| 33 | + ]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('B) Update with PK and INSERT works', function () { |
| 37 | + alasql('create table withpk (id int primary key, name string)'); |
| 38 | + alasql('insert into withpk values (1, ?)', ['Alice']); |
| 39 | + alasql('insert into withpk values (2, ?)', ['Bob']); |
| 40 | + |
| 41 | + var res = alasql('update withpk set name = ? where id = ?', ['Charlie', 1]); |
| 42 | + assert.equal(res, 1); |
| 43 | + |
| 44 | + var data = alasql('select * from withpk order by id'); |
| 45 | + assert.deepEqual(data, [ |
| 46 | + {id: 1, name: 'Charlie'}, |
| 47 | + {id: 2, name: 'Bob'}, |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('C) Update with PK and direct assignment should work', function () { |
| 52 | + alasql('create table withpk2 (id int primary key, name string)'); |
| 53 | + var db = alasql.databases['test' + test]; |
| 54 | + db.tables.withpk2.data = [ |
| 55 | + {id: 1, name: 'Alice'}, |
| 56 | + {id: 2, name: 'Bob'}, |
| 57 | + ]; |
| 58 | + |
| 59 | + // This should work but currently throws "Something wrong with index on table" |
| 60 | + var res = alasql('update withpk2 set name = ? where id = ?', ['Charlie', 1]); |
| 61 | + assert.equal(res, 1); |
| 62 | + |
| 63 | + var data = alasql('select * from withpk2 order by id'); |
| 64 | + assert.deepEqual(data, [ |
| 65 | + {id: 1, name: 'Charlie'}, |
| 66 | + {id: 2, name: 'Bob'}, |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('D) Update with composite PK and direct assignment should work', function () { |
| 71 | + alasql('create table composite (id1 int, id2 int, name string, primary key(id1, id2))'); |
| 72 | + var db = alasql.databases['test' + test]; |
| 73 | + db.tables.composite.data = [ |
| 74 | + {id1: 1, id2: 1, name: 'Alice'}, |
| 75 | + {id1: 1, id2: 2, name: 'Bob'}, |
| 76 | + ]; |
| 77 | + |
| 78 | + var res = alasql('update composite set name = ? where id1 = ? and id2 = ?', ['Charlie', 1, 1]); |
| 79 | + assert.equal(res, 1); |
| 80 | + |
| 81 | + var data = alasql('select * from composite order by id1, id2'); |
| 82 | + assert.deepEqual(data, [ |
| 83 | + {id1: 1, id2: 1, name: 'Charlie'}, |
| 84 | + {id1: 1, id2: 2, name: 'Bob'}, |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('E) Update with UNIQUE constraint and direct assignment should work', function () { |
| 89 | + alasql('create table withunique (id int, email string unique, name string)'); |
| 90 | + var db = alasql.databases['test' + test]; |
| 91 | + db.tables.withunique.data = [ |
| 92 | + {id: 1, email: '[email protected]', name: 'Alice'}, |
| 93 | + {id: 2, email: '[email protected]', name: 'Bob'}, |
| 94 | + ]; |
| 95 | + |
| 96 | + var res = alasql('update withunique set name = ? where email = ?', [ |
| 97 | + 'Charlie', |
| 98 | + |
| 99 | + ]); |
| 100 | + assert.equal(res, 1); |
| 101 | + |
| 102 | + var data = alasql('select * from withunique order by id'); |
| 103 | + assert.deepEqual(data, [ |
| 104 | + {id: 1, email: '[email protected]', name: 'Charlie'}, |
| 105 | + {id: 2, email: '[email protected]', name: 'Bob'}, |
| 106 | + ]); |
| 107 | + }); |
| 108 | +}); |
0 commit comments