2018-03-02 08:42:12 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const expect = require("chai").expect;
|
2020-02-09 12:21:45 +00:00
|
|
|
const util = require("../util");
|
2018-03-02 08:42:12 +00:00
|
|
|
const Msg = require("../../src/models/msg");
|
|
|
|
const Helper = require("../../src/helper");
|
2018-04-17 08:06:08 +00:00
|
|
|
const MessageStorage = require("../../src/plugins/messageStorage/sqlite.js");
|
2018-03-02 08:42:12 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("SQLite Message Storage", function () {
|
2019-11-27 18:25:29 +00:00
|
|
|
// Increase timeout due to unpredictable I/O on CI services
|
2020-02-09 12:21:45 +00:00
|
|
|
this.timeout(util.isRunningOnCI() ? 25000 : 5000);
|
|
|
|
this.slow(300);
|
2019-11-27 18:25:29 +00:00
|
|
|
|
2018-03-02 08:42:12 +00:00
|
|
|
const expectedPath = path.join(Helper.getHomePath(), "logs", "testUser.sqlite3");
|
|
|
|
let store;
|
|
|
|
|
|
|
|
// Delete database file from previous test run
|
2020-03-21 20:55:36 +00:00
|
|
|
before(function (done) {
|
2018-04-27 10:16:23 +00:00
|
|
|
store = new MessageStorage({
|
2018-04-17 08:06:08 +00:00
|
|
|
name: "testUser",
|
2018-04-27 10:16:23 +00:00
|
|
|
idMsg: 1,
|
|
|
|
});
|
2018-03-02 08:42:12 +00:00
|
|
|
|
|
|
|
if (fs.existsSync(expectedPath)) {
|
|
|
|
fs.unlink(expectedPath, done);
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should resolve an empty array when disabled", function (done) {
|
2018-03-02 08:42:12 +00:00
|
|
|
store.getMessages(null, null).then((messages) => {
|
|
|
|
expect(messages).to.be.empty;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should create database file", function () {
|
2018-03-02 08:42:12 +00:00
|
|
|
expect(store.isEnabled).to.be.false;
|
|
|
|
expect(fs.existsSync(expectedPath)).to.be.false;
|
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
store.enable();
|
2018-03-02 08:42:12 +00:00
|
|
|
|
|
|
|
expect(store.isEnabled).to.be.true;
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should create tables", function (done) {
|
2018-03-02 08:42:12 +00:00
|
|
|
store.database.serialize(() =>
|
2019-07-17 09:33:59 +00:00
|
|
|
store.database.all(
|
|
|
|
"SELECT name, tbl_name, sql FROM sqlite_master WHERE type = 'table'",
|
|
|
|
(err, row) => {
|
|
|
|
expect(err).to.be.null;
|
|
|
|
expect(row).to.deep.equal([
|
|
|
|
{
|
|
|
|
name: "options",
|
|
|
|
tbl_name: "options",
|
|
|
|
sql:
|
|
|
|
"CREATE TABLE options (name TEXT, value TEXT, CONSTRAINT name_unique UNIQUE (name))",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "messages",
|
|
|
|
tbl_name: "messages",
|
|
|
|
sql:
|
|
|
|
"CREATE TABLE messages (network TEXT, channel TEXT, time INTEGER, type TEXT, msg TEXT)",
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
)
|
2018-03-02 08:42:12 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should insert schema version to options table", function (done) {
|
2018-03-02 08:42:12 +00:00
|
|
|
store.database.serialize(() =>
|
2019-07-17 09:33:59 +00:00
|
|
|
store.database.get(
|
|
|
|
"SELECT value FROM options WHERE name = 'schema_version'",
|
|
|
|
(err, row) => {
|
|
|
|
expect(err).to.be.null;
|
|
|
|
|
|
|
|
// Should be sqlite.currentSchemaVersion,
|
|
|
|
// compared as string because it's returned as such from the database
|
|
|
|
expect(row.value).to.equal("1520239200");
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
)
|
2018-03-02 08:42:12 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should store a message", function (done) {
|
2018-03-25 13:00:48 +00:00
|
|
|
store.database.serialize(() => {
|
2019-07-17 09:33:59 +00:00
|
|
|
store.index(
|
|
|
|
{
|
|
|
|
uuid: "this-is-a-network-guid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "#thisISaCHANNEL",
|
|
|
|
},
|
|
|
|
new Msg({
|
|
|
|
time: 123456789,
|
|
|
|
text: "Hello from sqlite world!",
|
|
|
|
})
|
|
|
|
);
|
2018-03-02 08:42:12 +00:00
|
|
|
|
2018-03-25 13:00:48 +00:00
|
|
|
done();
|
|
|
|
});
|
2018-03-02 08:42:12 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should retrieve previously stored message", function (done) {
|
2019-07-17 09:33:59 +00:00
|
|
|
store.database.serialize(() =>
|
|
|
|
store
|
|
|
|
.getMessages(
|
|
|
|
{
|
|
|
|
uuid: "this-is-a-network-guid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "#thisisaCHANNEL",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then((messages) => {
|
|
|
|
expect(messages).to.have.lengthOf(1);
|
|
|
|
|
|
|
|
const msg = messages[0];
|
|
|
|
|
|
|
|
expect(msg.text).to.equal("Hello from sqlite world!");
|
|
|
|
expect(msg.type).to.equal(Msg.Type.MESSAGE);
|
|
|
|
expect(msg.time.getTime()).to.equal(123456789);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
);
|
2018-03-02 08:42:12 +00:00
|
|
|
});
|
2018-03-10 16:49:16 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should close database", function (done) {
|
2018-03-10 16:49:16 +00:00
|
|
|
store.close((err) => {
|
|
|
|
expect(err).to.be.null;
|
|
|
|
expect(fs.existsSync(expectedPath)).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-03-02 08:42:12 +00:00
|
|
|
});
|