deal_bills.test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const { app, assert } = require('egg-mock/bootstrap');
  10. const excel = require('node-xlsx');
  11. const xlsx = require('js-xlsx');
  12. const addData = {
  13. name: 'test_deal_bills',
  14. category: null,
  15. };
  16. const mockData = {};
  17. const _ = require('lodash');
  18. describe('test/app/service/deal_bills.test.js', () => {
  19. // 准备测试数据,新增测试标段
  20. before(function* () {
  21. const ctx = app.mockContext();
  22. // 模拟登录session
  23. const postData = {
  24. account: '734406061@qq.com',
  25. project: 'T201711273363',
  26. project_password: 'mai654321',
  27. };
  28. ctx.session = {};
  29. const loginResult = yield ctx.service.projectAccount.accountLogin(postData, 2);
  30. assert(loginResult);
  31. mockData.session = ctx.session;
  32. // 移除旧测试数据
  33. const testTender = yield ctx.service.tender.getDataByCondition({
  34. name: addData.name,
  35. project_id: ctx.session.sessionProject.id,
  36. });
  37. if (testTender) {
  38. const result = yield ctx.service.tender.deleteTenderNoBackup(testTender.id);
  39. assert(result);
  40. }
  41. // 新增测试用标段
  42. const result = yield ctx.service.tender.add(addData);
  43. assert(result);
  44. const tender = yield ctx.service.tender.getDataByCondition({
  45. name: addData.name,
  46. project_id: ctx.session.sessionProject.id,
  47. });
  48. mockData.tender = {id: tender.id, data: tender};
  49. ctx.tender = mockData.tender;
  50. });
  51. // 导入 Excel数据(数据源:node-xlsx导入)
  52. it('test import Excel data by node-xlsx', function* () {
  53. const ctx = app.mockContext(mockData);
  54. const file = app.baseDir + '/test/app/test_file/deal-upload-test.xls';
  55. const sheets = excel.parse(file);
  56. const result = yield ctx.service.dealBills.importData(sheets[0], ctx.tender.id);
  57. assert(result);
  58. const bills = yield ctx.service.dealBills.getAllDataByCondition({where: {tender_id: ctx.tender.id}});
  59. assert(bills.length === 1);
  60. assert(bills[0].code === '101-1');
  61. assert(bills[0].tender_id === ctx.tender.id);
  62. });
  63. // 导入 Excel数据(数据源:js-xlsx导入)
  64. it('test import Excel data by js-xlsx', function* () {
  65. const ctx = app.mockContext(mockData);
  66. const file = app.baseDir + '/test/app/test_file/deal-upload-test.xls';
  67. const wb = xlsx.readFile(file);
  68. const name = wb.SheetNames[0];
  69. const sheetData = {
  70. rows: xlsx.utils.sheet_to_json(wb.Sheets[name], {header: 1}),
  71. merge: wb.Sheets[name]["!merges"],
  72. };
  73. const result = yield ctx.service.dealBills.importDataJsXlsx(sheetData, ctx.tender.id);
  74. assert(result);
  75. const bills = yield ctx.service.dealBills.getAllDataByCondition({where: {tender_id: ctx.tender.id}});
  76. assert(bills.length === 1);
  77. assert(bills[0].code === '101-1');
  78. assert(bills[0].tender_id === ctx.tender.id);
  79. });
  80. // 导入 数据类型有误的excel(数据源:js-xlsx导入)
  81. it('test import Excel data with Type-Error by js-xlsx', function* () {
  82. const ctx = app.mockContext(mockData);
  83. const file = app.baseDir + '/test/app/test_file/deal-bills-typeError.xlsx';
  84. const wb = xlsx.readFile(file);
  85. const name = wb.SheetNames[0];
  86. const sheetData = {
  87. rows: xlsx.utils.sheet_to_json(wb.Sheets[name], {header: 1}),
  88. merge: wb.Sheets[name]["!merges"],
  89. };
  90. try {
  91. const result = yield ctx.service.dealBills.importDataJsXlsx(sheetData, ctx.tender.id);
  92. assert(false);
  93. } catch (err) {
  94. assert(err);
  95. assert(err.indexOf('数据类型有误') > 0);
  96. }
  97. });
  98. // 导入 表头定义有误的excel(数据源:js-xlsx导入)
  99. it('test import Excel data with Col-Define-Error by js-xlsx', function* () {
  100. const ctx = app.mockContext(mockData);
  101. const file = app.baseDir + '/test/app/test_file/deal-bills-colDefError.xlsx';
  102. const wb = xlsx.readFile(file);
  103. const name = wb.SheetNames[0];
  104. const sheetData = {
  105. rows: xlsx.utils.sheet_to_json(wb.Sheets[name], {header: 1}),
  106. merge: wb.Sheets[name]["!merges"],
  107. };
  108. try {
  109. const result = yield ctx.service.dealBills.importDataJsXlsx(sheetData, ctx.tender.id);
  110. assert(false);
  111. } catch (err) {
  112. assert(err);
  113. assert(err.indexOf('表头定义有误') > 0);
  114. }
  115. });
  116. // 导入 表头定义有误的excel(数据源:js-xlsx导入)
  117. it('test import Excel data with Col-Define-Error by js-xlsx', function* () {
  118. const ctx = app.mockContext(mockData);
  119. const file = app.baseDir + '/test/app/test_file/deal-bills-filterInvalidRows.xlsx';
  120. const wb = xlsx.readFile(file);
  121. const name = wb.SheetNames[0];
  122. const sheetData = {
  123. rows: xlsx.utils.sheet_to_json(wb.Sheets[name], {header: 1}),
  124. merge: wb.Sheets[name]["!merges"],
  125. };
  126. const result = yield ctx.service.dealBills.importDataJsXlsx(sheetData, ctx.tender.id);
  127. assert(result);
  128. const bills = yield ctx.service.dealBills.getAllDataByCondition({where: {tender_id: ctx.tender.id}});
  129. assert(bills.length === 98);
  130. });
  131. // 导入 存在数量单价为空的数据
  132. it('test import Excel data by js-xlsx', function* () {
  133. const ctx = app.mockContext(mockData);
  134. const file = app.baseDir + '/test/app/test_file/deal-bills-emptyCalcField.xls';
  135. const wb = xlsx.readFile(file);
  136. const name = wb.SheetNames[0];
  137. const sheetData = {
  138. rows: xlsx.utils.sheet_to_json(wb.Sheets[name], {header: 1}),
  139. merge: wb.Sheets[name]["!merges"],
  140. };
  141. const result = yield ctx.service.dealBills.importDataJsXlsx(sheetData, ctx.tender.id);
  142. assert(result);
  143. const bills = yield ctx.service.dealBills.getAllDataByCondition({where: {tender_id: ctx.tender.id}});
  144. assert(bills.length === 4);
  145. assert(bills[0].code === '101-1');
  146. assert(bills[0].unit_price === 20000);
  147. assert(bills[0].quantity === 1);
  148. assert(bills[0].total_price === 1000);
  149. assert(bills[1].code === '101-2');
  150. assert(bills[1].unit_price === 1200);
  151. assert(!bills[1].quantity || bills[0].quantity === 1);
  152. assert(bills[1].total_price === 200);
  153. assert(bills[2].code === '101-3');
  154. assert(!bills[2].unit_price || bills[0].unit_price === 0);
  155. assert(bills[2].quantity === 2);
  156. assert(bills[2].total_price === 4000);
  157. assert(bills[3].code === '101-4');
  158. assert(bills[3].unit_price === 2000);
  159. assert(bills[3].quantity === 3);
  160. assert(!bills[3].total_price || bills[0].total_price === 0);
  161. });
  162. });