Skip to content

Commit bbf9369

Browse files
committed
🎉 add some unit tests
1 parent cbc1e81 commit bbf9369

6 files changed

Lines changed: 73 additions & 50 deletions

File tree

__tests__/dml.spec.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getDBConfig, getTable } from './utils'
22

3-
describe('DDL', () => {
3+
describe('DML', () => {
44
let db: any;
55
const dbConfig = getDBConfig();
66

@@ -13,26 +13,67 @@ describe('DDL', () => {
1313
})
1414

1515
it('null type', async () => {
16-
const tableName = await getTable(db);
16+
const [tableName, drop] = await getTable(db);
17+
try {
18+
await db.query(`CREATE MODEL ${tableName}(username: string, null email_id: string)`)
1719

18-
await db.query(`CREATE MODEL ${tableName}(username: string, null email_id: string)`)
20+
await db.query(`INSERT INTO ${tableName}(?, ?)`, 'test', null)
21+
22+
expect(await db.query(`SELECT username,email_id FROM ${tableName} WHERE username = ?`, 'test')).toEqual(
23+
['test', null]
24+
)
25+
26+
} finally {
27+
await drop()
28+
}
29+
})
30+
31+
it('int number type', async () => {
32+
const [tableName, drop] = await getTable(db);
1933

20-
await db.query(`INSERT INTO ${tableName}(?, ?)`, 'test', null)
34+
try {
35+
await db.query(`CREATE MODEL ${tableName}(u8: uint8, u16: uint16, u32: uint32, u64: uint64)`)
2136

22-
expect(await db.query(`SELECT username,email_id FROM ${tableName} WHERE username = ?`, 'test')).toEqual([
23-
['test', null]
24-
])
37+
await db.query(`INSERT INTO ${tableName}(?, ?, ?, ?)`, 1, 2, 3312321, BigInt(478787872837218382))
38+
39+
// TODO why is the uint8 in bigint
40+
expect(await db.query(`SELECT * FROM ${tableName} WHERE u8 = ?`, 1)).toEqual(
41+
[BigInt(1), 2, 3312321, BigInt(478787872837218382)]
42+
)
43+
} finally {
44+
await drop();
45+
}
2546
})
2647

27-
it('int number type', async () => {
28-
const tableName = await getTable(db);
48+
it('list type', async () => {
49+
const [tableName, drop] = await getTable(db);
50+
51+
try {
52+
await db.query(`CREATE MODEL ${tableName}(id: uint64, list: list { type: string} )`)
53+
54+
await db.query(`INSERT INTO ${tableName}(?, [?])`, 1, 'test')
55+
56+
expect(await db.query(`SELECT * FROM ${tableName} WHERE id = ?`, 1)).toEqual(
57+
[BigInt(1), ['test']]
58+
)
59+
} finally {
60+
await drop();
61+
}
62+
})
63+
64+
it('list type', async () => {
65+
const [tableName, drop] = await getTable(db);
2966

30-
await db.query(`CREATE MODEL ${tableName}(u8: uint8, u16: uint16, u32: uint32, u32: uint64)`)
67+
try {
68+
await db.query(`CREATE MODEL ${tableName}(id: uint64, binary: binary )`)
3169

32-
await db.query(`INSERT INTO ${tableName}(?, ?, ?, ?)`, 1, 2, 3312321, BigInt(478787872837218382))
70+
await db.query(`INSERT INTO ${tableName}(?, ?)`, 1, Buffer.from('test'))
3371

34-
expect(await db.query(`SELECT * FROM ${tableName} WHERE u8 = ?`, 1)).toEqual([
35-
[1, 2, 3312321, BigInt(478787872837218382)]
36-
])
72+
expect(await db.query(`SELECT * FROM ${tableName} WHERE id = ?`, 1)).toEqual(
73+
[BigInt(1), Buffer.from('test')]
74+
)
75+
} finally {
76+
await drop();
77+
}
3778
})
3879
});

__tests__/utils.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,16 @@ export function getDBConfig() {
99
);
1010
}
1111

12-
export async function getSpace(db: any, space = 'testspace') {
13-
const isNotCreated = await db.query(`CREATE SPACE IF NOT EXISTS ${space}`);
14-
15-
if (isNotCreated) {
16-
await db.query(`CREATE SPACE ${space}`);
17-
}
12+
export async function getSpace(db: any, space = 'testspace'): Promise<[string, Function]> {
13+
await db.query(`CREATE SPACE IF NOT EXISTS ${space}`);
1814

1915
await db.query(`USE ${space}`);
2016

21-
afterAll(async () => {
22-
await db.query(`DROP SPACE ALLOW NOT EMPTY ${space}`);
23-
})
24-
25-
return space;
17+
return [space, async () => await db.query(`DROP SPACE ALLOW NOT EMPTY ${space}`)];
2618
}
2719

28-
export async function getTable(db: any) {
29-
const space = await getSpace(db, `testTableSpace${Date.now()}`);
30-
const isNotCreated = await db.query(`CREATE SPACE IF NOT EXISTS ${space}`);
20+
export async function getTable(db: any): Promise<[string, Function]> {
21+
const [space, drop] = await getSpace(db, `testTable${Date.now()}Space`);
3122

32-
if (isNotCreated) {
33-
await db.query(`CREATE SPACE ${space}`);
34-
}
35-
36-
await db.query(`USE ${space}`);
37-
38-
afterAll(async () => {
39-
await db.query(`DROP SPACE ALLOW NOT EMPTY ${space}`);
40-
})
41-
42-
return `${space}.testTable${Date.now()}`;
43-
}
23+
return [`${space}.testTable${Date.now()}`, drop as Function];
24+
}

examples/test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Row } from './../src/skytable';
12
import { Config } from "../src"
23

34
async function test() {
@@ -16,14 +17,17 @@ async function test() {
1617
await db.query('use ' + spaceName)
1718
await db.query(`CREATE MODEL ${tableName}(username: string, password: string, null email_id: string)`)
1819
await db.query(
19-
`INSERT INTO ${tableName} { username: ?, password: ?, email_id: ? }`,
20+
`INSERT INTO ${tableName}(?, ?, ?)`,
2021
'test',
2122
'password',
2223
null
2324
)
25+
const row = await db.query(`SELECT * FROM ${tableName} WHERE username = ?`, 'test')
26+
const [username, password, email_id] = (row as Row);
27+
console.assert(username === 'test');
28+
console.assert(password === 'password');
29+
console.assert(email_id == null);
2430
} finally {
25-
// db.query('DROP SPACE ALLOW NOT EMPTY ' + spaceName)
26-
2731
config.disconnect();
2832
}
2933
}

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
preset: 'ts-jest',
3+
maxWorkers: 1,
34
testEnvironment: 'node',
45
globals: {
56
'ts-jest': {

src/protocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ export function encodeParams(parameters: SQParam[]): string {
8484
if (Buffer.isBuffer(param)) {
8585
return [
8686
PARAMS_TYPE.BINARY,
87-
Buffer.byteLength(param),
87+
param.length,
8888
'\n',
89-
new Uint8Array(Array.from(param)).join(''),
89+
param.toString(),
9090
].join('');
9191
}
9292
// null undefined
9393
if (param == null) {
94-
return 0;
94+
return '\x00';
9595
}
9696
throw new TypeError(
9797
`un support type: ${typeof param}, val: ${param}`,

src/skytable.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@ export function createSkytable(connection: Socket | TLSSocket) {
3535
const data = [query.length, '\n', dataframe];
3636
const requestData = ['S', data.join('').length, '\n', ...data];
3737
const buffer = Buffer.from(requestData.join(''), 'utf-8');
38-
39-
console.log(dataframe, '===========query==========');
4038

4139
const res = await connectionWrite(connection, buffer);
4240

43-
console.log(res, '===============result==========');
44-
4541
return formatResponse(res);
4642
};
4743

0 commit comments

Comments
 (0)