template_node.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. /**
  3. * 指标节点业务逻辑 单元测试
  4. *
  5. * @author Mai
  6. * @data 2018/4/20
  7. * @version
  8. */
  9. const { app, assert } = require('egg-mock/bootstrap');
  10. // excel解析
  11. const excel = require('node-xlsx');
  12. describe('test/app/service/template_node.test.js', () => {
  13. it('_parseSheetData test xls File', function () {
  14. const ctx = app.mockContext();
  15. const fileName = app.baseDir + '/test/app/service/test_template.xls';
  16. const sheets = excel.parse(fileName);
  17. const nodes = [], indexes = [], params = [];
  18. for (const sheet of sheets) {
  19. ctx.service.templateNode._parseSheetData(sheet, nodes, indexes, params);
  20. }
  21. assert(nodes.length === 5);
  22. assert(indexes.length === 42);
  23. assert(params.length === 65);
  24. });
  25. it('_parseSheetData test xlsx File', function () {
  26. const ctx = app.mockContext();
  27. const fileName = app.baseDir + '/test/app/service/test_template.xlsx';
  28. const sheets = excel.parse(fileName);
  29. const nodes = [], indexes = [], params = [];
  30. for (const sheet of sheets) {
  31. ctx.service.templateNode._parseSheetData(sheet, nodes, indexes, params);
  32. }
  33. assert(nodes.length === 3);
  34. assert(indexes.length === 12);
  35. assert(params.length === 27);
  36. });
  37. it('importData test xls File', function* () {
  38. const ctx = app.mockContext();
  39. const fileName = app.baseDir + '/test/app/service/test_template.xls';
  40. const sheets = excel.parse(fileName), templateId = 0;
  41. yield ctx.service.templateNode.importData(sheets, templateId);
  42. const nodes = yield ctx.service.templateNode.getAllDataByCondition({where: {template_id: templateId}});
  43. assert(nodes.length === 5);
  44. const indexes = yield ctx.service.templateIndex.getAllDataByCondition({where: {template_id: templateId}});
  45. assert(indexes.length === 42);
  46. const params = yield ctx.service.templateParam.getAllDataByCondition({where: {template_id: templateId}});
  47. assert(params.length === 102);
  48. });
  49. it('importData test xlsx File', function* () {
  50. const ctx = app.mockContext();
  51. const fileName = app.baseDir + '/test/app/service/test_template.xlsx';
  52. const sheets = excel.parse(fileName), templateId = 0;
  53. yield ctx.service.templateNode.importData(sheets, templateId);
  54. const nodes = yield ctx.service.templateNode.getAllDataByCondition({where: {template_id: templateId}});
  55. assert(nodes.length === 3);
  56. const indexes = yield ctx.service.templateIndex.getAllDataByCondition({where: {template_id: templateId}});
  57. assert(indexes.length === 12);
  58. const params = yield ctx.service.templateParam.getAllDataByCondition({where: {template_id: templateId}});
  59. assert(params.length === 64);
  60. });
  61. });