Skip to content

Commit

Permalink
add test specific to trigger notification
Browse files Browse the repository at this point in the history
  • Loading branch information
chicco785 committed Oct 13, 2023
1 parent 51937fa commit 649dc20
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions pkg/task/v1/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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{}
Expand All @@ -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{}
Expand All @@ -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{}
Expand Down

0 comments on commit 649dc20

Please sign in to comment.