ledger.test.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /**
  2. * 标段--台账 模型 单元测试
  3. *
  4. * @author Mai
  5. * @date 2017/2/1
  6. * @version
  7. */
  8. 'use strict';
  9. const excel = require('node-xlsx');
  10. /*const testNodeData = [
  11. { ledger_id: 1, ledger_pid: -1, order: 1, level: 1, full_path: '1', code: '1', is_leaf: false },
  12. { ledger_id: 2, ledger_pid: 1, order: 1, level: 2, full_path: '1.2', code: '1-1', is_leaf: false },
  13. { ledger_id: 6, ledger_pid: 2, order: 1, level: 3, full_path: '1.2.6', code: '1-1-1', is_leaf: false },
  14. { ledger_id: 7, ledger_pid: 6, order: 1, level: 4, full_path: '1.2.6.7', code: '202-1', is_leaf: false },
  15. { ledger_id: 10, ledger_pid: 7, order: 2, level: 5, full_path: '1.2.6.7.10', code: '202-1-a', is_leaf: true },
  16. { ledger_id: 9, ledger_pid: 7, order: 1, level: 5, full_path: '1.2.6.7.9', code: '202-1-b', is_leaf: true },
  17. { ledger_id: 8, ledger_pid: 6, order: 2, level: 4, full_path: '1.2.6.8', code: '202-2', is_leaf: false },
  18. { ledger_id: 11, ledger_pid: 8, order: 1, level: 5, full_path: '1.2.6.8.11', code: '202-2-c', is_leaf: true },
  19. { ledger_id: 12, ledger_pid: 8, order: 2, level: 5, full_path: '1.2.6.8.12', code: '202-2-e', is_leaf: true },
  20. { ledger_id: 13, ledger_pid: 2, order: 2, level: 3, full_path: '1.2.13', code: '1-1-2', is_leaf: true },
  21. { ledger_id: 14, ledger_pid: 2, order: 3, level: 3, full_path: '1.2.14', code: '1-1-3', is_leaf: true },
  22. { ledger_id: 3, ledger_pid: 1, order: 2, level: 2, full_path: '1.3', code: '1-2', is_leaf: false },
  23. { ledger_id: 15, ledger_pid: 3, order: 1, level: 3, full_path: '1.3.15', code: '1-2-1', is_leaf: true },
  24. { ledger_id: 4, ledger_pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3', is_leaf: false },
  25. { ledger_id: 16, ledger_pid: 4, order: 1, level: 3, full_path: '1.4.16', code: '1-3-1', is_leaf: true },
  26. { ledger_id: 5, ledger_pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4', is_leaf: true },
  27. ];*/
  28. const testNodeData = [
  29. { id: 1, pid: -1, order: 1, level: 1, full_path: '1', code: '1', is_leaf: false },
  30. { id: 2, pid: 1, order: 1, level: 2, full_path: '1.2', code: '1-1', is_leaf: false },
  31. { id: 6, pid: 2, order: 1, level: 3, full_path: '1.2.6', code: '1-1-1', is_leaf: false },
  32. { id: 7, pid: 6, order: 1, level: 4, full_path: '1.2.6.7', code: '202-1', is_leaf: false },
  33. { id: 10, pid: 7, order: 2, level: 5, full_path: '1.2.6.7.10', code: '202-1-a', is_leaf: true },
  34. { id: 9, pid: 7, order: 1, level: 5, full_path: '1.2.6.7.9', code: '202-1-b', is_leaf: true },
  35. { id: 8, pid: 6, order: 2, level: 4, full_path: '1.2.6.8', code: '202-2', is_leaf: false },
  36. { id: 11, pid: 8, order: 1, level: 5, full_path: '1.2.6.8.11', code: '202-2-c', is_leaf: true },
  37. { id: 12, pid: 8, order: 2, level: 5, full_path: '1.2.6.8.12', code: '202-2-e', is_leaf: true },
  38. { id: 13, pid: 2, order: 2, level: 3, full_path: '1.2.13', code: '1-1-2', is_leaf: true },
  39. { id: 14, pid: 2, order: 3, level: 3, full_path: '1.2.14', code: '1-1-3', is_leaf: true },
  40. { id: 3, pid: 1, order: 2, level: 2, full_path: '1.3', code: '1-2', is_leaf: false },
  41. { id: 15, pid: 3, order: 1, level: 3, full_path: '1.3.15', code: '1-2-1', is_leaf: true },
  42. { id: 4, pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3', is_leaf: false },
  43. { id: 16, pid: 4, order: 1, level: 3, full_path: '1.4.16', code: '1-3-1', is_leaf: true },
  44. { id: 5, pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4', is_leaf: true },
  45. ];
  46. const testTenderId = 3;
  47. const testUserId = 2;
  48. const { app, assert } = require('egg-mock/bootstrap');
  49. const findById = function(nodes, Id) {
  50. const filters = nodes.filter(function(x) {
  51. return x.ledger_id === Id;
  52. });
  53. return filters.length > 0 ? filters[0] : undefined;
  54. };
  55. describe('test/app/service/ledger.test.js', () => {
  56. // 准备测试数据
  57. it('clear history test data', function* () {
  58. const ctx = app.mockContext();
  59. const result = yield ctx.service.ledger.db.delete(ctx.service.ledger.tableName, { tender_id: testTenderId });
  60. assert(result.affectedRows >= 0);
  61. });
  62. it('add test data(test add)', function* () {
  63. const ctx = app.mockContext();
  64. for (const data of testNodeData) {
  65. data.tender_id = testTenderId;
  66. }
  67. //const result = yield ctx.service.ledger.db.insert(ctx.service.ledger.tableName, testNodeData);
  68. //assert(result.affectedRows === testNodeData.length);
  69. const result = yield ctx.service.ledger.add(testNodeData, testTenderId);
  70. assert(result);
  71. ctx.service.ledger.cache.set('tender_node_maxId:' + testTenderId, 16, 'EX', ctx.app.config.cacheTime);
  72. });
  73. /* 期望运行结果:
  74. 1
  75. ├── 1-1
  76. │ ├── 1-1-1
  77. │ │ ├── 202-1
  78. │ │ │ ├── 202-1-a
  79. │ │ │ └── 202-1-b
  80. │ │ └── 202-2
  81. │ │ ├── 202-2-c
  82. │ │ └── 202-1-3
  83. │ ├── 1-1-2
  84. │ └── 1-1-3
  85. ├── 1-2
  86. │ └── 1-2-1
  87. ├── 1-3
  88. │ └── 1-3-1
  89. └── 1-4
  90. */
  91. // 测试R类方法
  92. it('test getDataByTenderId', function* () {
  93. const ctx = app.mockContext();
  94. // 查询前4层节点
  95. const result1 = yield ctx.service.ledger.getDataByTenderId(testTenderId);
  96. assert(result1.length === 12);
  97. // 查询前3层节点
  98. const result2 = yield ctx.service.ledger.getDataByTenderId(testTenderId, 3);
  99. assert(result2.length === 10);
  100. });
  101. it('test getDataByNodeId', function* () {
  102. const ctx = app.mockContext();
  103. // 查询节点202-1
  104. const node = yield ctx.service.ledger.getDataByNodeId(testTenderId, 7);
  105. assert(node);
  106. assert(node.code === '202-1');
  107. assert(node.full_path === '1.2.6.7');
  108. });
  109. it('test getDataByNodeIds', function* () {
  110. const ctx = app.mockContext();
  111. // 查询节点202-1-a与201-1-b
  112. const result = yield ctx.service.ledger.getDataByNodeIds(testTenderId, [10, 9]);
  113. assert(result.length === 2);
  114. let node = findById(result, 10);
  115. assert(node.code === '202-1-a');
  116. node = findById(result, 9);
  117. assert(node.full_path === '1.2.6.7.9');
  118. });
  119. it('test getDataByIds', function* () {
  120. const ctx = app.mockContext();
  121. // 查询节点202-1
  122. const node = yield ctx.service.ledger.getDataByNodeId(testTenderId, 7);
  123. const result = yield ctx.service.ledger.getDataByIds([node.id]);
  124. assert(result.length === 1);
  125. assert(node.code === result[0].code);
  126. assert(node.full_path === result[0].full_path);
  127. });
  128. it('test getLastChildData', function* () {
  129. const ctx = app.mockContext();
  130. // 查询节点202-1最后一个子节点
  131. const result = yield ctx.service.ledger.getLastChildData(testTenderId, 7);
  132. assert(result.ledger_id === 10);
  133. assert(result.full_path === '1.2.6.7.10');
  134. });
  135. it('test getDataByParentAndOrder', function* () {
  136. const ctx = app.mockContext();
  137. // 查询节点202-1 第1子节点
  138. const result1 = yield ctx.service.ledger.getDataByParentAndOrder(testTenderId, 8, 1);
  139. assert(result1.ledger_id === 11);
  140. assert(result1.full_path === '1.2.6.8.11');
  141. // 查询节点1-1 第2/3子节点
  142. const result2 = yield ctx.service.ledger.getDataByParentAndOrder(testTenderId, 2, [2, 3]);
  143. assert(result2.length === 2);
  144. let node = findById(result2, 13);
  145. assert(node);
  146. assert(node.code === '1-1-2');
  147. node = findById(result2, 14);
  148. assert(node);
  149. assert(node.code === '1-1-3');
  150. });
  151. it('test getChildrenByParentId', function* () {
  152. const ctx = app.mockContext();
  153. // 查询节点202-1最后一个子节点
  154. const result = yield ctx.service.ledger.getChildrenByParentId(testTenderId, 8);
  155. assert(result.length === 2);
  156. let node = findById(result, 11);
  157. assert(node);
  158. assert(node.code === '202-2-c');
  159. node = findById(result, 12);
  160. assert(node);
  161. assert(node.full_path === '1.2.6.8.12');
  162. });
  163. it('test getNextsData', function* () {
  164. const ctx = app.mockContext();
  165. // 查询节点1-1-1的全部子节点
  166. const result = yield ctx.service.ledger.getNextsData(testTenderId, 2, 1);
  167. assert(result.length === 2);
  168. let node = findById(result, 13);
  169. assert(node);
  170. assert(node.code === '1-1-2');
  171. node = findById(result, 14);
  172. assert(node);
  173. assert(node.code === '1-1-3');
  174. });
  175. it('test getDataByFullPath', function* () {
  176. const ctx = app.mockContext();
  177. // 查询节点202-2及其子节点
  178. const result = yield ctx.service.ledger.getDataByFullPath(testTenderId, '1.2.6.8%');
  179. assert(result.length === 3);
  180. let node = findById(result, 8);
  181. assert(node);
  182. assert(node.code === '202-2');
  183. node = findById(result, 11);
  184. assert(node);
  185. assert(node.code === '202-2-c');
  186. node = findById(result, 12);
  187. assert(node);
  188. assert(node.full_path === '1.2.6.8.12');
  189. // 查询1-1-1的子孙节点
  190. const result1 = yield ctx.service.ledger.getDataByFullPath(testTenderId, '1.2.6.%');
  191. assert(result1.length === 6);
  192. });
  193. it('test getFullLevelDataByFullPath', function* () {
  194. const ctx = app.mockContext();
  195. // 查询202-2-c及其全部父节点
  196. const result1 = yield ctx.service.ledger.getFullLevelDataByFullPath(testTenderId, '1.2.6.8.11');
  197. assert(result1.length === 5);
  198. const result2 = yield ctx.service.ledger.getFullLevelDataByFullPath(testTenderId, ['1.2.6.8.11', '1.2.6.7.9']);
  199. assert(result2.length === 7);
  200. });
  201. // 测试CUD类方法
  202. // 基本树结构操作
  203. it('test addNode', function* () {
  204. const ctx = app.mockContext();
  205. // 选中1-1-1,插入节点
  206. const resultData = yield ctx.service.ledger.addNode(testTenderId, 6);
  207. assert(resultData.create.length === 1);
  208. assert(resultData.update.length === 2);
  209. assert(resultData.create[0].is_leaf === 1);
  210. assert(resultData.create[0].ledger_id === 17);
  211. });
  212. /* 期望运行结果:
  213. 1
  214. ├── 1-1
  215. │ ├── 1-1-1
  216. │ │ ├── 202-1
  217. │ │ │ ├── 202-1-a
  218. │ │ │ └── 202-1-b
  219. │ │ └── 202-2
  220. │ │ ├── 202-2-c
  221. │ │ └── 202-1-3
  222. │ ├── new
  223. │ ├── 1-1-2
  224. │ └── 1-1-3
  225. ├── 1-2
  226. │ └── 1-2-1
  227. ├── 1-3
  228. │ └── 1-3-1
  229. └── 1-4
  230. */
  231. it('test deleteNode', function* () {
  232. const ctx = app.mockContext();
  233. // 选中202-1,删除节点
  234. const resultData = yield ctx.service.ledger.deleteNode(testTenderId, 7);
  235. assert(resultData.delete.length === 3);
  236. assert(resultData.update.length === 1);
  237. });
  238. /* 期望运行结果:
  239. 1
  240. ├── 1-1
  241. │ ├── 1-1-1
  242. │ │ └── 202-2
  243. │ │ ├── 202-2-c
  244. │ │ └── 202-2-e
  245. │ ├── new
  246. │ ├── 1-1-2
  247. │ └── 1-1-3
  248. ├── 1-2
  249. │ └── 1-2-1
  250. ├── 1-3
  251. │ └── 1-3-1
  252. └── 1-4
  253. */
  254. it('test upMoveNode', function* () {
  255. const ctx = app.mockContext();
  256. // 选中202-2-e上移
  257. const resultData = yield ctx.service.ledger.upMoveNode(testTenderId, 12);
  258. resultData.update.sort(function(x, y) {
  259. return x.order - y.order;
  260. });
  261. assert(resultData.update.length === 2);
  262. assert(resultData.update[0].code === '202-2-e');
  263. });
  264. /* 期望运行结果:
  265. 1
  266. ├── 1-1
  267. │ ├── 1-1-1
  268. │ │ └── 202-2
  269. │ │ ├── 202-2-c
  270. │ │ └── 202-2-e
  271. │ ├── new
  272. │ ├── 1-1-2
  273. │ └── 1-1-3
  274. ├── 1-2
  275. │ └── 1-2-1
  276. ├── 1-3
  277. │ └── 1-3-1
  278. └── 1-4
  279. */
  280. it('test downMoveNode', function* () {
  281. const ctx = app.mockContext();
  282. // 选中202-2-e下移
  283. const resultData = yield ctx.service.ledger.downMoveNode(testTenderId, 12);
  284. resultData.update.sort(function(x, y) {
  285. return x.order - y.order;
  286. });
  287. assert(resultData.update.length === 2);
  288. assert(resultData.update[0].code === '202-2-c');
  289. });
  290. /* 期望运行结果:
  291. 1
  292. ├── 1-1
  293. │ ├── 1-1-1
  294. │ │ └── 202-2
  295. │ │ ├── 202-2-c
  296. │ │ └── 202-2-e
  297. │ ├── new
  298. │ ├── 1-1-2
  299. │ └── 1-1-3
  300. ├── 1-2
  301. │ └── 1-2-1
  302. ├── 1-3
  303. │ └── 1-3-1
  304. └── 1-4
  305. */
  306. it('test upLevelNode', function* () {
  307. const ctx = app.mockContext();
  308. // 选中 1-1-2 升级
  309. const resultData = yield ctx.service.ledger.upLevelNode(testTenderId, 13);
  310. assert(resultData);
  311. assert(resultData.update.length === 6);
  312. let node = findById(resultData.update, 13);
  313. assert(node.full_path === '1.13');
  314. assert(node.ledger_pid === 1);
  315. assert(!node.is_leaf);
  316. node = findById(resultData.update, 14);
  317. assert(node.ledger_pid === 13);
  318. assert(node.full_path === '1.13.14');
  319. node = findById(resultData.update, 3);
  320. assert(node.order === 3);
  321. node = findById(resultData.update, 4);
  322. assert(node.order === 4);
  323. node = findById(resultData.update, 5);
  324. assert(node.order === 5);
  325. });
  326. /* 期望运行结果:
  327. 1
  328. ├── 1-1
  329. │ ├── 1-1-1
  330. │ │ └── 202-2
  331. │ │ ├── 202-2-c
  332. │ │ └── 202-2-e
  333. │ └── new
  334. ├── 1-1-2
  335. │ └── 1-1-3
  336. ├── 1-2
  337. │ └── 1-2-1
  338. ├── 1-3
  339. │ └── 1-3-1
  340. └── 1-4
  341. */
  342. it('test downLevelNode', function* () {
  343. const ctx = app.mockContext();
  344. // 选中1-3 降级
  345. const resultData = yield ctx.service.ledger.downLevelNode(testTenderId, 4);
  346. // 1-3/1-3-1/1-4修改
  347. assert(resultData.update.length === 3);
  348. let node = findById(resultData.update, 4);
  349. assert(node.full_path === '1.3.4');
  350. assert(node.ledger_pid === 3);
  351. assert(node.level === 3);
  352. assert(node.order === 2);
  353. node = findById(resultData.update, 16);
  354. assert(node.level === 4);
  355. assert(node.full_path === '1.3.4.16');
  356. node = findById(resultData.update, 5);
  357. assert(node.order === 4);
  358. });
  359. /* 期望运行结果:
  360. 1
  361. ├── 1-1
  362. │ ├── 1-1-1
  363. │ │ └── 202-2
  364. │ │ ├── 202-2-c
  365. │ │ └── 202-2-e
  366. │ └── new
  367. ├── 1-1-2
  368. │ └── 1-1-3
  369. ├── 1-2
  370. │ ├── 1-2-1
  371. │ └── 1-3
  372. │ └── 1-3-1
  373. └── 1-4
  374. */
  375. // 复制整块
  376. it('test pasteBlock', function* () {
  377. const ctx = app.mockContext();
  378. // 选中1-2-1, 粘贴1-1-1和new
  379. const resultData = yield ctx.service.ledger.pasteBlock(testTenderId, 15, [6, 17]);
  380. const ledger = resultData.ledger;
  381. assert(ledger.create.length === 5);
  382. assert(ledger.create[0].order = 2);
  383. assert(ledger.create[0].level = 3);
  384. assert(ledger.create[0].full_path = '1.3.18');
  385. assert(ledger.create[1].order = 3);
  386. assert(ledger.create[1].level = 3);
  387. assert(ledger.create[1].full_path = '1.3.22');
  388. assert(ledger.update.length === 1);
  389. assert(ledger.update[0].order = 3);
  390. assert(resultData.pos.length === 0);
  391. });
  392. /* 期望运行结果:
  393. 1
  394. ├── 1-1
  395. │ ├── 1-1-1
  396. │ │ └── 202-2
  397. │ │ ├── 202-2-c
  398. │ │ └── 202-2-e
  399. │ └── new
  400. ├── 1-1-2
  401. │ └── 1-1-3
  402. ├── 1-2
  403. │ ├── 1-2-1
  404. │ ├── 1-1-1
  405. │ │ └── 202-2
  406. │ │ ├── 202-2-c
  407. │ │ └── 202-2-e
  408. │ ├── new
  409. │ └── 1-3
  410. │ └── 1-3-1
  411. └── 1-4
  412. */
  413. // 增量计算
  414. it('test updateInfo', function* () {
  415. const ctx = app.mockContext();
  416. // 修改new(id=17)的code 为 1-1-4
  417. const node = yield ctx.service.ledger.getDataByNodeId(testTenderId, 17);
  418. assert(node);
  419. const resultData = yield ctx.service.ledger.updateInfo(testTenderId, {
  420. id: node.id,
  421. tender_id: node.tender_id,
  422. ledger_id: node.ledger_id,
  423. code: '1-1-4',
  424. });
  425. assert(resultData.code === '1-1-4');
  426. });
  427. /* 期望运行结果:
  428. 1
  429. ├── 1-1
  430. │ ├── 1-1-1
  431. │ │ └── 202-2
  432. │ │ ├── 202-2-c
  433. │ │ └── 202-2-e
  434. │ └── 1-1-4
  435. ├── 1-1-2
  436. │ └── 1-1-3
  437. ├── 1-2
  438. │ ├── 1-2-1
  439. │ ├── 1-1-1
  440. │ │ └── 202-2
  441. │ │ ├── 202-2-c
  442. │ │ └── 202-2-e
  443. │ ├── new
  444. │ └── 1-3
  445. │ └── 1-3-1
  446. └── 1-4
  447. */
  448. it('test updateInfos', function* () {
  449. const ctx = app.mockContext();
  450. // 修改1-1-1(id=18)的code 为 1-2-2、修改new(id=22)的code 为 1-2-3
  451. const node1 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 18);
  452. assert(node1);
  453. const node2 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 22);
  454. assert(node2);
  455. const resultData = yield ctx.service.ledger.updateInfos(testTenderId, [{
  456. id: node1.id,
  457. tender_id: node1.tender_id,
  458. ledger_id: node1.ledger_id,
  459. code: '1-2-2',
  460. }, {
  461. id: node2.id,
  462. tender_id: node2.tender_id,
  463. ledger_id: node2.ledger_id,
  464. code: '1-2-3',
  465. }]);
  466. assert(resultData.length === 2);
  467. assert(resultData[0].code === '1-2-2');
  468. assert(resultData[1].code === '1-2-3');
  469. });
  470. /* 期望运行结果:
  471. 1
  472. ├── 1-1
  473. │ ├── 1-1-1
  474. │ │ └── 202-2
  475. │ │ ├── 202-2-c
  476. │ │ └── 202-2-e
  477. │ └── 1-1-4
  478. ├── 1-1-2
  479. │ └── 1-1-3
  480. ├── 1-2
  481. │ ├── 1-2-1
  482. │ ├── 1-2-2
  483. │ │ └── 202-2
  484. │ │ ├── 202-2-c
  485. │ │ └── 202-2-e
  486. │ ├── 1-2-3
  487. │ └── 1-3
  488. │ └── 1-3-1
  489. └── 1-4
  490. */
  491. it('test updateCalc - update 1', function* () {
  492. const ctx = app.mockContext();
  493. // 修改202-2-e(1-1-1下)(id=12)的quantity为2.00000001, unit_price位3.0000005
  494. const qty = 2.00000001;
  495. const up = 3.0000005;
  496. const tp = 6.00000103;
  497. const node1 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 12);
  498. assert(node1);
  499. const resultData = yield ctx.service.ledger.updateCalc(testTenderId, {
  500. id: node1.id,
  501. tender_id: node1.tender_id,
  502. ledger_id: node1.ledger_id,
  503. quantity: qty,
  504. unit_price: up
  505. });
  506. assert(resultData.update.length === 1);
  507. let node = findById(resultData.update, 12);
  508. assert(node.total_price.toFixed(8) == tp);
  509. });
  510. /* 期望运行结果:
  511. 1
  512. ├── 1-1
  513. │ ├── 1-1-1
  514. │ │ └── 202-2
  515. │ │ ├── 202-2-c
  516. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  517. │ └── 1-1-4
  518. ├── 1-1-2
  519. │ └── 1-1-3
  520. ├── 1-2
  521. │ ├── 1-2-1
  522. │ ├── 1-2-2
  523. │ │ └── 202-2
  524. │ │ ├── 202-2-c
  525. │ │ └── 202-2-e
  526. │ ├── 1-2-3
  527. │ └── 1-3
  528. │ └── 1-3-1
  529. └── 1-4
  530. */
  531. it('test updateCalc - update N', function* () {
  532. const ctx = app.mockContext();
  533. // 修改202-2-c(1-1-1下)(id=11)的quantity为4.00000025, unit_price为6.0000083
  534. // 202-2-c(1-2-2下)(id=20)的quantity为2.0000001, unit_price为5.000065
  535. // 202-2-e(1-2-2下)(id=21)的quantity为8.0000579, unit_price为4.0000086
  536. const qty = [4.00000025, 2.0000001, 8.0000579];
  537. const up = [6.0000083, 5.000065, 4.0000086];
  538. const tp = [24.0000347, 10.0001305, 32.0003004];
  539. const sum = [30.00003573, 42.0004309, 72.00046663]
  540. const node1 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 11);
  541. assert(node1);
  542. const node2 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 20);
  543. assert(node2);
  544. const node3 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 21);
  545. assert(node3);
  546. const resultData = yield ctx.service.ledger.updateCalc(testTenderId, [{
  547. id: node1.id,
  548. tender_id: node1.tender_id,
  549. ledger_id: node1.ledger_id,
  550. quantity: qty[0],
  551. unit_price: up[0]
  552. }, {
  553. id: node2.id,
  554. tender_id: node2.tender_id,
  555. ledger_id: node2.ledger_id,
  556. quantity: qty[1],
  557. unit_price: up[1]
  558. }, {
  559. id: node3.id,
  560. tender_id: node3.tender_id,
  561. ledger_id: node3.ledger_id,
  562. quantity: qty[2],
  563. unit_price: up[2]
  564. }]);
  565. assert(resultData.update.length === 3);
  566. let node = findById(resultData.update, 11);
  567. assert(node.total_price.toFixed(8) == tp[0]);
  568. node = findById(resultData.update, 20);
  569. assert(node.total_price.toFixed(8) == tp[1]);
  570. node = findById(resultData.update, 21);
  571. assert(node.total_price.toFixed(8) == tp[2]);
  572. });
  573. /* 期望运行结果:
  574. 1
  575. ├── 1-1
  576. │ ├── 1-1-1
  577. │ │ └── 202-2
  578. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  579. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  580. │ └── 1-1-4
  581. ├── 1-1-2
  582. │ └── 1-1-3
  583. ├── 1-2
  584. │ ├── 1-2-1
  585. │ ├── 1-2-2
  586. │ │ └── 202-2
  587. │ │ ├── 202-2-c 2.0000001 5.000065 10.0001305
  588. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  589. │ ├── 1-2-3
  590. │ └── 1-3
  591. │ └── 1-3-1
  592. └── 1-4
  593. */
  594. // 复制整块+实时计算
  595. it('test pasteBlock - with Increment Calculate', function* () {
  596. const ctx = app.mockContext();
  597. // 选中1-1-4, 粘贴202-2(1-1-1下)
  598. const resultData = yield ctx.service.ledger.pasteBlock(testTenderId, 17, [8]);
  599. assert(resultData.ledger.create.length === 3);
  600. assert(resultData.ledger.update.length === 0);
  601. });
  602. /* 期望运行结果:
  603. 1
  604. ├── 1-1
  605. │ ├── 1-1-1
  606. │ │ └── 202-2
  607. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  608. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  609. │ ├── 1-1-4
  610. │ └── 202-2
  611. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  612. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  613. ├── 1-1-2
  614. │ └── 1-1-3
  615. ├── 1-2
  616. │ ├── 1-2-1
  617. │ ├── 1-2-2
  618. │ │ └── 202-2
  619. │ │ ├── 202-2-c 2.0000001 5.000065 10.0001305
  620. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  621. │ ├── 1-2-3
  622. │ └── 1-3
  623. │ └── 1-3-1
  624. └── 1-4
  625. */
  626. // 树结构基本操作+实时计算
  627. it('test downLevel - with Increment Calculate', function* () {
  628. const ctx = app.mockContext();
  629. // 选中202-2(1-1-4后兄弟节点) 降级
  630. const resultData = yield ctx.service.ledger.downLevelNode(testTenderId, 23);
  631. assert(resultData.update.length === 4);
  632. });
  633. /* 期望运行结果:
  634. 1
  635. ├── 1-1
  636. │ ├── 1-1-1
  637. │ │ └── 202-2
  638. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  639. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  640. │ └── 1-1-4
  641. │ └── 202-2
  642. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  643. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  644. ├── 1-1-2
  645. │ └── 1-1-3
  646. ├── 1-2
  647. │ ├── 1-2-1
  648. │ ├── 1-2-2
  649. │ │ └── 202-2
  650. │ │ ├── 202-2-c 2.0000001 5.000065 10.0001305
  651. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  652. │ ├── 1-2-3
  653. │ └── 1-3
  654. │ └── 1-3-1
  655. └── 1-4
  656. */
  657. it('test upLevel - with Increment Calculate', function* () {
  658. const ctx = app.mockContext();
  659. yield ctx.service.ledger.pasteBlock(testTenderId, 23, [23]);
  660. /* 期望运行结果:
  661. 1
  662. ├── 1-1
  663. │ ├── 1-1-1
  664. │ │ └── 202-2
  665. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  666. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  667. │ └── 1-1-4
  668. │ ├── 202-2
  669. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  670. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  671. │ └── 202-2
  672. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  673. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  674. ├── 1-1-2
  675. │ └── 1-1-3
  676. ├── 1-2
  677. │ ├── 1-2-1
  678. │ ├── 1-2-2
  679. │ │ └── 202-2
  680. │ │ ├── 202-2-c 2.0000001 5.000065 10.0001305
  681. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  682. │ ├── 1-2-3
  683. │ └── 1-3
  684. │ └── 1-3-1
  685. └── 1-4
  686. */
  687. // 选中202-2-c(1-1-4下)(id=23)升级
  688. const resultData = yield ctx.service.ledger.upLevelNode(testTenderId, 23);
  689. assert(resultData.update.length === 7);
  690. });
  691. /* 期望运行结果:
  692. 1
  693. ├── 1-1
  694. │ ├── 1-1-1
  695. │ │ └── 202-2
  696. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  697. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  698. │ ├── 1-1-4
  699. │ └── 202-2
  700. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  701. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  702. │ └── 202-2
  703. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  704. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  705. ├── 1-1-2
  706. │ └── 1-1-3
  707. ├── 1-2
  708. │ ├── 1-2-1
  709. │ ├── 1-2-2
  710. │ │ └── 202-2
  711. │ │ ├── 202-2-c 2.0000001 5.000065 10.0001305
  712. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  713. │ ├── 1-2-3
  714. │ └── 1-3
  715. │ └── 1-3-1
  716. └── 1-4
  717. */
  718. it('test deleteNode - with Increment Calculate', function* () {
  719. const ctx = app.mockContext();
  720. // 选中202-2-c(1-2-2下)(id=20),删除节点
  721. const resultData = yield ctx.service.ledger.deleteNode(testTenderId, 20);
  722. assert(resultData.delete.length === 1);
  723. assert(resultData.update.length === 1);
  724. });
  725. /* 期望运行结果:
  726. 1
  727. ├── 1-1
  728. │ ├── 1-1-1
  729. │ │ └── 202-2
  730. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  731. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  732. │ ├── 1-1-4
  733. │ └── 202-2
  734. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  735. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  736. │ └── 202-2
  737. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  738. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  739. ├── 1-1-2
  740. │ └── 1-1-3
  741. ├── 1-2
  742. │ ├── 1-2-1
  743. │ ├── 1-2-2
  744. │ │ └── 202-2
  745. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  746. │ ├── 1-2-3
  747. │ └── 1-3
  748. │ └── 1-3-1
  749. └── 1-4
  750. */
  751. // 从标准库添加数据
  752. // 检查添加,直接添加为选中节点子节点
  753. it('test addStdNode', function* () {
  754. const ctx = app.mockContext();
  755. // 选中1-1-4
  756. const selectData = yield ctx.service.ledger.getDataByCondition({ tender_id: testTenderId, code: '1-1-4' });
  757. assert(selectData);
  758. // 从标准库中添加1-1-5
  759. const condition1 = { list_id: 1, code: '1-1-5' };
  760. const libData1 = yield ctx.service.stdChapter.getDataByCondition(condition1);
  761. assert(libData1);
  762. const stdData1 = yield ctx.service.stdChapter.getDataByDataId(1, libData1.chapter_id);
  763. assert(stdData1);
  764. assert(stdData1.id === libData1.id);
  765. const result1 = yield ctx.service.ledger.addStdNode(testTenderId, selectData.ledger_id, stdData1);
  766. assert(result1);
  767. assert(result1.create.length === 1);
  768. assert(result1.update.length === 1);
  769. // 从标准库中添加101-1
  770. });
  771. /* 期望运行结果:
  772. 1 122.00040759
  773. ├── 1-1 90.00009719
  774. │ ├── 1-1-1 30.00003573
  775. │ │ └── 202-2 30.00003573
  776. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  777. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  778. │ ├── 1-1-4
  779. │ ├── 1-1-5
  780. │ └── 202-2 60.00007146
  781. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  782. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  783. │ └── 202-2 30.00003573
  784. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  785. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  786. ├── 1-1-2
  787. │ └── 1-1-3
  788. ├── 1-2 32.0003004
  789. │ ├── 1-2-1
  790. │ ├── 1-2-2 32.0003004
  791. │ │ └── 202-2 32.0003004
  792. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  793. │ ├── 1-2-3
  794. │ └── 1-3
  795. │ └── 1-3-1
  796. └── 1-4
  797. */
  798. it('test addStdNodeWithParent', function* () {
  799. const ctx = app.mockContext();
  800. // 从标准库添加1-4-2-1
  801. const condition1 = { list_id: 1, code: '1-4-2-1'};
  802. const libData1 = yield ctx.service.stdChapter.getDataByCondition(condition1);
  803. assert(libData1);
  804. const stdData1 = yield ctx.service.stdChapter.getDataByDataId(1, libData1.chapter_id);
  805. assert(stdData1);
  806. assert(stdData1.id === libData1.id);
  807. const result1 = yield ctx.service.ledger.addStdNodeWithParent(testTenderId, stdData1, ctx.service.stdChapter);
  808. assert(result1);
  809. assert(result1.create.length === 4);
  810. assert(!result1.update || result1.update.length === 0);
  811. assert(!result1.expand || result1.expand.length === 0);
  812. // 从标准库添加1-4-2-1-2
  813. const condition2 = { list_id: 1, code: '1-4-2-1-2'};
  814. const libData2 = yield ctx.service.stdChapter.getDataByCondition(condition2);
  815. assert(libData2);
  816. const stdData2 = yield ctx.service.stdChapter.getDataByDataId(1, libData2.chapter_id);
  817. assert(stdData2);
  818. assert(stdData2.id === libData2.id);
  819. const result2 = yield ctx.service.ledger.addStdNodeWithParent(testTenderId, stdData2, ctx.service.stdChapter);
  820. assert(result2);
  821. assert(result2.create.length === 1);
  822. assert(result2.update.length === 1);
  823. assert(result2.update[0].code = '1-4-2-1');
  824. assert(!result2.update[0].is_leaf);
  825. assert(result2.expand.length === 4);
  826. // 从标准库添加1-4-2-1-1
  827. const condition3 = { list_id: 1, code: '1-4-2-1-1'};
  828. const libData3 = yield ctx.service.stdChapter.getDataByCondition(condition3);
  829. assert(libData3);
  830. const stdData3 = yield ctx.service.stdChapter.getDataByDataId(1, libData3.chapter_id);
  831. assert(stdData3);
  832. assert(stdData3.id === libData3.id);
  833. const result3 = yield ctx.service.ledger.addStdNodeWithParent(testTenderId, stdData3, ctx.service.stdChapter);
  834. assert(result3);
  835. assert(result3.create.length === 1);
  836. assert(result3.create[0].order === 1);
  837. assert(result3.update.length === 1);
  838. assert(result3.update[0].code === '1-4-2-1-2');
  839. assert(result3.update[0].order === 2);
  840. assert(result3.expand.length === 5);
  841. });
  842. /* 期望运行结果:
  843. 1 122.00040759
  844. ├── 1-1 90.00009719
  845. │ ├── 1-1-1 30.00003573
  846. │ │ └── 202-2 30.00003573
  847. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  848. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  849. │ ├── 1-1-4
  850. │ ├── 1-1-5
  851. │ └── 202-2 60.00007146
  852. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  853. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  854. │ └── 202-2 30.00003573
  855. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  856. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  857. ├── 1-1-2
  858. │ └── 1-1-3
  859. ├── 1-2 32.0003004
  860. │ ├── 1-2-1
  861. │ ├── 1-2-2 32.0003004
  862. │ │ └── 202-2 32.0003004
  863. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  864. │ ├── 1-2-3
  865. │ └── 1-3
  866. │ └── 1-3-1
  867. └── 1-4
  868. └── 1-4-2
  869. └── 1-4-2-1
  870. ├── 1-4-2-1-1
  871. └── 1-4-2-1-2
  872. */
  873. // 批量插入
  874. it('test batchInsertChild', function* () {
  875. const ctx = app.mockContext();
  876. // pos需要记录createUserId
  877. ctx.session = {
  878. sessionUser: {
  879. accountId: testUserId,
  880. },
  881. };
  882. const batchData = [
  883. {
  884. b_code: 401-1, name: 'A1', unit: 'B1', price: 2,
  885. pos: [{name: 'X1', quantity: 1}, {name: 'X2', quantity: 2}],
  886. },
  887. {
  888. b_code: 402-1, name: 'A2', unit: 'B2', price: 3,
  889. pos: [{name: 'X1', quantity: 3}, {name: 'X2', quantity: 4}],
  890. },
  891. ];
  892. // 选中1-1-3(id=14)
  893. const result = yield ctx.service.ledger.batchInsertChild(testTenderId, 14, batchData);
  894. assert(result.ledger.create.length === 2);
  895. let node = findById(result.ledger.create, 36);
  896. assert(node.quantity.toFixed(8) == 3);
  897. assert(node.total_price.toFixed(8) == 6);
  898. node = findById(result.ledger.create, 37);
  899. assert(node.quantity.toFixed(8) == 7);
  900. assert(node.total_price.toFixed(8) == 21);
  901. assert(result.pos.length === 4);
  902. });
  903. /* 期望运行结果:
  904. 1
  905. ├── 1-1
  906. │ ├── 1-1-1
  907. │ │ └── 202-2
  908. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  909. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  910. │ ├── 1-1-4
  911. │ ├── 1-1-5
  912. │ └── 202-2
  913. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  914. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  915. │ └── 202-2
  916. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  917. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  918. ├── 1-1-2
  919. │ └── 1-1-3
  920. │ ├── 401-1 2 3 6
  921. │ │ ├── (pos)X1 1
  922. │ │ └── (pos)X1 2
  923. │ └── 402-1 3 7 21
  924. │ ├── (pos)X1 3
  925. │ └── (pos)X2 4
  926. ├── 1-2
  927. │ ├── 1-2-1
  928. │ ├── 1-2-2
  929. │ │ └── 202-2
  930. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  931. │ ├── 1-2-3
  932. │ └── 1-3
  933. │ └── 1-3-1
  934. └── 1-4
  935. └── 1-4-2
  936. └── 1-4-2-1
  937. ├── 1-4-2-1-1
  938. └── 1-4-2-1-2
  939. */
  940. // 批量插入
  941. it('test batchInsertNext', function* () {
  942. const ctx = app.mockContext();
  943. // pos需要记录createUserId
  944. ctx.session = {
  945. sessionUser: {
  946. accountId: testUserId,
  947. },
  948. };
  949. const batchData = [
  950. {
  951. b_code: 403-1, name: 'A1', unit: 'B1', price: 2,
  952. pos: [{name: 'Y1', quantity: 5}, {name: 'Y2', quantity: 6}],
  953. },
  954. {
  955. b_code: 404-1, name: 'A2', unit: 'B2', price: 3,
  956. pos: [{name: 'Y1', quantity: 7}, {name: 'Y2', quantity: 8}],
  957. },
  958. ];
  959. // 选中1-1-3(id=14)
  960. const result = yield ctx.service.ledger.batchInsertNext(testTenderId, 14, batchData);
  961. assert(result.ledger.create.length === 2);
  962. let node = findById(result.ledger.create, 39);
  963. assert(node.quantity.toFixed(8) == 11);
  964. assert(node.total_price.toFixed(8) == 22);
  965. node = findById(result.ledger.create, 40);
  966. assert(node.quantity.toFixed(8) == 15);
  967. assert(node.total_price.toFixed(8) == 45);
  968. assert(result.pos.length === 4);
  969. });
  970. /* 期望运行结果:
  971. 1
  972. ├── 1-1
  973. │ ├── 1-1-1
  974. │ │ └── 202-2
  975. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  976. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  977. │ ├── 1-1-4
  978. │ ├── 1-1-5
  979. │ └── 202-2
  980. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  981. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  982. │ └── 202-2
  983. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  984. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  985. ├── 1-1-2
  986. │ ├── 1-1-3
  987. │ │ ├── 401-1 2 3 6
  988. │ │ │ ├── (pos)X1 1
  989. │ │ │ └── (pos)X1 2
  990. │ │ └── 402-1 3 7 21
  991. │ │ ├── (pos)X1 3
  992. │ │ └── (pos)X2 4
  993. │ ├── 403-1 2 11 22
  994. │ │ ├── (pos)Y1 5
  995. │ │ └── (pos)Y2 6
  996. │ └── 404-1 3 15 45
  997. │ ├── (pos)Y1 7
  998. │ └── (pos)Y2 8
  999. ├── 1-2
  1000. │ ├── 1-2-1
  1001. │ ├── 1-2-2
  1002. │ │ └── 202-2
  1003. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  1004. │ ├── 1-2-3
  1005. │ └── 1-3
  1006. │ └── 1-3-1
  1007. └── 1-4
  1008. └── 1-4-2
  1009. └── 1-4-2-1
  1010. ├── 1-4-2-1-1
  1011. └── 1-4-2-1-2
  1012. */
  1013. // 测试统计类方法
  1014. it('test addUpChildren', function* () {
  1015. const ctx = app.mockContext();
  1016. // 统计202-2(id=23)前两个子节点的金额
  1017. const result1 = yield ctx.service.ledger.addUpChildren(testTenderId, 23, 2, '<=');
  1018. assert(result1 && result1.toFixed(8) == 30.00003573);
  1019. // 数据库不再存储父项金额,以下两个查询不能获得实际金额
  1020. // 统计202-2(id=23)后两个子节点的金额
  1021. //const result2 = yield ctx.service.ledger.addUpChildren(testTenderId, 23, 2, '>=');
  1022. //assert(result2 && result2.toFixed(8) == 36.00003676);
  1023. // 统计202-2(id=23)全部子节点的金额
  1024. //const result3 = yield ctx.service.ledger.addUpChildren(testTenderId, 23, 0, '>');
  1025. //assert(result3 && result3.toFixed(8) == 60.00007146);
  1026. });
  1027. // 测试搜索类方法
  1028. it('test search', function* () {
  1029. const ctx = app.mockContext();
  1030. const result = yield ctx.service.ledger.search(testTenderId, {
  1031. value: app.mysql.escape('%1-3%'),
  1032. operate: 'Like',
  1033. fields: ['code', 'name'],
  1034. });
  1035. assert(result.length === 3);
  1036. });
  1037. it('test searchRange', function* () {
  1038. const ctx = app.mockContext();
  1039. const result = yield ctx.service.ledger.searchRange(testTenderId, {
  1040. value: app.mysql.escape('%1-3%'),
  1041. operate: 'Like',
  1042. fields: ['code', 'name'],
  1043. }, [{
  1044. type: 'And',
  1045. value: app.mysql.escape('1.13.%'),
  1046. operate: 'Like',
  1047. fields: ['full_path'],
  1048. }]);
  1049. assert(result.length === 1);
  1050. assert(result[0].code === '1-1-3');
  1051. });
  1052. // it('test ImportExcelData', function* () {
  1053. // const ctx = app.mockContext();
  1054. //
  1055. // const file = ctx.app.baseDir + '/test/app/test_file/ledger-upload-test.xls';
  1056. // const sheets = excel.parse(file);
  1057. // const result = yield ctx.service.
  1058. // });
  1059. // 小数位数策略示例:
  1060. /* 先加总再保留3位小数:
  1061. 1 35.585
  1062. ├── 1-1 35.585
  1063. │ ├── 1-1-1 35.585
  1064. │ │ └── 202-2 35.585
  1065. │ │ ├── 202-2-c 4.25 6.83 29.028(29.0275)
  1066. │ │ └── 202-2-e 2.15 3.05 6.558(6.5575)
  1067. │ └── 1-1-3
  1068. ├── 1-1-2
  1069. │ └── 1-1-3
  1070. ├── 1-2
  1071. │ ├── 1-2-1
  1072. │ ├── 1-2-2
  1073. │ │ └── 202-2
  1074. │ │ ├── 202-2-c
  1075. │ │ └── 202-2-e
  1076. │ ├── 1-2-3
  1077. │ └── 1-3
  1078. │ └── 1-3-1
  1079. └── 1-4
  1080. */
  1081. /* 先保留3位小数再加总:
  1082. 1 35.586
  1083. ├── 1-1 35.586
  1084. │ ├── 1-1-1 35.586
  1085. │ │ └── 202-2 35.586
  1086. │ │ ├── 202-2-c 4.25 6.83 29.028(29.0275)
  1087. │ │ └── 202-2-e 2.15 3.05 6.558(6.5575)
  1088. │ └── 1-1-3
  1089. ├── 1-1-2
  1090. │ └── 1-1-3
  1091. ├── 1-2
  1092. │ ├── 1-2-1
  1093. │ ├── 1-2-2
  1094. │ │ └── 202-2
  1095. │ │ ├── 202-2-c
  1096. │ │ └── 202-2-e
  1097. │ ├── 1-2-3
  1098. │ └── 1-3
  1099. │ └── 1-3-1
  1100. └── 1-4
  1101. */
  1102. });