Skip to content

Commit 3ec8dfa

Browse files
Copilotmathiasrw
andauthored
Improve test coverage for SOURCE keyword to close #2218 (#2381)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]>
1 parent a671e74 commit 3ec8dfa

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

test/test2218.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 2218 - SOURCE keyword context', function () {
7+
const testId = '2218';
8+
9+
before(function () {
10+
alasql('create database test' + testId);
11+
alasql('use test' + testId);
12+
});
13+
14+
after(function () {
15+
alasql('drop database test' + testId);
16+
});
17+
18+
it('A) Should allow "source" as a table name', function () {
19+
alasql('CREATE TABLE source(id INT, data VARCHAR(50))');
20+
alasql('INSERT INTO source VALUES (1, "test1"), (2, "test2")');
21+
22+
var res = alasql('SELECT * FROM source ORDER BY id');
23+
assert.deepEqual(res, [
24+
{id: 1, data: 'test1'},
25+
{id: 2, data: 'test2'},
26+
]);
27+
});
28+
29+
it('B) Should allow "source" as a column name', function () {
30+
alasql('CREATE TABLE test_col(id INT, source VARCHAR(50))');
31+
alasql('INSERT INTO test_col VALUES (1, "data1"), (2, "data2")');
32+
33+
var res = alasql('SELECT id, source FROM test_col ORDER BY id');
34+
assert.deepEqual(res, [
35+
{id: 1, source: 'data1'},
36+
{id: 2, source: 'data2'},
37+
]);
38+
});
39+
40+
it('C) Should allow "source" as a JSON property name', function () {
41+
alasql('CREATE TABLE test_json(data JSON)');
42+
alasql('CREATE INDEX idx_source on test_json(data->source)');
43+
44+
alasql('INSERT INTO test_json VALUES (@{source:100})');
45+
alasql('INSERT INTO test_json VALUES (@{source:200})');
46+
47+
var res = alasql('SELECT * FROM test_json ORDER BY data->source');
48+
assert.equal(res.length, 2);
49+
assert.deepEqual(res, [{data: {source: 100}}, {data: {source: 200}}]);
50+
});
51+
52+
it('D) Should parse MERGE BY SOURCE syntax without error', function () {
53+
// Test that MERGE statement with BY SOURCE clause is properly parsed
54+
// The SOURCE keyword should be recognized in this context
55+
// We just verify it parses - actual MERGE functionality is tested elsewhere
56+
var sql = `
57+
MERGE tbl_target AS t
58+
USING tbl_source AS s
59+
ON t.id = s.id
60+
WHEN MATCHED THEN UPDATE SET t.name = s.name
61+
WHEN NOT MATCHED BY SOURCE AND t.name LIKE 'S%' THEN DELETE
62+
WHEN NOT MATCHED BY TARGET THEN INSERT VALUES (s.id, s.name)
63+
`;
64+
65+
// This should not throw a parse error
66+
var parsed = alasql.parse(sql);
67+
assert.ok(parsed);
68+
// The parsed object should have statements array
69+
assert.ok(parsed.statements);
70+
assert.ok(parsed.statements.length > 0);
71+
// Check that it contains a MERGE statement with BY SOURCE clause
72+
assert.ok(parsed.statements[0].using);
73+
});
74+
75+
it('E) Should work with source in UNIQUE constraint', function () {
76+
alasql('CREATE TABLE test_unique(id INT, source VARCHAR(50), UNIQUE(source))');
77+
78+
alasql('INSERT INTO test_unique VALUES (1, "unique1")');
79+
80+
// This should fail - duplicate source
81+
assert.throws(() => {
82+
alasql('INSERT INTO test_unique VALUES (2, "unique1")');
83+
}, Error);
84+
85+
// This should succeed - different source
86+
alasql('INSERT INTO test_unique VALUES (3, "unique2")');
87+
88+
var res = alasql('SELECT * FROM test_unique');
89+
assert.equal(res.length, 2);
90+
});
91+
92+
it('F) Should allow "source" in table alias', function () {
93+
alasql('CREATE TABLE test_alias(id INT, data VARCHAR(50))');
94+
alasql('INSERT INTO test_alias VALUES (1, "test1")');
95+
96+
var res = alasql('SELECT source.id, source.data FROM test_alias AS source');
97+
assert.deepEqual(res, [{id: 1, data: 'test1'}]);
98+
});
99+
100+
it('G) Should work with "source" in WHERE clause', function () {
101+
alasql('CREATE TABLE test_where(id INT, source VARCHAR(50))');
102+
alasql('INSERT INTO test_where VALUES (1, "value1"), (2, "value2")');
103+
104+
var res = alasql('SELECT * FROM test_where WHERE source = "value1"');
105+
assert.deepEqual(res, [{id: 1, source: 'value1'}]);
106+
});
107+
});

0 commit comments

Comments
 (0)