| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 | 'use strict';/** * 辅助方法扩展单元测试 * * @author CaiAoLin * @date 2017/10/24 * @version */const { app, assert } = require('egg-mock/bootstrap');const _ = require('lodash');const math = require('mathjs');const decimal = require('decimal.js');describe('test/app/extend/helper.test.js', () => {    it('generateRandomString test', function () {        // 创建 ctx        const ctx = app.mockContext();        const randomMix = ctx.helper.generateRandomString(4);        const mixReg = /[A-Za-z0-9]{4}/;        assert(mixReg.test(randomMix));        // 测试纯数字        const randomNumber = ctx.helper.generateRandomString(4, 2);        const numberReg = /\d{4}/;        assert(numberReg.test(randomNumber));        // 测试纯字母        const randomString = ctx.helper.generateRandomString(4, 3);        const stringReg = /[A-Za-z]{4}/;        assert(stringReg.test(randomString));    });    it('showSortFlag test', function () {        // 创建 ctx        const ctx = app.mockContext({            // sort: { test: 1 },            sort: ['id', 'asc'],        });        const flag = ctx.helper.showSortFlag('id');        assert(flag === '-');    });    it('isAjax test', function () {        // 创建 ctx        const ctx = app.mockContext();        const result = ctx.helper.isAjax(ctx);        assert(!result);    });    it('sendRequest test', function* () {        // 创建 ctx        const ctx = app.mockContext();        const url = 'http://ip.taobao.com/service/getIpInfo.php';        const responseData = yield ctx.helper.sendRequest(url, { ip: '116.7.222.12' }, 'GET');        assert(responseData.data.ip === '116.7.222.12');    });    it('explode paths', function () {        // 创建 ctx        const ctx = app.mockContext();        const paths = ['1.2.3.4.5', '1.2.3.5.7'];        const normal = ctx.helper.explodePath(paths, '.');        const normalExpect = ['1', '1.2', '1.2.3', '1.2.3.4', '1.2.3.4.5', '1.2.3.5', '1.2.3.5.7'];        let result = true;        try {            for (const index in normal) {                if (normal[index] !== normalExpect[index]) {                    throw '数据错误';                }            }        } catch (error) {            result = false;        }        assert(result);    });    it('test permission', function () {        // 创建 ctx        const ctx = app.mockContext();        ctx.session = {            sessionUser: {                permission: '1,2,3,4',            },        };        const permission = require('../../../config/permission');        let result = ctx.helper.hasPermission(permission.permission.VIEW_STAGE_MEASURE);        assert(!result);        // 多个权限判断        result = ctx.helper.hasPermission([permission.permission.CREATE_TENDER, permission.permission.VIEW_ALL_TENDER]);        assert(result);    });    it('test compareCode', function () {        const ctx = app.mockContext();        // 测试项目节        assert(ctx.helper.compareCode('1-1-1', '1-1-2') < 0);        assert(ctx.helper.compareCode('1-2-2', '1-3-1') < 0);        assert(ctx.helper.compareCode('1-2-2', '1-2-2-1') < 0);        // 测试广东清单编号        assert(ctx.helper.compareCode('404-1-1', '404-1-2') < 0);        assert(ctx.helper.compareCode('404-1-2', '404-2-1') < 0);        assert(ctx.helper.compareCode('404-1', '404-2-a') < 0);        assert(ctx.helper.compareCode('404', '404-1') < 0);        assert(ctx.helper.compareCode('404-1-b', '清单编号') < 0);        // 测试全国清单编号        assert(ctx.helper.compareCode('404-1-a', '404-1-b') < 0);        assert(ctx.helper.compareCode('404-1-1', '404-1-a') < 0);        assert(ctx.helper.compareCode('404-1-b', '404-2-a') < 0);        assert(ctx.helper.compareCode('404-1-b', '404-a-a') < 0);        // 测试日期        assert(ctx.helper.compareCode('2018-03-04', '2018-03-05') < 0);        assert(ctx.helper.compareCode('2018-02-09', '2018-03-06') < 0);        assert(ctx.helper.compareCode('2017-05-15', '2018-03-05') < 0);        // 测试时间        assert(ctx.helper.compareCode('03:05:20', '03:05:24') < 0);        assert(ctx.helper.compareCode('04:04:33', '04:05:33') < 0);        assert(ctx.helper.compareCode('07:20:50', '08:20:50') < 0);        // 测试层次编号        assert(ctx.helper.compareCode('1.1.1', '1.1.2', '.') < 0);        assert(ctx.helper.compareCode('1.1.3', '1.2.2', '.') < 0);        assert(ctx.helper.compareCode('2.4.3', '3.1.2', '.') < 0);        // 测试空编号        assert(ctx.helper.compareCode('', '1-1') < 0);        assert(ctx.helper.compareCode('1-1', '') > 0);    });    it('test arithmetic', function () {        const ctx = app.mockContext();        assert(ctx.helper.add(0.1, 0.2) === 0.3);        assert(ctx.helper.add(43223423.23423, -423423) === 42800000.23423);        assert(ctx.helper.sub(0.3, 0.2) === 0.1);        assert(ctx.helper.mul(0.07, 10) === 0.7);        assert(ctx.helper.div(0.32, 0.2) === 1.6);    });    it('test getInArrStrSqlFilter', function () {        const ctx = app.mockContext();        const id = ['abc', 'efj'];        const str = 'And id in (' + ctx.helper.getInArrStrSqlFilter(id) + ')';        assert(str === "And id in ('abc','efj')");    });    it('test getChapterCode', function () {        const ctx = app.mockContext();        const testData = ['A101-1-a', 'A201B102-1', '新增203-1', '新增1001-1'];        const targetData = ['100', '10000', '200', '1000'];        for (const i in testData) {            assert(ctx.helper.getChapterCode(testData[i]) === targetData[i]);        }    });});
 |