mares-mongoose-model
Version:
슬로그업에���� 사용하는 node.js DDD패턴 구현 시 필요합니다. mongoose 모델 생성시 반드시 해당 모듈을 상속받아�� 합니다.
275 lines (248 loc) • 6.47 kB
JavaScript
const should = require('should')
const BaseModel = require('./index')
const _ = require('lodash')
suite('Module MaresMongooseModel Test', () => {
suiteSetup(async () => {
this.obj = {
a: null,
b: null,
c: 1,
d: '',
e: function () {
return true
},
f: {
a: 1,
b: 2,
c: null,
d: {
a: 1,
b: null
}
},
g: {
a: {
a: null,
b: null
}
},
h: {
a: [null, 1, null]
},
i: {
a: [null, null]
},
j: {
a: null,
b: {
c: 1,
d: [null, {
a: null,
b: null
}]
}
},
k: {
a: [{
a: 1,
b: [null, null],
c: [null, 1]
}]
}
}
})
suite('toSetFromNotNullFields method', () => {
test('should get setQuery', () => {
let obj = _.cloneDeep(this.obj)
let refinedObj = BaseModel.toSetFromNotNullFields(obj)
refinedObj.should.property('c', 1)
refinedObj.should.property('d', '')
refinedObj.should.property('f.a', 1)
refinedObj.should.property('f.b', 2)
refinedObj.should.property('f.d.a', 1)
refinedObj.should.property('h.a', [1])
refinedObj.should.property('j.b.c', 1)
refinedObj.should.property('k.a', [{
a: 1,
c: [1]
}])
})
})
suite('removeNullFields method', () => {
test('should remove null fields', () => {
let obj = _.cloneDeep(this.obj)
let refinedObj = BaseModel.removeNullFields(obj)
refinedObj.should.not.property('a')
refinedObj.should.not.property('b')
refinedObj.f.should.not.property('c')
refinedObj.f.d.should.not.property('b')
refinedObj.should.property('c', 1)
refinedObj.should.property('d', '')
refinedObj.should.property('f', {
a: 1,
b: 2,
d: {
a: 1
}
})
refinedObj.should.not.property('g')
refinedObj.should.not.property('i')
refinedObj.h.should.property('a', [1])
refinedObj.j.should.not.property('a')
refinedObj.j.should.property('b', {
c: 1
})
refinedObj.k.a[0].should.property('a', 1)
refinedObj.k.a[0].c[0].should.eql(1)
})
})
suite('toUnsetFromNullFields method', () => {
test('should remove null fields', () => {
let obj = _.cloneDeep(this.obj)
const refinedObj = BaseModel.toUnsetFromNullFields(_.cloneDeep(obj), true)
refinedObj.should.property('a', '')
refinedObj.should.property('b', '')
refinedObj.should.property('f.c', '')
refinedObj.should.property('f.d.b', '')
refinedObj.should.property('g', '')
refinedObj.should.property('h.a.0', '')
refinedObj.should.property('h.a.2', '')
refinedObj.should.property('i', '')
refinedObj.should.property('j.a', '')
refinedObj.should.property('j.b.d', '')
const refinedObj2 = BaseModel.toUnsetFromNullFields(_.cloneDeep(obj), false)
refinedObj2.should.property('a', '')
refinedObj2.should.property('b', '')
refinedObj2.should.property('f.c', '')
refinedObj2.should.property('f.d.b', '')
refinedObj2.should.property('g', '')
refinedObj2.should.property('i', '')
refinedObj2.should.property('j.a', '')
refinedObj2.should.property('j.b.d', '')
})
})
suite('getValidFields method', async () => {
test('should get fields except undefined value', async () => {
let mock = {
a: 1,
b: undefined,
c: 100,
d: {
a: 100
}
}
let filtered = BaseModel.getValidFields(['a', 'b', 'c', 'd'], mock)
filtered.should.have.not.property('b')
filtered.should.properties(['a', 'c', 'd'])
})
})
suite('getProjection method', async () => {
test('should get projection fields', async () => {
class TestModel extends BaseModel {
static getAttributes() {
return 'id test value app.a app.b app.c.a app.c.b'
}
}
let projection = TestModel.getProjection()
projection.should.properties({
'id': 1,
'test': 1,
'value': 1,
'app.a': 1,
'app.b': 1,
'app.c.a': 1,
'app.c.b': 1
})
})
test('should get projection fields with prefix', async () => {
class TestModel extends BaseModel {
static getAttributes() {
return 'id test value app.a app.b app.c.a app.c.b'
}
}
let projection = TestModel.getProjection('', 'prefix')
projection.should.properties({
'prefix.id': 1,
'prefix.test': 1,
'prefix.value': 1,
'prefix.app.a': 1,
'prefix.app.b': 1,
'prefix.app.c.a': 1,
'prefix.app.c.b': 1
})
})
})
suite('buildGroupQuery method', () => {
test('should make json query', () => {
const intputNamespace = 'orderItems.inventories.locations'
const fields = [
'_id detail',
'_id sellerKey',
'_id name',
'_id locName'
]
let a = {
_id: '$orderItems.inventories._id',
rootId: {$first: '$_id'},
detail: {'$first': '$detail'},
'orderItems---_id': {$first: '$orderItems._id'},
'orderItems---sellerKey': {$first: '$orderItems.sellerKey'},
'orderItems---inventories---_id': {$first: '$orderItems.inventories._id'},
'orderItems---inventories---name': {$first: '$orderItems.inventories.name'},
'orderItems---inventories---locations': {
$push: {
_id: '$orderItems.inventories.locations._id',
locName: '$orderItems.inventories.locations.locName'
}
}
}
let b = {
_id: '$orderItems---_id',
rootId: {$first: '$rootId'},
detail: {'$first': '$detail'},
'orderItems---_id': {$first: '$orderItems._id'},
'orderItems---sellerKey': {$first: '$orderItems.sellerKey'},
'orderItems---inventories': {
$push: {
_id: '$orderItems---inventories---_id',
name: '$orderItems---inventories---name',
locations: '$orderItems---inventories---locations'
}
}
}
let c = {
_id: '$rootId',
detail: {'$first': '$detail'},
orderItems: {
$push: {
_id: '$orderItems---_id',
sellerKey: '$orderItems---sellerKey',
inventories: '$orderItems---inventories'
}
}
}
let query = BaseModel.buildGroupQuery(intputNamespace, fields)
let mocks = [a, b, c]
const recursive = (json, mock) => {
_.map(json, (value, key) => {
if (value instanceof Object) {
recursive(value, mock[key])
} else if (value instanceof Array) {
_.map(value, (item, idx) => {
recursive(item, mock[key][idx])
})
} else {
value.should.eql(mock[key])
}
})
}
for (let i = 0; i < query.length; ++i) {
let json = query[i]['$group']
let mock = mocks[i]
recursive(json, mock)
}
})
})
suiteTeardown(() => {
})
})