Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy committed Apr 10, 2021
1 parent e5a9b1f commit 242e3bd
Show file tree
Hide file tree
Showing 12 changed files with 572 additions and 2 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.gitignore
.eslint.js
/src
/test
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"build": "npm run build:esm && npm run build:cjs",
"build:esm": "babel --delete-dir-on-start -d esm/ src/ && cp src/index.js esm && cp src/cluster.js esm && cp src/auth.js esm",
"build:cjs": "babel --delete-dir-on-start --env-name cjs -d lib/ src/ && cp src/index.js lib && cp src/cluster.js lib && cp src/auth.js esm",
"prepublish": "yarn build"
"prepublish": "yarn build",
"test": "mocha --recursive -r esm"
},
"peerDependencies": {
"core-js": "^3"
Expand All @@ -31,7 +32,16 @@
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.2",
"prettier": "^2.1.2"
"prettier": "^2.1.2",
"@babel/node": "^7.10.5",
"@babel/polyfill": "^7.11.5",
"esm": "^3.2.25",
"express-cli": "0.0.1",
"mocha": "^8.2.0",
"nodemon": "^2.0.6",
"rimraf": "^3.0.2",
"webpack": "^5.4.0",
"webpack-cli": "^3.3.12"
},
"dependencies": {
"dotenv": "^8.2.0",
Expand Down
51 changes: 51 additions & 0 deletions test/adapters/event-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

// var assert = require('assert');
// import Model from '../../src/models/model';
// import { listen } from '../../src/adapters/event-adapter';
// import { Event } from '../services/event-service';

// describe('event-adapter', function () {
// // describe('listen()', async function () {
// it('should automatically unsubscribe on receipt of message', async function () {
// const id = {};
// function make() {
// return (...b) => ({ a: 'a', b });
// }
// const adapters = {
// listen: listen(Event),
// async test({ model }) {
// const subscription = await model.listen({
// topic: 'test',
// id: id,
// filter: 'test',
// once: true,
// model,
// callback: ({ subscription }) => subscription
// });
// console.log({ subscriptions: subscription.getSubscriptions()[0] });
// }
// }
// const model = await Model.create({
// spec: {
// modelName: 'ABC',
// factory: make(),
// ports: {
// listen: {
// type: 'outbound'
// },
// test: {
// type: 'outbound'
// }
// },
// dependencies: adapters
// },
// args: [{ c: 'c' }]
// });
// const subscription = await model.test();
// console.log({ subscriptions: subscription.getSubscriptions()[0] });
// assert.strictEqual(
// false, subscription.getSubscriptions()[0][1].delete(id)
// );
// });
// });
// });
41 changes: 41 additions & 0 deletions test/controllers/post-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

var assert = require('assert');

import addModelFactory from '../../lib/use-cases/add-model'
import postModelFactory from '../../lib/controllers/post-model'

import DataSourceFactory from '../../lib/datasources'
import ModelFactory from '../../lib/models';
import ObserverFactory from '../../lib/models/observer';
import hash from '../../lib/lib/hash'

describe('Controllers', function () {
describe('postModel()', function () {
it('should add new model', async function () {
ModelFactory.registerModel({
modelName: 'ABC',
factory: ({ a }) => ({ a, b: 'c' }),
endpoint: 'abcs',
dependencies: {}
});
ModelFactory.registerEvent(
ModelFactory.EventTypes.CREATE,
'ABC',
(model) => ({ model })
);
const addModel = await addModelFactory({
modelName: 'ABC',
models: ModelFactory,
repository: DataSourceFactory.getDataSource('ABC'),
observer: ObserverFactory.getInstance()
});
const resp = await postModelFactory(
addModel,
ModelFactory.getModelId,
hash
)({ body: { a: 'a' }, headers: { 'User-Agent': 'test' }, ip: '127.0.0.1' });
assert.strictEqual(resp.statusCode, 201);
});
});
});
15 changes: 15 additions & 0 deletions test/datasources/datasource-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

var assert = require("assert");
process.env.DATASOURCE_ADAPTER = "DataSourceFile";
const { default: DataSourceFactory } = require("../../lib/datasources");

describe("datasources", function () {
var ds = DataSourceFactory.getDataSource("test");
ds.load({
name: "test"
});
ds.save(1, "data");
console.log("record", ds.find(1));
it("read from file", function () {});
});
145 changes: 145 additions & 0 deletions test/models/mixins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
"use strict";

var assert = require("assert");
const {
fromSymbol,
toSymbol,
fromTimestamp,
withSerializers,
withDeserializers,
} = require("../../src/models/mixins");

describe("Mixins", function () {
it("should return strings in place of symbols", function () {
const ID = Symbol("id");
const CREATETIME = Symbol("createTime");

const keyMap = {
id: ID,
createTime: CREATETIME,
};
var time = new Date().getTime();
var obj1 = {
[ID]: "123",
[CREATETIME]: time,
id: "123",
createTime: time,
};
// console.log("obj1", obj1);
var obj2 = fromSymbol(keyMap)(obj1);
// console.log("obj2", obj2);
assert.strictEqual(JSON.stringify(obj1), JSON.stringify(obj2));
});
it("should return Symbols in place of strings", function () {
const ID = Symbol("id");
const CREATETIME = Symbol("createTime");

const keyMap = {
id: ID,
createTime: CREATETIME,
};
var time = new Date().getTime();
var obj1 = {
id: "123",
createTime: time,
};
// console.log("obj1", obj1);
var obj2 = toSymbol(keyMap)(obj1);
// console.log("obj2", obj2);
assert.strictEqual(JSON.stringify(obj1), JSON.stringify(obj2));
});
it("should return utc in place of timestamp", function () {
var time = new Date().getTime();
var obj1 = {
createTime: time,
updateTime: time,
};
// console.log("obj1", obj1);
var obj2 = fromTimestamp(["createTime", "updateTime"])(obj1);
// console.log("obj2", obj2);
assert.strictEqual(
new Date(obj1.createTime).toUTCString(),
obj2.createTime
);
assert.strictEqual(
new Date(obj1.updateTime).toUTCString(),
obj2.updateTime
);
});
it("should return serialized output", function () {
var time = new Date().getTime();
const ID = Symbol("id");
const CREATETIME = Symbol("createTime");
const keyMap = {
id: ID,
createTime: CREATETIME,
};
var obj1 = {
[ID]: "123",
[CREATETIME]: time,
};
var serialize = withSerializers(
fromSymbol(keyMap),
fromTimestamp(["createTime"])
);
var obj2 = serialize(obj1);
var obj3 = {
createTime: new Date(time).toUTCString(),
id: "123",
};
// console.log(fromTimestamps(["createTime"]).toString());
// console.log(fromSymbols(keyMap).toString());
// console.log(withSerializer(
// fromSymbols(keyMap),
// fromTimestamps(["createTime"])
// ).toString());
//console.log(makeModel.toString());
// console.log("obj1", obj1);
// console.log("obj2", obj2);
// console.log("obj3", obj3);
// console.log("stringify(obj1)", JSON.stringify(obj1));
// console.log("stringify(obj2)", JSON.stringify(obj2));
// console.log("stringify(obj3)", JSON.stringify(obj3));
assert.strictEqual(JSON.stringify(obj2), JSON.stringify(obj3));
});

it("should return deserialized output", function () {
var time = new Date().getTime();
const ID = Symbol("id");
const CREATETIME = Symbol("createTime");
const keyMap = {
id: ID,
createTime: CREATETIME,
};
var obj1 = {
[ID]: "123",
[CREATETIME]: time,
};
var serialize = withSerializers(
fromSymbol(keyMap),
fromTimestamp(["createTime"])
);
var deserialize = withDeserializers(
toSymbol(keyMap),
);
var obj2 = deserialize(serialize(obj1));
var obj3 = {
createTime: new Date(time).toUTCString(),
id: "123",
};
// console.log(fromTimestamps(["createTime"]).toString());
// console.log(fromSymbols(keyMap).toString());
// console.log(withSerializer(
// fromSymbols(keyMap),
// fromTimestamps(["createTime"])
// ).toString());
//console.log(makeModel.toString());
console.log("obj1", obj1);
console.log("obj2", obj2);
console.log("obj3", obj3);
console.log("parse(obj1)", JSON.parse(JSON.stringify(obj1)));
console.log("parse(obj2)", JSON.parse(JSON.stringify(obj2)));
console.log("parse(obj3)", JSON.parse(JSON.stringify(obj3)));
assert.strictEqual(JSON.parse(JSON.stringify(obj2)), JSON.parse(JSON.stringify(obj)));
});
});
31 changes: 31 additions & 0 deletions test/models/model-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

var assert = require('assert');

import ModelFactory from '../../src/models';


describe('ModelFactory', function () {
describe('#createModel()', function () {
it('should register & create model', async function () {
ModelFactory.registerModel({
modelName: 'ABC',
factory: ({ a }) => ({ a, b: 'c' }),
endpoint: 'abcs',
dependencies: {}
});
const model = await ModelFactory.createModel('ABC', { a: 'a' });
assert.strictEqual(ModelFactory.getModelName(model), 'ABC');
});
it('should have props from args', async function () {
ModelFactory.registerModel({
modelName: 'ABC',
factory: ({ a }) => ({ a, b: 'c' }),
endpoint: 'abcs',
dependencies: {}
});
const model = await ModelFactory.createModel('ABC', { a: 'a' });
assert.strictEqual(model.a, 'a');
});
});
});
Loading

0 comments on commit 242e3bd

Please sign in to comment.