Skip to content

Commit a1a90a8

Browse files
committed
built-in commands tests: query analyzer & settings
1 parent c88c0ce commit a1a90a8

2 files changed

Lines changed: 179 additions & 14 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.220",
3+
"version": "1.0.224",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

test/core/built-in-commands.test.ts

Lines changed: 178 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ const randomDate = (fromTime = new Date(new Date().getTime() + 4 * 60 * 60 * 100
3434
const randomBool = (): boolean => Math.random() < 0.5
3535
const date = () => expect.stringMatching(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/)
3636
const ip = () => expect.stringMatching(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
37+
const uuid = () => expect.stringMatching(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
3738
const bool = () => expect.any(Number)
3839
const colseq = () => expect.stringMatching(/^(BINARY|RTRIM|NOCASE)$/)
3940
const screaming_snake_case = () => expect.stringMatching(/^[A-Z]+[_]*[A-Z]*$/)
41+
const regex_IP_UUID_N = /(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)|(^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$)|[0-9]/
4042

4143
/* const sqlOk = async (command: string, database: SQLiteCloudTlsConnection, done: jest.DoneCallback, ok: boolean) => { //testing with .sql
4244
try {
@@ -93,7 +95,7 @@ const test = (done: jest.DoneCallback, chinook: SQLiteCloudConnection, ok: boole
9395
try {
9496
expect(error).toBeInstanceOf(SQLiteCloudError)
9597
expect((error as SQLiteCloudError).message).toMatch(
96-
/(not found|doesn\'t exist|does not exist|invalid|unable|fail|cannot|must be unique|unknown|undefined|error|no such|not available|try again later|wrong|has no)/i
98+
/(not found|doesn\'t exist|does not exist|invalid|unable|fail|cannot|must be unique|unknown|undefined|error|no such|not available|try again later|wrong|has no|is read-only)/i
9799
)
98100
expect(results).toBeUndefined()
99101
} catch {
@@ -1386,16 +1388,179 @@ describe.each([[randomName(), false]])('plugins', (name, ok) => {
13861388
})
13871389
})
13881390

1389-
describe.each([[1, 1, undefined, 'chinook.sqlite', 75, true, true, true, false]])(
1390-
'analyzer',
1391-
(query, node, group, database, percentage, all, apply, grouped, ok) => {
1392-
it(`should${ok ? '' : "n't"} list analyzer`, done => {
1393-
const chinook = getConnection()
1394-
chinook.sendCommands(
1395-
`LIST ANALYZER${group ? ` GROUPID ${group}` : ''}${database ? ` DATABASE ${database}` : ''}${grouped ? ' GROUPED' : ''}${node ? ` NODE ${node}` : ''}`,
1396-
test(done, chinook, true, [])
1391+
describe.each([
1392+
[1, undefined, 'chinook.sqlite', 75, true, true, false, true],
1393+
[1, undefined, undefined, undefined, false, false, false, true],
1394+
[1, undefined, undefined, undefined, true, true, false, true],
1395+
[_, undefined, undefined, undefined, true, true, false, true],
1396+
['\0\0\0\0', 'undefined', '\0\0\0\0', undefined, true, true, true, false]
1397+
])('analyzer', (node, group, database, percentage, all, apply, grouped, ok) => {
1398+
it(`enables query analyzer`, done => {
1399+
const chinook = getConnection()
1400+
chinook.sendCommands(`SET KEY query_analyzer_enabled TO 1; SET KEY query_analyzer_threshold TO 0;`, test(done, chinook, ok))
1401+
})
1402+
1403+
it(`executes a query to analyze`, done => {
1404+
const chinook = getConnection()
1405+
chinook.sendCommands(`SELECT * FROM customers`, test(done, chinook, true, expect.anything()))
1406+
})
1407+
1408+
let query: string
1409+
1410+
it(`should${ok ? '' : "n't"} list analyzer`, done => {
1411+
const chinook = getConnection()
1412+
chinook.sendCommands(
1413+
`LIST ANALYZER${group ? ` GROUPID ${group}` : ''}${database ? ` DATABASE ${database}` : ''}${grouped ? ' GROUPED' : ''}${node ? ` NODE ${node}` : ''}`,
1414+
test(
1415+
done,
1416+
chinook,
1417+
ok,
1418+
grouped
1419+
? {
1420+
group_id: expect.any(Number),
1421+
sql: expect.any(String),
1422+
database: database ?? expect.any(String),
1423+
'AVG(query_time)': expect.any(Number),
1424+
'MAX(query_time)': expect.any(Number),
1425+
'COUNT(query_time)': expect.any(Number)
1426+
}
1427+
: {
1428+
id: expect.any(Number),
1429+
sql: expect.any(String),
1430+
database: database ?? expect.any(String),
1431+
query_time: expect.anything(),
1432+
datetime: expect.anything()
1433+
},
1434+
(res: any) => (ok ? (query = res[0].id) : undefined)
13971435
)
1398-
})
1399-
//wip..
1400-
}
1401-
)
1436+
)
1437+
})
1438+
1439+
it(`should${ok ? '' : "n't"} suggest`, done => {
1440+
const chinook = getConnection()
1441+
chinook.sendCommands(
1442+
`ANALYZER SUGGEST ID ${query}${percentage ? ` PERCENTAGE ${percentage}` : ''}${apply ? ' APPLY' : ''}${node ? ` NODE ${node}` : ''}`,
1443+
test(done, chinook, ok, {
1444+
statement: expect.any(Number),
1445+
type: expect.any(Number),
1446+
report: expect.any(String)
1447+
})
1448+
)
1449+
})
1450+
1451+
it(`should${ok ? '' : "n't"} plan`, done => {
1452+
const chinook = getConnection()
1453+
chinook.sendCommands(
1454+
`ANALYZER PLAN ID ${query}${node ? ` NODE ${node}` : ''}`,
1455+
test(done, chinook, ok, {
1456+
id: expect.any(Number),
1457+
parent: expect.any(Number),
1458+
notused: 0,
1459+
detail: expect.any(String)
1460+
})
1461+
)
1462+
})
1463+
1464+
it(`should${ok ? '' : "n't"} reset analyzer`, done => {
1465+
const chinook = getConnection()
1466+
chinook.sendCommands(
1467+
`ANALYZER RESET${query ? ` ID ${query}` : group ? ` GROUPID ${group}` : database ? ` DATABASE ${database}` : all ? ' ALL' : ''}${node ? ` NODE ${node}` : ''}`,
1468+
test(done, chinook, ok)
1469+
)
1470+
})
1471+
1472+
it(`disables query analyzer`, done => {
1473+
const chinook = getConnection()
1474+
chinook.sendCommands(`REMOVE KEY query_analyzer_enabled; REMOVE KEY query_analyzer_threshold;`, test(done, chinook, ok))
1475+
})
1476+
})
1477+
1478+
describe.each([
1479+
['ID', 'autocheckpoint', true, true, false, true],
1480+
['IP', 'backlog', false, true, false, true],
1481+
['UUID', 'cluster_port', false, false, false, true],
1482+
['MAXROWS', 'newcluster', true, false, true, true],
1483+
[undefined, undefined, false, false, false, false],
1484+
['\0\0\\\\', '\0\0\\\\', true, true, true, false],
1485+
[99, 99, false, true, false, false]
1486+
])('settings', (client_key, cluster_key, detailed, no_read_only, client_editable, ok) => {
1487+
it(`should${ok ? '' : "n't"} get client key`, done => {
1488+
const chinook = getConnection()
1489+
chinook.sendCommands(`GET CLIENT KEY ${client_key}`, test(done, chinook, ok, regex_IP_UUID_N))
1490+
})
1491+
1492+
it(`shouldn't get database key`, done => {
1493+
const chinook = getConnection()
1494+
chinook.sendCommands(`GET DATABASE chinook.sqlite KEY ${client_key}`, test(done, chinook, false /* fails everytime in ci */))
1495+
})
1496+
1497+
it(`should${ok ? '' : "n't"} get cluster key`, done => {
1498+
const chinook = getConnection()
1499+
chinook.sendCommands(`GET KEY ${cluster_key}`, test(done, chinook, ok, /[0-9]/))
1500+
})
1501+
1502+
it(`should${ok ? '' : "n't"} list client keys`, done => {
1503+
const chinook = getConnection()
1504+
chinook.sendCommands(
1505+
`LIST CLIENT KEYS`,
1506+
test(done, chinook, ok, {
1507+
key: client_key,
1508+
value: expect.stringMatching(regex_IP_UUID_N)
1509+
})
1510+
)
1511+
})
1512+
1513+
it(`should${ok ? '' : "n't"} list cluster keys`, done => {
1514+
const chinook = getConnection()
1515+
chinook.sendCommands(
1516+
`LIST KEYS${detailed ? ' DETAILED' : ''}${no_read_only ? ' NOREADONLY' : ''}`,
1517+
test(
1518+
done,
1519+
chinook,
1520+
ok,
1521+
detailed
1522+
? {
1523+
key: cluster_key,
1524+
value: expect.anything(),
1525+
default_value: no_read_only ? expect.anything() : null,
1526+
readonly: no_read_only ? 0 : expect.any(Number),
1527+
description: expect.any(String)
1528+
}
1529+
: {
1530+
key: cluster_key,
1531+
value: expect.anything()
1532+
}
1533+
)
1534+
)
1535+
})
1536+
1537+
it(`should${ok ? '' : "n't"} set client key`, done => {
1538+
const chinook = getConnection()
1539+
chinook.sendCommands(`SET CLIENT KEY ${client_key} TO 10`, test(done, chinook, client_editable && ok))
1540+
})
1541+
1542+
it(`should${ok ? '' : "n't"} set database key`, done => {
1543+
const chinook = getConnection()
1544+
chinook.sendCommands(`SET DATABASE chinook.sqlite KEY ${client_key} TO 10`, test(done, chinook, ok))
1545+
})
1546+
1547+
it(`should${ok ? '' : "n't"} set cluster key`, done => {
1548+
const chinook = getConnection()
1549+
chinook.sendCommands(`SET KEY ${cluster_key} TO 1001`, test(done, chinook, no_read_only ? ok : false))
1550+
})
1551+
1552+
it(`should${ok ? '' : "n't"} remove client key`, done => {
1553+
const chinook = getConnection()
1554+
chinook.sendCommands(`REMOVE CLIENT KEY ${client_key}`, test(done, chinook, ok))
1555+
})
1556+
1557+
it(`should remove database key`, done => {
1558+
const chinook = getConnection()
1559+
chinook.sendCommands(`REMOVE DATABASE chinook.sqlite KEY ${client_key}`, test(done, chinook, ok))
1560+
})
1561+
1562+
it(`should${ok ? '' : "n't"} remove cluster key`, done => {
1563+
const chinook = getConnection()
1564+
chinook.sendCommands(`REMOVE KEY ${cluster_key}`, test(done, chinook, no_read_only ? ok : false))
1565+
})
1566+
})

0 commit comments

Comments
 (0)