test TestNotFound

This commit is contained in:
chyroc 2019-10-20 20:52:51 +08:00
parent 9a80de3090
commit 33761d423f
No known key found for this signature in database
GPG Key ID: 5CEACB2EE6A2FEFA

View File

@ -12,6 +12,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -651,6 +652,31 @@ func TestRaw(t *testing.T) {
} }
} }
func TestNotFound(t *testing.T) {
var user User
err := DB.Where("name = ?", "not found").First(&user).Error
if err != gorm.ErrRecordNotFound {
t.Error("should not found")
}
err = fmt.Errorf("get user fail: %w", err)
gover := strings.TrimPrefix(runtime.Version(), "go")
govers := strings.Split(gover, ".")
majorVer, err1 := strconv.Atoi(govers[0])
minorVer, err2 := strconv.Atoi(govers[1])
if err1 != nil || err2 != nil {
t.Errorf("invalid go version %s", gover)
}
if majorVer >= 1 && minorVer >= 13 {
if !gorm.IsRecordNotFoundError(err) {
t.Errorf("%s should IsRecordNotFoundError", err)
}
t.Logf("err is %s, >=1.13, test IsRecordNotFoundError success", err)
} else {
t.Logf("err is %s, <1.13, skip test", err)
}
}
func TestGroup(t *testing.T) { func TestGroup(t *testing.T) {
rows, err := DB.Select("name").Table("users").Group("name").Rows() rows, err := DB.Select("name").Table("users").Group("name").Rows()