2025-06-19 12:31:54 +02:00
|
|
|
/* global it, describe, before, after */
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2025-06-19 12:31:54 +02:00
|
|
|
const BoxError = require('../boxerror.js'),
|
|
|
|
|
database = require('../database'),
|
2021-09-16 13:59:03 -07:00
|
|
|
expect = require('expect.js'),
|
|
|
|
|
safe = require('safetydance');
|
2018-01-26 18:32:13 +01:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
describe('database', function () {
|
2021-08-20 09:19:44 -07:00
|
|
|
describe('init', function () {
|
|
|
|
|
it('can init database', async function () {
|
|
|
|
|
await database.initialize();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
it('can clear database', async function () {
|
|
|
|
|
await database._clear();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
it('can uninitialize database', async function () {
|
|
|
|
|
await database.uninitialize();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-19 12:31:54 +02:00
|
|
|
describe('query', function () {
|
|
|
|
|
before(async () => { await database.initialize(); });
|
|
|
|
|
after(async () => { await database.uninitialize(); });
|
|
|
|
|
|
|
|
|
|
it('basic query', async function () {
|
|
|
|
|
const result = await database.query('SELECT COUNT(*) as count FROM apps');
|
|
|
|
|
expect(result[0].count).to.be(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('bad queries', async function () {
|
|
|
|
|
const queries = [
|
|
|
|
|
'SELECT COUNT(*) FROM oops', // bad table
|
|
|
|
|
'SELECT', // incomplete
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const query of queries) {
|
|
|
|
|
const [error] = await safe(database.query(query));
|
|
|
|
|
expect(error.reason).to.be(BoxError.DATABASE_ERROR);
|
|
|
|
|
expect(error.sqlMessage).to.be.a('string');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('transaction', function () {
|
|
|
|
|
before(async () => { await database.initialize(); });
|
|
|
|
|
after(async () => { await database.uninitialize(); });
|
|
|
|
|
|
|
|
|
|
it('basic transaction', async function () {
|
|
|
|
|
const results = await database.transaction([
|
|
|
|
|
{ query: 'SELECT COUNT(*) as count FROM apps', args: [] },
|
|
|
|
|
{ query: 'SELECT COUNT(*) as count FROM settings', args: [] },
|
|
|
|
|
]);
|
|
|
|
|
expect(results[0][0].count).to.be(0);
|
|
|
|
|
expect(results[1][0].count).to.be(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('bad transaction', async function () {
|
|
|
|
|
const [error] = await safe(database.transaction([
|
|
|
|
|
{ query: 'SELECT COUNT(*) as count FROM apps', args: [] },
|
|
|
|
|
{ query: 'stuff', args: [] },
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
expect(error.reason).to.be(BoxError.DATABASE_ERROR);
|
|
|
|
|
expect(error.sqlMessage).to.be.a('string');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('import/export', function () {
|
2021-08-20 09:19:44 -07:00
|
|
|
before(async function () {
|
|
|
|
|
await database.initialize();
|
|
|
|
|
await database._clear();
|
2017-11-22 10:57:56 -08:00
|
|
|
});
|
|
|
|
|
|
2021-09-16 13:59:03 -07:00
|
|
|
it('cannot import from non-existent file', async function () {
|
|
|
|
|
const [error] = await safe(database.importFromFile('/does/not/exist'));
|
|
|
|
|
expect(error).to.be.ok();
|
2017-11-22 10:57:56 -08:00
|
|
|
});
|
|
|
|
|
|
2021-09-16 13:59:03 -07:00
|
|
|
it('can export to file', async function () {
|
|
|
|
|
await database.exportToFile('/tmp/box.mysqldump');
|
2017-11-24 15:31:03 -08:00
|
|
|
});
|
|
|
|
|
|
2021-09-16 13:59:03 -07:00
|
|
|
it('can import from file', async function () {
|
|
|
|
|
await database.importFromFile('/tmp/box.mysqldump');
|
2017-11-22 10:57:56 -08:00
|
|
|
});
|
2025-06-19 12:31:54 +02:00
|
|
|
|
|
|
|
|
it('can uninitialize database', async function () {
|
|
|
|
|
await database.uninitialize();
|
|
|
|
|
});
|
2017-11-22 10:57:56 -08:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|