Skip to content

Commit 1ca7be8

Browse files
Copilotmathiasrw
andauthored
Confirm PRIMARY KEY constraint to close #1406 (#2309)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]> Co-authored-by: Mathias Wulff <[email protected]>
1 parent 06de4db commit 1ca7be8

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

test/test1406.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 1406 - PRIMARY KEY constraint on INSERT...SELECT', function () {
7+
const test = '1406';
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 table with PRIMARY KEY', function () {
19+
var res = alasql('CREATE TABLE cities (city STRING PRIMARY KEY, population NUMBER)');
20+
assert.equal(res, 1);
21+
});
22+
23+
it('B) Insert records using INSERT...SELECT', function () {
24+
var res = alasql('INSERT INTO cities SELECT * FROM ?', [
25+
[
26+
{city: 'Redmond', population: 57530},
27+
{city: 'Atlanta', population: 447841},
28+
],
29+
]);
30+
assert.equal(res, 2);
31+
32+
var data = alasql('SELECT * FROM cities ORDER BY city');
33+
assert.deepEqual(data, [
34+
{city: 'Atlanta', population: 447841},
35+
{city: 'Redmond', population: 57530},
36+
]);
37+
});
38+
39+
it('C) Attempt to insert duplicate PRIMARY KEY value should throw error', function () {
40+
assert.throws(
41+
function () {
42+
alasql('INSERT INTO cities SELECT * FROM ?', [[{city: 'Redmond', population: 42}]]);
43+
},
44+
function (err) {
45+
return err.message.indexOf('primary key') > -1;
46+
},
47+
'Expected error for duplicate PRIMARY KEY'
48+
);
49+
});
50+
51+
it('D) Verify no duplicate was inserted', function () {
52+
var data = alasql('SELECT * FROM cities ORDER BY city');
53+
assert.equal(data.length, 2);
54+
assert.deepEqual(data, [
55+
{city: 'Atlanta', population: 447841},
56+
{city: 'Redmond', population: 57530},
57+
]);
58+
});
59+
});
60+
61+
describe('Test 1406 - PRIMARY KEY constraint verification', function () {
62+
const test = '1406';
63+
64+
it('A) Test with in-memory database (not IndexedDB)', function () {
65+
alasql('CREATE DATABASE test' + test + 'a');
66+
alasql('USE test' + test + 'a');
67+
68+
alasql('CREATE TABLE cities (city STRING PRIMARY KEY, population NUMBER)');
69+
70+
// Round 1
71+
var res1 = alasql('INSERT INTO cities SELECT * FROM ?', [
72+
[
73+
{city: 'Redmond', population: 57530},
74+
{city: 'Atlanta', population: 447841},
75+
],
76+
]);
77+
assert.equal(res1, 2);
78+
79+
var data1 = alasql('SELECT * FROM cities');
80+
assert.equal(data1.length, 2);
81+
82+
// Round 2 - Try to insert duplicate
83+
var errorThrown = false;
84+
try {
85+
alasql('INSERT INTO cities SELECT * FROM ?', [[{city: 'Redmond', population: 42}]]);
86+
} catch (e) {
87+
errorThrown = true;
88+
assert(e.message.indexOf('primary key') > -1, 'Error message should mention primary key');
89+
}
90+
91+
assert(errorThrown, 'Expected error for duplicate PRIMARY KEY');
92+
93+
// Verify no duplicate was inserted
94+
var data2 = alasql('SELECT * FROM cities');
95+
assert.equal(data2.length, 2, 'Should still have 2 records');
96+
97+
alasql('DROP DATABASE test' + test + 'a');
98+
});
99+
100+
it('B) Test with regular INSERT VALUES', function () {
101+
alasql('CREATE DATABASE test' + test + 'b');
102+
alasql('USE test' + test + 'b');
103+
104+
alasql('CREATE TABLE cities (city STRING PRIMARY KEY, population NUMBER)');
105+
106+
// Insert first record
107+
var res1 = alasql("INSERT INTO cities VALUES ('Redmond', 57530), ('Atlanta', 447841)");
108+
assert.equal(res1, 2);
109+
110+
// Try to insert duplicate
111+
var errorThrown = false;
112+
try {
113+
alasql("INSERT INTO cities VALUES ('Redmond', 42)");
114+
} catch (e) {
115+
errorThrown = true;
116+
assert(e.message.indexOf('primary key') > -1, 'Error message should mention primary key');
117+
}
118+
119+
assert(errorThrown, 'Expected error for duplicate PRIMARY KEY');
120+
121+
// Verify no duplicate was inserted
122+
var data = alasql('SELECT * FROM cities');
123+
assert.equal(data.length, 2, 'Should still have 2 records');
124+
125+
alasql('DROP DATABASE test' + test + 'b');
126+
});
127+
});

0 commit comments

Comments
 (0)