From 5cc04d9e0c2e5dd0e3fc946aab9b20f6706c39b2 Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Fri, 2 Aug 2024 21:21:10 +0800 Subject: [PATCH 01/10] feat: add history api --- cmd/root.go | 2 +- pkg/convert.go | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ pkg/server.go | 90 ++++++++++++++++++++++++++---- pkg/types.go | 40 ++++++++++++++ 4 files changed, 266 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 0fe8a36..de3ac7e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,7 +39,7 @@ func NewRootCommand() (c *cobra.Command) { func (o *option) runE(c *cobra.Command, args []string) (err error) { if o.version { c.Println(version.GetVersion()) - c.Println(version.GetDate()) + //c.Println(version.GetDate()) return } remoteServer := pkg.NewRemoteServer() diff --git a/pkg/convert.go b/pkg/convert.go index 62a58cb..e57c7e7 100644 --- a/pkg/convert.go +++ b/pkg/convert.go @@ -17,9 +17,12 @@ package pkg import ( "encoding/json" + "fmt" + "time" "github.com/linuxsuren/api-testing/pkg/server" "github.com/linuxsuren/api-testing/pkg/testing/remote" + "google.golang.org/protobuf/types/known/timestamppb" ) func ConverToDBTestCase(testcase *server.TestCase) (result *TestCase) { @@ -91,6 +94,105 @@ func ConvertToDBTestSuite(suite *remote.TestSuite) (result *TestSuite) { return } +func ConvertToDBHistoryTestResult(historyTestResult *server.HistoryTestResult) (result *HistoryTestResult) { + id := fmt.Sprintf("%s_%s_%s", historyTestResult.CreateTime.AsTime().Local().Format("2006-01-02T15:04:05.999999999"), historyTestResult.Data.SuiteName, historyTestResult.Data.CaseName) + result = &HistoryTestResult{ + ID: id, + Message: historyTestResult.Message, + Error: historyTestResult.Error, + HistorySuiteName: historyTestResult.CreateTime.AsTime().Local().Format("2006-1-2"), + CaseName: historyTestResult.Data.CaseName, + SuiteName: historyTestResult.Data.SuiteName, + SuiteAPI: historyTestResult.Data.SuiteApi, + Param: pairToJSON(historyTestResult.Data.SuiteParam), + CreateTime: historyTestResult.CreateTime.AsTime().Local().Format("2006-01-02T15:04:05.999999999"), + } + for _, testCase := range historyTestResult.TestCaseResult { + result.StatusCode = int32(testCase.StatusCode) + result.Output = testCase.Output + result.Body = testCase.Body + } + + request := historyTestResult.Data.Request + if request != nil { + result.CaseAPI = request.Api + result.Method = request.Method + result.Header = pairToJSON(request.Header) + result.Cookie = pairToJSON(request.Cookie) + result.Form = pairToJSON(request.Form) + result.Query = pairToJSON(request.Query) + } + + resp := historyTestResult.Data.Response + if resp != nil { + result.ExpectBody = resp.Body + result.ExpectSchema = resp.Schema + result.ExpectStatusCode = int(resp.StatusCode) + result.ExpectHeader = pairToJSON(resp.Header) + result.ExpectBodyFields = pairToJSON(resp.BodyFieldsExpect) + result.ExpectVerify = SliceToJSON(resp.Verify) + } + + if historyTestResult.Data.SuiteSpec != nil { + result.SpecKind = historyTestResult.Data.SuiteSpec.Kind + result.SpecURL = historyTestResult.Data.SuiteSpec.Url + } + + return +} + +func ConvertToRemoteHistoryTestResult(historyTestResult *HistoryTestResult) (result *server.HistoryTestResult) { + createTime, err := time.Parse("2006-01-02T15:04:05.999999999", historyTestResult.CreateTime) + if err != nil { + fmt.Println("Error parsing time:", err) + } + result = &server.HistoryTestResult{ + Message: historyTestResult.Message, + Error: historyTestResult.Error, + CreateTime: timestamppb.New(createTime), + + Data: &server.HistoryTestCase{ + HistorySuiteName: historyTestResult.HistorySuiteName, + CaseName: historyTestResult.CaseName, + CreateTime: timestamppb.New(createTime), + SuiteName: historyTestResult.SuiteName, + SuiteApi: historyTestResult.SuiteAPI, + SuiteParam: jsonToPair(historyTestResult.Param), + SuiteSpec: &server.APISpec{ + Kind: historyTestResult.SpecKind, + Url: historyTestResult.SpecURL, + }, + Request: &server.Request{ + Api: historyTestResult.CaseAPI, + Method: historyTestResult.Method, + Body: historyTestResult.Body, + Header: jsonToPair(historyTestResult.Header), + Cookie: jsonToPair(historyTestResult.Cookie), + Query: jsonToPair(historyTestResult.Query), + Form: jsonToPair(historyTestResult.Form), + }, + + Response: &server.Response{ + StatusCode: int32(historyTestResult.ExpectStatusCode), + Body: historyTestResult.ExpectBody, + Schema: historyTestResult.ExpectSchema, + Verify: jsonToSlice(historyTestResult.ExpectVerify), + BodyFieldsExpect: jsonToPair(historyTestResult.ExpectBodyFields), + Header: jsonToPair(historyTestResult.ExpectHeader), + }, + }, + } + TestCaseResult := &server.TestCaseResult{ + StatusCode: historyTestResult.StatusCode, + Body: historyTestResult.Body, + Output: historyTestResult.Output, + Error: historyTestResult.Error, + Header: jsonToPair(historyTestResult.Header), + } + result.TestCaseResult = append(result.TestCaseResult, TestCaseResult) + return +} + func ConvertToGRPCTestSuite(suite *TestSuite) (result *remote.TestSuite) { result = &remote.TestSuite{ Name: suite.Name, @@ -104,6 +206,51 @@ func ConvertToGRPCTestSuite(suite *TestSuite) (result *remote.TestSuite) { return } +func ConvertToGRPCHistoryTestSuite(historyTestResult *HistoryTestResult) (result *remote.HistoryTestSuite) { + createTime, err := time.Parse("2006-01-02T15:04:05.999999999", historyTestResult.CreateTime) + if err != nil { + fmt.Println("Error parsing time:", err) + } + result = &remote.HistoryTestSuite{ + HistorySuiteName: historyTestResult.HistorySuiteName, + } + item := &server.HistoryTestCase{ + ID: historyTestResult.ID, + SuiteName: historyTestResult.SuiteName, + CaseName: historyTestResult.CaseName, + SuiteApi: historyTestResult.SuiteAPI, + SuiteParam: jsonToPair(historyTestResult.Param), + HistorySuiteName: historyTestResult.HistorySuiteName, + CreateTime: timestamppb.New(createTime), + + SuiteSpec: &server.APISpec{ + Kind: historyTestResult.SpecKind, + Url: historyTestResult.SpecURL, + }, + + Request: &server.Request{ + Api: historyTestResult.CaseAPI, + Method: historyTestResult.Method, + Body: historyTestResult.Body, + Header: jsonToPair(historyTestResult.Header), + Cookie: jsonToPair(historyTestResult.Cookie), + Query: jsonToPair(historyTestResult.Query), + Form: jsonToPair(historyTestResult.Form), + }, + + Response: &server.Response{ + StatusCode: int32(historyTestResult.ExpectStatusCode), + Body: historyTestResult.ExpectBody, + Schema: historyTestResult.ExpectSchema, + Verify: jsonToSlice(historyTestResult.ExpectVerify), + BodyFieldsExpect: jsonToPair(historyTestResult.ExpectBodyFields), + Header: jsonToPair(historyTestResult.ExpectHeader), + }, + } + result.Items = append(result.Items, item) + return +} + func SliceToJSON(slice []string) (result string) { var data []byte var err error diff --git a/pkg/server.go b/pkg/server.go index 7496159..5472b11 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -19,9 +19,6 @@ import ( "context" "errors" "fmt" - "log" - "strings" - "github.com/linuxsuren/api-testing/pkg/extension" "github.com/linuxsuren/api-testing/pkg/server" "github.com/linuxsuren/api-testing/pkg/testing/remote" @@ -32,6 +29,8 @@ import ( "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" + "log" + "strings" ) type dbserver struct { @@ -49,7 +48,7 @@ func createDB(user, password, address, database, driver string) (db *gorm.DB, er var dsn string switch driver { case "mysql", "": - dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", user, password, address, database) + dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true", user, password, address, database) dialector = mysql.Open(dsn) case "sqlite": dsn = fmt.Sprintf("%s.db", database) @@ -78,6 +77,7 @@ func createDB(user, password, address, database, driver string) (db *gorm.DB, er db.AutoMigrate(&TestCase{}) db.AutoMigrate(&TestSuite{}) + db.AutoMigrate(&HistoryTestResult{}) return } @@ -165,6 +165,19 @@ func (s *dbserver) GetTestSuite(ctx context.Context, suite *remote.TestSuite) (r return } +func (s *dbserver) GetHistoryTestSuite(ctx context.Context, suite *remote.HistoryTestSuite) (reply *remote.HistoryTestSuite, err error) { + query := &HistoryTestResult{} + var db *gorm.DB + if db, err = s.getClient(ctx); err != nil { + return + } + + db.Find(&query, nameQuery, suite.HistorySuiteName) + + reply = ConvertToGRPCHistoryTestSuite(query) + return +} + func (s *dbserver) UpdateTestSuite(ctx context.Context, suite *remote.TestSuite) (reply *remote.TestSuite, err error) { reply = &remote.TestSuite{} input := ConvertToDBTestSuite(suite) @@ -218,6 +231,47 @@ func (s *dbserver) CreateTestCase(ctx context.Context, testcase *server.TestCase return } +func (s *dbserver) CreateTestCaseHistory(ctx context.Context, historyTestResult *server.HistoryTestResult) (reply *server.Empty, err error) { + reply = &server.Empty{} + var db *gorm.DB + if db, err = s.getClient(ctx); err != nil { + return + } + + db.Create(ConvertToDBHistoryTestResult(historyTestResult)) + return +} + +func (s *dbserver) ListHistoryTestSuite(ctx context.Context, _ *server.Empty) (suites *remote.HistoryTestSuites, err error) { + items := make([]*HistoryTestResult, 0) + + var db *gorm.DB + if db, err = s.getClient(ctx); err != nil { + return + } + + db.Find(&items) + + groupedItems := make(map[string][]*HistoryTestResult) + for _, item := range items { + groupedItems[item.HistorySuiteName] = append(groupedItems[item.HistorySuiteName], item) + } + + suites = &remote.HistoryTestSuites{} + + for historySuiteName, group := range groupedItems { + suite := &remote.HistoryTestSuite{ + HistorySuiteName: historySuiteName, + } + for _, item := range group { + converted := ConvertToGRPCHistoryTestSuite(item) + suite.Items = append(suite.Items, converted.Items[0]) + } + suites.Data = append(suites.Data, suite) + } + return +} + func (s *dbserver) GetTestCase(ctx context.Context, testcase *server.TestCase) (result *server.TestCase, err error) { item := &TestCase{} var db *gorm.DB @@ -230,6 +284,18 @@ func (s *dbserver) GetTestCase(ctx context.Context, testcase *server.TestCase) ( return } +func (s *dbserver) GetHistoryTestCase(ctx context.Context, testcase *server.HistoryTestCase) (result *server.HistoryTestResult, err error) { + item := &HistoryTestResult{} + var db *gorm.DB + if db, err = s.getClient(ctx); err != nil { + return + } + db.Find(&item, "id = ? ", testcase.ID) + + result = ConvertToRemoteHistoryTestResult(item) + return +} + func (s *dbserver) UpdateTestCase(ctx context.Context, testcase *server.TestCase) (reply *server.TestCase, err error) { reply = &server.TestCase{} input := ConverToDBTestCase(testcase) @@ -274,14 +340,14 @@ func (s *dbserver) Verify(ctx context.Context, in *server.Empty) (reply *server. return } -func (s *dbserver) GetVersion(context.Context, *server.Empty) (ver *server.Version, err error) { - ver = &server.Version{ - Version: version.GetVersion(), - Commit: version.GetCommit(), - Date: version.GetDate(), - } - return -} +// func (s *dbserver) GetVersion(context.Context, *server.Empty) (ver *server.Version, err error) { +// ver = &server.Version{ +// Version: version.GetVersion(), +// Commit: version.GetCommit(), +// Date: version.GetDate(), +// } +// return +// } func (s *dbserver) PProf(ctx context.Context, in *server.PProfRequest) (data *server.PProfData, err error) { log.Println("pprof", in.Name) diff --git a/pkg/types.go b/pkg/types.go index e50db11..4edac73 100644 --- a/pkg/types.go +++ b/pkg/types.go @@ -26,3 +26,43 @@ type TestSuite struct { SpecURL string Param string } + +type HistoryTestSuite struct { + Name string +} + +type HistoryTestResult struct { + ID string `gorm:"primaryKey"` + HistorySuiteName string + CreateTime string + + //suite information + SuiteName string `json:"suiteName"` + SuiteAPI string + SpecKind string + SpecURL string + Param string + + //case information + CaseName string `json:"caseName"` + CaseAPI string + Method string + Body string + Header string + Cookie string + Query string + Form string + + ExpectStatusCode int + ExpectBody string + ExpectSchema string + ExpectHeader string + ExpectBodyFields string + ExpectVerify string + + //result information + Message string `json:"message"` + Error string `json:"error"` + StatusCode int32 `json:"statusCode"` + Output string `json:"output"` +} From ef74ea3219f8879c6f3ba7214953c7bbd205dfbe Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Sun, 4 Aug 2024 21:00:43 +0800 Subject: [PATCH 02/10] feat: add history delete and run api --- pkg/convert.go | 48 ++++++++++++------------------------------------ pkg/server.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/pkg/convert.go b/pkg/convert.go index e57c7e7..4f12a86 100644 --- a/pkg/convert.go +++ b/pkg/convert.go @@ -150,37 +150,6 @@ func ConvertToRemoteHistoryTestResult(historyTestResult *HistoryTestResult) (res Message: historyTestResult.Message, Error: historyTestResult.Error, CreateTime: timestamppb.New(createTime), - - Data: &server.HistoryTestCase{ - HistorySuiteName: historyTestResult.HistorySuiteName, - CaseName: historyTestResult.CaseName, - CreateTime: timestamppb.New(createTime), - SuiteName: historyTestResult.SuiteName, - SuiteApi: historyTestResult.SuiteAPI, - SuiteParam: jsonToPair(historyTestResult.Param), - SuiteSpec: &server.APISpec{ - Kind: historyTestResult.SpecKind, - Url: historyTestResult.SpecURL, - }, - Request: &server.Request{ - Api: historyTestResult.CaseAPI, - Method: historyTestResult.Method, - Body: historyTestResult.Body, - Header: jsonToPair(historyTestResult.Header), - Cookie: jsonToPair(historyTestResult.Cookie), - Query: jsonToPair(historyTestResult.Query), - Form: jsonToPair(historyTestResult.Form), - }, - - Response: &server.Response{ - StatusCode: int32(historyTestResult.ExpectStatusCode), - Body: historyTestResult.ExpectBody, - Schema: historyTestResult.ExpectSchema, - Verify: jsonToSlice(historyTestResult.ExpectVerify), - BodyFieldsExpect: jsonToPair(historyTestResult.ExpectBodyFields), - Header: jsonToPair(historyTestResult.ExpectHeader), - }, - }, } TestCaseResult := &server.TestCaseResult{ StatusCode: historyTestResult.StatusCode, @@ -190,6 +159,7 @@ func ConvertToRemoteHistoryTestResult(historyTestResult *HistoryTestResult) (res Header: jsonToPair(historyTestResult.Header), } result.TestCaseResult = append(result.TestCaseResult, TestCaseResult) + result.Data = ConvertToGRPCHistoryTestCase(historyTestResult) return } @@ -207,14 +177,21 @@ func ConvertToGRPCTestSuite(suite *TestSuite) (result *remote.TestSuite) { } func ConvertToGRPCHistoryTestSuite(historyTestResult *HistoryTestResult) (result *remote.HistoryTestSuite) { + result = &remote.HistoryTestSuite{ + HistorySuiteName: historyTestResult.HistorySuiteName, + } + + item := ConvertToGRPCHistoryTestCase(historyTestResult) + result.Items = append(result.Items, item) + return +} + +func ConvertToGRPCHistoryTestCase(historyTestResult *HistoryTestResult) (result *server.HistoryTestCase) { createTime, err := time.Parse("2006-01-02T15:04:05.999999999", historyTestResult.CreateTime) if err != nil { fmt.Println("Error parsing time:", err) } - result = &remote.HistoryTestSuite{ - HistorySuiteName: historyTestResult.HistorySuiteName, - } - item := &server.HistoryTestCase{ + result = &server.HistoryTestCase{ ID: historyTestResult.ID, SuiteName: historyTestResult.SuiteName, CaseName: historyTestResult.CaseName, @@ -247,7 +224,6 @@ func ConvertToGRPCHistoryTestSuite(historyTestResult *HistoryTestResult) (result Header: jsonToPair(historyTestResult.ExpectHeader), }, } - result.Items = append(result.Items, item) return } diff --git a/pkg/server.go b/pkg/server.go index 5472b11..20e8881 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -284,7 +284,7 @@ func (s *dbserver) GetTestCase(ctx context.Context, testcase *server.TestCase) ( return } -func (s *dbserver) GetHistoryTestCase(ctx context.Context, testcase *server.HistoryTestCase) (result *server.HistoryTestResult, err error) { +func (s *dbserver) GetHistoryTestCaseWithResult(ctx context.Context, testcase *server.HistoryTestCase) (result *server.HistoryTestResult, err error) { item := &HistoryTestResult{} var db *gorm.DB if db, err = s.getClient(ctx); err != nil { @@ -296,6 +296,18 @@ func (s *dbserver) GetHistoryTestCase(ctx context.Context, testcase *server.Hist return } +func (s *dbserver) GetHistoryTestCase(ctx context.Context, testcase *server.HistoryTestCase) (result *server.HistoryTestCase, err error) { + item := &HistoryTestResult{} + var db *gorm.DB + if db, err = s.getClient(ctx); err != nil { + return + } + db.Find(&item, "id = ? ", testcase.ID) + + result = ConvertToGRPCHistoryTestCase(item) + return +} + func (s *dbserver) UpdateTestCase(ctx context.Context, testcase *server.TestCase) (reply *server.TestCase, err error) { reply = &server.TestCase{} input := ConverToDBTestCase(testcase) @@ -330,6 +342,19 @@ func (s *dbserver) DeleteTestCase(ctx context.Context, testcase *server.TestCase return } +func (s *dbserver) DeleteHistoryTestCase(ctx context.Context, historyTestCase *server.HistoryTestCase) (reply *server.Empty, err error) { + reply = &server.Empty{} + input := &HistoryTestResult{ + ID: historyTestCase.ID, + } + var db *gorm.DB + if db, err = s.getClient(ctx); err != nil { + return + } + historyTestCaseIdentiy(db, input).Delete(input) + return +} + func (s *dbserver) Verify(ctx context.Context, in *server.Empty) (reply *server.ExtensionStatus, err error) { _, vErr := s.ListTestSuite(ctx, in) reply = &server.ExtensionStatus{ @@ -361,3 +386,7 @@ func (s *dbserver) PProf(ctx context.Context, in *server.PProfRequest) (data *se func testCaseIdentiy(db *gorm.DB, testcase *TestCase) *gorm.DB { return db.Model(testcase).Where(fmt.Sprintf("suite_name = '%s' AND name = '%s'", testcase.SuiteName, testcase.Name)) } + +func historyTestCaseIdentiy(db *gorm.DB, historyTestResult *HistoryTestResult) *gorm.DB { + return db.Model(historyTestResult).Where(fmt.Sprintf("id = '%s'", historyTestResult.ID)) +} From 413077624af5fbb3742b68380bac04261dc05974 Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Thu, 8 Aug 2024 12:53:03 +0800 Subject: [PATCH 03/10] feat: add get testcase all history --- pkg/convert.go | 27 +++++++++++++++++++++++++++ pkg/server.go | 15 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pkg/convert.go b/pkg/convert.go index 4f12a86..6b5e7f8 100644 --- a/pkg/convert.go +++ b/pkg/convert.go @@ -79,6 +79,33 @@ func ConvertToRemoteTestCase(testcase *TestCase) (result *server.TestCase) { return } +func ConvertHistoryToRemoteTestCase(historyTestcase *HistoryTestResult) (result *server.TestCase) { + result = &server.TestCase{ + Name: historyTestcase.CaseName, + SuiteName: historyTestcase.SuiteName, + + Request: &server.Request{ + Api: historyTestcase.CaseAPI, + Method: historyTestcase.Method, + Body: historyTestcase.Body, + Header: jsonToPair(historyTestcase.Header), + Cookie: jsonToPair(historyTestcase.Cookie), + Query: jsonToPair(historyTestcase.Query), + Form: jsonToPair(historyTestcase.Form), + }, + + Response: &server.Response{ + StatusCode: int32(historyTestcase.ExpectStatusCode), + Body: historyTestcase.ExpectBody, + Schema: historyTestcase.ExpectSchema, + Verify: jsonToSlice(historyTestcase.ExpectVerify), + BodyFieldsExpect: jsonToPair(historyTestcase.ExpectBodyFields), + Header: jsonToPair(historyTestcase.ExpectHeader), + }, + } + return +} + func ConvertToDBTestSuite(suite *remote.TestSuite) (result *TestSuite) { result = &TestSuite{ Name: suite.Name, diff --git a/pkg/server.go b/pkg/server.go index 20e8881..b58f39f 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -308,6 +308,21 @@ func (s *dbserver) GetHistoryTestCase(ctx context.Context, testcase *server.Hist return } +func (s *dbserver) GetTestCaseAllHistory(ctx context.Context, testcase *server.TestCase) (result *server.HistoryTestCases, err error) { + items := make([]*HistoryTestResult, 0) + var db *gorm.DB + if db, err = s.getClient(ctx); err != nil { + return + } + db.Find(&items, "suite_name = ? AND case_name = ? ", testcase.SuiteName, testcase.Name) + + result = &server.HistoryTestCases{} + for i := range items { + result.Data = append(result.Data, ConvertToGRPCHistoryTestCase(items[i])) + } + return +} + func (s *dbserver) UpdateTestCase(ctx context.Context, testcase *server.TestCase) (reply *server.TestCase, err error) { reply = &server.TestCase{} input := ConverToDBTestCase(testcase) From 61d28b05a863af7918c994eff6d843c7594d79ee Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Fri, 9 Aug 2024 11:23:36 +0800 Subject: [PATCH 04/10] feat: add limit history testcase amount --- cmd/root.go | 2 +- go.mod | 12 +++++++----- pkg/server.go | 33 +++++++++++++++++++++++++-------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index de3ac7e..0fe8a36 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,7 +39,7 @@ func NewRootCommand() (c *cobra.Command) { func (o *option) runE(c *cobra.Command, args []string) (err error) { if o.version { c.Println(version.GetVersion()) - //c.Println(version.GetDate()) + c.Println(version.GetDate()) return } remoteServer := pkg.NewRemoteServer() diff --git a/go.mod b/go.mod index cf78e7f..3c73bd0 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,14 @@ module github.com/linuxsuren/atest-ext-store-orm -go 1.22.2 +go 1.22.4 -toolchain go1.22.4 +toolchain go1.22.6 require ( github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 + google.golang.org/protobuf v1.33.0 gorm.io/driver/mysql v1.5.2 gorm.io/driver/postgres v1.5.4 gorm.io/driver/sqlite v1.5.6 @@ -53,6 +54,7 @@ require ( github.com/jinzhu/now v1.1.5 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/linuxsuren/go-fake-runtime v0.0.4 // indirect + github.com/linuxsuren/oauth-hub v0.0.0-20240809060240-e78c21b5d8d4 // indirect github.com/linuxsuren/unstructured v0.0.1 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-sqlite3 v1.14.22 // indirect @@ -85,15 +87,15 @@ require ( go.uber.org/zap v1.27.0 // indirect golang.org/x/crypto v0.21.0 // indirect golang.org/x/net v0.23.0 // indirect - golang.org/x/oauth2 v0.18.0 // indirect + golang.org/x/oauth2 v0.22.0 // indirect golang.org/x/sync v0.6.0 // indirect golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect - google.golang.org/appengine v1.6.8 // indirect google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect - google.golang.org/protobuf v1.33.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/linuxsuren/api-testing => github.com/SamYSF/api-testing v0.0.0-20240827074726-d0b782cb43d6 diff --git a/pkg/server.go b/pkg/server.go index b58f39f..367aec7 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -30,6 +30,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/logger" "log" + "strconv" "strings" ) @@ -238,6 +239,22 @@ func (s *dbserver) CreateTestCaseHistory(ctx context.Context, historyTestResult return } + store := remote.GetStoreFromContext(ctx) + historyLimit := 10 + if v, ok := store.Properties["historyLimit"]; ok { + if parsedHistoryLimit, parseErr := strconv.Atoi(v); parseErr == nil { + historyLimit = parsedHistoryLimit + } + } + + var count int64 + db.Model(&HistoryTestResult{}).Count(&count) + + if count >= int64(historyLimit) { + fmt.Printf("Existing count: %d, limit: %d\nmaximum number of entries reached, cannot create new TestCaseHistory\n", count, historyLimit) + return + } + db.Create(ConvertToDBHistoryTestResult(historyTestResult)) return } @@ -380,14 +397,14 @@ func (s *dbserver) Verify(ctx context.Context, in *server.Empty) (reply *server. return } -// func (s *dbserver) GetVersion(context.Context, *server.Empty) (ver *server.Version, err error) { -// ver = &server.Version{ -// Version: version.GetVersion(), -// Commit: version.GetCommit(), -// Date: version.GetDate(), -// } -// return -// } +func (s *dbserver) GetVersion(context.Context, *server.Empty) (ver *server.Version, err error) { + ver = &server.Version{ + Version: version.GetVersion(), + Commit: version.GetCommit(), + Date: version.GetDate(), + } + return +} func (s *dbserver) PProf(ctx context.Context, in *server.PProfRequest) (data *server.PProfData, err error) { log.Println("pprof", in.Name) From 65a5ad7df802c653e1312e22f7c7abb8a76d1608 Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Wed, 4 Sep 2024 13:11:55 +0800 Subject: [PATCH 05/10] feat: delete testcase all history --- pkg/server.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/pkg/server.go b/pkg/server.go index 367aec7..4e45ba3 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -347,7 +347,7 @@ func (s *dbserver) UpdateTestCase(ctx context.Context, testcase *server.TestCase if db, err = s.getClient(ctx); err != nil { return } - testCaseIdentiy(db, input).Updates(input) + testCaseIdentity(db, input).Updates(input) data := make(map[string]interface{}) if input.ExpectBody == "" { @@ -358,7 +358,7 @@ func (s *dbserver) UpdateTestCase(ctx context.Context, testcase *server.TestCase } if len(data) > 0 { - testCaseIdentiy(db, input).Updates(data) + testCaseIdentity(db, input).Updates(data) } return } @@ -370,7 +370,7 @@ func (s *dbserver) DeleteTestCase(ctx context.Context, testcase *server.TestCase if db, err = s.getClient(ctx); err != nil { return } - testCaseIdentiy(db, input).Delete(input) + testCaseIdentity(db, input).Delete(input) return } @@ -383,7 +383,21 @@ func (s *dbserver) DeleteHistoryTestCase(ctx context.Context, historyTestCase *s if db, err = s.getClient(ctx); err != nil { return } - historyTestCaseIdentiy(db, input).Delete(input) + historyTestCaseIdentity(db, input).Delete(input) + return +} + +func (s *dbserver) DeleteAllHistoryTestCase(ctx context.Context, historyTestCase *server.HistoryTestCase) (reply *server.Empty, err error) { + reply = &server.Empty{} + input := &HistoryTestResult{ + SuiteName: historyTestCase.SuiteName, + CaseName: historyTestCase.CaseName, + } + var db *gorm.DB + if db, err = s.getClient(ctx); err != nil { + return + } + allHistoryTestCaseIdentity(db, input).Delete(input) return } @@ -415,10 +429,14 @@ func (s *dbserver) PProf(ctx context.Context, in *server.PProfRequest) (data *se return } -func testCaseIdentiy(db *gorm.DB, testcase *TestCase) *gorm.DB { +func testCaseIdentity(db *gorm.DB, testcase *TestCase) *gorm.DB { return db.Model(testcase).Where(fmt.Sprintf("suite_name = '%s' AND name = '%s'", testcase.SuiteName, testcase.Name)) } -func historyTestCaseIdentiy(db *gorm.DB, historyTestResult *HistoryTestResult) *gorm.DB { +func historyTestCaseIdentity(db *gorm.DB, historyTestResult *HistoryTestResult) *gorm.DB { return db.Model(historyTestResult).Where(fmt.Sprintf("id = '%s'", historyTestResult.ID)) } + +func allHistoryTestCaseIdentity(db *gorm.DB, historyTestResult *HistoryTestResult) *gorm.DB { + return db.Model(historyTestResult).Where(fmt.Sprintf("suite_name = '%s' AND case_name = '%s'", historyTestResult.SuiteName, historyTestResult.CaseName)) +} From 81ca301124aebc4661c9b3361a97a53fa158d4ec Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Wed, 4 Sep 2024 18:15:21 +0800 Subject: [PATCH 06/10] fix: Replace the oldest history record with the latest one. --- pkg/server.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/server.go b/pkg/server.go index 4e45ba3..d991261 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -251,8 +251,17 @@ func (s *dbserver) CreateTestCaseHistory(ctx context.Context, historyTestResult db.Model(&HistoryTestResult{}).Count(&count) if count >= int64(historyLimit) { - fmt.Printf("Existing count: %d, limit: %d\nmaximum number of entries reached, cannot create new TestCaseHistory\n", count, historyLimit) - return + var oldestRecord HistoryTestResult + if err = db.Order("create_time").First(&oldestRecord).Error; err != nil { + fmt.Printf("Error find oldest record: %v\n", err) + return + } + + if err = db.Delete(&oldestRecord).Error; err != nil { + fmt.Printf("Error delete oldest record: %v\n", err) + return + } + fmt.Printf("Existing count: %d, limit: %d\nmaximum number of entries reached.\n", count, historyLimit) } db.Create(ConvertToDBHistoryTestResult(historyTestResult)) From 76219565a0ccca02afc3d58de4c630df0b43f84a Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Wed, 11 Sep 2024 17:19:26 +0800 Subject: [PATCH 07/10] feat: add delete relate file --- go.mod | 3 ++- go.sum | 19 ++++++------------- pkg/server.go | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 3c73bd0..a8e0246 100644 --- a/go.mod +++ b/go.mod @@ -98,4 +98,5 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/linuxsuren/api-testing => github.com/SamYSF/api-testing v0.0.0-20240827074726-d0b782cb43d6 +replace github.com/linuxsuren/api-testing => github.com/SamYSF/api-testing v0.0.0-20240911062137-52c9a040fe24 +//replace github.com/linuxsuren/api-testing => /home/ysf/project/api-testing diff --git a/go.sum b/go.sum index 4347643..59d7fd6 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0 github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA= github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM= +github.com/SamYSF/api-testing v0.0.0-20240911062137-52c9a040fe24 h1:7lDwr+elqc46aIqGW8fi6kWCdlrufPborAi1DasUPm0= +github.com/SamYSF/api-testing v0.0.0-20240911062137-52c9a040fe24/go.mod h1:uu0hyEYSgdSju31EugGwIGE2I/uv7VYNM/6ODPKKD7I= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -47,11 +49,8 @@ github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+Gr github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -96,10 +95,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07 h1:NmjqkiR+4KXjWs9H6wGmgQo7r7FJE+RED+E3EwAGsp4= -github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07/go.mod h1:8G3GZtQmSUHyqA/gZlSdFaDIveHQYbFInTibD7g3hMs= github.com/linuxsuren/go-fake-runtime v0.0.4 h1:y+tvBuw6MKTCav8Bo5HWwaXhBx1Z//VAvqI3gpOWqvw= github.com/linuxsuren/go-fake-runtime v0.0.4/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ= +github.com/linuxsuren/oauth-hub v0.0.0-20240809060240-e78c21b5d8d4 h1:muVmKxx+JneaVgUKHqLc+As5vpgKXZAfVu6h+iyb5LQ= +github.com/linuxsuren/oauth-hub v0.0.0-20240809060240-e78c21b5d8d4/go.mod h1:6K1L5ajpFTNO8iJSsNrxMWAigAqczI0UPfEV9NSE0nc= github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU= github.com/linuxsuren/unstructured v0.0.1/go.mod h1:KH6aTj+FegzGBzc1vS6mzZx3/duhTUTEVyW5sO7p4as= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= @@ -205,8 +204,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= -golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI= -golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8= +golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA= +golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= @@ -225,7 +224,6 @@ golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= @@ -233,9 +231,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM= -google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 h1:KAeGQVN3M9nD0/bQXnr/ClcEMJ968gUXJQ9pwfSynuQ= google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80/go.mod h1:cc8bqMqtv9gMOr0zHg2Vzff5ULhhL2IXP4sbcn32Dro= google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 h1:Lj5rbfG876hIAYFjqiJnPHfhXbv+nzTWfm04Fg/XSVU= @@ -244,8 +239,6 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 h1: google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s= google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk= google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/pkg/server.go b/pkg/server.go index d991261..845be2a 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -30,6 +30,8 @@ import ( "gorm.io/gorm" "gorm.io/gorm/logger" "log" + "os" + "path/filepath" "strconv" "strings" ) @@ -392,7 +394,21 @@ func (s *dbserver) DeleteHistoryTestCase(ctx context.Context, historyTestCase *s if db, err = s.getClient(ctx); err != nil { return } - historyTestCaseIdentity(db, input).Delete(input) + var historyTestResult HistoryTestResult + if err = historyTestCaseIdentity(db, input).Find(&historyTestResult).Error; err != nil { + return nil, err + } + fileName := historyTestResult.Body + if strings.HasPrefix(fileName, "isFilePath-") { + tempDir := os.TempDir() + fullFilePath := filepath.Join(tempDir, fileName) + + if err = os.Remove(fullFilePath); err != nil { + log.Printf("Failed to delete file: %s, error: %v\n", fullFilePath, err) + } + } + + db.Delete(&historyTestResult) return } @@ -406,7 +422,23 @@ func (s *dbserver) DeleteAllHistoryTestCase(ctx context.Context, historyTestCase if db, err = s.getClient(ctx); err != nil { return } - allHistoryTestCaseIdentity(db, input).Delete(input) + + var historyTestResults []HistoryTestResult + if err = allHistoryTestCaseIdentity(db, input).Find(&historyTestResults).Error; err != nil { + return nil, err + } + for _, historyTestResult := range historyTestResults { + fileName := historyTestResult.Body + if strings.HasPrefix(fileName, "isFilePath-") { + tempDir := os.TempDir() + fullFilePath := filepath.Join(tempDir, fileName) + + if err = os.Remove(fullFilePath); err != nil { + log.Printf("Failed to delete file: %s, error: %v\n", fullFilePath, err) + } + } + db.Delete(&historyTestResult) + } return } From 46712161a236518330ef437715cca106eaf4f7eb Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Thu, 12 Sep 2024 14:47:33 +0800 Subject: [PATCH 08/10] fix: add tests --- go.mod | 1 - pkg/convert.go | 72 ++++++++++++++-------------- pkg/convert_test.go | 112 +++++++++++++++++++++++++++++++++++++++++++- pkg/server_test.go | 102 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 245 insertions(+), 42 deletions(-) diff --git a/go.mod b/go.mod index a8e0246..7d3fa59 100644 --- a/go.mod +++ b/go.mod @@ -91,7 +91,6 @@ require ( golang.org/x/sync v0.6.0 // indirect golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect - google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/pkg/convert.go b/pkg/convert.go index 6b5e7f8..956d8ee 100644 --- a/pkg/convert.go +++ b/pkg/convert.go @@ -122,49 +122,49 @@ func ConvertToDBTestSuite(suite *remote.TestSuite) (result *TestSuite) { } func ConvertToDBHistoryTestResult(historyTestResult *server.HistoryTestResult) (result *HistoryTestResult) { - id := fmt.Sprintf("%s_%s_%s", historyTestResult.CreateTime.AsTime().Local().Format("2006-01-02T15:04:05.999999999"), historyTestResult.Data.SuiteName, historyTestResult.Data.CaseName) result = &HistoryTestResult{ - ID: id, - Message: historyTestResult.Message, - Error: historyTestResult.Error, - HistorySuiteName: historyTestResult.CreateTime.AsTime().Local().Format("2006-1-2"), - CaseName: historyTestResult.Data.CaseName, - SuiteName: historyTestResult.Data.SuiteName, - SuiteAPI: historyTestResult.Data.SuiteApi, - Param: pairToJSON(historyTestResult.Data.SuiteParam), - CreateTime: historyTestResult.CreateTime.AsTime().Local().Format("2006-01-02T15:04:05.999999999"), + Message: historyTestResult.Message, + Error: historyTestResult.Error, + } + if historyTestResult.CreateTime != nil { + id := fmt.Sprintf("%s_%s_%s", historyTestResult.CreateTime.AsTime().Local().Format("2006-01-02T15:04:05.999999999"), historyTestResult.Data.SuiteName, historyTestResult.Data.CaseName) + result.ID = id + result.CreateTime = historyTestResult.CreateTime.AsTime().Local().Format("2006-01-02T15:04:05.999999999") + result.HistorySuiteName = historyTestResult.CreateTime.AsTime().Local().Format("2006-1-2") + } + if historyTestResult.Data != nil { + result.Param = pairToJSON(historyTestResult.Data.SuiteParam) + result.CaseName = historyTestResult.Data.CaseName + result.SuiteName = historyTestResult.Data.SuiteName + result.SuiteAPI = historyTestResult.Data.SuiteApi + if historyTestResult.Data.Request != nil { + request := historyTestResult.Data.Request + result.CaseAPI = request.Api + result.Method = request.Method + result.Header = pairToJSON(request.Header) + result.Cookie = pairToJSON(request.Cookie) + result.Form = pairToJSON(request.Form) + result.Query = pairToJSON(request.Query) + } + if historyTestResult.Data.Response != nil { + resp := historyTestResult.Data.Response + result.ExpectBody = resp.Body + result.ExpectSchema = resp.Schema + result.ExpectStatusCode = int(resp.StatusCode) + result.ExpectHeader = pairToJSON(resp.Header) + result.ExpectBodyFields = pairToJSON(resp.BodyFieldsExpect) + result.ExpectVerify = SliceToJSON(resp.Verify) + } + if historyTestResult.Data.SuiteSpec != nil { + result.SpecKind = historyTestResult.Data.SuiteSpec.Kind + result.SpecURL = historyTestResult.Data.SuiteSpec.Url + } } for _, testCase := range historyTestResult.TestCaseResult { result.StatusCode = int32(testCase.StatusCode) result.Output = testCase.Output result.Body = testCase.Body } - - request := historyTestResult.Data.Request - if request != nil { - result.CaseAPI = request.Api - result.Method = request.Method - result.Header = pairToJSON(request.Header) - result.Cookie = pairToJSON(request.Cookie) - result.Form = pairToJSON(request.Form) - result.Query = pairToJSON(request.Query) - } - - resp := historyTestResult.Data.Response - if resp != nil { - result.ExpectBody = resp.Body - result.ExpectSchema = resp.Schema - result.ExpectStatusCode = int(resp.StatusCode) - result.ExpectHeader = pairToJSON(resp.Header) - result.ExpectBodyFields = pairToJSON(resp.BodyFieldsExpect) - result.ExpectVerify = SliceToJSON(resp.Verify) - } - - if historyTestResult.Data.SuiteSpec != nil { - result.SpecKind = historyTestResult.Data.SuiteSpec.Kind - result.SpecURL = historyTestResult.Data.SuiteSpec.Url - } - return } diff --git a/pkg/convert_test.go b/pkg/convert_test.go index 71764eb..be912a0 100644 --- a/pkg/convert_test.go +++ b/pkg/convert_test.go @@ -16,12 +16,13 @@ limitations under the License. package pkg_test import ( - "testing" - "github.com/linuxsuren/api-testing/pkg/server" "github.com/linuxsuren/api-testing/pkg/testing/remote" "github.com/linuxsuren/atest-ext-store-orm/pkg" "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/types/known/timestamppb" + "testing" + "time" ) func TestConvertToRemoteTestCase(t *testing.T) { @@ -148,6 +149,113 @@ func TestConvertTestSuite(t *testing.T) { }) } +func TestConvertToDBHistoryTestResult(t *testing.T) { + t.Run("without testcaseResult and historyTestcase", func(t *testing.T) { + result := pkg.ConvertToDBHistoryTestResult(&server.HistoryTestResult{}) + assert.Equal(t, &pkg.HistoryTestResult{}, result) + }) + + t.Run("have testcaseResult", func(t *testing.T) { + result := pkg.ConvertToDBHistoryTestResult(&server.HistoryTestResult{ + TestCaseResult: []*server.TestCaseResult{ + { + StatusCode: 200, + Body: "Test body", + Output: "Test output", + }, + }, + }) + assert.Equal(t, &pkg.HistoryTestResult{ + StatusCode: 200, + Body: "Test body", + Output: "Test output", + }, result) + }) +} + +var now = time.Now().UTC() +var nowString = now.Format("2006-01-02T15:04:05.999999999") + +func TestConvertToRemoteHistoryTestResult(t *testing.T) { + assert.Equal(t, &server.HistoryTestResult{ + CreateTime: timestamppb.New(now), + TestCaseResult: []*server.TestCaseResult{ + { + Body: "body", + Output: "output", + Header: samplePairs, + }, + }, + Data: &server.HistoryTestCase{ + CreateTime: timestamppb.New(now), + SuiteSpec: &server.APISpec{ + Kind: "kind", + }, + Request: &server.Request{ + Api: "", + Method: "", + Header: []*server.Pair{ + {Key: "key", Value: "value"}, + }, + Body: "body", + }, + Response: &server.Response{ + StatusCode: 0, + Body: "", + Header: nil, + }, + }, + }, pkg.ConvertToRemoteHistoryTestResult(&pkg.HistoryTestResult{ + CreateTime: nowString, + Body: "body", + Output: "output", + Header: sampleJSONMap, + SpecKind: "kind", + })) +} + +func TestConvertToGRPCHistoryTestSuite(t *testing.T) { + assert.Equal(t, &remote.HistoryTestSuite{ + Items: []*server.HistoryTestCase{ + { + CreateTime: timestamppb.New(now), + SuiteName: "name", + SuiteSpec: &server.APISpec{ + Kind: "kind", + }, + Request: &server.Request{ + Body: "Test Body", + }, + Response: &server.Response{}, + }, + }, + }, pkg.ConvertToGRPCHistoryTestSuite(&pkg.HistoryTestResult{ + CreateTime: nowString, + SuiteName: "name", + Body: "Test Body", + SpecKind: "kind", + })) +} + +func TestConvertToGRPCHistoryTestCase(t *testing.T) { + assert.Equal(t, &server.HistoryTestCase{ + CreateTime: timestamppb.New(now), + SuiteName: "name", + SuiteSpec: &server.APISpec{ + Kind: "kind", + }, + Request: &server.Request{ + Body: "Test Body", + }, + Response: &server.Response{}, + }, pkg.ConvertToGRPCHistoryTestCase(&pkg.HistoryTestResult{ + CreateTime: nowString, + SuiteName: "name", + Body: "Test Body", + SpecKind: "kind", + })) +} + const sampleJSONMap = `{"key":"value"}` var samplePairs []*server.Pair = []*server.Pair{{ diff --git a/pkg/server_test.go b/pkg/server_test.go index 742a95b..aca8fa9 100644 --- a/pkg/server_test.go +++ b/pkg/server_test.go @@ -17,13 +17,15 @@ package pkg import ( "context" - "os" - "testing" - + "fmt" "github.com/linuxsuren/api-testing/pkg/server" atest "github.com/linuxsuren/api-testing/pkg/testing" "github.com/linuxsuren/api-testing/pkg/testing/remote" "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/types/known/timestamppb" + "os" + "testing" + "time" ) func TestNewRemoteServer(t *testing.T) { @@ -87,6 +89,36 @@ func TestNewRemoteServer(t *testing.T) { assert.False(t, reply.Ready) }) + t.Run("CreateTestCaseHistory", func(t *testing.T) { + _, err := remoteServer.CreateTestCaseHistory(defaultCtx, nil) + assert.Error(t, err) + }) + + t.Run("ListHistoryTestSuite", func(t *testing.T) { + _, err := remoteServer.ListHistoryTestSuite(defaultCtx, nil) + assert.Error(t, err) + }) + + t.Run("GetHistoryTestCase", func(t *testing.T) { + _, err := remoteServer.GetHistoryTestCase(defaultCtx, nil) + assert.Error(t, err) + }) + + t.Run("GetTestCaseAllHistory", func(t *testing.T) { + _, err := remoteServer.GetTestCaseAllHistory(defaultCtx, nil) + assert.Error(t, err) + }) + + t.Run("DeleteHistoryTestCase", func(t *testing.T) { + _, err := remoteServer.DeleteHistoryTestCase(defaultCtx, nil) + assert.Error(t, err) + }) + + t.Run("DeleteAllHistoryTestCase", func(t *testing.T) { + _, err := remoteServer.DeleteAllHistoryTestCase(defaultCtx, nil) + assert.Error(t, err) + }) + t.Run("invalid orm driver", func(t *testing.T) { remoteServer := NewRemoteServer() assert.NotNil(t, remoteServer) @@ -225,4 +257,68 @@ func TestSQLite(t *testing.T) { _, err := remoteServer.GetVersion(defaultCtx, &server.Empty{}) assert.NoError(t, err) }) + + now := time.Now() + t.Run("CreateTestCaseHistory", func(t *testing.T) { + _, err := remoteServer.CreateTestCaseHistory(defaultCtx, &server.HistoryTestResult{ + CreateTime: timestamppb.New(now), + Data: &server.HistoryTestCase{ + CaseName: "test", + SuiteName: "test", + }, + TestCaseResult: []*server.TestCaseResult{ + {Output: "test output"}, + }, + }) + assert.NoError(t, err) + }) + id := fmt.Sprintf("%s_test_test", now.Local().Format("2006-01-02T15:04:05.999999999")) + + t.Run("ListHistoryTestSuite", func(t *testing.T) { + result, err := remoteServer.ListHistoryTestSuite(defaultCtx, nil) + assert.NoError(t, err) + assert.Equal(t, 1, len(result.Data)) + assert.Equal(t, "test", result.Data[0].Items[0].SuiteName) + }) + + t.Run("GetHistoryTestCase", func(t *testing.T) { + result, err := remoteServer.GetHistoryTestCase(defaultCtx, &server.HistoryTestCase{ + ID: id, + }) + assert.NoError(t, err) + assert.Equal(t, "test", result.CaseName) + }) + + t.Run("GetHistoryTestCaseWithResult", func(t *testing.T) { + result, err := remoteServer.GetHistoryTestCaseWithResult(defaultCtx, &server.HistoryTestCase{ + ID: id, + }) + assert.NoError(t, err) + assert.Equal(t, "test", result.Data.CaseName) + assert.Equal(t, "test output", result.TestCaseResult[0].Output) + }) + + t.Run("GetTestCaseAllHistory", func(t *testing.T) { + result, err := remoteServer.GetTestCaseAllHistory(defaultCtx, &server.TestCase{ + Name: "test", + SuiteName: "test", + }) + assert.NoError(t, err) + assert.Equal(t, 1, len(result.Data)) + }) + + t.Run("DeleteHistoryTestCase", func(t *testing.T) { + _, err := remoteServer.DeleteHistoryTestCase(defaultCtx, &server.HistoryTestCase{ + ID: id, + }) + assert.NoError(t, err) + }) + + t.Run("DeleteAllHistoryTestCase", func(t *testing.T) { + _, err := remoteServer.DeleteAllHistoryTestCase(defaultCtx, &server.HistoryTestCase{ + CaseName: "test", + SuiteName: "test", + }) + assert.NoError(t, err) + }) } From aff24be053d4c5a0549edfe170269f8d827c4743 Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Fri, 13 Sep 2024 11:10:45 +0800 Subject: [PATCH 09/10] fix: add history header for history page display --- go.mod | 4 +++- go.sum | 4 ++-- pkg/convert.go | 2 ++ pkg/types.go | 17 +++++++++-------- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 7d3fa59..f15da4b 100644 --- a/go.mod +++ b/go.mod @@ -91,11 +91,13 @@ require ( golang.org/x/sync v0.6.0 // indirect golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect + google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/linuxsuren/api-testing => github.com/SamYSF/api-testing v0.0.0-20240911062137-52c9a040fe24 +replace github.com/linuxsuren/api-testing => github.com/SamYSF/api-testing v0.0.0-20240913030350-eef572d3a842 + //replace github.com/linuxsuren/api-testing => /home/ysf/project/api-testing diff --git a/go.sum b/go.sum index 59d7fd6..fc2bb4c 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0 github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA= github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM= -github.com/SamYSF/api-testing v0.0.0-20240911062137-52c9a040fe24 h1:7lDwr+elqc46aIqGW8fi6kWCdlrufPborAi1DasUPm0= -github.com/SamYSF/api-testing v0.0.0-20240911062137-52c9a040fe24/go.mod h1:uu0hyEYSgdSju31EugGwIGE2I/uv7VYNM/6ODPKKD7I= +github.com/SamYSF/api-testing v0.0.0-20240913030350-eef572d3a842 h1:LBkbEAx3GFJNvdVU66ahvHwk4F7722kCEXyVw67MFtU= +github.com/SamYSF/api-testing v0.0.0-20240913030350-eef572d3a842/go.mod h1:uu0hyEYSgdSju31EugGwIGE2I/uv7VYNM/6ODPKKD7I= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= diff --git a/pkg/convert.go b/pkg/convert.go index 956d8ee..f291d2f 100644 --- a/pkg/convert.go +++ b/pkg/convert.go @@ -137,6 +137,7 @@ func ConvertToDBHistoryTestResult(historyTestResult *server.HistoryTestResult) ( result.CaseName = historyTestResult.Data.CaseName result.SuiteName = historyTestResult.Data.SuiteName result.SuiteAPI = historyTestResult.Data.SuiteApi + result.HistoryHeader = pairToJSON(historyTestResult.Data.HistoryHeader) if historyTestResult.Data.Request != nil { request := historyTestResult.Data.Request result.CaseAPI = request.Api @@ -226,6 +227,7 @@ func ConvertToGRPCHistoryTestCase(historyTestResult *HistoryTestResult) (result SuiteParam: jsonToPair(historyTestResult.Param), HistorySuiteName: historyTestResult.HistorySuiteName, CreateTime: timestamppb.New(createTime), + HistoryHeader: jsonToPair(historyTestResult.HistoryHeader), SuiteSpec: &server.APISpec{ Kind: historyTestResult.SpecKind, diff --git a/pkg/types.go b/pkg/types.go index 4edac73..0804d0f 100644 --- a/pkg/types.go +++ b/pkg/types.go @@ -44,14 +44,15 @@ type HistoryTestResult struct { Param string //case information - CaseName string `json:"caseName"` - CaseAPI string - Method string - Body string - Header string - Cookie string - Query string - Form string + CaseName string `json:"caseName"` + CaseAPI string + Method string + Body string + Header string + HistoryHeader string + Cookie string + Query string + Form string ExpectStatusCode int ExpectBody string From cdf3cb60e153794e602de17cce36265c8e6814fa Mon Sep 17 00:00:00 2001 From: ysf <1807100869@qq.com> Date: Fri, 13 Sep 2024 15:19:14 +0800 Subject: [PATCH 10/10] fix: fix test --- go.mod | 2 +- go.sum | 4 ++-- pkg/server_test.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index f15da4b..c0eb6c2 100644 --- a/go.mod +++ b/go.mod @@ -98,6 +98,6 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/linuxsuren/api-testing => github.com/SamYSF/api-testing v0.0.0-20240913030350-eef572d3a842 +replace github.com/linuxsuren/api-testing => github.com/SamYSF/api-testing v0.0.0-20240913070731-63814573b192 //replace github.com/linuxsuren/api-testing => /home/ysf/project/api-testing diff --git a/go.sum b/go.sum index fc2bb4c..c9b51d5 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0 github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA= github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM= -github.com/SamYSF/api-testing v0.0.0-20240913030350-eef572d3a842 h1:LBkbEAx3GFJNvdVU66ahvHwk4F7722kCEXyVw67MFtU= -github.com/SamYSF/api-testing v0.0.0-20240913030350-eef572d3a842/go.mod h1:uu0hyEYSgdSju31EugGwIGE2I/uv7VYNM/6ODPKKD7I= +github.com/SamYSF/api-testing v0.0.0-20240913070731-63814573b192 h1:hHFszkA9lWWQ0UmyE8/C8uXRP4hAyQHAXilVSg87e/w= +github.com/SamYSF/api-testing v0.0.0-20240913070731-63814573b192/go.mod h1:uu0hyEYSgdSju31EugGwIGE2I/uv7VYNM/6ODPKKD7I= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= diff --git a/pkg/server_test.go b/pkg/server_test.go index aca8fa9..2978578 100644 --- a/pkg/server_test.go +++ b/pkg/server_test.go @@ -110,12 +110,12 @@ func TestNewRemoteServer(t *testing.T) { }) t.Run("DeleteHistoryTestCase", func(t *testing.T) { - _, err := remoteServer.DeleteHistoryTestCase(defaultCtx, nil) + _, err := remoteServer.DeleteHistoryTestCase(defaultCtx, &server.HistoryTestCase{}) assert.Error(t, err) }) t.Run("DeleteAllHistoryTestCase", func(t *testing.T) { - _, err := remoteServer.DeleteAllHistoryTestCase(defaultCtx, nil) + _, err := remoteServer.DeleteAllHistoryTestCase(defaultCtx, &server.HistoryTestCase{}) assert.Error(t, err) })