From 649dc2068f2836a259d06e10cfd7c931cc80b1e6 Mon Sep 17 00:00:00 2001 From: "Federico M. Facca" Date: Fri, 13 Oct 2023 09:03:49 +0200 Subject: [PATCH] add test specific to trigger notification --- pkg/task/v1/task_test.go | 51 +++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/pkg/task/v1/task_test.go b/pkg/task/v1/task_test.go index a72eeac..f332742 100644 --- a/pkg/task/v1/task_test.go +++ b/pkg/task/v1/task_test.go @@ -11,17 +11,17 @@ import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" ) -func generateTask(taskId string, taskType int, createdAt int64) *Task { +func generateTask(taskId string, taskType TaskType, createdAt int64) *Task { return &Task{ Id: taskId, - TaskType: TaskType(taskType), + TaskType: taskType, CreatedAt: createdAt, } } func TestTask(t *testing.T) { for k := 0; k < 5; k++ { - test := generateTask(uuid.NewString(), k, time.Now().UnixNano()) + test := generateTask(uuid.NewString(), TaskType(k), time.Now().UnixNano()) buf, err := proto.Marshal(test) assert.NoError(t, err) data := &Task{} @@ -36,21 +36,26 @@ func TestTask(t *testing.T) { func generateNotification( notificationId string, - notificationType int, + notificationType NotificationType, createdAt int64, message string, ) *Notification { return &Notification{ Id: notificationId, - NotificationType: NotificationType(notificationType), + NotificationType: notificationType, CreatedAt: createdAt, Message: message, } } func TestNotification(t *testing.T) { - for k := 0; k < 4; k++ { - test := generateNotification(uuid.NewString(), k, time.Now().UnixNano(), "myMessage") + for k := 0; k < 5; k++ { + test := generateNotification( + uuid.NewString(), + NotificationType(k), + time.Now().UnixNano(), + "myMessage", + ) buf, err := proto.Marshal(test) assert.NoError(t, err) data := &Notification{} @@ -64,8 +69,36 @@ func TestNotification(t *testing.T) { } } +func TestTriggerNotification(t *testing.T) { + test := generateNotification( + uuid.NewString(), + NotificationType_NOTIFICATION_TYPE_TRIGGER, + time.Now().UnixNano(), + "1", //alternatively a string code + ) + test.TimestampID = func() *int64 { i := int64(4); return &i }() + test.ProducerID = func() *string { i := "CIM_PMU_CODE"; return &i }() + buf, err := proto.Marshal(test) + assert.NoError(t, err) + data := &Notification{} + err = proto.Unmarshal(buf, data) + assert.NoError(t, err) + assert.Equal(t, data.Id, test.Id) + assert.Equal(t, data.CreatedAt, test.CreatedAt) + assert.Equal(t, data.NotificationType, test.NotificationType) + assert.Equal(t, data.Message, "1") + assert.Equal(t, data.NotificationType, NotificationType_NOTIFICATION_TYPE_TRIGGER) + assert.Equal(t, *data.TimestampID, int64(4)) + assert.Equal(t, *data.ProducerID, "CIM_PMU_CODE") +} + func BenchmarkNotificationSerialization(b *testing.B) { - test := generateNotification(uuid.NewString(), rand.Intn(4), time.Now().UnixNano(), "myMessage") + test := generateNotification( + uuid.NewString(), + NotificationType(rand.Intn(4)), + time.Now().UnixNano(), + "myMessage", + ) for i := 0; i < b.N; i++ { buf, _ := proto.Marshal(test) conf := &Notification{} @@ -74,7 +107,7 @@ func BenchmarkNotificationSerialization(b *testing.B) { } func BenchmarkTaskSerialization(b *testing.B) { - test := generateTask(uuid.NewString(), rand.Intn(5), time.Now().UnixNano()) + test := generateTask(uuid.NewString(), TaskType(rand.Intn(5)), time.Now().UnixNano()) for i := 0; i < b.N; i++ { buf, _ := proto.Marshal(test) conf := &Task{}