update_task_test.go 4.1 KB
package task

import (
	"github.com/go-pg/pg"
	"net/http"

	"github.com/gavv/httpexpect"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	pG "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg"
)

var _ = Describe("更新任务", func() {
	Describe("提交数据更新任务", func() {
		Context("任务未发布状态下提交正确的任务数据", func() {
			BeforeEach(func() {
				_, err := pG.DB.QueryOne(
					pg.Scan(),
					"INSERT INTO tasks (id, company_id, task_name, task_type, sponsor, task_status, reference_resource, customer_value, task_nature, su_money, acceptance_standard, task_description, task_picture_urls, is_reward_take) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
					1, 101, "testTaskName", 1, "{}", 1, "{}", pg.Array([]string{"口感"}), "testTaskNature", 100.00, "testAcceptanceStandard", "testTaskDescription", pg.Array([]string{"url"}), false)
				Expect(err).NotTo(HaveOccurred())
			})
			It("返回更新后的任务数据", func() {
				httpExpect := httpexpect.New(GinkgoT(), server.URL)
				body := map[string]interface{}{
					"taskName":              "抢单任务",
					"taskType":              1,
					"sponsor":               2499036607974745088,
					"referenceResourceType": 1,
					"referenceResourceItems": []map[string]interface{}{
						{"serialNumber": 1, "title": "问题标题1"},
						{"serialNumber": 2, "title": "问题标题2"},
						{"serialNumber": 3, "title": "问题标题3"},
					},
					"customerValue": []string{
						"口味",
						"色泽",
						"商务服务",
					},
					"taskNature":         "线",
					"suMoney":            1000.00,
					"acceptanceStandard": "验收标准",
					"taskDescription":    "任务描述",
					"taskPictureUrls": []string{
						"url-1",
						"url-2",
						"url-3",
					},
					"isRewardTake": false,
				}
				httpExpect.PUT("/tasks/1").
					WithJSON(body).
					Expect().
					Status(http.StatusOK).
					JSON().
					Object().
					ContainsKey("code").ValueEqual("code", 0).
					ContainsKey("msg").ValueEqual("msg", "ok").
					ContainsKey("data").Value("data").Object().
					ContainsKey("taskId").ValueEqual("taskId", 1)
			})
		})
		Context("任务不在未发布状态下提交正确的任务数据", func() {
			BeforeEach(func() {
				_, err := pG.DB.QueryOne(
					pg.Scan(),
					"INSERT INTO tasks (id, company_id, task_name, task_type, sponsor, task_status, reference_resource, customer_value, task_nature, su_money, acceptance_standard, task_description, task_picture_urls, is_reward_take) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
					1, 101, "testTaskName", 1, "{}", 3, "null", pg.Array([]string{"口感"}), "testTaskNature", 100.00, "testAcceptanceStandard", "testTaskDescription", pg.Array([]string{"url"}), false)
				Expect(err).NotTo(HaveOccurred())
			})
			It("返回当前状态不能进行编辑操作的提示", func() {
				httpExpect := httpexpect.New(GinkgoT(), server.URL)
				body := map[string]interface{}{
					"taskName":              "抢单任务",
					"taskType":              1,
					"sponsor":               2499036607974745088,
					"referenceResourceType": 1,
					"referenceResourceItems": []map[string]interface{}{
						{"serialNumber": 1, "title": "问题标题1"},
						{"serialNumber": 2, "title": "问题标题2"},
						{"serialNumber": 3, "title": "问题标题3"},
					},
					"customerValue": []string{
						"口味",
						"色泽",
						"商务服务",
					},
					"taskNature":         "线",
					"suMoney":            1000.00,
					"acceptanceStandard": "验收标准",
					"taskDescription":    "任务描述",
					"taskPictureUrls": []string{
						"url-1",
						"url-2",
						"url-3",
					},
					"isRewardTake": false,
				}
				httpExpect.PUT("/tasks/1").
					WithJSON(body).
					Expect().
					Status(http.StatusOK).
					JSON().
					Object().
					ContainsKey("code").ValueEqual("code", 506).
					ContainsKey("msg").ValueEqual("msg", "业务逻辑错误:进行中的任务不允许编辑")
			})
		})
	})
	AfterEach(func() {
		_, err := pG.DB.Exec("DELETE FROM tasks WHERE true")
		Expect(err).NotTo(HaveOccurred())
	})
})