72 lines
2.7 KiB
Go
72 lines
2.7 KiB
Go
|
|
// 功能描述:合同申请答复的原气站归属、状态锁与幂等;版本:1.0.0。
|
|||
|
|
package gas
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"git.apinb.com/bsm-sdk/core/types"
|
|||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
|||
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
"gorm.io/driver/postgres"
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
"net/http/httptest"
|
|||
|
|
"strings"
|
|||
|
|
"testing"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func TestResolveContractRequestScopeAndState(t *testing.T) {
|
|||
|
|
for _, tc := range []struct {
|
|||
|
|
name string
|
|||
|
|
state int
|
|||
|
|
result string
|
|||
|
|
found, success, update bool
|
|||
|
|
}{
|
|||
|
|
{"待受理答复", 32, "", true, true, true}, {"处理中答复", 11, "", true, true, true},
|
|||
|
|
{"重复答复", 34, "已处理", true, true, false}, {"不得覆盖答复", 34, "原答复", true, false, false},
|
|||
|
|
{"取消后拒绝", 22, "", true, false, false}, {"其他气站", 0, "", false, false, false},
|
|||
|
|
} {
|
|||
|
|
t.Run(tc.name, func(t *testing.T) {
|
|||
|
|
connection, mock, err := sqlmock.New()
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatal(err)
|
|||
|
|
}
|
|||
|
|
defer connection.Close()
|
|||
|
|
db, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{})
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatal(err)
|
|||
|
|
}
|
|||
|
|
previous := impl.DBService
|
|||
|
|
impl.DBService = db
|
|||
|
|
defer func() { impl.DBService = previous }()
|
|||
|
|
mock.ExpectQuery(`SELECT .* FROM "gas_account"`).WillReturnRows(sqlmock.NewRows([]string{"id", "identity", "gas_basic_id"}).AddRow(2, "gas", 9))
|
|||
|
|
mock.ExpectQuery(`SELECT .* FROM "gas_basic"`).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(9))
|
|||
|
|
mock.ExpectBegin()
|
|||
|
|
rows := sqlmock.NewRows([]string{"id", "ticket_status", "result"})
|
|||
|
|
if tc.found {
|
|||
|
|
rows.AddRow(7, tc.state, tc.result)
|
|||
|
|
}
|
|||
|
|
mock.ExpectQuery(`SELECT .* FROM "cs_ticket".*identity = \$1 AND gas_basic_id = \$2 AND gasorder_contract_id <> 0 AND category = \$3 AND status <> \$4.*FOR UPDATE`).WithArgs("request", 9, "contract_change", 3, 1).WillReturnRows(rows)
|
|||
|
|
if tc.update {
|
|||
|
|
mock.ExpectExec(`UPDATE "cs_ticket" SET .*"result"=.*"ticket_status"=`).WillReturnResult(sqlmock.NewResult(0, 1))
|
|||
|
|
}
|
|||
|
|
if tc.success {
|
|||
|
|
mock.ExpectCommit()
|
|||
|
|
} else {
|
|||
|
|
mock.ExpectRollback()
|
|||
|
|
}
|
|||
|
|
response := httptest.NewRecorder()
|
|||
|
|
ctx, _ := gin.CreateTestContext(response)
|
|||
|
|
ctx.Set("Auth", &types.JwtClaims{Client: "gas_admin", Identity: "gas"})
|
|||
|
|
ctx.Params = gin.Params{{Key: "identity", Value: "request"}}
|
|||
|
|
ctx.Request = httptest.NewRequest("POST", "/requests", strings.NewReader(`{"result":"已处理"}`))
|
|||
|
|
ctx.Request.Header.Set("Content-Type", "application/json")
|
|||
|
|
ResolveContractRequest(ctx)
|
|||
|
|
if strings.Contains(response.Body.String(), `"code":0`) != tc.success {
|
|||
|
|
t.Fatal(response.Body.String())
|
|||
|
|
}
|
|||
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|||
|
|
t.Fatal(err)
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
}
|