/** * 标段--台账 模型 单元测试 * * @author Mai * @date 2017/12/1 * @version */ 'use strict'; /* const testNodeData = [ { ledger_id: 1, ledger_pid: -1, order: 1, level: 1, full_path: '1', code: '1' }, { ledger_id: 2, ledger_pid: 1, order: 1, level: 2, full_path: '1.2', code: '1-1' }, { ledger_id: 6, ledger_pid: 2, order: 1, level: 3, full_path: '1.2.6', code: '1-1-1' }, { ledger_id: 7, ledger_pid: 6, order: 1, level: 4, full_path: '1.2.6.7', code: '202-1' }, { ledger_id: 10, ledger_pid: 7, order: 2, level: 5, full_path: '1.2.6.7.10', code: '202-1-a' }, { ledger_id: 9, ledger_pid: 7, order: 1, level: 5, full_path: '1.2.6.7.9', code: '202-1-b' }, { ledger_id: 8, ledger_pid: 6, order: 2, level: 4, full_path: '1.2.6.8', code: '202-2' }, { ledger_id: 11, ledger_pid: 8, order: 1, level: 5, full_path: '1.2.6.8.11', code: '202-2-c' }, { ledger_id: 12, ledger_pid: 8, order: 2, level: 5, full_path: '1.2.6.8.12', code: '202-2-e' }, { ledger_id: 13, ledger_pid: 2, order: 2, level: 3, full_path: '1.2.13', code: '1-1-2' }, { ledger_id: 14, ledger_pid: 2, order: 3, level: 3, full_path: '1.2.14', code: '1-1-3' }, { ledger_id: 3, ledger_pid: 1, order: 2, level: 2, full_path: '1.3', code: '1-2' }, { ledger_id: 4, ledger_pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3' }, { ledger_id: 5, ledger_pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4' }, ];*/ const testNodeData = [ { ledger_id: 1, ledger_pid: -1, order: 1, level: 1, full_path: '1', code: '1', is_leaf: false }, { ledger_id: 2, ledger_pid: 1, order: 1, level: 2, full_path: '1.2', code: '1-1', is_leaf: false }, { ledger_id: 6, ledger_pid: 2, order: 1, level: 3, full_path: '1.2.6', code: '1-1-1', is_leaf: false }, { ledger_id: 7, ledger_pid: 6, order: 1, level: 4, full_path: '1.2.6.7', code: '202-1', is_leaf: false }, { ledger_id: 10, ledger_pid: 7, order: 2, level: 5, full_path: '1.2.6.7.10', code: '202-1-a', is_leaf: true }, { ledger_id: 9, ledger_pid: 7, order: 1, level: 5, full_path: '1.2.6.7.9', code: '202-1-b', is_leaf: true }, { ledger_id: 8, ledger_pid: 6, order: 2, level: 4, full_path: '1.2.6.8', code: '202-2', is_leaf: false }, { ledger_id: 11, ledger_pid: 8, order: 1, level: 5, full_path: '1.2.6.8.11', code: '202-2-c', is_leaf: true }, { ledger_id: 12, ledger_pid: 8, order: 2, level: 5, full_path: '1.2.6.8.12', code: '202-2-e', is_leaf: true }, { ledger_id: 13, ledger_pid: 2, order: 2, level: 3, full_path: '1.2.13', code: '1-1-2', is_leaf: true }, { ledger_id: 14, ledger_pid: 2, order: 3, level: 3, full_path: '1.2.14', code: '1-1-3', is_leaf: true }, { ledger_id: 3, ledger_pid: 1, order: 2, level: 2, full_path: '1.3', code: '1-2', is_leaf: false }, { ledger_id: 15, ledger_pid: 3, order: 1, level: 3, full_path: '1.3.15', code: '1-2-1', is_leaf: true }, { ledger_id: 4, ledger_pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3', is_leaf: false }, { ledger_id: 16, ledger_pid: 4, order: 1, level: 3, full_path: '1.4.16', code: '1-3-1', is_leaf: true }, { ledger_id: 5, ledger_pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4', is_leaf: true }, ]; const testTenderId = 3; const { app, assert } = require('egg-mock/bootstrap'); const findById = function (nodes, Id) { const filters = nodes.filter(function (x) { return x.ledger_id === Id; }); return filters.length > 0 ? filters[0] : undefined; } describe('test/app/service/ledger.test.js', () => { it('clear history test data', function* () { const ctx = app.mockContext(); const result = yield ctx.service.ledger.db.delete(ctx.service.ledger.tableName, { tender_id: testTenderId }); assert(result.affectedRows >= 0); }); it('add test data', function* () { const ctx = app.mockContext(); for (const data of testNodeData) { data.tender_id = testTenderId; } const result = yield ctx.service.ledger.db.insert(ctx.service.ledger.tableName, testNodeData); assert(result.affectedRows === testNodeData.length); }); it('test addNode', function* () { const ctx = app.mockContext(); // 选中1-1-1,插入节点 const resultData = yield ctx.service.ledger.addNode(testTenderId, 6); assert(resultData.create.length === 1); assert(resultData.update.length === 2); assert(resultData.create[0].is_leaf === 1); }); it('test deleteNode', function* () { const ctx = app.mockContext(); // 选中202-1,删除节点 const resultData = yield ctx.service.ledger.deleteNode(testTenderId, 7); assert(resultData.delete.length === 3); assert(resultData.update.length === 1); }); it('test upMoveNode', function* () { const ctx = app.mockContext(); // 选中202-2-e上移 let resultData = yield ctx.service.ledger.upMoveNode(testTenderId, 12); resultData.update.sort(function (x, y) { return x.order - y.order; }); assert(resultData.update.length === 2); assert(resultData.update[0].code === '202-2-e'); }); it('test downMoveNode', function* () { const ctx = app.mockContext(); // 选中202-2-e下移 let resultData = yield ctx.service.ledger.downMoveNode(testTenderId, 12); resultData.update.sort(function (x, y) { return x.order - y.order; }); assert(resultData.update.length === 2); assert(resultData.update[0].code === '202-2-c'); }); it('test upLevelNode', function* () { const ctx = app.mockContext(); // 选中 1-1-2 升级 let resultData = yield ctx.service.ledger.upLevelNode(testTenderId, 13); assert(resultData); assert(resultData.update.length === 5); let node = findById(resultData.update, 13); assert(node.full_path === '1.13'); assert(node.ledger_pid === 1); assert(!node.is_leaf); node = findById(resultData.update, 14); assert(node.ledger_pid === 13); assert(node.full_path === '1.13.14'); node = findById(resultData.update, 3); assert(node.order === 3); node = findById(resultData.update, 4); assert(node.order === 4); node = findById(resultData.update, 5); assert(node.order === 5); }); it('test downLevelNode', function* () { const ctx = app.mockContext(); // 选中1-3 降级 let resultData = yield ctx.service.ledger.downLevelNode(testTenderId, 4); assert(resultData.update.length === 3); let node = findById(resultData.update, 4); assert(node.full_path === '1.3.4'); assert(node.ledger_pid === 3); assert(node.level === 3); assert(node.order === 2); node = findById(resultData.update, 16); assert(node.level === 4); assert(node.full_path === '1.3.4.16'); node = findById(resultData.update, 5); assert(node.order === 4); }) });