Commit b163fa47 by Chavee Issariyapat

add topicstore with test

parent 8869db29
{
"name": "flowagent",
"version": "1.1.1",
"version": "1.2.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......@@ -301,6 +301,12 @@
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=",
"optional": true
},
"assertion-error": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
"integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==",
"dev": true
},
"async": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz",
......@@ -523,6 +529,32 @@
"integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=",
"optional": true
},
"chai": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz",
"integrity": "sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==",
"dev": true,
"requires": {
"assertion-error": "^1.1.0",
"check-error": "^1.0.2",
"deep-eql": "^3.0.1",
"get-func-name": "^2.0.0",
"pathval": "^1.1.0",
"type-detect": "^4.0.5"
}
},
"chai-each": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/chai-each/-/chai-each-0.0.1.tgz",
"integrity": "sha512-W0G98eDRmV4ahXjGBXHsljhXU2MBLQquP2Eo+oOikqywGO0QLYK8tBUKkFGwl7KPQWeeQPLssvRbQXFsIk6Xlg==",
"dev": true
},
"chai-things": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/chai-things/-/chai-things-0.2.0.tgz",
"integrity": "sha1-xVEoN4+bs5nplPAAUhUZhO1uvnA=",
"dev": true
},
"chainsaw": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/chainsaw/-/chainsaw-0.1.0.tgz",
......@@ -544,6 +576,12 @@
"supports-color": "^2.0.0"
}
},
"check-error": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz",
"integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=",
"dev": true
},
"chownr": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.3.tgz",
......@@ -662,6 +700,15 @@
"ms": "2.0.0"
}
},
"deep-eql": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz",
"integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==",
"dev": true,
"requires": {
"type-detect": "^4.0.0"
}
},
"deep-extend": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz",
......@@ -1116,6 +1163,12 @@
"wide-align": "^1.1.0"
}
},
"get-func-name": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz",
"integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=",
"dev": true
},
"getpass": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz",
......@@ -2574,6 +2627,12 @@
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz",
"integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw=="
},
"pathval": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz",
"integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=",
"dev": true
},
"pbkdf2-password": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/pbkdf2-password/-/pbkdf2-password-1.2.1.tgz",
......@@ -3363,6 +3422,12 @@
"prelude-ls": "~1.1.2"
}
},
"type-detect": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
"integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==",
"dev": true
},
"typedarray": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
......
......@@ -4,6 +4,7 @@
"description": "",
"main": "flowagent.js",
"scripts": {
"test": "mocha spec --require spec/helpers/chai.js --reporter spec --exit",
"start": "node run.js"
},
"author": "NEXPIE Company Limited (https://nexpie.com)",
......@@ -14,5 +15,10 @@
"lodash.merge": "^4.6.2",
"mosca": "^2.8.3",
"mqtt": "^3.0.0"
},
"devDependencies": {
"chai": "^4.2.0",
"chai-each": "0.0.1",
"chai-things": "^0.2.0"
}
}
require('dotenv').config();
var chai = require('chai');
chai.use(require('chai-things'));
chai.use(require('chai-each'));
chai.config.includeStack = false;
global.expect = chai.expect;
global.AssertionError = chai.AssertionError;
global.Assertion = chai.Assertion;
global.assert = chai.assert;
describe("Test TopicStore", function () {
let Topicstore = require('../topicstore');
let topicstore;
beforeEach(function (done) {
topicstore = Topicstore.create();
done();
});
afterEach(function (done) {
delete topicstore;
done();
});
it("topicstore should be empty when firstly created", function (done) {
expect(topicstore.getTopics()).to.have.length(0);
done();
});
it("After addTopic(), topicstore should hold that topic", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
});
it("If add duplicate topics, topicstore.addTopic() should return false", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
});
it("If add more topics, topicstore.addTopic() should hold all", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/ok','1234.5678')).to.equal(true);
expect(await topicstore.addTopic('@msg/bye','1234.5678')).to.equal(true);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello','@msg/ok','@msg/bye']);
});
it("If add duplicate topics by different block, topicstore.addTopic() should return false", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(topicstore.getTopics()).to.deep.equal(['@msg/hello']);
});
it("topicstore.delTopic() should return true if one block holds that topic", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.delTopic('@msg/hello','1234.1234')).to.equal(true);
});
it("topicstore.delTopic() should return false if two block holds that topic", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.1234')).to.equal(false);
});
it("topicstore.delTopic() should return true only if delete by the last block holding that topic", async function () {
expect(await topicstore.addTopic('@msg/hello','1234.1234')).to.equal(true);
expect(await topicstore.addTopic('@msg/hello','1234.5678')).to.equal(false);
expect(await topicstore.addTopic('@msg/hello','1234.9012')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.9012')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.1234')).to.equal(false);
expect(await topicstore.delTopic('@msg/hello','1234.5678')).to.equal(true);
});
});
module.exports.create = create
/*
{
'topic' : ['blockid1', 'blockid2','blockid2']
}
*/
let TopicStore = function() {
this.data = {};
}
TopicStore.prototype.getTopics = function() {
let out = [];
for (let k in this.data) {
out.push(k);
}
return out;
}
TopicStore.prototype.getRawData = function() {
return this.data;
}
TopicStore.prototype.addTopic = function(topic, blockid) {
return new Promise((resolve, reject) => {
if (this.data[topic] == undefined || this.data[topic].length==0) {
this.data[topic] = [blockid];
resolve(true)
}
else {
let p = this.data[topic].indexOf(blockid);
if (p>=0) {
resolve(false);
}
else {
this.data[topic].push(blockid);
resolve(false)
}
}
});
}
TopicStore.prototype.delTopic = function(topic, blockid) {
return new Promise((resolve, reject) => {
if (this.data[topic] == undefined || this.data[topic].length==0) {
resolve(true); // still force unsub
}
else {
let p = this.data[topic].indexOf(blockid);
if (p>=0) {
this.data[topic].splice(p,1);
if (this.data[topic].length == 0) resolve(true);
else resolve(false);
}
else {
resolve(false);
}
}
});
}
function create() {
return new TopicStore();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment