Merge pull request #4609 from y4h2/develop
Update orm_test.go/TestInsertOrUpdate with table-driven.
This commit is contained in:
commit
9668949822
@ -1,4 +1,5 @@
|
|||||||
# developing
|
# developing
|
||||||
|
- Update orm_test.go/TestInsertOrUpdate with table-driven. [4609](https://github.com/beego/beego/pull/4609)
|
||||||
- Add: Resp() method for web.Controller. [4588](https://github.com/beego/beego/pull/4588)
|
- Add: Resp() method for web.Controller. [4588](https://github.com/beego/beego/pull/4588)
|
||||||
- Web mock and test support. [4565](https://github.com/beego/beego/pull/4565) [4574](https://github.com/beego/beego/pull/4574)
|
- Web mock and test support. [4565](https://github.com/beego/beego/pull/4565) [4574](https://github.com/beego/beego/pull/4574)
|
||||||
- Error codes definition of cache module. [4493](https://github.com/beego/beego/pull/4493)
|
- Error codes definition of cache module. [4493](https://github.com/beego/beego/pull/4493)
|
||||||
|
|||||||
@ -2269,7 +2269,6 @@ func TestTransaction(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, int64(1), num)
|
assert.Equal(t, int64(1), num)
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTxOrmRollbackUnlessCommit(t *testing.T) {
|
func TestTxOrmRollbackUnlessCommit(t *testing.T) {
|
||||||
@ -2642,93 +2641,101 @@ func TestIgnoreCaseTag(t *testing.T) {
|
|||||||
|
|
||||||
func TestInsertOrUpdate(t *testing.T) {
|
func TestInsertOrUpdate(t *testing.T) {
|
||||||
RegisterModel(new(User))
|
RegisterModel(new(User))
|
||||||
user := User{UserName: "unique_username133", Status: 1, Password: "o"}
|
userName := "unique_username133"
|
||||||
user1 := User{UserName: "unique_username133", Status: 2, Password: "o"}
|
column := "user_name"
|
||||||
user2 := User{UserName: "unique_username133", Status: 3, Password: "oo"}
|
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)
|
dORM.Insert(&user)
|
||||||
test := User{UserName: "unique_username133"}
|
|
||||||
fmt.Println(dORM.Driver().Name())
|
fmt.Println(dORM.Driver().Name())
|
||||||
if dORM.Driver().Name() == "sqlite3" {
|
if dORM.Driver().Name() == "sqlite3" {
|
||||||
fmt.Println("sqlite3 is nonsupport")
|
fmt.Println("sqlite3 is nonsupport")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// test1
|
|
||||||
_, err := dORM.InsertOrUpdate(&user1, "user_name")
|
specs := []struct {
|
||||||
if err != nil {
|
description string
|
||||||
fmt.Println(err)
|
user User
|
||||||
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
|
colConflitAndArgs []string
|
||||||
} else {
|
assertion func(expected User, actual User)
|
||||||
throwFailNow(t, err)
|
isPostgresCompatible bool
|
||||||
}
|
}{
|
||||||
} else {
|
{
|
||||||
dORM.Read(&test, "user_name")
|
description: "test1",
|
||||||
throwFailNow(t, AssertIs(user1.Status, test.Status))
|
user: user1,
|
||||||
}
|
colConflitAndArgs: []string{column},
|
||||||
// test2
|
assertion: func(expected, actual User) {
|
||||||
_, err = dORM.InsertOrUpdate(&user2, "user_name")
|
throwFailNow(t, AssertIs(expected.Status, actual.Status))
|
||||||
if err != nil {
|
},
|
||||||
fmt.Println(err)
|
isPostgresCompatible: true,
|
||||||
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
|
},
|
||||||
} else {
|
{
|
||||||
throwFailNow(t, err)
|
description: "test2",
|
||||||
}
|
user: user2,
|
||||||
} else {
|
colConflitAndArgs: []string{column},
|
||||||
dORM.Read(&test, "user_name")
|
assertion: func(expected, actual User) {
|
||||||
throwFailNow(t, AssertIs(user2.Status, test.Status))
|
throwFailNow(t, AssertIs(expected.Status, actual.Status))
|
||||||
throwFailNow(t, AssertIs(user2.Password, strings.TrimSpace(test.Password)))
|
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
|
for _, spec := range specs {
|
||||||
if IsPostgres {
|
// postgres ON CONFLICT DO UPDATE SET can`t use colu=colu+values
|
||||||
return
|
if IsPostgres && !spec.isPostgresCompatible {
|
||||||
}
|
continue
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
dORM.Read(&test, "user_name")
|
_, err := dORM.InsertOrUpdate(&spec.user, spec.colConflitAndArgs...)
|
||||||
throwFailNow(t, AssertIs(user2.Status+1, test.Status))
|
if err != nil {
|
||||||
}
|
fmt.Println(err)
|
||||||
// test4 -
|
if !(err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego") {
|
||||||
_, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status-1")
|
throwFailNow(t, err)
|
||||||
if err != nil {
|
}
|
||||||
fmt.Println(err)
|
continue
|
||||||
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")
|
test := User{UserName: userName}
|
||||||
throwFailNow(t, AssertIs((user2.Status+1)-1, test.Status))
|
err = dORM.Read(&test, column)
|
||||||
}
|
throwFailNow(t, AssertIs(err, nil))
|
||||||
// test5 *
|
spec.assertion(spec.user, test)
|
||||||
_, 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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user