From be340a2c54e283d40584f65787ec7ab6e2406d7c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 4 Jul 2024 17:20:15 +0200 Subject: [PATCH 1/5] Test types.MakeString() --- types/string_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 types/string_test.go diff --git a/types/string_test.go b/types/string_test.go new file mode 100644 index 00000000..9ea1de30 --- /dev/null +++ b/types/string_test.go @@ -0,0 +1,25 @@ +package types + +import ( + "database/sql" + "github.com/stretchr/testify/require" + "testing" +) + +func TestMakeString(t *testing.T) { + subtests := []struct { + name string + io string + }{ + {"empty", ""}, + {"nul", "\x00"}, + {"space", " "}, + {"multiple", "abc"}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + require.Equal(t, String{NullString: sql.NullString{String: st.io, Valid: true}}, MakeString(st.io)) + }) + } +} From 49d04b5c78d94ad6dfeaa4aebe9942bc4c1fe779 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 4 Jul 2024 17:38:09 +0200 Subject: [PATCH 2/5] Test types.String#MarshalJSON() --- types/string_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/types/string_test.go b/types/string_test.go index 9ea1de30..7de53971 100644 --- a/types/string_test.go +++ b/types/string_test.go @@ -4,6 +4,7 @@ import ( "database/sql" "github.com/stretchr/testify/require" "testing" + "unicode/utf8" ) func TestMakeString(t *testing.T) { @@ -23,3 +24,28 @@ func TestMakeString(t *testing.T) { }) } } + +func TestString_MarshalJSON(t *testing.T) { + subtests := []struct { + name string + input sql.NullString + output string + }{ + {"null", sql.NullString{}, `null`}, + {"invalid", sql.NullString{String: "abc"}, `null`}, + {"empty", sql.NullString{String: "", Valid: true}, `""`}, + {"nul", sql.NullString{String: "\x00", Valid: true}, `"\u0000"`}, + {"space", sql.NullString{String: " ", Valid: true}, `" "`}, + {"multiple", sql.NullString{String: "abc", Valid: true}, `"abc"`}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + actual, err := String{st.input}.MarshalJSON() + + require.NoError(t, err) + require.True(t, utf8.Valid(actual)) + require.Equal(t, st.output, string(actual)) + }) + } +} From 156ebc7af8e35f8ae21d76277272a43bc9b87e37 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 4 Jul 2024 17:51:09 +0200 Subject: [PATCH 3/5] Test types.String#UnmarshalText() --- types/string_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/types/string_test.go b/types/string_test.go index 7de53971..10f0c018 100644 --- a/types/string_test.go +++ b/types/string_test.go @@ -49,3 +49,24 @@ func TestString_MarshalJSON(t *testing.T) { }) } } + +func TestString_UnmarshalText(t *testing.T) { + subtests := []struct { + name string + io string + }{ + {"empty", ""}, + {"nul", "\x00"}, + {"space", " "}, + {"multiple", "abc"}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + var actual String + + require.NoError(t, actual.UnmarshalText([]byte(st.io))) + require.Equal(t, String{NullString: sql.NullString{String: st.io, Valid: true}}, actual) + }) + } +} From e592a7e60d875a33b567b05df28e287bb4934bba Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 4 Jul 2024 18:07:28 +0200 Subject: [PATCH 4/5] Test types.String#UnmarshalJSON() --- types/string_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/types/string_test.go b/types/string_test.go index 10f0c018..d039f0b6 100644 --- a/types/string_test.go +++ b/types/string_test.go @@ -70,3 +70,32 @@ func TestString_UnmarshalText(t *testing.T) { }) } } + +func TestString_UnmarshalJSON(t *testing.T) { + subtests := []struct { + name string + input string + output sql.NullString + error bool + }{ + {"null", `null`, sql.NullString{}, false}, + {"bool", `false`, sql.NullString{}, true}, + {"number", `0`, sql.NullString{}, true}, + {"empty", `""`, sql.NullString{String: "", Valid: true}, false}, + {"nul", `"\u0000"`, sql.NullString{String: "\x00", Valid: true}, false}, + {"space", `" "`, sql.NullString{String: " ", Valid: true}, false}, + {"multiple", `"abc"`, sql.NullString{String: "abc", Valid: true}, false}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + var actual String + if err := actual.UnmarshalJSON([]byte(st.input)); st.error { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, String{NullString: st.output}, actual) + } + }) + } +} From ab12f0a00a6e538d051784ab3a9e63acd0776f24 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 4 Jul 2024 18:12:04 +0200 Subject: [PATCH 5/5] Test types.String#Value() --- types/string_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/types/string_test.go b/types/string_test.go index d039f0b6..1ebc9927 100644 --- a/types/string_test.go +++ b/types/string_test.go @@ -99,3 +99,28 @@ func TestString_UnmarshalJSON(t *testing.T) { }) } } + +func TestString_Value(t *testing.T) { + subtests := []struct { + name string + input sql.NullString + output any + }{ + {"nil", sql.NullString{}, nil}, + {"invalid", sql.NullString{String: "abc"}, nil}, + {"empty", sql.NullString{String: "", Valid: true}, ""}, + {"nul", sql.NullString{String: "\x00", Valid: true}, ""}, + {"space", sql.NullString{String: " ", Valid: true}, " "}, + {"multiple", sql.NullString{String: "abc", Valid: true}, "abc"}, + {"nuls", sql.NullString{String: "\x00 \x00", Valid: true}, " "}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + actual, err := String{st.input}.Value() + + require.NoError(t, err) + require.Equal(t, st.output, actual) + }) + } +}