This commit is contained in:
chyroc 2019-10-20 20:58:48 +08:00
parent 33761d423f
commit 454fb120c7
No known key found for this signature in database
GPG Key ID: 5CEACB2EE6A2FEFA
2 changed files with 23 additions and 26 deletions

23
errors_13_test.go Normal file
View File

@ -0,0 +1,23 @@
// +build go1.13
package gorm_test
import (
"fmt"
"testing"
"github.com/jinzhu/gorm"
)
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)
if !gorm.IsRecordNotFoundError(err) {
t.Errorf("%s should IsRecordNotFoundError", err)
}
}

View File

@ -12,7 +12,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -652,31 +651,6 @@ 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()