helper.test.js 5.6 KB

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