match.test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/5/6
  7. * @version
  8. */
  9. const { app, assert } = require('egg-mock/bootstrap');
  10. // excel解析
  11. const excel = require('node-xlsx');
  12. const fs = require('fs');
  13. describe('test/app/service/match.test.js', () => {
  14. it('match bills', function* () {
  15. const ctx = app.mockContext();
  16. // 初始化测试用指标模板
  17. const fileName = app.baseDir + '/test/app/service/test_template.xls';
  18. const sheets = excel.parse(fileName), templateId = 0;
  19. yield ctx.service.templateNode.importData(sheets, templateId);
  20. // 读取清单,并匹配指标
  21. const fileStream = fs.readFileSync(app.baseDir + '/test/app/service/FS08.json', 'utf8');
  22. const proj = JSON.parse(fileStream);
  23. const bills = proj.bills;
  24. yield ctx.service.match.matchBills(bills, templateId);
  25. const assertBillsMatch = function (billsCode, nodeName, firstIndexValue) {
  26. const bill = ctx.helper.findObj(bills, 'code', billsCode);
  27. assert(bills && bill.match_node > 0);
  28. const node = ctx.helper.findObj(ctx.service.match.nodes, 'bills_id', bill.n_id);
  29. assert(node.name === nodeName);
  30. const indexes = ctx.service.match.indexes.filter(function (i) {
  31. return i.node_id === node.node_id;
  32. })
  33. const firstIndex = ctx.helper.findObj(indexes, 'code', node.code + '-1');
  34. if (firstIndexValue) {
  35. assert(firstIndex.value.toFixed(4) == firstIndexValue);
  36. } else {
  37. assert(firstIndex.value === firstIndexValue);
  38. }
  39. };
  40. assertBillsMatch('1-1', '临时工程', null);
  41. assertBillsMatch('1-1-1', '临时道路', 973218.3702);
  42. assertBillsMatch('1-1-2', '临时道路', 1000075.7324);
  43. assertBillsMatch('1-1-4', '临时道路', 213610.5923);
  44. assertBillsMatch('1-1-5', '临时道路', 1650.8891);
  45. assertBillsMatch('1-1-6', '临时道路', 176719.3297);
  46. assertBillsMatch('1-1-7', '临时道路', 2614445.5674);
  47. assertBillsMatch('1-1-8', '临时道路', null);
  48. assertBillsMatch('1-2', '路基工程', null);
  49. assert(ctx.service.match.nodes.length === 9);
  50. });
  51. });