
/**
 * 标段--台账 模型 单元测试
 *
 * @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' },
    { 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: 15, ledger_pid: 3, order: 1, level: 3, full_path: '1.3.15', code: '1-2-1'},
    { ledger_id: 4, ledger_pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3' },
    { ledger_id: 16, ledger_pid: 4, order: 1, level: 3, full_path: '1.4.16', code: '1-3-1'},
    { ledger_id: 5, ledger_pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4' },
];
const testTenderId = 3;

const { app, assert } = require('egg-mock/bootstrap');

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.length === 3);
    });

    it('test deleteNode', function* () {
        const ctx = app.mockContext();
        // 选中202-1，删除节点
        const resultData = yield ctx.service.ledger.deleteNode(testTenderId, 7);
        assert(resultData.length === 3);
    });

    it('test upMoveNode', function* () {
        const ctx = app.mockContext();
        // 选中202-2-e上移
        let resultData = yield ctx.service.ledger.upMoveNode(testTenderId, 12);
        resultData.sort(function (x, y) {
            return x.order - y.order;
        });
        assert(resultData.length === 2);
        assert(resultData[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.sort(function (x, y) {
            return x.order - y.order;
        });
        assert(resultData.length === 2);
        assert(resultData[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.length === 5);
        assert(resultData[0].full_path === '1.13');
        assert(resultData[0].ledger_pid === 1);
        assert(resultData[1].ledger_pid === 13);
        assert(resultData[1].full_path === '1.13.14');
        assert(resultData[2].order === 3);
        assert(resultData[3].order === 4);
        assert(resultData[4].order === 5);
    });

    it('test downLevelNode', function* () {
        const ctx = app.mockContext();
        // 选中1-3 降级
        let resultData = yield ctx.service.ledger.downLevelNode(testTenderId, 4);
        assert(resultData.length === 3);
        assert(resultData[0].full_path === '1.3.4');
        assert(resultData[0].level === 3);
        assert(resultData[0].order === 2);
        assert(resultData[1].level === 4);
        assert(resultData[1].full_path === '1.3.4.16');
        assert(resultData[2].order === 4);
    })
});
