Comparing version 3.0.2 to 3.0.3
@@ -180,3 +180,3 @@ 'use strict'; | ||
dbField[10] = ' '; // Replace T with a space | ||
dbField = dbField.substring(0, dbField.length - 1); // Cut the last Z off | ||
dbField = dbField.substring(0, dbField.length - 1); // Cut the last Z off | ||
} | ||
@@ -183,0 +183,0 @@ } |
{ | ||
"name": "larvitdb", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "DB wrapper module for node.js", | ||
@@ -10,11 +10,14 @@ "main": "index.js", | ||
"dependencies": { | ||
"larvitutils": "^2.2.0", | ||
"larvitutils": "2.3.0", | ||
"mysql2": "1.6.5" | ||
}, | ||
"devDependencies": { | ||
"mocha": "6.1.3", | ||
"mocha-eslint": "5.0.0" | ||
"coveralls": "3.0.4", | ||
"eslint": "6.0.1", | ||
"nyc": "14.1.1", | ||
"tape": "4.11.0", | ||
"tape-eslint": "1.2.1" | ||
}, | ||
"scripts": { | ||
"test": "mocha --exit" | ||
"test": "npx tape test/*" | ||
}, | ||
@@ -21,0 +24,0 @@ "repository": { |
@@ -1,2 +0,4 @@ | ||
[](https://travis-ci.org/larvit/larvitdb) [](https://david-dm.org/larvit/larvitdb.svg) | ||
[](https://travis-ci.org/larvit/larvitdb) | ||
[](https://david-dm.org/larvit/larvitdb.svg) | ||
[](https://coveralls.io/github/larvit/larvitdb) | ||
@@ -3,0 +5,0 @@ # larvitdb |
'use strict'; | ||
require('mocha-eslint')([__dirname + '/..'], { | ||
// Increase the timeout of the test if linting takes to long | ||
timeout: 5000, // Defaults to the global mocha `timeout` option | ||
const test = require('tape'); | ||
// Increase the time until a test is marked as slow | ||
slow: 1000 // Defaults to the global mocha `slow` option | ||
}); | ||
test('eslint', require('tape-eslint')()); |
255
test/test.js
@@ -6,2 +6,3 @@ 'use strict'; | ||
const lUtils = new LUtils(); | ||
const test = require('tape'); | ||
const log = new lUtils.Log('warn'); | ||
@@ -15,3 +16,24 @@ const Db = require('../index.js'); | ||
before(done => { | ||
async function dbTests(dbCon) { | ||
await dbCon.query('CREATE TABLE `fjant` (`test` int NOT NULL) ENGINE=\'InnoDB\';'); | ||
await dbCon.query('INSERT INTO `fjant` VALUES(13);'); | ||
const testFromFjant = await dbCon.query('SELECT test FROM fjant'); | ||
assert.strictEqual(testFromFjant.rows.length, 1); | ||
assert.strictEqual(testFromFjant.rows[0].test, 13); | ||
await dbCon.query('UPDATE fjant SET test = ?', 7); | ||
const testFromFjantAgain = await dbCon.query('SELECT test FROM fjant'); | ||
assert.strictEqual(testFromFjantAgain.rows.length, 1); | ||
assert.strictEqual(testFromFjantAgain.rows[0].test, 7); | ||
await dbCon.query('DELETE FROM fjant'); | ||
const testFromFjantLast = await dbCon.query('SELECT test FROM fjant'); | ||
assert.strictEqual(testFromFjantLast.rows.length, 0); | ||
await dbCon.query('DROP TABLE fjant'); | ||
} | ||
test('Setup db and do checks', t => { | ||
let confFile; | ||
@@ -24,3 +46,3 @@ | ||
done(); | ||
t.end(); | ||
}); | ||
@@ -38,5 +60,3 @@ } | ||
db = new Db(conf); | ||
db.ready().then(() => { | ||
checkEmptyDb(); | ||
}); | ||
db.ready().then(checkEmptyDb); | ||
} | ||
@@ -71,155 +91,140 @@ | ||
after(done => { | ||
(new Promise(async resolve => { | ||
await db.pool.end(); | ||
resolve(); | ||
})).then(done); | ||
test('Simple queries', t => { | ||
dbTests(db).then(() => t.end()); | ||
}); | ||
describe('Db tests', function () { | ||
async function dbTests(dbCon) { | ||
await dbCon.query('CREATE TABLE `fjant` (`test` int NOT NULL) ENGINE=\'InnoDB\';'); | ||
await dbCon.query('INSERT INTO `fjant` VALUES(13);'); | ||
test('Queries on a single connection from the pool', t => { | ||
db.getConnection().then(dbCon => { | ||
dbTests(dbCon).then(() => t.end()); | ||
}); | ||
}); | ||
const testFromFjant = await dbCon.query('SELECT test FROM fjant'); | ||
test('Remove all tables from database', t => { | ||
async function runTest() { | ||
assert.strictEqual(testFromFjant.rows.length, 1); | ||
assert.strictEqual(testFromFjant.rows[0].test, 13); | ||
// Create tables with internal relations | ||
await db.query(`CREATE TABLE foo ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
name int(11) NOT NULL, | ||
PRIMARY KEY (id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`); | ||
await dbCon.query('UPDATE fjant SET test = ?', 7); | ||
const testFromFjantAgain = await dbCon.query('SELECT test FROM fjant'); | ||
assert.strictEqual(testFromFjantAgain.rows.length, 1); | ||
assert.strictEqual(testFromFjantAgain.rows[0].test, 7); | ||
await db.query(`CREATE TABLE bar ( | ||
fooId int(11) NOT NULL, | ||
stuff varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, | ||
KEY fooId (fooId), | ||
CONSTRAINT bar_ibfk_1 FOREIGN KEY (fooId) REFERENCES foo (id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`); | ||
await dbCon.query('DELETE FROM fjant'); | ||
const testFromFjantLast = await dbCon.query('SELECT test FROM fjant'); | ||
assert.strictEqual(testFromFjantLast.rows.length, 0); | ||
await dbCon.query('DROP TABLE fjant'); | ||
// Try to remove all tables from the database | ||
await db.removeAllTables(); | ||
// Check so no tables exists | ||
const result = await db.query('SHOW TABLES'); | ||
assert.strictEqual(result.rows.length, 0); | ||
t.end(); | ||
} | ||
it('Simple queries', done => { | ||
dbTests(db).then(done); | ||
}); | ||
runTest(); | ||
}); | ||
it('Queries on a single connection from the pool', done => { | ||
db.getConnection().then(dbCon => { | ||
dbTests(dbCon).then(done); | ||
}); | ||
}); | ||
test('Stream rows', t => { | ||
(new Promise(async resolve => { | ||
let rowNr = 0; | ||
it('Remove all tables from database', done => { | ||
async function runTest() { | ||
// Create table with data | ||
await db.query('CREATE TABLE `plutt` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(191) NOT NULL);'); | ||
await db.query('INSERT INTO plutt (name) VALUES(\'bosse\'),(\'hasse\'),(\'vråkbert\');'); | ||
// Create tables with internal relations | ||
await db.query(`CREATE TABLE foo ( | ||
id int(11) NOT NULL AUTO_INCREMENT, | ||
name int(11) NOT NULL, | ||
PRIMARY KEY (id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`); | ||
// Get dbCon | ||
const dbCon = await db.getConnection(); | ||
await db.query(`CREATE TABLE bar ( | ||
fooId int(11) NOT NULL, | ||
stuff varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, | ||
KEY fooId (fooId), | ||
CONSTRAINT bar_ibfk_1 FOREIGN KEY (fooId) REFERENCES foo (id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`); | ||
// Check contents | ||
const query = db.streamQuery('SELECT * FROM plutt'); | ||
query.on('error', err => { | ||
throw err; | ||
}); | ||
// Try to remove all tables from the database | ||
await db.removeAllTables(); | ||
query.on('fields', fields => { | ||
assert.strictEqual(fields[0].name, 'id'); | ||
assert.strictEqual(fields[1].name, 'name'); | ||
}); | ||
// Check so no tables exists | ||
const result = await db.query('SHOW TABLES'); | ||
assert.strictEqual(result.rows.length, 0); | ||
done(); | ||
} | ||
query.on('result', row => { | ||
dbCon.pause(); // Pause streaming while handling row | ||
runTest(); | ||
}); | ||
rowNr++; | ||
it('Stream rows', done => { | ||
(new Promise(async resolve => { | ||
let rowNr = 0; | ||
if (rowNr === 1) { | ||
assert.strictEqual(row.id, 1); | ||
assert.strictEqual(row.name, 'bosse'); | ||
} else if (rowNr === 2) { | ||
assert.strictEqual(row.id, 2); | ||
assert.strictEqual(row.name, 'hasse'); | ||
} else if (rowNr === 2) { | ||
assert.strictEqual(row.id, 3); | ||
assert.strictEqual(row.name, 'vråkbert'); | ||
} | ||
// Create table with data | ||
await db.query('CREATE TABLE `plutt` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(191) NOT NULL);'); | ||
await db.query('INSERT INTO plutt (name) VALUES(\'bosse\'),(\'hasse\'),(\'vråkbert\');'); | ||
dbCon.resume(); // Resume streaming when processing of row is done (this is normally done in async, doh) | ||
}); | ||
// Get dbCon | ||
const dbCon = await db.getConnection(); | ||
query.on('end', async () => { | ||
assert.strictEqual(rowNr, 3); | ||
dbCon.release(); | ||
// Check contents | ||
const query = db.streamQuery('SELECT * FROM plutt'); | ||
await db.removeAllTables(); | ||
query.on('error', err => { | ||
throw err; | ||
}); | ||
resolve(); | ||
}); | ||
})).then(() => t.end()); | ||
}); | ||
query.on('fields', fields => { | ||
assert.strictEqual(fields[0].name, 'id'); | ||
assert.strictEqual(fields[1].name, 'name'); | ||
}); | ||
test('Time zone dependent data', t => { | ||
(new Promise(async resolve => { | ||
// Create table | ||
const sql = 'CREATE TABLE tzstuff (id int(11), tzstamp timestamp, tzdatetime datetime);'; | ||
await db.query(sql); | ||
query.on('result', row => { | ||
dbCon.pause(); // Pause streaming while handling row | ||
// Set datetime as javascript Date object | ||
const dateObj = new Date('2018-03-04T17:38:20Z'); | ||
await db.query('INSERT INTO tzstuff VALUES(?,?,?);', [1, dateObj, dateObj]); | ||
rowNr++; | ||
// Check the values | ||
const result = await db.query('SELECT * FROM tzstuff ORDER BY id'); | ||
let foundRows = 0; | ||
if (rowNr === 1) { | ||
assert.strictEqual(row.id, 1); | ||
assert.strictEqual(row.name, 'bosse'); | ||
} else if (rowNr === 2) { | ||
assert.strictEqual(row.id, 2); | ||
assert.strictEqual(row.name, 'hasse'); | ||
} else if (rowNr === 2) { | ||
assert.strictEqual(row.id, 3); | ||
assert.strictEqual(row.name, 'vråkbert'); | ||
} | ||
for (let i = 0; result.rows[i] !== undefined; i++) { | ||
const row = result.rows[i]; | ||
dbCon.resume(); // Resume streaming when processing of row is done (this is normally done in async, doh) | ||
}); | ||
if (row.id === 1) { | ||
foundRows++; | ||
assert.strictEqual(row.tzstamp.toISOString(), '2018-03-04T17:38:20.000Z'); | ||
assert.strictEqual(row.tzdatetime.toISOString(), '2018-03-04T17:38:20.000Z'); | ||
} | ||
} | ||
query.on('end', async () => { | ||
assert.strictEqual(rowNr, 3); | ||
dbCon.release(); | ||
assert.strictEqual(foundRows, 1); | ||
await db.removeAllTables(); | ||
// Remove table | ||
await db.query('DROP TABLE tzstuff;'); | ||
resolve(); | ||
})).then(() => t.end()); | ||
}); | ||
resolve(); | ||
}); | ||
})).then(done); | ||
}); | ||
test('Close the db pool', t => { | ||
(new Promise(async resolve => { | ||
await db.pool.end(); | ||
it('Time zone dependent data', done => { | ||
(new Promise(async resolve => { | ||
// Create table | ||
const sql = 'CREATE TABLE tzstuff (id int(11), tzstamp timestamp, tzdatetime datetime);'; | ||
await db.query(sql); | ||
// There are several active handles at this stage, don't know why | ||
// process._getActiveHandles()[0]; | ||
// Set datetime as javascript Date object | ||
const dateObj = new Date('2018-03-04T17:38:20Z'); | ||
await db.query('INSERT INTO tzstuff VALUES(?,?,?);', [1, dateObj, dateObj]); | ||
// Check the values | ||
const result = await db.query('SELECT * FROM tzstuff ORDER BY id'); | ||
let foundRows = 0; | ||
for (let i = 0; result.rows[i] !== undefined; i++) { | ||
const row = result.rows[i]; | ||
if (row.id === 1) { | ||
foundRows++; | ||
assert.strictEqual(row.tzstamp.toISOString(), '2018-03-04T17:38:20.000Z'); | ||
assert.strictEqual(row.tzdatetime.toISOString(), '2018-03-04T17:38:20.000Z'); | ||
} | ||
} | ||
assert.strictEqual(foundRows, 1); | ||
// Remove table | ||
await db.query('DROP TABLE tzstuff;'); | ||
resolve(); | ||
})).then(done); | ||
resolve(); | ||
})).then(() => { | ||
t.end(); | ||
process.exit(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26129
184
5
421
+ Addedlarvitutils@2.3.0(transitive)
- Removedlarvitutils@2.3.3(transitive)
Updatedlarvitutils@2.3.0