From 882c001f472325f0d4d5e23bec8cc746c91df09c Mon Sep 17 00:00:00 2001 From: Luka Giorgadze Date: Wed, 27 Dec 2023 17:39:15 +0400 Subject: [PATCH] add enum tests update enum test --- gonull_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/gonull_test.go b/gonull_test.go index badebd7..d4e3bed 100644 --- a/gonull_test.go +++ b/gonull_test.go @@ -253,6 +253,37 @@ func TestConvertToTypeFromInt64(t *testing.T) { } } +func TestNullableScanWithCustomEnum(t *testing.T) { + type TestEnum float32 + + const ( + TestEnumA TestEnum = iota + TestEnumB + ) + + type TestModel struct { + ID int + Field gonull.Nullable[TestEnum] + } + + // Simulate the scenario where the SQL driver returns an int64 + // This is common as database integer types are usually scanned as int64 in Go + // + // sqlReturnedValue (int64(0)) is convertible to float32. + // The converted value 0 (as float32) matches TestEnumA, which is also 0 when converted to float32. + sqlReturnedValue := int64(0) + + model := TestModel{ID: 1, Field: gonull.NewNullable(TestEnumA)} + + err := model.Field.Scan(sqlReturnedValue) + if err != nil { + assert.Error(t, err, "Scan failed with unsupported type conversion") + } else { + assert.Equal(t, TestEnumA, model.Field.Val, "Scanned value does not match expected enum value") + } + +} + type testStruct struct { Foo gonull.Nullable[*string] `json:"foo"` }