Skip to content

Commit e35a541

Browse files
Daniel1984JacobPlaster
authored andcommitted
adding new fund keeper model and tests
1 parent 248ff6e commit e35a541

3 files changed

Lines changed: 181 additions & 0 deletions

File tree

lib/fund_keeper.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict'
2+
3+
const stringValidator = require('./validators/string')
4+
const dateValidator = require('./validators/date')
5+
const Model = require('./model')
6+
7+
const fields = {
8+
mts: 0,
9+
type: 1,
10+
// PLACEHOLDER,
11+
// PLACEHOLDER,
12+
// PLACEHOLDER,
13+
// PLACEHOLDER,
14+
status: 6,
15+
text: 7
16+
}
17+
18+
/**
19+
* Fund Keeper model
20+
*/
21+
class FundKeeper extends Model {
22+
/**
23+
* @param {object|Array} data - fund keeper data
24+
* @param {number} data.mts - Millisecond Time Stamp of the update
25+
* @param {string} data.type - Purpose of notification ('fk-req' (funding keep request))
26+
* @param {string} data.status - Status of the notification; it may vary over time (SUCCESS, ERROR, FAILURE, ...)
27+
* @param {string} data.text - Text of the notification
28+
*/
29+
constructor (data = {}) {
30+
super({ data, fields })
31+
}
32+
33+
/**
34+
* @param {object[]|object|Array[]|Array} data - data to convert to POJO
35+
* @returns {object} pojo
36+
*/
37+
static unserialize (data) {
38+
return super.unserialize({ data, fields })
39+
}
40+
41+
/**
42+
* @returns {string} str
43+
*/
44+
toString () {
45+
const { mts, type, status, text } = this
46+
47+
return [
48+
new Date(mts).toLocaleString(),
49+
`type:${type}`,
50+
`status:${status}`,
51+
`msg:${text}`
52+
].join(' ')
53+
}
54+
55+
/**
56+
* Validates a given fund keeper instance
57+
*
58+
* @param {object[]|object|FundKeeper[]|FundKeeper|Array} data - instance to validate
59+
* @returns {string} error - null if instance is valid
60+
*/
61+
static validate (data) {
62+
return super.validate({
63+
data,
64+
fields,
65+
validators: {
66+
mts: dateValidator,
67+
type: stringValidator,
68+
status: stringValidator,
69+
text: stringValidator
70+
}
71+
})
72+
}
73+
}
74+
75+
module.exports = FundKeeper

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
Invoice: require('./invoice'),
3636
TradingAvgPrice: require('./trading_avg_price'),
3737
FundingAvgRate: require('./funding_avg_rate'),
38+
FundKeeper: require('./fund_keeper'),
3839

3940
isCollection: require('./util/is_collection')
4041
}

test/lib/models/fund_keeper.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const assert = require('assert')
5+
const _includes = require('lodash/includes')
6+
const _compact = require('lodash/compact')
7+
const { FundKeeper } = require('../../../lib')
8+
const testModel = require('../../helpers/test_model')
9+
const testModelValidation = require('../../helpers/test_model_validation')
10+
11+
describe('Fund Keeper model', () => {
12+
testModel({
13+
model: FundKeeper,
14+
orderedFields: [
15+
'mts',
16+
'type',
17+
null,
18+
null,
19+
null,
20+
null,
21+
'status',
22+
'text'
23+
]
24+
})
25+
26+
testModelValidation({
27+
model: FundKeeper,
28+
validData: {
29+
mts: new Array(...(new Array(5))).map(() => Math.random()),
30+
type: ['foo', 'bar', 'baz', 'qux'],
31+
status: ['foo', 'bar', 'baz', 'qux'],
32+
text: ['foo', 'bar', 'baz', 'qux']
33+
}
34+
})
35+
36+
it('initializes correctly', () => {
37+
const fk = new FundKeeper([
38+
1590052551928,
39+
'fk-req',
40+
null,
41+
null,
42+
null,
43+
null,
44+
'success',
45+
'text'
46+
])
47+
48+
assert.strictEqual(fk.mts, 1590052551928)
49+
assert.strictEqual(fk.type, 'fk-req')
50+
assert.strictEqual(fk.status, 'success')
51+
assert.strictEqual(fk.text, 'text')
52+
})
53+
54+
it('serializes correctly', () => {
55+
const fk = new FundKeeper([
56+
1590052551928,
57+
'fk-req',
58+
null,
59+
null,
60+
null,
61+
null,
62+
'success',
63+
'text'
64+
])
65+
66+
assert.deepStrictEqual(_compact(fk.serialize()), [
67+
1590052551928,
68+
'fk-req',
69+
'success',
70+
'text'
71+
])
72+
})
73+
74+
it('unserializes correctly', () => {
75+
const obj = FundKeeper.unserialize([
76+
1590052551928,
77+
'fk-req',
78+
null,
79+
null,
80+
null,
81+
null,
82+
'success',
83+
'text'
84+
])
85+
86+
assert.strictEqual(obj.mts, 1590052551928)
87+
assert.strictEqual(obj.type, 'fk-req')
88+
assert.strictEqual(obj.status, 'success')
89+
assert.strictEqual(obj.text, 'text')
90+
})
91+
92+
describe('toString', () => {
93+
it('includes pertinent information', () => {
94+
const fk = new FundKeeper({
95+
mts: 1590052551928,
96+
type: 'fk-req',
97+
status: 'success',
98+
text: 'text'
99+
})
100+
101+
const str = fk.toString()
102+
assert.ok(_includes(str, 'type:fk-req status:success msg:text'), 'missing mandatory fields')
103+
})
104+
})
105+
})

0 commit comments

Comments
 (0)