You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by wa...@apache.org on 2022/09/16 07:42:35 UTC
[incubator-devlake] branch main updated: fix: non-empty body of get request
This is an automated email from the ASF dual-hosted git repository.
warren pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/main by this push:
new 49770fe4 fix: non-empty body of get request
49770fe4 is described below
commit 49770fe474d3b0bda5d30e77293bfabd3fe2e266
Author: zhangliang <li...@merico.dev>
AuthorDate: Fri Sep 16 14:40:02 2022 +0800
fix: non-empty body of get request
---
plugins/helper/api_async_client.go | 8 +-
plugins/helper/api_async_client_test.go.old | 357 ----------------------------
plugins/helper/api_extractor_test.go.old | 198 ---------------
plugins/helper/batch_save_test.go.old | 173 --------------
plugins/helper/data_convertor_test.go.old | 190 ---------------
5 files changed, 4 insertions(+), 922 deletions(-)
diff --git a/plugins/helper/api_async_client.go b/plugins/helper/api_async_client.go
index 90119c02..979029e6 100644
--- a/plugins/helper/api_async_client.go
+++ b/plugins/helper/api_async_client.go
@@ -153,18 +153,18 @@ func (apiClient *ApiAsyncClient) DoAsync(
request = func() errors.Error {
var err error
var res *http.Response
- var body []byte
+ var respBody []byte
apiClient.logger.Debug("endpoint: %s method: %s header: %s body: %s query: %s", path, method, header, body, query)
res, err = apiClient.Do(method, path, query, body, header)
// make sure response body is read successfully, or we might have to retry
if err == nil {
// make sure response.Body stream will be closed to avoid running out of file handle
- defer func(body io.ReadCloser) { body.Close() }(res.Body)
+ defer func(readCloser io.ReadCloser) { _ = readCloser.Close() }(res.Body)
// replace NetworkStream with MemoryBuffer
- body, err = io.ReadAll(res.Body)
+ respBody, err = io.ReadAll(res.Body)
if err == nil {
- res.Body = io.NopCloser(bytes.NewBuffer(body))
+ res.Body = io.NopCloser(bytes.NewBuffer(respBody))
}
}
if err == ErrIgnoreAndContinue {
diff --git a/plugins/helper/api_async_client_test.go.old b/plugins/helper/api_async_client_test.go.old
deleted file mode 100644
index 266c22b8..00000000
--- a/plugins/helper/api_async_client_test.go.old
+++ /dev/null
@@ -1,357 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements. See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package helper
-
-import (
- "context"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "net/url"
- "os"
- "reflect"
- "sync/atomic"
- "testing"
- "time"
-
- "github.com/agiledragon/gomonkey/v2"
- "github.com/apache/incubator-devlake/logger"
- "github.com/apache/incubator-devlake/plugins/core"
- "github.com/sirupsen/logrus"
- "github.com/spf13/viper"
- "github.com/stretchr/testify/assert"
- "gorm.io/gorm"
-)
-
-// TestReadder for test io data
-type TestReader struct {
- Err error
-}
-
-func (r *TestReader) Read(p []byte) (n int, err error) {
- return 0, r.Err
-}
-
-func (r *TestReader) Close() error {
- return nil
-}
-
-// it is better to move some where more public.
-var ErrUnitTest error = fmt.Errorf("ErrorForTest[%d]", time.Now().UnixNano())
-
-func callback(_ *http.Response, err error) error {
- if err == nil {
- return nil
- }
- return ErrUnitTest
-}
-
-func GetConfigForTest(basepath string) *viper.Viper {
- // create the object and load the .env file
- v := viper.New()
- envfile := ".env"
- envbasefile := basepath + ".env.example"
- bytesRead, err := ioutil.ReadFile(envbasefile)
- if err != nil {
- logrus.Warn("Failed to read ["+envbasefile+"] file:", err)
- }
- err = ioutil.WriteFile(envfile, bytesRead, 0644)
-
- if err != nil {
- logrus.Warn("Failed to write config file ["+envfile+"] file:", err)
- }
-
- v.SetConfigFile(envfile)
- err = v.ReadInConfig()
- if err != nil {
- path, _ := os.Getwd()
- logrus.Warn("Now in the path [" + path + "]")
- logrus.Warn("Failed to read ["+envfile+"] file:", err)
- }
- v.AutomaticEnv()
- // This line is essential for reading and writing
- v.WatchConfig()
- return v
-}
-
-func CreateTestAsyncApiClientWithRateLimitAndCtx(t *testing.T, rateLimiter *ApiRateLimitCalculator, ctx context.Context) (*ApiAsyncClient, error) {
- // set the function of create new default taskcontext for the AsyncApiClient
- gm := gomonkey.ApplyFunc(NewDefaultTaskContext, func(
- cfg *viper.Viper,
- _ core.Logger,
- db *gorm.DB,
- _ context.Context,
- name string,
- subtasks map[string]bool,
- progress chan core.RunningProgress,
- ) core.TaskContext {
- return &DefaultTaskContext{
- &defaultExecContext{
- cfg: cfg,
- logger: &logger.DefaultLogger{},
- db: db,
- ctx: ctx,
- name: "Test",
- data: nil,
- progress: progress,
- },
- subtasks,
- make(map[string]*DefaultSubTaskContext),
- }
- })
- defer gm.Reset()
- taskCtx := NewDefaultTaskContext(GetConfigForTest("../../"), nil, nil, nil, "", nil, nil)
-
- // create ApiClient
- apiClient := &ApiClient{}
- apiClient.Setup("", nil, 3*time.Second)
- apiClient.SetContext(taskCtx.GetContext())
-
- return CreateAsyncApiClient(taskCtx, apiClient, rateLimiter)
-}
-
-// Create an AsyncApiClient object for test
-func CreateTestAsyncApiClient(t *testing.T) (*ApiAsyncClient, error) {
- // create rate limit calculator
- rateLimiter := &ApiRateLimitCalculator{
- UserRateLimitPerHour: 36000, // ten times each seconed
- }
- return CreateTestAsyncApiClientWithRateLimitAndCtx(t, rateLimiter, context.Background())
-}
-
-// go test -gcflags=all=-l -run ^TestWaitAsync_EmptyWork
-func TestWaitAsync_EmptyWork(t *testing.T) {
- asyncApiClient, _ := CreateTestAsyncApiClient(t)
-
- err := asyncApiClient.WaitAsync()
- assert.Equal(t, err, nil)
-}
-
-// go test -gcflags=all=-l -run ^TestWaitAsync_WithWork
-func TestWaitAsync_WithWork(t *testing.T) {
- asyncApiClient, _ := CreateTestAsyncApiClient(t)
-
- gm_info := gomonkey.ApplyMethod(reflect.TypeOf(&logger.DefaultLogger{}), "Info", func(_ *logger.DefaultLogger, _ string, _ ...interface{}) {
- })
- defer gm_info.Reset()
-
- gm_do := gomonkey.ApplyMethod(reflect.TypeOf(&ApiClient{}), "Do", func(
- apiClient *ApiClient,
- method string,
- path string,
- query url.Values,
- body interface{},
- headers http.Header,
- ) (*http.Response, error) {
- return &http.Response{
- Body: &TestReader{Err: io.EOF},
- StatusCode: http.StatusOK,
- }, nil
- })
- defer gm_do.Reset()
-
- // check if the callback1 has been finished
- waitSuc := false
- callback1 := func(_ *http.Response, err error) error {
- // wait 0.5 second for wait
- time.Sleep(500 * time.Millisecond)
- waitSuc = true
- return nil
- }
-
- // begin to test
- err := asyncApiClient.DoAsync("", "", nil, nil, nil, callback1, 0)
- assert.Equal(t, err, nil)
-
- err = asyncApiClient.WaitAsync()
- assert.Equal(t, err, nil)
- assert.Equal(t, waitSuc, true)
-}
-
-// go test -gcflags=all=-l -run ^TestWaitAsync_MutiWork
-func TestWaitAsync_MutiWork(t *testing.T) {
- asyncApiClient, _ := CreateTestAsyncApiClient(t)
-
- gm_info := gomonkey.ApplyMethod(reflect.TypeOf(&logger.DefaultLogger{}), "Info", func(_ *logger.DefaultLogger, _ string, _ ...interface{}) {
- })
- defer gm_info.Reset()
-
- gm_do := gomonkey.ApplyMethod(reflect.TypeOf(&ApiClient{}), "Do", func(
- apiClient *ApiClient,
- method string,
- path string,
- query url.Values,
- body interface{},
- headers http.Header,
- ) (*http.Response, error) {
- return &http.Response{
- Body: &TestReader{Err: io.EOF},
- StatusCode: http.StatusOK,
- }, nil
- })
- defer gm_do.Reset()
-
- // check if the callback2 has been finished
- finishedCount := int64(0)
- callback2 := func(_ *http.Response, err error) error {
- // wait 0.5 second for wait
- time.Sleep(500 * time.Millisecond)
- atomic.AddInt64(&finishedCount, 1)
- return nil
- }
-
- testCount := int64(5)
-
- // begin to test
- for i := int64(0); i < testCount; i++ {
- err := asyncApiClient.DoAsync("", "", nil, nil, nil, callback2, 0)
- assert.Equal(t, err, nil)
- }
-
- err := asyncApiClient.WaitAsync()
- assert.Equal(t, err, nil)
- assert.Equal(t, finishedCount, testCount)
-}
-
-// go test -gcflags=all=-l -run ^TestDoAsync_OnceSuceess
-func TestDoAsync_OnceSuceess(t *testing.T) {
- asyncApiClient, _ := CreateTestAsyncApiClient(t)
- gm_info := gomonkey.ApplyMethod(reflect.TypeOf(&logger.DefaultLogger{}), "Info", func(_ *logger.DefaultLogger, _ string, _ ...interface{}) {
- })
- defer gm_info.Reset()
-
- gm_do := gomonkey.ApplyMethod(reflect.TypeOf(&ApiClient{}), "Do", func(
- apiClient *ApiClient,
- method string,
- path string,
- query url.Values,
- body interface{},
- headers http.Header,
- ) (*http.Response, error) {
- return &http.Response{
- Body: &TestReader{Err: io.EOF},
- StatusCode: http.StatusOK,
- }, nil
- })
- defer gm_do.Reset()
-
- // ready to test
- err := asyncApiClient.DoAsync("", "", nil, nil, nil, callback, 0)
- assert.Equal(t, err, nil)
-
- err = asyncApiClient.WaitAsync()
- assert.Equal(t, err, nil)
-}
-
-// go test -gcflags=all=-l -run ^TestDoAsync_TryAndFail
-func TestDoAsync_TryAndFail(t *testing.T) {
- asyncApiClient, _ := CreateTestAsyncApiClient(t)
- gm_info := gomonkey.ApplyMethod(reflect.TypeOf(&logger.DefaultLogger{}), "Info", func(_ *logger.DefaultLogger, _ string, _ ...interface{}) {
- })
- defer gm_info.Reset()
-
- gm_do := gomonkey.ApplyMethod(reflect.TypeOf(&ApiClient{}), "Do", func(
- apiClient *ApiClient,
- method string,
- path string,
- query url.Values,
- body interface{},
- headers http.Header,
- ) (*http.Response, error) {
- return &http.Response{
- Body: &TestReader{Err: ErrUnitTest},
- StatusCode: http.StatusInternalServerError,
- }, nil
- })
- defer gm_do.Reset()
-
- // ready to test
- err := asyncApiClient.DoAsync("", "", nil, nil, nil, callback, 0)
- assert.Equal(t, err, nil)
-
- err = asyncApiClient.WaitAsync()
- // there must have err and the err must be ErrUnitTest
- if assert.NotNil(t, err) {
- assert.Contains(t, err.Error(), ErrUnitTest.Error())
- }
-}
-
-// go test -gcflags=all=-l -run ^TestDoAsync_TryAndSuceess
-func TestDoAsync_TryAndSuceess(t *testing.T) {
- asyncApiClient, _ := CreateTestAsyncApiClient(t)
- gm_info := gomonkey.ApplyMethod(reflect.TypeOf(&logger.DefaultLogger{}), "Info", func(_ *logger.DefaultLogger, _ string, _ ...interface{}) {
- })
- defer gm_info.Reset()
-
- // counting the retry times
- times := 0
- gm_do := gomonkey.ApplyMethod(reflect.TypeOf(&ApiClient{}), "Do", func(
- apiClient *ApiClient,
- method string,
- path string,
- query url.Values,
- body interface{},
- headers http.Header,
- ) (*http.Response, error) {
- times++
- switch times {
- case 1:
- return &http.Response{
- Body: &TestReader{Err: ErrUnitTest},
- StatusCode: http.StatusInternalServerError,
- }, nil
- case 2:
- return &http.Response{
- Body: &TestReader{Err: io.EOF},
- StatusCode: http.StatusInternalServerError,
- }, nil
- case 3:
- return &http.Response{
- Body: &TestReader{Err: io.EOF},
- StatusCode: http.StatusBadRequest,
- }, nil
- case 4:
- return &http.Response{
- Body: &TestReader{Err: io.EOF},
- StatusCode: http.StatusMultipleChoices,
- }, nil
- case 5:
- return &http.Response{
- Body: &TestReader{Err: io.EOF},
- StatusCode: http.StatusOK,
- }, nil
- default:
- assert.Empty(t, TestNoRunHere)
- return &http.Response{
- Body: &TestReader{Err: io.EOF},
- StatusCode: http.StatusOK,
- }, TestError
- }
- })
- defer gm_do.Reset()
- asyncApiClient.SetMaxRetry(5)
-
- // ready to test
- err := asyncApiClient.DoAsync("", "", nil, nil, nil, callback, 0)
- assert.Equal(t, err, nil)
-
- err = asyncApiClient.WaitAsync()
- assert.Equal(t, err, nil)
- assert.Equal(t, times, 4)
-}
diff --git a/plugins/helper/api_extractor_test.go.old b/plugins/helper/api_extractor_test.go.old
deleted file mode 100644
index d225e71d..00000000
--- a/plugins/helper/api_extractor_test.go.old
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements. See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package helper
-
-import (
- "context"
- "database/sql"
- "fmt"
- "github.com/apache/incubator-devlake/logger"
- "github.com/apache/incubator-devlake/plugins/core"
- "reflect"
- "testing"
- "time"
-
- "github.com/agiledragon/gomonkey/v2"
- "github.com/magiconair/properties/assert"
- "github.com/sirupsen/logrus"
- "gorm.io/datatypes"
- "gorm.io/gorm"
-)
-
-var TestRawData *RawData = &RawData{
- ID: 110100100116102,
- Params: "TestParams",
- Data: []byte{46, 99, 111, 109},
- Url: "http://devlake.io/",
- Input: datatypes.JSON(TestRawMessage),
- CreatedAt: time.Now(),
-}
-
-// go test -gcflags=all=-l
-func CreateTestApiExtractor(t *testing.T) (*ApiExtractor, error) {
- var ctx context.Context
- ctx, Cancel = context.WithCancel(context.Background())
- return NewApiExtractor(ApiExtractorArgs{
- RawDataSubTaskArgs: RawDataSubTaskArgs{
- Ctx: &DefaultSubTaskContext{
- defaultExecContext: newDefaultExecContext(GetConfigForTest("../../"), logger.NewDefaultLogger(logrus.New(), "Test", make(map[string]*logrus.Logger)), &gorm.DB{}, ctx, "Test", nil, nil),
- },
- Table: TestTable{}.TableName(),
- Params: &TestParam{
- Test: TestUrlParam,
- },
- },
- BatchSize: TestBatchSize,
- Extract: func(row *RawData) ([]interface{}, error) {
- assert.Equal(t, row, TestRawData)
- results := make([]interface{}, 0, TestDataCount)
- for i := 0; i < TestDataCount; i++ {
- results = append(results, TestTableData)
- }
- return results, nil
- },
- })
-}
-
-func TestApiExtractorExecute(t *testing.T) {
- MockDB(t)
- defer UnMockDB()
-
- gt.Reset()
- gt = gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "Table", func(db *gorm.DB, name string, args ...interface{}) *gorm.DB {
- assert.Equal(t, name, "_raw_"+TestTableData.TableName())
- return db
- },
- )
-
- apiExtractor, _ := CreateTestApiExtractor(t)
-
- datacount := TestDataCount
- gn := gomonkey.ApplyMethod(reflect.TypeOf(&sql.Rows{}), "Next", func(r *sql.Rows) bool {
- if datacount > 0 {
- datacount--
- return true
- } else {
- return false
- }
- })
- defer gn.Reset()
-
- gcl := gomonkey.ApplyMethod(reflect.TypeOf(&sql.Rows{}), "Close", func(r *sql.Rows) error {
- return nil
- })
- defer gcl.Reset()
-
- gs.Reset()
-
- scanrowTimes := 0
- gs = gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "ScanRows", func(db *gorm.DB, rows *sql.Rows, dest interface{}) error {
- scanrowTimes++
- *dest.(*RawData) = *TestRawData
- return nil
- },
- )
-
- fortypeTimes := 0
- gf := gomonkey.ApplyMethod(reflect.TypeOf(&BatchSaveDivider{}), "ForType", func(d *BatchSaveDivider, rowType reflect.Type, log core.Logger) (*BatchSave, error) {
- fortypeTimes++
- assert.Equal(t, rowType, reflect.TypeOf(TestTableData))
- err := d.onNewBatchSave(rowType)
- assert.Equal(t, err, nil)
-
- return &BatchSave{}, nil
- })
- defer gf.Reset()
-
- addTimes := 0
- gsv := gomonkey.ApplyMethod(reflect.TypeOf(&BatchSave{}), "Add", func(c *BatchSave, slot interface{}) error {
- addTimes++
- assert.Equal(t, slot, TestTableData)
- return nil
- })
- defer gsv.Reset()
-
- // begin testing
- err := apiExtractor.Execute()
- assert.Equal(t, err, nil)
- assert.Equal(t, scanrowTimes, TestDataCount)
- assert.Equal(t, fortypeTimes, TestDataCount*TestDataCount)
- assert.Equal(t, addTimes, TestDataCount*TestDataCount)
-}
-
-func TestApiExtractorExecute_Cancel(t *testing.T) {
- MockDB(t)
- defer UnMockDB()
-
- gt.Reset()
- gt = gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "Table", func(db *gorm.DB, name string, args ...interface{}) *gorm.DB {
- assert.Equal(t, name, "_raw_"+TestTableData.TableName())
- return db
- },
- )
-
- apiExtractor, _ := CreateTestApiExtractor(t)
-
- gn := gomonkey.ApplyMethod(reflect.TypeOf(&sql.Rows{}), "Next", func(r *sql.Rows) bool {
- // death loop for testing cancel
- return true
- })
- defer gn.Reset()
-
- gcl := gomonkey.ApplyMethod(reflect.TypeOf(&sql.Rows{}), "Close", func(r *sql.Rows) error {
- return nil
- })
- defer gcl.Reset()
-
- gs.Reset()
-
- scanrowTimes := 0
- gs = gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "ScanRows", func(db *gorm.DB, rows *sql.Rows, dest interface{}) error {
- scanrowTimes++
- *dest.(*RawData) = *TestRawData
- return nil
- },
- )
-
- fortypeTimes := 0
- gf := gomonkey.ApplyMethod(reflect.TypeOf(&BatchSaveDivider{}), "ForType", func(d *BatchSaveDivider, rowType reflect.Type, log core.Logger) (*BatchSave, error) {
- fortypeTimes++
- assert.Equal(t, rowType, reflect.TypeOf(TestTableData))
- err := d.onNewBatchSave(rowType)
- assert.Equal(t, err, nil)
-
- return &BatchSave{}, nil
- })
- defer gf.Reset()
-
- addTimes := 0
- gsv := gomonkey.ApplyMethod(reflect.TypeOf(&BatchSave{}), "Add", func(c *BatchSave, slot interface{}) error {
- addTimes++
- assert.Equal(t, slot, TestTableData)
- return nil
- })
- defer gsv.Reset()
-
- go func() {
- time.Sleep(time.Duration(500) * time.Microsecond)
- Cancel()
- }()
-
- err := apiExtractor.Execute()
- assert.Equal(t, err, fmt.Errorf("context canceled"))
-}
diff --git a/plugins/helper/batch_save_test.go.old b/plugins/helper/batch_save_test.go.old
deleted file mode 100644
index 464be8ee..00000000
--- a/plugins/helper/batch_save_test.go.old
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements. See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package helper
-
-import (
- "reflect"
- "testing"
-
- "github.com/agiledragon/gomonkey/v2"
- "github.com/apache/incubator-devlake/logger"
- "github.com/apache/incubator-devlake/models/domainlayer"
- "github.com/apache/incubator-devlake/models/domainlayer/ticket"
- "github.com/stretchr/testify/assert"
- "gorm.io/gorm"
- "gorm.io/gorm/clause"
-)
-
-func Test_getPrimaryKeyValue(t *testing.T) {
- type args struct {
- iface interface{}
- }
- tests := []struct {
- name string
- args args
- want string
- }{
- {
- "",
- args{&ticket.Sprint{
- DomainEntity: domainlayer.DomainEntity{Id: "abc"},
- },
- },
- "abc",
- },
- {
- "",
- args{ticket.Sprint{
- DomainEntity: domainlayer.DomainEntity{Id: "abc"},
- },
- },
- "abc",
- },
- {
- "",
- args{ticket.SprintIssue{
- SprintId: "abc",
- IssueId: "123",
- },
- },
- "abc:123",
- },
- {
- "",
- args{&ticket.SprintIssue{
- SprintId: "abc",
- IssueId: "123",
- },
- },
- "abc:123",
- },
- {
- "",
- args{ticket.Issue{}},
- "",
- },
- {
- "",
- args{&ticket.Issue{}},
- "",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- assert.Equalf(t, tt.want, getPrimaryKeyValue(tt.args.iface), "getPrimaryKeyValue(%v)", tt.args.iface)
- })
- }
-}
-
-func Test_hasPrimaryKey(t *testing.T) {
- type args struct {
- iface interface{}
- }
- tests := []struct {
- name string
- args args
- want bool
- }{
- {
- "",
- args{ticket.SprintIssue{
- SprintId: "abc",
- IssueId: "123",
- },
- },
- true,
- },
- {
- "",
- args{&ticket.SprintIssue{
- SprintId: "abc",
- IssueId: "123",
- },
- },
- true,
- },
- {
- "",
- args{ticket.Issue{}},
- true,
- },
- {
- "",
- args{&ticket.Issue{}},
- true,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- assert.Equalf(t, tt.want, hasPrimaryKey(reflect.TypeOf(tt.args.iface)), "hasPrimaryKey(%v)", tt.args.iface)
- })
- }
-}
-
-// go test -gcflags=all=-l
-func TestBatchSave(t *testing.T) {
- db := &gorm.DB{}
- sqlTimes := 0
-
- gcl := gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "Clauses", func(db *gorm.DB, conds ...clause.Expression) (tx *gorm.DB) {
- sqlTimes++
- return db
- },
- )
- defer gcl.Reset()
-
- gcr := gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "Create", func(db *gorm.DB, value interface{}) *gorm.DB {
- assert.Equal(t, TestTableData, value.([]*TestTable)[0])
- return db
- },
- )
- defer gcr.Reset()
-
- TestBatchSize = 1
- rowType := reflect.TypeOf(TestTableData)
- batch, err := NewBatchSave(db, logger.Global, rowType, TestBatchSize)
-
- // test diff type
- assert.Equal(t, err, nil)
- err = batch.Add(&TestBatchSize)
- assert.NotEqual(t, err, nil)
-
- // test right type
- err = batch.Add(TestTableData)
- assert.Equal(t, err, nil)
-
- assert.Equal(t, sqlTimes, 1)
-}
diff --git a/plugins/helper/data_convertor_test.go.old b/plugins/helper/data_convertor_test.go.old
deleted file mode 100644
index 5791c54d..00000000
--- a/plugins/helper/data_convertor_test.go.old
+++ /dev/null
@@ -1,190 +0,0 @@
-/*
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements. See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-
-package helper
-
-import (
- "context"
- "database/sql"
- "fmt"
- "reflect"
- "testing"
- "time"
-
- "github.com/agiledragon/gomonkey/v2"
- "github.com/apache/incubator-devlake/logger"
- "github.com/apache/incubator-devlake/plugins/core"
- "github.com/magiconair/properties/assert"
- "github.com/sirupsen/logrus"
- "gorm.io/gorm"
-)
-
-// go test -gcflags=all=-l
-func CreateTestDataConverter(t *testing.T) (*DataConverter, error) {
- var ctx context.Context
- ctx, Cancel = context.WithCancel(context.Background())
- return NewDataConverter(DataConverterArgs{
- RawDataSubTaskArgs: RawDataSubTaskArgs{
- Ctx: &DefaultSubTaskContext{
- defaultExecContext: newDefaultExecContext(GetConfigForTest("../../"), logger.NewDefaultLogger(logrus.New(), "Test", make(map[string]*logrus.Logger)), &gorm.DB{}, ctx, "Test", nil, nil),
- },
- Table: TestTable{}.TableName(),
- Params: &TestParam{
- Test: TestUrlParam,
- },
- },
- InputRowType: reflect.TypeOf(TestTable{}),
- Input: &sql.Rows{},
- BatchSize: TestBatchSize,
- Convert: func(row interface{}) ([]interface{}, error) {
- assert.Equal(t, row, TestTableData)
- results := make([]interface{}, 0, TestDataCount)
- for i := 0; i < TestDataCount; i++ {
- results = append(results, TestTableData)
- }
- return results, nil
- },
- })
-}
-
-func TestDataConvertorExecute(t *testing.T) {
- MockDB(t)
- defer UnMockDB()
-
- gt.Reset()
- gt = gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "Table", func(db *gorm.DB, name string, args ...interface{}) *gorm.DB {
- assert.Equal(t, name, "_raw_"+TestTableData.TableName())
- return db
- },
- )
-
- dataConvertor, _ := CreateTestDataConverter(t)
-
- datacount := TestDataCount
- gn := gomonkey.ApplyMethod(reflect.TypeOf(&sql.Rows{}), "Next", func(r *sql.Rows) bool {
- if datacount > 0 {
- datacount--
- return true
- } else {
- return false
- }
- })
- defer gn.Reset()
-
- gcl := gomonkey.ApplyMethod(reflect.TypeOf(&sql.Rows{}), "Close", func(r *sql.Rows) error {
- return nil
- })
- defer gcl.Reset()
-
- gs.Reset()
-
- scanrowTimes := 0
- gs = gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "ScanRows", func(db *gorm.DB, rows *sql.Rows, dest interface{}) error {
- scanrowTimes++
- *dest.(*TestTable) = *TestTableData
- return nil
- },
- )
-
- fortypeTimes := 0
- gf := gomonkey.ApplyMethod(reflect.TypeOf(&BatchSaveDivider{}), "ForType", func(d *BatchSaveDivider, rowType reflect.Type, log core.Logger) (*BatchSave, error) {
- fortypeTimes++
- assert.Equal(t, rowType, reflect.TypeOf(TestTableData))
- err := d.onNewBatchSave(rowType)
- assert.Equal(t, err, nil)
-
- return &BatchSave{}, nil
- })
- defer gf.Reset()
-
- addTimes := 0
- gsv := gomonkey.ApplyMethod(reflect.TypeOf(&BatchSave{}), "Add", func(c *BatchSave, slot interface{}) error {
- addTimes++
- assert.Equal(t, slot, TestTableData)
- return nil
- })
- defer gsv.Reset()
-
- // begin testing
- err := dataConvertor.Execute()
- assert.Equal(t, err, nil)
- assert.Equal(t, scanrowTimes, TestDataCount)
- assert.Equal(t, fortypeTimes, TestDataCount*TestDataCount)
- assert.Equal(t, addTimes, TestDataCount*TestDataCount)
-}
-
-func TestDataConvertorExecute_Cancel(t *testing.T) {
- MockDB(t)
- defer UnMockDB()
-
- gt.Reset()
- gt = gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "Table", func(db *gorm.DB, name string, args ...interface{}) *gorm.DB {
- assert.Equal(t, name, "_raw_"+TestTableData.TableName())
- return db
- },
- )
-
- dataConvertor, _ := CreateTestDataConverter(t)
-
- gn := gomonkey.ApplyMethod(reflect.TypeOf(&sql.Rows{}), "Next", func(r *sql.Rows) bool {
- // death loop for testing cancel
- return true
- })
- defer gn.Reset()
-
- gcl := gomonkey.ApplyMethod(reflect.TypeOf(&sql.Rows{}), "Close", func(r *sql.Rows) error {
- return nil
- })
- defer gcl.Reset()
-
- gs.Reset()
-
- scanrowTimes := 0
- gs = gomonkey.ApplyMethod(reflect.TypeOf(&gorm.DB{}), "ScanRows", func(db *gorm.DB, rows *sql.Rows, dest interface{}) error {
- scanrowTimes++
- *dest.(*TestTable) = *TestTableData
- return nil
- },
- )
-
- fortypeTimes := 0
- gf := gomonkey.ApplyMethod(reflect.TypeOf(&BatchSaveDivider{}), "ForType", func(d *BatchSaveDivider, rowType reflect.Type, log core.Logger) (*BatchSave, error) {
- fortypeTimes++
- assert.Equal(t, rowType, reflect.TypeOf(TestTableData))
- err := d.onNewBatchSave(rowType)
- assert.Equal(t, err, nil)
-
- return &BatchSave{}, nil
- })
- defer gf.Reset()
-
- addTimes := 0
- gsv := gomonkey.ApplyMethod(reflect.TypeOf(&BatchSave{}), "Add", func(c *BatchSave, slot interface{}) error {
- addTimes++
- assert.Equal(t, slot, TestTableData)
- return nil
- })
- defer gsv.Reset()
-
- go func() {
- time.Sleep(time.Duration(500) * time.Microsecond)
- Cancel()
- }()
-
- err := dataConvertor.Execute()
- assert.Equal(t, err, fmt.Errorf("context canceled"))
-}