'use strict';

/**
 * 指标节点业务逻辑 单元测试
 *
 * @author Mai
 * @data 2018/4/20
 * @version
 */

const { app, assert } = require('egg-mock/bootstrap');
// excel解析
const excel = require('node-xlsx');

describe('test/app/service/template_node.test.js', () => {
    it('_parseSheetData test xls File', function () {
        const ctx = app.mockContext();
        const fileName = app.baseDir + '/test/app/service/test_template.xls';
        const sheets = excel.parse(fileName);
        const nodes = [], indexes = [], params = [];

        for (const sheet of sheets) {
            ctx.service.templateNode._parseSheetData(sheet, nodes, indexes, params);
        }

        assert(nodes.length === 5);
        assert(indexes.length === 42);
        assert(params.length === 64);
    });
    it('_parseSheetData test xlsx File', function () {
        const ctx = app.mockContext();
        const fileName = app.baseDir + '/test/app/service/test_template.xlsx';
        const sheets = excel.parse(fileName);
        const nodes = [], indexes = [], params = [];

        for (const sheet of sheets) {
            ctx.service.templateNode._parseSheetData(sheet, nodes, indexes, params);
        }

        assert(nodes.length === 3);
        assert(indexes.length === 12);
        assert(params.length === 26);
    });

    it('importData test xls File', function* () {
        const ctx = app.mockContext();
        const fileName = app.baseDir + '/test/app/service/test_template.xls';
        const sheets = excel.parse(fileName), templateId = 0;

        yield ctx.service.templateNode.importData(sheets, templateId);

        const nodes = yield ctx.service.templateNode.getAllDataByCondition({where: {template_id: templateId}});
        assert(nodes.length === 5);
        const indexes = yield ctx.service.templateIndex.getAllDataByCondition({where: {template_id: templateId}});
        assert(indexes.length === 42);
        const params = yield ctx.service.templateParam.getAllDataByCondition({where: {template_id: templateId}});
        assert(params.length === 103);
    });
    it('importData test xlsx File', function* () {
        const ctx = app.mockContext();
        const fileName = app.baseDir + '/test/app/service/test_template.xlsx';
        const sheets = excel.parse(fileName), templateId = 0;

        yield ctx.service.templateNode.importData(sheets, templateId);

        const nodes = yield ctx.service.templateNode.getAllDataByCondition({where: {template_id: templateId}});
        assert(nodes.length === 3);
        const indexes = yield ctx.service.templateIndex.getAllDataByCondition({where: {template_id: templateId}});
        assert(indexes.length === 12);
        const params = yield ctx.service.templateParam.getAllDataByCondition({where: {template_id: templateId}});
        assert(params.length === 65);
    });
});