浏览代码

合同支付,单元测试

MaiXinRong 6 年之前
父节点
当前提交
297c577735
共有 3 个文件被更改,包括 115 次插入2 次删除
  1. 2 1
      app/service/pay.js
  2. 0 1
      app/service/stage_pay.js
  3. 113 0
      test/app/service/pay.test.js

+ 2 - 1
app/service/pay.js

@@ -123,6 +123,7 @@ module.exports = app => {
                     throw '删除合同支付项失败'
                 }
                 await transaction.commit();
+                return true;
             } catch(err) {
                 await transaction.rollback();
                 throw err;
@@ -153,6 +154,7 @@ module.exports = app => {
                 await transaction.update(this.tableName, {id: pay1.id, order: pay1.order});
                 await transaction.update(this.tableName, {id: pay2.id, order: pay2.order});
                 await transaction.commit();
+                return true;
             } catch (err) {
                 await transaction.rollback();
                 throw err;
@@ -165,7 +167,6 @@ module.exports = app => {
          * @returns {Promise<boolean>}
          */
         async save(data) {
-            console.log(data);
             if (!this.ctx.tender || !this.ctx.stage) { return false; }
             const pay = await this.getDataByCondition({tid: this.ctx.tender.id, id: data.id});
             if(!pay) {

+ 0 - 1
app/service/stage_pay.js

@@ -139,7 +139,6 @@ module.exports = app => {
                 '  WHERE SP.`sid` = ? AND P.`valid` = true' +
                 '  ORDER BY P.`order`';
             const sqlParam = [sid, sid];
-            console.log(this.db.format(sql, sqlParam));
             return await this.db.query(sql, sqlParam);
         }
 

+ 113 - 0
test/app/service/pay.test.js

@@ -0,0 +1,113 @@
+'use strict';
+
+/**
+ * 合同支付 模型 单元测试
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+
+const { app, assert } = require('egg-mock/bootstrap');
+const addData = {
+    name: 'test_pay',
+    category: null,
+};
+const mockData = {};
+let testPayId;
+const _ = require('lodash');
+
+describe('test/app/service/pay.test.js', () => {
+    // 准备测试数据,新增测试标段
+    before(function* () {
+        const ctx = app.mockContext();
+        // 模拟登录session
+        const postData = {
+            account: '734406061@qq.com',
+            project: 'T201711273363',
+            project_password: 'mai654321',
+        };
+        ctx.session = {};
+        const loginResult = yield ctx.service.projectAccount.accountLogin(postData, 2);
+        assert(loginResult);
+        mockData.session = ctx.session;
+        // 移除旧测试数据
+        const testTender = yield ctx.service.tender.getDataByCondition({
+            name: addData.name,
+            project_id: ctx.session.sessionProject.id,
+        });
+        if (testTender) {
+            const result = yield ctx.service.tender.deleteTenderNoBackup(testTender.id);
+            assert(result);
+        }
+        // 新增测试用标段
+        const result = yield ctx.service.tender.add(addData);
+        assert(result);
+        const tender = yield ctx.service.tender.getDataByCondition({
+            name: addData.name,
+            project_id: ctx.session.sessionProject.id,
+        });
+        mockData.tender = {id: tender.id, data: tender};
+        ctx.tender = mockData.tender;
+        // 新增测试期
+        const stage = yield ctx.service.stage.addStage(mockData.tender.id, '2019-06', '2019-06-10 ~ 2019-06-30');
+        assert(stage);
+        mockData.stage = stage;
+        mockData.stage.curTimes = 1;
+        mockData.stage.curOrder = 0;
+    });
+    it('test add', function* () {
+        const ctx = app.mockContext(mockData);
+
+        const orgStagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
+
+        const result = yield ctx.service.pay.add();
+        assert(result);
+        testPayId = result.pid;
+
+        const stagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
+        assert(stagePays.length === orgStagePays.length + 1);
+    });
+    it('test del', function* () {
+        const ctx = app.mockContext(mockData);
+
+        const orgStagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
+
+        const result = yield ctx.service.pay.del(testPayId);
+        assert(result);
+
+        const stagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
+        assert(stagePays.length === orgStagePays.length - 1);
+    });
+    it('test changeOrder', function* () {
+        const ctx = app.mockContext(mockData);
+
+        const node1 = yield ctx.service.pay.add();
+        assert(node1);
+        const node2 = yield ctx.service.pay.add();
+        assert(node2);
+
+        const result = yield ctx.service.pay.changeOrder(node1.pid, node2.pid);
+        assert(result);
+
+        const stagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
+        const newNode1 = _.find(stagePays, {order: node1.order});
+        assert(newNode1.pid === node2.pid);
+        const newNode2 = _.find(stagePays, {order: node2.order});
+        assert(newNode2.pid === node1.pid);
+
+        testPayId = node1.pid;
+    });
+    it('test save', function* () {
+        const ctx = app.mockContext(mockData);
+        const data = {id: testPayId, name: '12345', invalidField: 'invalid'};
+        console.log(data);
+        const result = yield ctx.service.pay.save(data);
+        assert(result);
+        assert(result.name);
+        assert(!result.invalidField);
+        const stagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
+        const node = _.find(stagePays, {pid: testPayId});
+        assert(node.name === data.name);
+    });
+});