-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5a9b1f
commit 242e3bd
Showing
12 changed files
with
572 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.gitignore | ||
.eslint.js | ||
/src | ||
/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
// ); | ||
// }); | ||
// }); | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () {}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.