From 6e3ca2d1aa09943dcfb5d9a4b93bea28212f71be Mon Sep 17 00:00:00 2001 From: a631807682 <631807682@qq.com> Date: Sun, 13 Mar 2022 12:52:08 +0800 Subject: [PATCH] test: add test for LoadOrStoreVisitMap --- tests/callbacks_test.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/callbacks_test.go b/tests/callbacks_test.go index 02765b8c..e5a4a1c1 100644 --- a/tests/callbacks_test.go +++ b/tests/callbacks_test.go @@ -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) { @@ -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) +}