Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions test/esm/integration/config/test-connect-timeout.test.mts
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
import assert from 'node:assert';
import process from 'node:process';
import { assert, describe, it } from 'poku';
import portfinder from 'portfinder';
import mysql from '../../../../index.js';

// The process is not terminated in Deno
if (typeof Deno !== 'undefined') process.exit(0);

console.log('test connect timeout');
await describe('Connect Timeout', async () => {
await it('should emit ETIMEDOUT error on connection timeout', async () => {
await new Promise<void>((resolve) => {
portfinder.getPort((_, port) => {
// @ts-expect-error: TODO: implement typings
const server = mysql.createServer();
server.on('connection', (conn) => {
conn.on('error', (err: NodeJS.ErrnoException) => {
assert.equal(
err.message,
'Connection lost: The server closed the connection.'
);
assert.equal(err.code, 'PROTOCOL_CONNECTION_LOST');
});
});

portfinder.getPort((_, port) => {
// @ts-expect-error: TODO: implement typings
const server = mysql.createServer();
server.on('connection', () => {
// Let connection time out
});

server.listen(port);
server.listen(port);

const connection = mysql.createConnection({
host: 'localhost',
port: port,
connectTimeout: 1000,
});
const connection = mysql.createConnection({
host: 'localhost',
port: port,
connectTimeout: 1000,
});

connection.on('error', (err) => {
assert.equal(err.code, 'ETIMEDOUT');
connection.destroy();
// @ts-expect-error: internal access
server._server.close();
console.log('ok');
connection.on('error', (err) => {
assert.equal(err.code, 'ETIMEDOUT');
connection.destroy();
// @ts-expect-error: internal access
server._server.close(() => {
resolve();
});
});
});
});
});
});

process.on('uncaughtException', (err: NodeJS.ErrnoException) => {
assert.equal(
err.message,
'Connection lost: The server closed the connection.'
);
assert.equal(err.code, 'PROTOCOL_CONNECTION_LOST');
});
64 changes: 36 additions & 28 deletions test/esm/integration/config/test-typecast-global-false.test.mts
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import type { RowDataPacket } from '../../../../index.js';
import { assert } from 'poku';
import { assert, describe, it } from 'poku';
import { createConnection } from '../../common.test.mjs';

const connection = createConnection({
typeCast: false,
});

const COL_1_VALUE = 'col v1';
const COL_2_VALUE = 'col v2';
await describe('Typecast Global False', async () => {
const connection = createConnection({
typeCast: false,
});

function executeTests(res: RowDataPacket[]) {
assert.equal(res[0].v1.toString('ascii'), COL_1_VALUE);
assert.equal(res[0].n1, null);
assert.equal(res[0].v2.toString('ascii'), COL_2_VALUE);
}
const COL_1_VALUE = 'col v1';
const COL_2_VALUE = 'col v2';

connection.query(
'CREATE TEMPORARY TABLE binpar_null_test (v1 VARCHAR(16) NOT NULL, n1 VARCHAR(16), v2 VARCHAR(16) NOT NULL)'
);
connection.query(
`INSERT INTO binpar_null_test (v1, n1, v2) VALUES ("${COL_1_VALUE}", NULL, "${COL_2_VALUE}")`,
(err) => {
if (err) throw err;
function executeTests(res: RowDataPacket[]) {
assert.equal(res[0].v1.toString('ascii'), COL_1_VALUE);
assert.equal(res[0].n1, null);
assert.equal(res[0].v2.toString('ascii'), COL_2_VALUE);
}
);

connection.execute(
'SELECT * FROM binpar_null_test',
(err, res: RowDataPacket[]) => {
if (err) throw err;
executeTests(res);
connection.end();
}
);
connection.query(
'CREATE TEMPORARY TABLE binpar_null_test (v1 VARCHAR(16) NOT NULL, n1 VARCHAR(16), v2 VARCHAR(16) NOT NULL)'
);
connection.query(
`INSERT INTO binpar_null_test (v1, n1, v2) VALUES ("${COL_1_VALUE}", NULL, "${COL_2_VALUE}")`,
(err) => {
if (err) throw err;
}
);

await it('should return raw buffers when typeCast is false', async () => {
await new Promise<void>((resolve, reject) => {
connection.execute(
'SELECT * FROM binpar_null_test',
(err, res: RowDataPacket[]) => {
if (err) return reject(err);
executeTests(res);
resolve();
}
);
});
});

connection.end();
});
82 changes: 47 additions & 35 deletions test/esm/integration/config/test-typecast-global-option.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,58 @@ import type {
TypeCastField,
TypeCastNext,
} from '../../../../index.js';
import { assert } from 'poku';
import { assert, describe, it } from 'poku';
import { createConnection } from '../../common.test.mjs';

type StringMethod = 'toUpperCase' | 'toLowerCase';
await describe('Typecast Global Option', async () => {
type StringMethod = 'toUpperCase' | 'toLowerCase';

const typeCastWrapper = function (stringMethod: StringMethod) {
return function (field: TypeCastField, next: TypeCastNext) {
if (field.type === 'VAR_STRING') {
const value = field.string();
return value?.[stringMethod]();
}
return next();
const typeCastWrapper = function (stringMethod: StringMethod) {
return function (field: TypeCastField, next: TypeCastNext) {
if (field.type === 'VAR_STRING') {
const value = field.string();
return value?.[stringMethod]();
}
return next();
};
};
};

const connection = createConnection({
typeCast: typeCastWrapper('toUpperCase'),
});
const connection = createConnection({
typeCast: typeCastWrapper('toUpperCase'),
});

// query option override global typeCast
connection.query<RowDataPacket[]>(
{
sql: 'select "FOOBAR" as foo',
typeCast: typeCastWrapper('toLowerCase'),
},
(err, res) => {
assert.ifError(err);
assert.equal(res[0].foo, 'foobar');
}
);
// query option override global typeCast
await it('should override global typeCast with query option', async () => {
await new Promise<void>((resolve, reject) => {
connection.query<RowDataPacket[]>(
{
sql: 'select "FOOBAR" as foo',
typeCast: typeCastWrapper('toLowerCase'),
},
(err, res) => {
if (err) return reject(err);
assert.equal(res[0].foo, 'foobar');
resolve();
}
);
});
});

// global typecast works
connection.query<RowDataPacket[]>(
{
sql: 'select "foobar" as foo',
},
(err, res) => {
assert.ifError(err);
assert.equal(res[0].foo, 'FOOBAR');
}
);
// global typecast works
await it('should apply global typeCast', async () => {
await new Promise<void>((resolve, reject) => {
connection.query<RowDataPacket[]>(
{
sql: 'select "foobar" as foo',
},
(err, res) => {
if (err) return reject(err);
assert.equal(res[0].foo, 'FOOBAR');
resolve();
}
);
});
});

connection.end();
connection.end();
});
Loading
Loading