| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
. "gorm.io/gorm/utils/tests" |
| 7 |
) |
| 8 |
|
| 9 |
func TestExceptionsWithInvalidSql(t *testing.T) { |
| 10 |
if name := DB.Dialector.Name(); name == "sqlserver" { |
| 11 |
t.Skip("skip sqlserver due to it will raise data race for invalid sql") |
| 12 |
} |
| 13 |
|
| 14 |
var columns []string |
| 15 |
if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil { |
| 16 |
t.Errorf("Should got error with invalid SQL") |
| 17 |
} |
| 18 |
|
| 19 |
if DB.Model(&User{}).Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil { |
| 20 |
t.Errorf("Should got error with invalid SQL") |
| 21 |
} |
| 22 |
|
| 23 |
if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Find(&User{}).Error == nil { |
| 24 |
t.Errorf("Should got error with invalid SQL") |
| 25 |
} |
| 26 |
|
| 27 |
var count1, count2 int64 |
| 28 |
DB.Model(&User{}).Count(&count1) |
| 29 |
if count1 <= 0 { |
| 30 |
t.Errorf("Should find some users") |
| 31 |
} |
| 32 |
|
| 33 |
if DB.Where("name = ?", "jinzhu; delete * from users").First(&User{}).Error == nil { |
| 34 |
t.Errorf("Should got error with invalid SQL") |
| 35 |
} |
| 36 |
|
| 37 |
DB.Model(&User{}).Count(&count2) |
| 38 |
if count1 != count2 { |
| 39 |
t.Errorf("No user should not be deleted by invalid SQL") |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestSetAndGet(t *testing.T) { |
| 44 |
if value, ok := DB.Set("hello", "world").Get("hello"); !ok { |
| 45 |
t.Errorf("Should be able to get setting after set") |
| 46 |
} else if value.(string) != "world" { |
| 47 |
t.Errorf("Set value should not be changed") |
| 48 |
} |
| 49 |
|
| 50 |
if _, ok := DB.Get("non_existing"); ok { |
| 51 |
t.Errorf("Get non existing key should return error") |
| 52 |
} |
| 53 |
} |