Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3376387
D7119.id24134.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D7119.id24134.diff
View Options
diff --git a/web/database/utils/db-utils.test.js b/web/database/utils/db-utils.test.js
new file mode 100644
--- /dev/null
+++ b/web/database/utils/db-utils.test.js
@@ -0,0 +1,118 @@
+// @flow
+
+import initSqlJs from 'sql.js';
+
+import { parseMultiStatementSQLiteResult } from './db-utils.js';
+
+describe('SQLite database result parsing', () => {
+ let database;
+
+ const tableA = [
+ {
+ key: 'key_a',
+ val: 42,
+ },
+ ];
+
+ const tableB = [
+ {
+ idx: 1,
+ text: 'test1',
+ },
+ {
+ idx: 2,
+ text: null,
+ },
+ ];
+
+ const initQuery = `
+ CREATE TABLE table_a (
+ key TEXT UNIQUE PRIMARY KEY NOT NULL,
+ val INTEGER
+ );
+ CREATE TABLE table_b (
+ idx INTEGER PRIMARY KEY AUTOINCREMENT,
+ text TEXT
+ );
+ INSERT INTO table_a VALUES ($keyA, $valA);
+ INSERT INTO table_b(text) VALUES ($textB);
+ INSERT INTO table_b(text) VALUES (NULL);
+`;
+
+ const initValues = {
+ $keyA: tableA[0].key,
+ $valA: tableA[0].val,
+ $textB: tableB[0].text,
+ };
+
+ beforeEach(async () => {
+ const SQL = await initSqlJs();
+ database = new SQL.Database();
+
+ database.exec(initQuery, initValues);
+ });
+
+ it('should return result for one query', () => {
+ const rawResult = database.exec(`SELECT * FROM table_b`);
+
+ const result = parseMultiStatementSQLiteResult(rawResult);
+ expect(result.length).toBe(1);
+
+ const [data] = result;
+ expect(data).toEqual(expect.arrayContaining(tableB));
+ expect(data.length).toEqual(tableB.length);
+ });
+
+ it('should return result for more than one query', () => {
+ const rawResult = database.exec(`
+ SELECT * FROM table_a;
+ SELECT * FROM table_b;
+ `);
+
+ const result = parseMultiStatementSQLiteResult(rawResult);
+ expect(result.length).toBe(2);
+
+ const [dataA, dataB] = result;
+ expect(dataA).toEqual(expect.arrayContaining(tableA));
+ expect(dataA.length).toEqual(tableA.length);
+ expect(dataB).toEqual(expect.arrayContaining(tableB));
+ expect(dataB.length).toEqual(tableB.length);
+ });
+
+ it('should return an empty array for 0 results', async () => {
+ const rawResult = database.exec(`
+ SELECT * FROM table_b
+ WHERE 1 = 0
+ `);
+
+ const result = parseMultiStatementSQLiteResult(rawResult);
+ expect(result).toStrictEqual([]);
+ });
+
+ it('should return exact row for specific query', async () => {
+ const rawResult = database.exec(`
+ SELECT * FROM table_b
+ WHERE idx = 1
+ `);
+
+ const result = parseMultiStatementSQLiteResult(rawResult);
+ expect(result.length).toBe(1);
+
+ const [data] = result;
+ expect(data.length).toBe(1);
+ expect(data[0]).toStrictEqual(tableB[0]);
+ });
+
+ it('should return aggregated value', async () => {
+ const rawResult = database.exec(`
+ SELECT AVG(val) as average FROM table_a
+ `);
+
+ const result = parseMultiStatementSQLiteResult(rawResult);
+ expect(result.length).toBe(1);
+
+ const [data] = result;
+ expect(data.length).toBe(1);
+ expect(data[0]).toStrictEqual({ average: 42 });
+ });
+});
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Nov 27, 11:42 PM (21 h, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2592109
Default Alt Text
D7119.id24134.diff (3 KB)
Attached To
Mode
D7119: [web-db] add unit tests to function for parsing SQLite query
Attached
Detach File
Event Timeline
Log In to Comment