Skip to content

Commit

Permalink
test: add test for LoadOrStoreVisitMap
Browse files Browse the repository at this point in the history
  • Loading branch information
a631807682 committed Mar 13, 2022
1 parent 9d5c68e commit 6e3ca2d
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions tests/callbacks_test.go
Expand Up @@ -2,12 +2,13 @@ package tests_test

import (
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
. "gorm.io/gorm/utils/tests"
"reflect"
"runtime"
"strings"
"testing"

"gorm.io/gorm"
)

func assertCallbacks(v interface{}, fnames []string) (result bool, msg string) {
Expand Down Expand Up @@ -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)
}

0 comments on commit 6e3ca2d

Please sign in to comment.