2020-05-31 13:51:43 +03:00
|
|
|
package tests_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-06-02 05:34:50 +03:00
|
|
|
. "gorm.io/gorm/utils/tests"
|
2020-05-31 13:51:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExceptionsWithInvalidSql(t *testing.T) {
|
2020-06-24 14:09:19 +03:00
|
|
|
if name := DB.Dialector.Name(); name == "sqlserver" {
|
|
|
|
t.Skip("skip sqlserver due to it will raise data race for invalid sql")
|
|
|
|
}
|
|
|
|
|
2020-05-31 13:51:43 +03:00
|
|
|
var columns []string
|
|
|
|
if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
|
|
|
|
t.Errorf("Should got error with invalid SQL")
|
|
|
|
}
|
|
|
|
|
|
|
|
if DB.Model(&User{}).Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
|
|
|
|
t.Errorf("Should got error with invalid SQL")
|
|
|
|
}
|
|
|
|
|
|
|
|
if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Find(&User{}).Error == nil {
|
|
|
|
t.Errorf("Should got error with invalid SQL")
|
|
|
|
}
|
|
|
|
|
|
|
|
var count1, count2 int64
|
|
|
|
DB.Model(&User{}).Count(&count1)
|
|
|
|
if count1 <= 0 {
|
|
|
|
t.Errorf("Should find some users")
|
|
|
|
}
|
|
|
|
|
|
|
|
if DB.Where("name = ?", "jinzhu; delete * from users").First(&User{}).Error == nil {
|
|
|
|
t.Errorf("Should got error with invalid SQL")
|
|
|
|
}
|
|
|
|
|
|
|
|
DB.Model(&User{}).Count(&count2)
|
|
|
|
if count1 != count2 {
|
|
|
|
t.Errorf("No user should not be deleted by invalid SQL")
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 15:42:07 +03:00
|
|
|
|
|
|
|
func TestSetAndGet(t *testing.T) {
|
|
|
|
if value, ok := DB.Set("hello", "world").Get("hello"); !ok {
|
|
|
|
t.Errorf("Should be able to get setting after set")
|
|
|
|
} else {
|
|
|
|
if value.(string) != "world" {
|
|
|
|
t.Errorf("Setted value should not be changed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := DB.Get("non_existing"); ok {
|
|
|
|
t.Errorf("Get non existing key should return error")
|
|
|
|
}
|
|
|
|
}
|