作者 王健

新增【公司 http and test code】

  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/command"
  6 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/query"
  7 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/service"
  8 +)
  9 +
  10 +type CompanyController struct {
  11 + beego.BaseController
  12 +}
  13 +
  14 +func (controller *CompanyController) CreateCompany() {
  15 + companyService := service.NewCompanyService(nil)
  16 + createCompanyCommand := &command.CreateCompanyCommand{}
  17 + controller.Unmarshal(createCompanyCommand)
  18 + data, err := companyService.CreateCompany(createCompanyCommand)
  19 + controller.Response(data, err)
  20 +}
  21 +
  22 +func (controller *CompanyController) UpdateCompany() {
  23 + companyService := service.NewCompanyService(nil)
  24 + updateCompanyCommand := &command.UpdateCompanyCommand{}
  25 + controller.Unmarshal(updateCompanyCommand)
  26 + companyId, _ := controller.GetInt64(":companyId")
  27 + updateCompanyCommand.CompanyId = companyId
  28 + data, err := companyService.UpdateCompany(updateCompanyCommand)
  29 + controller.Response(data, err)
  30 +}
  31 +
  32 +func (controller *CompanyController) GetCompany() {
  33 + companyService := service.NewCompanyService(nil)
  34 + getCompanyQuery := &query.GetCompanyQuery{}
  35 + companyId, _ := controller.GetInt64(":companyId")
  36 + getCompanyQuery.CompanyId = companyId
  37 + data, err := companyService.GetCompany(getCompanyQuery)
  38 + controller.Response(data, err)
  39 +}
  40 +
  41 +func (controller *CompanyController) RemoveCompany() {
  42 + companyService := service.NewCompanyService(nil)
  43 + removeCompanyCommand := &command.RemoveCompanyCommand{}
  44 + controller.Unmarshal(removeCompanyCommand)
  45 + companyId, _ := controller.GetInt64(":companyId")
  46 + removeCompanyCommand.CompanyId = companyId
  47 + data, err := companyService.RemoveCompany(removeCompanyCommand)
  48 + controller.Response(data, err)
  49 +}
  50 +
  51 +func (controller *CompanyController) ListCompany() {
  52 + companyService := service.NewCompanyService(nil)
  53 + listCompanyQuery := &query.ListCompanyQuery{}
  54 + offset, _ := controller.GetInt("offset")
  55 + listCompanyQuery.Offset = offset
  56 + limit, _ := controller.GetInt("limit")
  57 + listCompanyQuery.Limit = limit
  58 + data, err := companyService.ListCompany(listCompanyQuery)
  59 + controller.Response(data, err)
  60 +}
  61 +
  62 +func (controller *CompanyController) ConvertCompanyStatus() {
  63 + companyService := service.NewCompanyService(nil)
  64 + convertCompanyStatusCommand := &command.ConvertCompanyStatusCommand{}
  65 + controller.Unmarshal(convertCompanyStatusCommand)
  66 + data, err := companyService.ConvertCompanyStatus(convertCompanyStatusCommand)
  67 + controller.Response(data, err)
  68 +}
  69 +
  70 +func (controller *CompanyController) SetCompanyAdmin() {
  71 + companyService := service.NewCompanyService(nil)
  72 + setCompanyAdminCommand := &command.SetCompanyAdminCommand{}
  73 + controller.Unmarshal(setCompanyAdminCommand)
  74 + data, err := companyService.SetCompanyAdmin(setCompanyAdminCommand)
  75 + controller.Response(data, err)
  76 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/companies/", &controllers.CompanyController{}, "Post:CreateCompany")
  10 + web.Router("/companies/:companyId", &controllers.CompanyController{}, "Put:UpdateCompany")
  11 + web.Router("/companies/:companyId", &controllers.CompanyController{}, "Get:GetCompany")
  12 + web.Router("/companies/:companyId", &controllers.CompanyController{}, "Delete:RemoveCompany")
  13 + web.Router("/companies/", &controllers.CompanyController{}, "Get:ListCompany")
  14 + web.Router("/companies/convertCompanyStatus", &controllers.CompanyController{}, "Post:ConvertCompanyStatus")
  15 + web.Router("/companies/setCompanyAdmin", &controllers.CompanyController{}, "Post:SetCompanyAdmin")
  16 +}
  1 +package company
  2 +
  3 +import (
  4 + "net/http"
  5 + "net/http/httptest"
  6 + "testing"
  7 +
  8 + "github.com/beego/beego/v2/server/web"
  9 + . "github.com/onsi/ginkgo"
  10 + . "github.com/onsi/gomega"
  11 + _ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  12 + _ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego"
  13 +)
  14 +
  15 +func TestCompany(t *testing.T) {
  16 + RegisterFailHandler(Fail)
  17 + RunSpecs(t, "Beego Port Company Correlations Test Case Suite")
  18 +}
  19 +
  20 +var handler http.Handler
  21 +var server *httptest.Server
  22 +
  23 +var _ = BeforeSuite(func() {
  24 + handler = web.BeeApp.Handlers
  25 + server = httptest.NewServer(handler)
  26 +})
  27 +
  28 +var _ = AfterSuite(func() {
  29 + server.Close()
  30 +})
  1 +package company
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("公司状态转换 禁用、启用", func() {
  13 + var companyId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&companyId),
  17 + "INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
  18 + "testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("公司状态转换 禁用、启用", func() {
  22 + Context("", func() {
  23 + It("", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + body := map[string]interface{}{
  26 + "companyId": "int64",
  27 + "status": "int64",
  28 + }
  29 + httpExpect.POST("/companys/convertCompanyStatus").
  30 + WithJSON(body).
  31 + Expect().
  32 + Status(http.StatusOK).
  33 + JSON().
  34 + Object().
  35 + ContainsKey("code").ValueEqual("code", 0).
  36 + ContainsKey("msg").ValueEqual("msg", "ok").
  37 + ContainsKey("data").Value("data").Object()
  38 + })
  39 + })
  40 + })
  41 + AfterEach(func() {
  42 + _, err := pG.DB.Exec("DELETE FROM companys WHERE true")
  43 + Expect(err).NotTo(HaveOccurred())
  44 + })
  45 +})
  1 +package company
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("创建", func() {
  13 + Describe("提交数据创建", func() {
  14 + Context("提交正确的新公司信息数据", func() {
  15 + It("返回公司信息数据", func() {
  16 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  17 + body := map[string]interface{}{
  18 + "name": "string",
  19 + "abbreviation": "string",
  20 + "status": "int64",
  21 + "remarks": "string",
  22 + "userAccount": "string",
  23 + "userName": "string",
  24 + }
  25 + httpExpect.POST("/companys/").
  26 + WithJSON(body).
  27 + Expect().
  28 + Status(http.StatusOK).
  29 + JSON().
  30 + Object().
  31 + ContainsKey("code").ValueEqual("code", 0).
  32 + ContainsKey("msg").ValueEqual("msg", "ok").
  33 + ContainsKey("data").Value("data").Object().
  34 + ContainsKey("companyId").ValueNotEqual("companyId", BeZero())
  35 + })
  36 + })
  37 + })
  38 + AfterEach(func() {
  39 + _, err := pG.DB.Exec("DELETE FROM companys WHERE true")
  40 + Expect(err).NotTo(HaveOccurred())
  41 + })
  42 +})
  1 +package company
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("返回", func() {
  13 + var companyId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&companyId),
  17 + "INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
  18 + "testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据companyId参数返回公司信息", func() {
  22 + Context("传入有效的companyId", func() {
  23 + It("返回公司信息数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.GET("/companys/{companyId}").
  26 + Expect().
  27 + Status(http.StatusOK).
  28 + JSON().
  29 + Object().
  30 + ContainsKey("code").ValueEqual("code", 0).
  31 + ContainsKey("msg").ValueEqual("msg", "ok").
  32 + ContainsKey("data").Value("data").Object()
  33 + })
  34 + })
  35 + })
  36 + AfterEach(func() {
  37 + _, err := pG.DB.Exec("DELETE FROM companys WHERE true")
  38 + Expect(err).NotTo(HaveOccurred())
  39 + })
  40 +})
  1 +package company
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("返回列表", func() {
  13 + var companyId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&companyId),
  17 + "INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
  18 + "testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据参数返回公司信息列表", func() {
  22 + Context("传入有效的参数", func() {
  23 + It("返回公司信息数据列表", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.GET("/companys/").
  26 + WithQuery("offset", "int").
  27 + WithQuery("limit", "int").
  28 + Expect().
  29 + Status(http.StatusOK).
  30 + JSON().
  31 + Object().
  32 + ContainsKey("code").ValueEqual("code", 0).
  33 + ContainsKey("msg").ValueEqual("msg", "ok").
  34 + ContainsKey("data").Value("data").Object().
  35 + ContainsKey("count").ValueEqual("count", 1).
  36 + ContainsKey("companys").Value("companys").Array()
  37 + })
  38 + })
  39 + })
  40 + AfterEach(func() {
  41 + _, err := pG.DB.Exec("DELETE FROM companys WHERE true")
  42 + Expect(err).NotTo(HaveOccurred())
  43 + })
  44 +})
  1 +package company
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("移除", func() {
  13 + var companyId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&companyId),
  17 + "INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
  18 + "testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据参数移除", func() {
  22 + Context("传入有效的companyId", func() {
  23 + It("返回被移除公司信息的数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.DELETE("/companys/{companyId}").
  26 + Expect().
  27 + Status(http.StatusOK).
  28 + JSON().
  29 + Object().
  30 + ContainsKey("code").ValueEqual("code", 0).
  31 + ContainsKey("msg").ValueEqual("msg", "ok").
  32 + ContainsKey("data").Value("data").Object()
  33 + })
  34 + })
  35 + })
  36 + AfterEach(func() {
  37 + _, err := pG.DB.Exec("DELETE FROM companys WHERE true")
  38 + Expect(err).NotTo(HaveOccurred())
  39 + })
  40 +})
  1 +package company
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("设置公司管理员", func() {
  13 + var companyId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&companyId),
  17 + "INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
  18 + "testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("设置公司管理员", func() {
  22 + Context("", func() {
  23 + It("", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + body := map[string]interface{}{
  26 + "companyId": "int64",
  27 + "userAccount": "string",
  28 + "userName": "string",
  29 + }
  30 + httpExpect.POST("/companys/setCompanyAdmin").
  31 + WithJSON(body).
  32 + Expect().
  33 + Status(http.StatusOK).
  34 + JSON().
  35 + Object().
  36 + ContainsKey("code").ValueEqual("code", 0).
  37 + ContainsKey("msg").ValueEqual("msg", "ok").
  38 + ContainsKey("data").Value("data").Object()
  39 + })
  40 + })
  41 + })
  42 + AfterEach(func() {
  43 + _, err := pG.DB.Exec("DELETE FROM companys WHERE true")
  44 + Expect(err).NotTo(HaveOccurred())
  45 + })
  46 +})
  1 +package company
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("更新", func() {
  13 + var companyId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&companyId),
  17 + "INSERT INTO companys (company_id, company_info, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?) RETURNING company_id",
  18 + "testCompanyId", "testCompanyInfo", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("提交数据更新", func() {
  22 + Context("提交正确的公司信息数据", func() {
  23 + It("返回更新后的公司信息数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + body := map[string]interface{}{
  26 + "name": "string",
  27 + "abbreviation": "string",
  28 + "status": "int64",
  29 + "remarks": "string",
  30 + "userAccount": "string",
  31 + "userName": "string",
  32 + }
  33 + httpExpect.PUT("/companys/{companyId}").
  34 + WithJSON(body).
  35 + Expect().
  36 + Status(http.StatusOK).
  37 + JSON().
  38 + Object().
  39 + ContainsKey("code").ValueEqual("code", 0).
  40 + ContainsKey("msg").ValueEqual("msg", "ok").
  41 + ContainsKey("data").Value("data").Object().
  42 + ContainsKey("companyId").ValueEqual("companyId", companyId)
  43 + })
  44 + })
  45 + })
  46 + AfterEach(func() {
  47 + _, err := pG.DB.Exec("DELETE FROM companys WHERE true")
  48 + Expect(err).NotTo(HaveOccurred())
  49 + })
  50 +})