From ae6864ba558281ec40d5b0cf67b7021cc946ab52 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sun, 4 Feb 2018 00:31:24 +0300 Subject: [PATCH] feat(tests): db test --- src/background/background.js | 22 ++++++++++++ src/background/config.js | 4 +-- src/background/config.json | 3 -- tests/basic.test.js | 4 +-- tests/sphinx.test.js | 70 ++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 7 deletions(-) delete mode 100644 src/background/config.json create mode 100644 tests/sphinx.test.js diff --git a/src/background/background.js b/src/background/background.js index bd40789..6729c0e 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -152,6 +152,28 @@ const writeSphinxConfig = (path) => { } `; + // clear dir in test env + if(env.name === 'test') + { + if (fs.existsSync(`${path}/database`)) { + fs.readdirSync(`${path}/database`).forEach(function(file, index){ + const curPath = `${path}/database` + "/" + file; + if (!fs.lstatSync(curPath).isDirectory()) { + fs.unlinkSync(curPath); + } + }); + + fs.readdirSync(path).forEach(function(file, index){ + if(!file.startsWith('binlog')) + return; + const curPath = path + "/" + file; + if (!fs.lstatSync(curPath).isDirectory()) { + fs.unlinkSync(curPath); + } + }); + } + } + if (!fs.existsSync(`${path}/database`)){ fs.mkdirSync(`${path}/database`); } diff --git a/src/background/config.js b/src/background/config.js index 0f7b274..8ed58ed 100644 --- a/src/background/config.js +++ b/src/background/config.js @@ -42,10 +42,10 @@ let config = { const fs = require('fs'); const debug = require('debug')('config') -let configPath = 'config.json' +let configPath = 'rats.json' if(app && app.getPath("userData") && app.getPath("userData").length > 0) { - configPath = app.getPath("userData") + '/config.json' + configPath = app.getPath("userData") + '/rats.json' } const configProxy = new Proxy(config, { diff --git a/src/background/config.json b/src/background/config.json deleted file mode 100644 index 4e07834..0000000 --- a/src/background/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "indexer": true -} \ No newline at end of file diff --git a/tests/basic.test.js b/tests/basic.test.js index 4aa3e8f..9158546 100644 --- a/tests/basic.test.js +++ b/tests/basic.test.js @@ -2,8 +2,8 @@ import { expect } from "chai"; import testUtils from "./utils"; describe("application launch", () => { - beforeEach(testUtils.beforeEach); - afterEach(testUtils.afterEach); + before(testUtils.beforeEach); + after(testUtils.afterEach); it("index page loaded", async function() { const { app } = this diff --git a/tests/sphinx.test.js b/tests/sphinx.test.js new file mode 100644 index 0000000..94e71bd --- /dev/null +++ b/tests/sphinx.test.js @@ -0,0 +1,70 @@ +import { expect } from "chai"; +import testUtils from "./utils"; + +const mysql = require('mysql') +const config = require('../src/background/config') + +describe("sphinx", () => { + before(testUtils.beforeEach); + after(testUtils.afterEach); + + it("runned", async function() { + const { app } = this + await app.client.waitForExist('#index-window') + }); + + let sphinx; + + it("init", function() { + sphinx = mysql.createPool({ + connectionLimit: config.sphinx.connectionLimit, + host : config.sphinx.host, + port : config.sphinx.port + }); + expect(sphinx) + }) + + it("insert", function(done) { + sphinx.query("INSERT INTO files(id, hash, path, pathIndex, size) VALUES(1, 'a', 'bashaa', 'bashaa', 50)", (err) => { + if(err) + throw new Error(err) + + sphinx.query("INSERT INTO files(id, hash, path, pathIndex, size) VALUES(2, 'b', 'biotu', 'biotu', 30)", (err) => { + if(err) + throw new Error(err) + + done() + }) + }) + }) + + it("select", function(done) { + sphinx.query("select * from files where hash = 'a'", (err, result) => { + if(!result) + throw new Error(err) + if(result.length !== 1) + throw new Error('not one result') + + if(result[0].size !== 50) + throw new Error('not 50 in field') + + done() + }) + }) + + it("search", function(done) { + sphinx.query("select * from files where MATCH('bashaa')", (err, result) => { + if(!result) + throw new Error(err) + if(result.length !== 1) + throw new Error('not one result') + + if(result[0].hash !== 'a') + throw new Error('not a in hash') + if(result[0].size !== 50) + throw new Error('not 50 in field') + + done() + }) + }) +});