test: add test for LoadOrStoreVisitMap

This commit is contained in:
a631807682 2022-03-13 12:52:08 +08:00
parent 9d5c68e410
commit 6e3ca2d1aa
1 changed files with 26 additions and 2 deletions

View File

@ -2,12 +2,13 @@ package tests_test
import ( import (
"fmt" "fmt"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
. "gorm.io/gorm/utils/tests"
"reflect" "reflect"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"gorm.io/gorm"
) )
func assertCallbacks(v interface{}, fnames []string) (result bool, msg string) { func assertCallbacks(v interface{}, fnames []string) (result bool, msg string) {
@ -168,3 +169,26 @@ func TestCallbacks(t *testing.T) {
} }
} }
} }
func TestLoadOrStoreVisitMap(t *testing.T) {
var vistMap callbacks.VisitMap
var loaded bool
type testM struct {
Name string
}
t1 := testM{Name: "t1"}
t2 := testM{Name: "t2"}
t3 := testM{Name: "t3"}
vistMap = make(callbacks.VisitMap)
loaded = callbacks.LoadOrStoreVisitMap(&vistMap, &t1)
AssertEqual(t, loaded, false)
loaded = callbacks.LoadOrStoreVisitMap(&vistMap, &t1)
AssertEqual(t, loaded, true)
// t1 already exist but t2 not
loaded = callbacks.LoadOrStoreVisitMap(&vistMap, []*testM{&t1, &t2, &t3})
AssertEqual(t, loaded, false)
loaded = callbacks.LoadOrStoreVisitMap(&vistMap, []*testM{&t2, &t3})
AssertEqual(t, loaded, true)
}