helper.test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 'use strict';
  2. /**
  3. * 辅助方法扩展单元测试
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/24
  7. * @version
  8. */
  9. const { app, assert } = require('egg-mock/bootstrap');
  10. const _ = require('lodash');
  11. const math = require('mathjs');
  12. const np = require('number-precision');
  13. const decimal = require('decimal.js');
  14. describe('test/app/extend/helper.test.js', () => {
  15. it('generateRandomString test', function () {
  16. // 创建 ctx
  17. const ctx = app.mockContext();
  18. const randomMix = ctx.helper.generateRandomString(4);
  19. const mixReg = /[A-Za-z0-9]{4}/;
  20. assert(mixReg.test(randomMix));
  21. // 测试纯数字
  22. const randomNumber = ctx.helper.generateRandomString(4, 2);
  23. const numberReg = /\d{4}/;
  24. assert(numberReg.test(randomNumber));
  25. // 测试纯字母
  26. const randomString = ctx.helper.generateRandomString(4, 3);
  27. const stringReg = /[A-Za-z]{4}/;
  28. assert(stringReg.test(randomString));
  29. });
  30. it('showSortFlag test', function () {
  31. // 创建 ctx
  32. const ctx = app.mockContext({
  33. // sort: { test: 1 },
  34. sort: ['id', 'asc'],
  35. });
  36. const flag = ctx.helper.showSortFlag('id');
  37. assert(flag === '-');
  38. });
  39. it('isAjax test', function () {
  40. // 创建 ctx
  41. const ctx = app.mockContext();
  42. const result = ctx.helper.isAjax(ctx);
  43. assert(!result);
  44. });
  45. it('sendRequest test', function* () {
  46. // 创建 ctx
  47. const ctx = app.mockContext();
  48. const url = 'http://ip.taobao.com/service/getIpInfo.php';
  49. const responseData = yield ctx.helper.sendRequest(url, { ip: '116.7.222.12' }, 'GET');
  50. assert(responseData.data.ip === '116.7.222.12');
  51. });
  52. it('explode paths', function () {
  53. // 创建 ctx
  54. const ctx = app.mockContext();
  55. const paths = ['1.2.3.4.5', '1.2.3.5.7'];
  56. const normal = ctx.helper.explodePath(paths, '.');
  57. 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'];
  58. let result = true;
  59. try {
  60. for (const index in normal) {
  61. if (normal[index] !== normalExpect[index]) {
  62. throw '数据错误';
  63. }
  64. }
  65. } catch (error) {
  66. result = false;
  67. }
  68. assert(result);
  69. });
  70. it('test permission', function () {
  71. // 创建 ctx
  72. const ctx = app.mockContext();
  73. ctx.session = {
  74. sessionUser: {
  75. permission: '1,2,3,4',
  76. },
  77. };
  78. const permission = require('../../../config/permission');
  79. let result = ctx.helper.hasPermission(permission.permission.VIEW_STAGE_MEASURE);
  80. assert(!result);
  81. // 多个权限判断
  82. result = ctx.helper.hasPermission([permission.permission.CREATE_TENDER, permission.permission.VIEW_ALL_TENDER]);
  83. assert(result);
  84. });
  85. it('test compareCode', function () {
  86. const ctx = app.mockContext();
  87. // 测试项目节
  88. assert(ctx.helper.compareCode('1-1-1', '1-1-2') < 0);
  89. assert(ctx.helper.compareCode('1-2-2', '1-3-1') < 0);
  90. assert(ctx.helper.compareCode('1-2-2', '1-2-2-1') < 0);
  91. // 测试广东清单编号
  92. assert(ctx.helper.compareCode('404-1-1', '404-1-2') < 0);
  93. assert(ctx.helper.compareCode('404-1-2', '404-2-1') < 0);
  94. assert(ctx.helper.compareCode('404-1', '404-2-a') < 0);
  95. assert(ctx.helper.compareCode('404', '404-1') < 0);
  96. assert(ctx.helper.compareCode('404-1-b', '清单编号') < 0);
  97. // 测试全国清单编号
  98. assert(ctx.helper.compareCode('404-1-a', '404-1-b') < 0);
  99. assert(ctx.helper.compareCode('404-1-1', '404-1-a') < 0);
  100. assert(ctx.helper.compareCode('404-1-b', '404-2-a') < 0);
  101. assert(ctx.helper.compareCode('404-1-b', '404-a-a') < 0);
  102. // 测试日期
  103. assert(ctx.helper.compareCode('2018-03-04', '2018-03-05') < 0);
  104. assert(ctx.helper.compareCode('2018-02-09', '2018-03-06') < 0);
  105. assert(ctx.helper.compareCode('2017-05-15', '2018-03-05') < 0);
  106. // 测试时间
  107. assert(ctx.helper.compareCode('03:05:20', '03:05:24') < 0);
  108. assert(ctx.helper.compareCode('04:04:33', '04:05:33') < 0);
  109. assert(ctx.helper.compareCode('07:20:50', '08:20:50') < 0);
  110. // 测试层次编号
  111. assert(ctx.helper.compareCode('1.1.1', '1.1.2', '.') < 0);
  112. assert(ctx.helper.compareCode('1.1.3', '1.2.2', '.') < 0);
  113. assert(ctx.helper.compareCode('2.4.3', '3.1.2', '.') < 0);
  114. // 测试空编号
  115. assert(ctx.helper.compareCode('', '1-1') < 0);
  116. assert(ctx.helper.compareCode('1-1', '') > 0);
  117. });
  118. it('test arithmetic', function () {
  119. const ctx = app.mockContext();
  120. assert(ctx.helper.add(0.1, 0.2) === 0.3);
  121. assert(ctx.helper.add(43223423.23423, -423423) === 42800000.23423);
  122. assert(ctx.helper.sub(0.3, 0.2) === 0.1);
  123. assert(ctx.helper.mul(0.07, 10) === 0.7);
  124. assert(ctx.helper.div(0.32, 0.2) === 1.6);
  125. });
  126. it('test getInArrStrSqlFilter', function () {
  127. const ctx = app.mockContext();
  128. const id = ['abc', 'efj'];
  129. const str = 'And id in (' + ctx.helper.getInArrStrSqlFilter(id) + ')';
  130. assert(str === "And id in ('abc','efj')");
  131. });
  132. it('test getChapterCode', function () {
  133. const ctx = app.mockContext();
  134. const testData = ['A101-1-a', 'A201B102-1', '新增203-1', '新增1001-1'];
  135. const targetData = ['100', '10000', '200', '1000'];
  136. for (const i in testData) {
  137. assert(ctx.helper.getChapterCode(testData[i]) === targetData[i]);
  138. }
  139. });
  140. });