From 2d47db83ea027988a9809c17a12f25321eba4c7c Mon Sep 17 00:00:00 2001 From: wschoepke Date: Fri, 17 Jan 2025 11:46:00 +0100 Subject: [PATCH] test: add unit tests for event content unmarshaller --- ...aller.go => event_content_unmarshaller.go} | 0 .../api/event_content_unmarshaller_test.go | 60 +++++++++++++++++++ 2 files changed, 60 insertions(+) rename server/src/api/{event_unmarshaller.go => event_content_unmarshaller.go} (100%) create mode 100644 server/src/api/event_content_unmarshaller_test.go diff --git a/server/src/api/event_unmarshaller.go b/server/src/api/event_content_unmarshaller.go similarity index 100% rename from server/src/api/event_unmarshaller.go rename to server/src/api/event_content_unmarshaller.go diff --git a/server/src/api/event_content_unmarshaller_test.go b/server/src/api/event_content_unmarshaller_test.go new file mode 100644 index 0000000000..e98d87ea84 --- /dev/null +++ b/server/src/api/event_content_unmarshaller_test.go @@ -0,0 +1,60 @@ +package api + +import ( + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "reflect" + "testing" +) + +type TestStruct struct { + ID string `json:"id"` +} + +func TestCorrectString(t *testing.T) { + given := "TEST_STRING" + actual, err := unmarshal[string](given) + + assert.NoError(t, err) + assert.Equal(t, given, *actual) +} + +func TestCorrectStringSlice(t *testing.T) { + s := "TEST_STRING" + given := []*string{&s} + actual, err := unmarshalSlice[string](given) + + assert.NoError(t, err) + assert.Equal(t, given, actual) +} + +func TestCorrectEmptySlice(t *testing.T) { + var given []*string + actual, err := unmarshalSlice[string](given) + + assert.NoError(t, err) + assert.Equal(t, given, actual) +} + +func TestCorrectUUID(t *testing.T) { + given, _ := uuid.NewRandom() + actual, err := unmarshal[uuid.UUID](given) + + assert.NoError(t, err) + assert.Equal(t, given, *actual) +} + +func TestCorrectInterfaceTypeStruct(t *testing.T) { + given := "TEST_ID" + actual, err := unmarshal[TestStruct](reflect.ValueOf(TestStruct{ID: given}).Interface()) + + assert.NoError(t, err) + assert.Equal(t, given, actual.ID) +} + +func TestNil(t *testing.T) { + actual, err := unmarshal[TestStruct](nil) + + assert.NoError(t, err) + assert.Nil(t, actual) +}