Skip to content

Commit 4c77bec

Browse files
Copilotmathiasrw
andauthored
Limit impact of having PATH as keyword to close #2222 (#2382)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]>
1 parent 5fd3639 commit 4c77bec

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

test/test2222.js

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 2222 - PATH keyword limitations', function () {
7+
const test = '2222'; // insert test file number
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+
// NOTE: Tests should use assert.deepEqual to verify the complete expected output
19+
// against the actual result object. This ensures comprehensive validation and
20+
// makes test failures more informative by showing the full diff.
21+
22+
describe('PATH as table name', function () {
23+
it('A) CREATE TABLE with name "path"', function () {
24+
var res = alasql('CREATE TABLE path (id INT, data STRING)');
25+
assert.equal(res, 1);
26+
});
27+
28+
it('B) INSERT INTO table named "path"', function () {
29+
var res = alasql('INSERT INTO path VALUES (1, "test1"), (2, "test2")');
30+
assert.equal(res, 2);
31+
});
32+
33+
it('C) SELECT FROM table named "path"', function () {
34+
var res = alasql('SELECT * FROM path ORDER BY id');
35+
assert.deepEqual(res, [
36+
{id: 1, data: 'test1'},
37+
{id: 2, data: 'test2'},
38+
]);
39+
});
40+
41+
it('D) UPDATE table named "path"', function () {
42+
var res = alasql('UPDATE path SET data = "updated" WHERE id = 1');
43+
assert.equal(res, 1);
44+
var check = alasql('SELECT data FROM path WHERE id = 1');
45+
assert.deepEqual(check, [{data: 'updated'}]);
46+
});
47+
48+
it('E) DELETE FROM table named "path"', function () {
49+
var res = alasql('DELETE FROM path WHERE id = 2');
50+
assert.equal(res, 1);
51+
});
52+
53+
it('F) DROP TABLE named "path"', function () {
54+
var res = alasql('DROP TABLE path');
55+
assert.equal(res, 1);
56+
});
57+
});
58+
59+
describe('PATH as column name', function () {
60+
it('A) CREATE TABLE with "path" column', function () {
61+
var res = alasql('CREATE TABLE files (id INT, path STRING, size INT)');
62+
assert.equal(res, 1);
63+
});
64+
65+
it('B) INSERT with "path" column', function () {
66+
var res = alasql(
67+
'INSERT INTO files VALUES (1, "/home/user", 100), (2, "/tmp", 200), (3, "/var/log", 300)'
68+
);
69+
assert.equal(res, 3);
70+
});
71+
72+
it('C) SELECT "path" column', function () {
73+
var res = alasql('SELECT path FROM files ORDER BY id');
74+
assert.deepEqual(res, [{path: '/home/user'}, {path: '/tmp'}, {path: '/var/log'}]);
75+
});
76+
77+
it('D) WHERE clause with "path" column', function () {
78+
var res = alasql('SELECT * FROM files WHERE path = "/tmp"');
79+
assert.deepEqual(res, [{id: 2, path: '/tmp', size: 200}]);
80+
});
81+
82+
it('E) ORDER BY "path" column', function () {
83+
var res = alasql('SELECT path FROM files ORDER BY path');
84+
assert.deepEqual(res, [{path: '/home/user'}, {path: '/tmp'}, {path: '/var/log'}]);
85+
});
86+
87+
it('F) GROUP BY "path" column', function () {
88+
alasql('INSERT INTO files VALUES (4, "/tmp", 150)');
89+
var res = alasql('SELECT path, SUM(size) as sumsize FROM files GROUP BY path');
90+
assert.equal(res.length, 3);
91+
var tmpRow = res.find(r => r.path === '/tmp');
92+
assert.equal(tmpRow.sumsize, 350); // 200 + 150
93+
});
94+
95+
it('Z) Cleanup', function () {
96+
alasql('DROP TABLE files');
97+
});
98+
});
99+
100+
describe('PATH as column alias', function () {
101+
it('A) SELECT with "path" as alias', function () {
102+
var res = alasql('SELECT 1 as path');
103+
assert.deepEqual(res, [{path: 1}]);
104+
});
105+
106+
it('B) SELECT with "path" alias from data', function () {
107+
var data = [{location: '/home'}, {location: '/tmp'}];
108+
var res = alasql('SELECT location as path FROM ?', [data]);
109+
assert.deepEqual(res, [{path: '/home'}, {path: '/tmp'}]);
110+
});
111+
});
112+
113+
describe('PATH in parameter queries', function () {
114+
it('A) Query with "path" column from parameter array', function () {
115+
var data = [
116+
{id: 1, path: '/a'},
117+
{id: 2, path: '/b'},
118+
];
119+
var res = alasql('SELECT path FROM ?', [data]);
120+
assert.deepEqual(res, [{path: '/a'}, {path: '/b'}]);
121+
});
122+
123+
it('B) WHERE clause with "path" column from parameters', function () {
124+
var data = [
125+
{id: 1, path: '/a'},
126+
{id: 2, path: '/b'},
127+
];
128+
var res = alasql('SELECT * FROM ? WHERE path = ?', [data, '/a']);
129+
assert.deepEqual(res, [{id: 1, path: '/a'}]);
130+
});
131+
132+
it('C) ORDER BY "path" from parameters', function () {
133+
var data = [{path: 'z'}, {path: 'a'}, {path: 'm'}];
134+
var res = alasql('SELECT path FROM ? ORDER BY path', [data]);
135+
assert.deepEqual(res, [{path: 'a'}, {path: 'm'}, {path: 'z'}]);
136+
});
137+
});
138+
139+
describe('PATH in graph search (should continue to work)', function () {
140+
before(function () {
141+
alasql('CREATE DATABASE test_graph_path; USE test_graph_path');
142+
});
143+
144+
after(function () {
145+
alasql('DROP DATABASE test_graph_path');
146+
});
147+
148+
it('A) PATH selector in SEARCH query', function () {
149+
alasql(
150+
'CREATE GRAPH Napoleon, Josephine, Pablo, \
151+
#Napoleon > "loves" > #Josephine, \
152+
#Josephine > "knows" > #Pablo'
153+
);
154+
155+
var res = alasql('SEARCH PATH(#Josephine) name FROM #Napoleon');
156+
assert.deepEqual(res, ['loves', 'Josephine']);
157+
});
158+
159+
it('B) PATH with EDGE selector', function () {
160+
var res = alasql('SEARCH PATH(#Josephine) EDGE name FROM #Napoleon');
161+
assert.deepEqual(res, ['loves']);
162+
});
163+
164+
it('C) PATH to find longer paths', function () {
165+
var res = alasql('SEARCH PATH(#Pablo) name FROM #Napoleon');
166+
assert.deepEqual(res, ['loves', 'Josephine', 'knows', 'Pablo']);
167+
});
168+
169+
it('D) PATH within DISTINCT', function () {
170+
alasql('CREATE GRAPH Alice, Bob, #Alice >> #Bob, #Alice >> #Bob');
171+
var res = alasql('SEARCH DISTINCT(PATH(#Bob) name) FROM #Alice');
172+
// Should work even with PATH inside DISTINCT
173+
assert(Array.isArray(res));
174+
});
175+
});
176+
177+
describe('PATH keyword edge cases', function () {
178+
it('A) Mixed case variations', function () {
179+
// Test that PATH works with different casing (if case-insensitive mode)
180+
var data = [{PATH: 'value1'}, {Path: 'value2'}, {path: 'value3'}];
181+
var res = alasql('SELECT * FROM ?', [data]);
182+
assert.equal(res.length, 3);
183+
});
184+
185+
it('B) PATH in JOIN', function () {
186+
alasql('CREATE TABLE t1 (id INT, path STRING)');
187+
alasql('CREATE TABLE t2 (id INT, path STRING)');
188+
alasql('INSERT INTO t1 VALUES (1, "/a")');
189+
alasql('INSERT INTO t2 VALUES (1, "/b")');
190+
191+
var res = alasql(
192+
'SELECT t1.path as path1, t2.path as path2 FROM t1 JOIN t2 ON t1.id = t2.id'
193+
);
194+
assert.deepEqual(res, [{path1: '/a', path2: '/b'}]);
195+
196+
alasql('DROP TABLE t1');
197+
alasql('DROP TABLE t2');
198+
});
199+
200+
it('C) PATH in subquery', function () {
201+
var data = [
202+
{id: 1, path: '/x'},
203+
{id: 2, path: '/y'},
204+
];
205+
var res = alasql('SELECT path FROM (SELECT path FROM ?) WHERE path = "/x"', [data]);
206+
assert.deepEqual(res, [{path: '/x'}]);
207+
});
208+
209+
it('D) PATH in UNION', function () {
210+
var data1 = [{path: 'a'}];
211+
var data2 = [{path: 'b'}];
212+
var res = alasql('SELECT path FROM ? UNION SELECT path FROM ?', [data1, data2]);
213+
assert.equal(res.length, 2);
214+
});
215+
});
216+
});

0 commit comments

Comments
 (0)