From 1fd1df29da5ad82968662bb97d0ffb306bf97c67 Mon Sep 17 00:00:00 2001 From: Nachtalb Date: Sun, 23 May 2021 00:55:47 +0200 Subject: [PATCH] Update tests --- test/plugins/sqlite.js | 116 ++++++++++++++---------------------- test/tests/textLogFolder.js | 10 ++-- 2 files changed, 50 insertions(+), 76 deletions(-) diff --git a/test/plugins/sqlite.js b/test/plugins/sqlite.js index 0971d00f..84b2ca05 100644 --- a/test/plugins/sqlite.js +++ b/test/plugins/sqlite.js @@ -54,96 +54,68 @@ describe("SQLite Message Storage", function () { }); it("should create tables", function (done) { - store.database.serialize(() => - 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)", - }, - ]); + const rows = store.database + .prepare("SELECT name, tbl_name, sql FROM sqlite_master WHERE type = 'table'") + .all(); - done(); - } - ) - ); + expect(rows).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(); }); it("should insert schema version to options table", function (done) { - store.database.serialize(() => - 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(); - } - ) - ); + const row = store.database + .prepare("SELECT value FROM options WHERE name = 'schema_version'") + .get(); + expect(row.value).to.equal("1520239200"); + done(); }); it("should store a message", function (done) { - store.database.serialize(() => { - store.index( - { - uuid: "this-is-a-network-guid", - }, - { - name: "#thisISaCHANNEL", - }, - new Msg({ - time: 123456789, - text: "Hello from sqlite world!", - }) - ); + store.index( + {uuid: "this-is-a-network-guid"}, + {name: "#thisISaCHANNEL"}, + new Msg({ + time: 123456789, + text: "Hello from sqlite world!", + }) + ); - done(); - }); + done(); }); it("should retrieve previously stored message", function (done) { - store.database.serialize(() => - store - .getMessages( - { - uuid: "this-is-a-network-guid", - }, - { - name: "#thisisaCHANNEL", - } - ) - .then((messages) => { - expect(messages).to.have.lengthOf(1); + store + .getMessages({uuid: "this-is-a-network-guid"}, {name: "#thisisaCHANNEL"}) + .then((messages) => { + expect(messages).to.have.lengthOf(1); - const msg = messages[0]; + 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); + 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(); - }) - ); + done(); + }); }); it("should close database", function (done) { store.close((err) => { - expect(err).to.be.null; + expect(err).to.be.undefined; expect(fs.existsSync(expectedPath)).to.be.true; done(); }); diff --git a/test/tests/textLogFolder.js b/test/tests/textLogFolder.js index e00d7839..b65520a5 100644 --- a/test/tests/textLogFolder.js +++ b/test/tests/textLogFolder.js @@ -4,9 +4,11 @@ const expect = require("chai").expect; const TextFileMessageStorage = require("../../src/plugins/messageStorage/text"); describe("TextFileMessageStorage", function () { + const store = new TextFileMessageStorage(); + it("should combine network name and uuid into a safe name", function () { expect( - TextFileMessageStorage.getNetworkFolderName({ + store._getNetworkFolderName({ name: "Freenode", uuid: "f9042ec9-4016-45e0-a8a8-d378fb252628", }) @@ -15,7 +17,7 @@ describe("TextFileMessageStorage", function () { it("network name should be cleaned up and lowercased", function () { expect( - TextFileMessageStorage.getNetworkFolderName({ + store._getNetworkFolderName({ name: '@ TeSt ../..\\<>:"/\\|?*', uuid: "f9042ec9-4016-45e0-a8a8-d378fb252628", }) @@ -24,7 +26,7 @@ describe("TextFileMessageStorage", function () { it("folder name may contain two dashes if on boundary", function () { expect( - TextFileMessageStorage.getNetworkFolderName({ + store._getNetworkFolderName({ name: "Freenod", uuid: "f9042ec9-4016-45e0-a8a8-d378fb252628", }) @@ -33,7 +35,7 @@ describe("TextFileMessageStorage", function () { it("should limit network name length", function () { expect( - TextFileMessageStorage.getNetworkFolderName({ + store._getNetworkFolderName({ name: "This network name is longer than the uuid itself but it should be limited", uuid: "f9042ec9-4016-45e0-a8a8-d378fb252628", })