Skip to content

Commit

Permalink
test(db): added failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaSelvaggini committed May 17, 2024
1 parent 65b658d commit f1fb71b
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 10 deletions.
4 changes: 0 additions & 4 deletions pkg/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "1.28.2",
"dependencies": {
"@slangroom/core": "workspace:*",
"@slangroom/shared": "workspace:*",
"sequelize": "^6.16.0",
"sqlite3": "^5.0.0"
},
Expand All @@ -28,8 +27,5 @@
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/supertest": "^6.0.2"
}
}
5 changes: 2 additions & 3 deletions pkg/db/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

import { Plugin } from '@slangroom/core';
import { Jsonable } from '@slangroom/shared';
import { BindOrReplacements, DataTypes, Model, Sequelize } from "sequelize";
import { QueryGetRecord, QuerySaveVar } from "./interfaces.js";

Expand Down Expand Up @@ -55,7 +54,7 @@ export const execute = p.new('connect',
const t = await db.transaction();
const [o, m] = await db.query(statement, { transaction: t });
await t.commit();
const output = { output: o ? o : m } as Jsonable;
const output: {[key:string]: any} = { output: o ? o : m };
db.close();
return ctx.pass(output);
} catch (error) {
Expand Down Expand Up @@ -83,7 +82,7 @@ export const executeParams = p.new('connect',
replacements: parameters
});
await t.commit();
const output = { output: o ? o : m } as Jsonable;
const output: {[key:string]: any} = { output: o ? o : m };
db.close();
return ctx.pass(output);
} catch (error) {
Expand Down
109 changes: 109 additions & 0 deletions pkg/db/test/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2024 Dyne.org foundation
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import test from 'ava';
import { Slangroom } from '@slangroom/core';
import { db } from '@slangroom/db';
Expand All @@ -8,6 +12,8 @@ process.env['FILES_DIR'] = "./test";
const dbPath1 = "sqlite://./test/db1.db";
const dbPath2 = "sqlite://./test/db2.db";

const stripAnsiCodes = (str: string) => str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');

const complexQuery = `Rule unknown ignore
Scenario 'ecdh': Create the keypair
# the value of the record could be 0 to max could be
Expand Down Expand Up @@ -156,3 +162,106 @@ test.serial(
}
}
);

test.serial('Db should fail for wrong record', async (t) => {
const failQuery = `Rule unknown ignore
Scenario 'ecdh': Create the keypair
Given I connect to 'myDb2' and send variable 'obj_1' and send name 'var_name' and send table 'myCache' and save the variable in the database table
# the value of the record could be 0 to max could be
Given I connect to 'myDb1' and send record 'n' and send table 'myTable' and read the record of the table and output into 'myZenroomStringDictionary'
Given I am 'John'
Given I have a 'string' named 'myDb1'
Given I have a 'string' named 'myDb2'
Given I have a 'string' named 'myTable'
Given I have a 'string' named 'myCache'
Given I have a 'string dictionary' named 'myZenroomStringDictionary'
Then print all data
`;
const slangroom = new Slangroom(db);
const result = slangroom.execute(failQuery, {
data: {
"n": 30,
"var_name": "obj_1",
"obj_1": "pippo",
"myDb1": "sqlite://./test/db1.db",
"myDb2": "sqlite://./test/db2.db",
"myTable": "firstTable",
"myCache": "firstCache",
"myOtherZenroomStringDictionary": {
"data": {
"data1": "9WgBlK+Kcq3AKpmhituXQe4UPkzH3zpZiQa4Szm1Q40=",
"data2": "BCEo8MgRiSxtLfxE4UEDVnbdZ21h+xc+egLIRk3VTagpJxlBfu9MjqXGUi2N7tIewpcDrr5V7Z2cmMcNsbKWSGQ="
}
}
},
});

const error = await t.throwsAsync(result);
t.is(stripAnsiCodes((error as Error).message),
`3 | # the value of the record could be 0 to max could be
4 | Given I connect to 'myDb1' and send record 'n' and send table 'myTable' and read the record of the table and output into 'myZenroomStringDictionary'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | Given I am 'John'
6 | Given I have a 'string' named 'myDb1'
Error colors:
- error
- suggested words
- missing words
- extra words
Slangroom @slangroom/db Error: [DATABASE]
Returned null for id "30" in table "firstTable" in db "sqlite://./test/db1.db".
`);
});

test.serial('Db should fail for wrong db', async (t) => {
const failQuery = `Rule unknown ignore
Scenario 'ecdh': Create the keypair
# the value of the record could be 0 to max could be
Given I connect to 'myDb1' and send record 'n' and send table 'myTable' and read the record of the table and output into 'myZenroomStringDictionary'
Given I am 'John'
Given I have a 'string' named 'obj_1'
Given I have a 'string' named 'var_name'
Given I have a 'string' named 'myDb1'
Given I have a 'string' named 'myDb2'
Given I have a 'string' named 'myTable'
Given I have a 'string' named 'myCache'
Given I have a 'string dictionary' named 'myZenroomStringDictionary'
Then print all data
Then I connect to 'myDb2' and send variable 'obj_1' and send name 'var_name' and send table 'myCache' and save the variable in the database table
Then I connect to 'myTable' and send variable 'obj_1' and send name 'var_name' and send table 'myCache' and save the variable in the database table`;
const slangroom = new Slangroom(db);
const result = slangroom.execute(failQuery, {
data: {
"n": 3,
"var_name": "obj_1",
"obj_1": "pippo",
"myDb1": "sqlite://./test/db1.db",
"myDb2": "sqlite://./test/db2.db",
"myTable": "firstTable",
"myCache": "firstCache",
"myOtherZenroomStringDictionary": {
"data": {
"data1": "9WgBlK+Kcq3AKpmhituXQe4UPkzH3zpZiQa4Szm1Q40=",
"data2": "BCEo8MgRiSxtLfxE4UEDVnbdZ21h+xc+egLIRk3VTagpJxlBfu9MjqXGUi2N7tIewpcDrr5V7Z2cmMcNsbKWSGQ="
}
}
},
});

const error = await t.throwsAsync(result);
t.is(stripAnsiCodes((error as Error).message),
`13 | Then I connect to 'myDb2' and send variable 'obj_1' and send name 'var_name' and send table 'myCache' and save the variable in the database table
14 | Then I connect to 'myTable' and send variable 'obj_1' and send name 'var_name' and send table 'myCache' and save the variable in the database table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error colors:
- error
- suggested words
- missing words
- extra words
Slangroom @slangroom/db Error: [DATABASE] Database error: TypeError: Cannot read properties of null (reading 'replace')
`);
});
82 changes: 79 additions & 3 deletions pkg/db/test/raw_query.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { SuperTest, Test } from "supertest";
import anyTest, { TestFn } from "ava";
// SPDX-FileCopyrightText: 2024 Dyne.org foundation
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import test from "ava";
import { Slangroom } from '@slangroom/core';
import { db } from '@slangroom/db';
import sqlite3 from "sqlite3";

const test = anyTest as TestFn<{ app: SuperTest<Test> }>;
const stripAnsiCodes = (str: string) => str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');

test('Db should execute raw queries', async (t) => {
const rawQuery = `Rule unknown ignore
Expand Down Expand Up @@ -75,3 +78,76 @@ test('Db should execute raw queries', async (t) => {
const lastID_6 = Number(res_6?.output?.lastID);
t.true(lastID_6 && lastID_6 > 1);
});

test('Db should fail for wrong database', async (t) => {
const rawQuery = `Rule unknown ignore
Given I connect to 'database' and send statement 'query_1' and execute sql statement and output into 'result_1'
Given I connect to 'database' and send statement 'query_5' and send parameters 'query5_params' and execute sql statement with parameters and output into 'result_5'
Given I have a 'string dictionary' named 'result_1'
Given I have a 'string dictionary' named 'result_5'
Then print all data
`;

const slangroom = new Slangroom(db);
const res = slangroom.execute(rawQuery, {
data: {
"database": "sqlite://./test/fake_test.db",
"query_1": "INSERT INTO member (name) VALUES ('Alice')",
"query_5": "INSERT INTO member (name) VALUES (?)",
"query5_params": ["Eve"],
},
});
const error = await t.throwsAsync(res);
t.is(stripAnsiCodes((error as Error).message),
`0 | Rule unknown ignore
1 | Given I connect to 'database' and send statement 'query_1' and execute sql statement and output into 'result_1'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 | Given I connect to 'database' and send statement 'query_5' and send parameters 'query5_params' and execute sql statement with parameters and output into 'result_5'
3 | Given I have a 'string dictionary' named 'result_1'
Error colors:
- error
- suggested words
- missing words
- extra words
Slangroom @slangroom/db Error: SQLITE_ERROR: no such table: member
`);
});


test('Db should fail for wrong statement', async (t) => {
const rawQuery = `Rule unknown ignore
Given I connect to 'database' and send statement 'query_1' and execute sql statement and output into 'result_1'
Given I connect to 'database' and send statement 'query_5' and send parameters 'query5_params' and execute sql statement with parameters and output into 'result_5'
Given I have a 'string dictionary' named 'result_1'
Given I have a 'string dictionary' named 'result_5'
Then print all data
`;

const slangroom = new Slangroom(db);
const res = slangroom.execute(rawQuery, {
data: {
"database": "sqlite://./test/test.db",
"query_1": "INSTERT INTO member (name) VALUES ('Alice')",
"query_5": "INSERT INTO member (name) VALUES (?)",
"query5_params": ["Eve"],
},
});
const error = await t.throwsAsync(res);
t.is(stripAnsiCodes((error as Error).message),
`0 | Rule unknown ignore
1 | Given I connect to 'database' and send statement 'query_1' and execute sql statement and output into 'result_1'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 | Given I connect to 'database' and send statement 'query_5' and send parameters 'query5_params' and execute sql statement with parameters and output into 'result_5'
3 | Given I have a 'string dictionary' named 'result_1'
Error colors:
- error
- suggested words
- missing words
- extra words
Slangroom @slangroom/db Error: SQLITE_ERROR: near "INSTERT": syntax error
`);
});

0 comments on commit f1fb71b

Please sign in to comment.