From 6f83955657f25663c388ab51aff002e49b76c6c9 Mon Sep 17 00:00:00 2001 From: Chef Date: Thu, 22 Feb 2024 21:19:49 +0700 Subject: [PATCH 1/2] CHORE add unittest test function ConvertMapToValueForCreate --- callbacks/convert_map_test.go | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 callbacks/convert_map_test.go diff --git a/callbacks/convert_map_test.go b/callbacks/convert_map_test.go new file mode 100644 index 000000000..48b3933b8 --- /dev/null +++ b/callbacks/convert_map_test.go @@ -0,0 +1,67 @@ +package callbacks + +import ( + "reflect" + "testing" + + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +func TestConvertMapToValuesForCreate(t *testing.T) { + testCase := []struct { + name string + input map[string]interface{} + expect clause.Values + }{ + { + name: "Test convert string value", + input: map[string]interface{}{ + "name": "my name", + }, + expect: clause.Values{ + Columns: []clause.Column{{Name: "name"}}, + Values: [][]interface{}{{"my name"}}, + }, + }, + { + name: "Test convert int value", + input: map[string]interface{}{ + "age": 18, + }, + expect: clause.Values{ + Columns: []clause.Column{{Name: "age"}}, + Values: [][]interface{}{{18}}, + }, + }, + { + name: "Test convert float value", + input: map[string]interface{}{ + "score": 99.5, + }, + expect: clause.Values{ + Columns: []clause.Column{{Name: "score"}}, + Values: [][]interface{}{{99.5}}, + }, + }, + { + name: "Test convert bool value", + input: map[string]interface{}{ + "active": true, + }, + expect: clause.Values{ + Columns: []clause.Column{{Name: "active"}}, + Values: [][]interface{}{{true}}, + }, + }, + } + + for _, tc := range testCase { + t.Run(tc.name, func(t *testing.T) { + actual := ConvertMapToValuesForCreate(&gorm.Statement{}, tc.input) + if !reflect.DeepEqual(actual, tc.expect) { + t.Errorf("expect %v got %v", tc.expect, actual) + } + }) + } +} From 6d975db6e83d5909fe255f73789f9e1e2de520e7 Mon Sep 17 00:00:00 2001 From: Chef Date: Fri, 23 Feb 2024 11:44:49 +0700 Subject: [PATCH 2/2] CHORE move the test cases located in the files convert_map_test.go and visit_map_test.go into the file helper_test.go. --- .../{convert_map_test.go => helper_test.go} | 30 ++++++++++++++++ callbacks/visit_map_test.go | 36 ------------------- 2 files changed, 30 insertions(+), 36 deletions(-) rename callbacks/{convert_map_test.go => helper_test.go} (65%) delete mode 100644 callbacks/visit_map_test.go diff --git a/callbacks/convert_map_test.go b/callbacks/helper_test.go similarity index 65% rename from callbacks/convert_map_test.go rename to callbacks/helper_test.go index 48b3933b8..6b76a415a 100644 --- a/callbacks/convert_map_test.go +++ b/callbacks/helper_test.go @@ -8,6 +8,36 @@ import ( "gorm.io/gorm/clause" ) +func TestLoadOrStoreVisitMap(t *testing.T) { + var vm visitMap + var loaded bool + type testM struct { + Name string + } + + t1 := testM{Name: "t1"} + t2 := testM{Name: "t2"} + t3 := testM{Name: "t3"} + + vm = make(visitMap) + if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf(&t1)); loaded { + t.Fatalf("loaded should be false") + } + + if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf(&t1)); !loaded { + t.Fatalf("loaded should be true") + } + + // t1 already exist but t2 not + if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf([]*testM{&t1, &t2, &t3})); loaded { + t.Fatalf("loaded should be false") + } + + if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf([]*testM{&t2, &t3})); !loaded { + t.Fatalf("loaded should be true") + } +} + func TestConvertMapToValuesForCreate(t *testing.T) { testCase := []struct { name string diff --git a/callbacks/visit_map_test.go b/callbacks/visit_map_test.go deleted file mode 100644 index b1fb86dbe..000000000 --- a/callbacks/visit_map_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package callbacks - -import ( - "reflect" - "testing" -) - -func TestLoadOrStoreVisitMap(t *testing.T) { - var vm visitMap - var loaded bool - type testM struct { - Name string - } - - t1 := testM{Name: "t1"} - t2 := testM{Name: "t2"} - t3 := testM{Name: "t3"} - - vm = make(visitMap) - if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf(&t1)); loaded { - t.Fatalf("loaded should be false") - } - - if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf(&t1)); !loaded { - t.Fatalf("loaded should be true") - } - - // t1 already exist but t2 not - if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf([]*testM{&t1, &t2, &t3})); loaded { - t.Fatalf("loaded should be false") - } - - if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf([]*testM{&t2, &t3})); !loaded { - t.Fatalf("loaded should be true") - } -}