Merge branch 'develop' of https://github.com/beego/beego into develop

This commit is contained in:
holooooo
2021-05-22 15:00:25 +08:00
82 changed files with 828 additions and 510 deletions

View File

@@ -99,9 +99,7 @@ func TestCache(t *testing.T) {
vv, _ := bm.GetMulti(context.Background(), []string{"astaxie", "astaxie1"})
assert.Equal(t, 2, len(vv))
assert.Equal(t, "author", vv[0])
assert.Equal(t,"author1", vv[1])
assert.Equal(t, "author1", vv[1])
vv, err = bm.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"})
assert.Equal(t, 2, len(vv))
@@ -116,7 +114,7 @@ func TestFileCache(t *testing.T) {
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
assert.Nil(t, err)
timeoutDuration := 10 * time.Second
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
res, _ := bm.IsExist(context.Background(), "astaxie")
assert.True(t, res)
@@ -179,7 +177,6 @@ func testIncrDecr(t *testing.T, c Cache, beforeIncr interface{}, afterIncr inter
assert.Nil(t, c.Put(ctx, key, beforeIncr, timeout))
assert.Nil(t, c.Incr(ctx, key))
v, _ := c.Get(ctx, key)
assert.Equal(t, afterIncr, v)

View File

@@ -9,11 +9,9 @@ import (
var (
ErrIncrementOverflow = berror.Error(IncrementOverflow, "this incr invocation will overflow.")
ErrDecrementOverflow = berror.Error(DecrementOverflow, "this decr invocation will overflow.")
ErrNotIntegerType = berror.Error(NotIntegerType, "item val is not (u)int (u)int32 (u)int64")
ErrNotIntegerType = berror.Error(NotIntegerType, "item val is not (u)int (u)int32 (u)int64")
)
func incr(originVal interface{}) (interface{}, error) {
switch val := originVal.(type) {
case int:

View File

@@ -123,14 +123,6 @@ var InvalidSsdbCacheValue = berror.DefineCode(4002022, moduleName, "InvalidSsdbC
SSDB cache only accept string value. Please check your input.
`)
var DeleteFileCacheItemFailed = berror.DefineCode(5002001, moduleName, "DeleteFileCacheItemFailed", `
Beego try to delete file cache item failed.
Please check whether Beego generated file correctly.
@@ -179,4 +171,4 @@ Usually it indicates something wrong on server side.
`)
var ErrKeyExpired = berror.Error(KeyExpired, "the key is expired")
var ErrKeyNotExist = berror.Error(KeyNotExist, "the key isn't exist")
var ErrKeyNotExist = berror.Error(KeyNotExist, "the key isn't exist")

View File

@@ -140,7 +140,7 @@ func (fc *FileCache) getCacheFileName(key string) (string, error) {
return "", err
}
if !ok {
err = os.MkdirAll(cachePath, os.ModePerm)
err = os.MkdirAll(cachePath, os.ModePerm)
if err != nil {
return "", berror.Wrapf(err, CreateFileCacheDirFailed,
"could not create the directory: %s", cachePath)
@@ -299,8 +299,8 @@ func FileGetContents(filename string) ([]byte, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, berror.Wrapf(err, ReadFileCacheContentFailed,
"could not read the data from the file: %s, " +
"please confirm that file exist and Beego has the permission to read the content.", filename)
"could not read the data from the file: %s, "+
"please confirm that file exist and Beego has the permission to read the content.", filename)
}
return data, nil
}

View File

@@ -105,4 +105,4 @@ func TestFileCacheDelete(t *testing.T) {
func getTestCacheFilePath() string {
return filepath.Join(os.TempDir(), "test", "file.txt")
}
}

View File

@@ -71,8 +71,8 @@ func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, er
mv, err := rc.conn.GetMulti(keys)
if err != nil {
return rv, berror.Wrapf(err, cache.MemCacheCurdFailed,
"could not read multiple key-values from memcache, " +
"please check your keys, network and connection. Root cause: %s",
"could not read multiple key-values from memcache, "+
"please check your keys, network and connection. Root cause: %s",
err.Error())
}

View File

@@ -74,7 +74,7 @@ func TestMemcacheCache(t *testing.T) {
res, _ = bm.IsExist(context.Background(), "astaxie")
assert.False(t, res)
assert.Nil(t,bm.Put(context.Background(), "astaxie", "author", timeoutDuration) )
assert.Nil(t, bm.Put(context.Background(), "astaxie", "author", timeoutDuration))
// test string
res, _ = bm.IsExist(context.Background(), "astaxie")
assert.True(t, res)
@@ -86,7 +86,6 @@ func TestMemcacheCache(t *testing.T) {
// test GetMulti
assert.Nil(t, bm.Put(context.Background(), "astaxie1", "author1", timeoutDuration))
res, _ = bm.IsExist(context.Background(), "astaxie1")
assert.True(t, res)

View File

@@ -40,7 +40,6 @@ func TestRedisCache(t *testing.T) {
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
res, _ := bm.IsExist(context.Background(), "astaxie")
assert.True(t, res)
@@ -51,7 +50,6 @@ func TestRedisCache(t *testing.T) {
assert.Nil(t, bm.Put(context.Background(), "astaxie", 1, timeoutDuration))
val, _ := bm.Get(context.Background(), "astaxie")
v, _ := redis.Int(val, err)
assert.Equal(t, 1, v)

View File

@@ -30,7 +30,7 @@ func TestSsdbcacheCache(t *testing.T) {
timeoutDuration := 3 * time.Second
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration))
assert.Nil(t, ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration))
res, _ = ssdb.IsExist(context.Background(), "ssdb")
assert.True(t, res)
@@ -87,7 +87,7 @@ func TestSsdbcacheCache(t *testing.T) {
assert.Equal(t, 2, len(vv))
assert.Equal(t, "ssdb", vv[0])
assert.Nil(t, vv[1])
assert.Nil(t, vv[1])
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "key not exist"))

View File

@@ -20,8 +20,9 @@ import (
"testing"
"time"
"github.com/beego/beego/v2/client/httplib"
"github.com/stretchr/testify/assert"
"github.com/beego/beego/v2/client/httplib"
)
func TestFilterChain(t *testing.T) {

View File

@@ -351,7 +351,7 @@ func TestNewBeegoRequest(t *testing.T) {
assert.NotNil(t, req)
}
func TestBeegoHTTPRequest_SetProtocolVersion(t *testing.T) {
func TestBeegoHTTPRequestSetProtocolVersion(t *testing.T) {
req := NewBeegoRequest("http://beego.me", "GET")
req.SetProtocolVersion("HTTP/3.10")
assert.Equal(t, "HTTP/3.10", req.req.Proto)
@@ -376,21 +376,21 @@ func TestPut(t *testing.T) {
assert.Equal(t, "PUT", req.req.Method)
}
func TestBeegoHTTPRequest_Header(t *testing.T) {
func TestBeegoHTTPRequestHeader(t *testing.T) {
req := Post("http://beego.me")
key, value := "test-header", "test-header-value"
req.Header(key, value)
assert.Equal(t, value, req.req.Header.Get(key))
}
func TestBeegoHTTPRequest_SetHost(t *testing.T) {
func TestBeegoHTTPRequestSetHost(t *testing.T) {
req := Post("http://beego.me")
host := "test-hose"
req.SetHost(host)
assert.Equal(t, host, req.req.Host)
}
func TestBeegoHTTPRequest_Param(t *testing.T) {
func TestBeegoHTTPRequestParam(t *testing.T) {
req := Post("http://beego.me")
key, value := "test-param", "test-param-value"
req.Param(key, value)
@@ -401,7 +401,7 @@ func TestBeegoHTTPRequest_Param(t *testing.T) {
assert.Equal(t, value1, req.params[key][1])
}
func TestBeegoHTTPRequest_Body(t *testing.T) {
func TestBeegoHTTPRequestBody(t *testing.T) {
req := Post("http://beego.me")
body := `hello, world`
req.Body([]byte(body))
@@ -409,7 +409,7 @@ func TestBeegoHTTPRequest_Body(t *testing.T) {
assert.NotNil(t, req.req.GetBody)
assert.NotNil(t, req.req.Body)
body = "hhhh, i am test"
body = "hhhh, I am test"
req.Body(body)
assert.Equal(t, int64(len(body)), req.req.ContentLength)
assert.NotNil(t, req.req.GetBody)
@@ -423,7 +423,7 @@ type user struct {
Name string `xml:"name"`
}
func TestBeegoHTTPRequest_XMLBody(t *testing.T) {
func TestBeegoHTTPRequestXMLBody(t *testing.T) {
req := Post("http://beego.me")
body := &user{
Name: "Tom",

View File

@@ -19,10 +19,9 @@ import (
"reflect"
"strings"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/bean"
"github.com/beego/beego/v2/core/logs"
)
// DefaultValueFilterChainBuilder only works for InsertXXX method,

View File

@@ -21,9 +21,9 @@ import (
"sync"
"testing"
"github.com/beego/beego/v2/core/utils"
"github.com/stretchr/testify/assert"
"github.com/beego/beego/v2/core/utils"
)
func TestFilterOrmDecorator_Read(t *testing.T) {

View File

@@ -164,4 +164,4 @@ func MockRollback(err error) *Mock {
// MockRollbackUnlessCommit support RollbackUnlessCommit
func MockRollbackUnlessCommit(err error) *Mock {
return NewMock(NewSimpleCondition("", "RollbackUnlessCommit"), []interface{}{err}, nil)
}
}

View File

@@ -241,7 +241,7 @@ func TestTransactionRollback(t *testing.T) {
assert.Equal(t, mock, err)
}
func TestTransactionRollbackUnlessCommit(t *testing.T) {
func TestTransactionRollbackUnlessCommit(t *testing.T) {
s := StartMock()
defer s.Clear()
mock := errors.New(mockErrorMsg)

View File

@@ -25,8 +25,6 @@ import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
// As tidb can't use go get, so disable the tidb testing now
// _ "github.com/pingcap/tidb"
)
// A slice string field.

View File

@@ -63,11 +63,9 @@ import (
"time"
"github.com/beego/beego/v2/client/orm/clauses/order_clause"
"github.com/beego/beego/v2/client/orm/hints"
"github.com/beego/beego/v2/core/utils"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/core/utils"
)
// DebugQueries define the debug

View File

@@ -31,11 +31,10 @@ import (
"testing"
"time"
"github.com/beego/beego/v2/client/orm/clauses/order_clause"
"github.com/beego/beego/v2/client/orm/hints"
"github.com/stretchr/testify/assert"
"github.com/beego/beego/v2/client/orm/clauses/order_clause"
"github.com/beego/beego/v2/client/orm/hints"
)
var _ = os.PathSeparator
@@ -2269,7 +2268,6 @@ func TestTransaction(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, int64(1), num)
}
func TestTxOrmRollbackUnlessCommit(t *testing.T) {
@@ -2642,93 +2640,101 @@ func TestIgnoreCaseTag(t *testing.T) {
func TestInsertOrUpdate(t *testing.T) {
RegisterModel(new(User))
user := User{UserName: "unique_username133", Status: 1, Password: "o"}
user1 := User{UserName: "unique_username133", Status: 2, Password: "o"}
user2 := User{UserName: "unique_username133", Status: 3, Password: "oo"}
userName := "unique_username133"
column := "user_name"
user := User{UserName: userName, Status: 1, Password: "o"}
user1 := User{UserName: userName, Status: 2, Password: "o"}
user2 := User{UserName: userName, Status: 3, Password: "oo"}
dORM.Insert(&user)
test := User{UserName: "unique_username133"}
fmt.Println(dORM.Driver().Name())
if dORM.Driver().Name() == "sqlite3" {
fmt.Println("sqlite3 is nonsupport")
return
}
// test1
_, err := dORM.InsertOrUpdate(&user1, "user_name")
if err != nil {
fmt.Println(err)
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
} else {
throwFailNow(t, err)
}
} else {
dORM.Read(&test, "user_name")
throwFailNow(t, AssertIs(user1.Status, test.Status))
}
// test2
_, err = dORM.InsertOrUpdate(&user2, "user_name")
if err != nil {
fmt.Println(err)
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
} else {
throwFailNow(t, err)
}
} else {
dORM.Read(&test, "user_name")
throwFailNow(t, AssertIs(user2.Status, test.Status))
throwFailNow(t, AssertIs(user2.Password, strings.TrimSpace(test.Password)))
specs := []struct {
description string
user User
colConflitAndArgs []string
assertion func(expected User, actual User)
isPostgresCompatible bool
}{
{
description: "test1",
user: user1,
colConflitAndArgs: []string{column},
assertion: func(expected, actual User) {
throwFailNow(t, AssertIs(expected.Status, actual.Status))
},
isPostgresCompatible: true,
},
{
description: "test2",
user: user2,
colConflitAndArgs: []string{column},
assertion: func(expected, actual User) {
throwFailNow(t, AssertIs(expected.Status, actual.Status))
throwFailNow(t, AssertIs(expected.Password, strings.TrimSpace(actual.Password)))
},
isPostgresCompatible: true,
},
{
description: "test3 +",
user: user2,
colConflitAndArgs: []string{column, "status=status+1"},
assertion: func(expected, actual User) {
throwFailNow(t, AssertIs(expected.Status+1, actual.Status))
},
isPostgresCompatible: false,
},
{
description: "test4 -",
user: user2,
colConflitAndArgs: []string{column, "status=status-1"},
assertion: func(expected, actual User) {
throwFailNow(t, AssertIs((expected.Status+1)-1, actual.Status))
},
isPostgresCompatible: false,
},
{
description: "test5 *",
user: user2,
colConflitAndArgs: []string{column, "status=status*3"},
assertion: func(expected, actual User) {
throwFailNow(t, AssertIs(((expected.Status+1)-1)*3, actual.Status))
},
isPostgresCompatible: false,
},
{
description: "test6 /",
user: user2,
colConflitAndArgs: []string{column, "Status=Status/3"},
assertion: func(expected, actual User) {
throwFailNow(t, AssertIs((((expected.Status+1)-1)*3)/3, actual.Status))
},
isPostgresCompatible: false,
},
}
// postgres ON CONFLICT DO UPDATE SET can`t use colu=colu+values
if IsPostgres {
return
}
// test3 +
_, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status+1")
if err != nil {
fmt.Println(err)
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
} else {
throwFailNow(t, err)
for _, spec := range specs {
// postgres ON CONFLICT DO UPDATE SET can`t use colu=colu+values
if IsPostgres && !spec.isPostgresCompatible {
continue
}
} else {
dORM.Read(&test, "user_name")
throwFailNow(t, AssertIs(user2.Status+1, test.Status))
}
// test4 -
_, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status-1")
if err != nil {
fmt.Println(err)
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
} else {
throwFailNow(t, err)
_, err := dORM.InsertOrUpdate(&spec.user, spec.colConflitAndArgs...)
if err != nil {
fmt.Println(err)
if !(err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego") {
throwFailNow(t, err)
}
continue
}
} else {
dORM.Read(&test, "user_name")
throwFailNow(t, AssertIs((user2.Status+1)-1, test.Status))
}
// test5 *
_, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status*3")
if err != nil {
fmt.Println(err)
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
} else {
throwFailNow(t, err)
}
} else {
dORM.Read(&test, "user_name")
throwFailNow(t, AssertIs(((user2.Status+1)-1)*3, test.Status))
}
// test6 /
_, err = dORM.InsertOrUpdate(&user2, "user_name", "Status=Status/3")
if err != nil {
fmt.Println(err)
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
} else {
throwFailNow(t, err)
}
} else {
dORM.Read(&test, "user_name")
throwFailNow(t, AssertIs((((user2.Status+1)-1)*3)/3, test.Status))
test := User{UserName: userName}
err = dORM.Read(&test, column)
throwFailNow(t, AssertIs(err, nil))
spec.assertion(spec.user, test)
}
}

View File

@@ -138,7 +138,6 @@ type txEnder interface {
RollbackUnlessCommit() error
}
// Data Manipulation Language
type DML interface {
// insert model data to database
@@ -241,7 +240,6 @@ type DriverGetter interface {
Driver() Driver
}
type ormer interface {
DQL
DML