From de973dfb75a868611f78976bce9b65f0a5f977c9 Mon Sep 17 00:00:00 2001 From: simross Date: Mon, 7 Oct 2019 21:17:09 +0300 Subject: [PATCH] test(data): add tests for data/consumer ref #57 --- data/consumer_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 data/consumer_test.go diff --git a/data/consumer_test.go b/data/consumer_test.go new file mode 100644 index 0000000..efcf39b --- /dev/null +++ b/data/consumer_test.go @@ -0,0 +1,111 @@ +package data + +import ( + "errors" + "reflect" + "testing" +) + +func TestConsumer_HandleConsumeSuccess(t *testing.T) { + tests := []struct { + name string + existingAlert *Alert + expectedAlert *Alert + }{ + {"alert nil", nil, nil}, + {"alert present, not recoverable", &Alert{Recoverable: false}, &Alert{Recoverable: false}}, + {"alert present, recoverable", &Alert{Recoverable: true}, nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := NewConsumer() + c.Alert = tt.existingAlert + c.HandleConsumeSuccess() + if !reflect.DeepEqual(c.Alert, tt.expectedAlert) { + t.Errorf("unexpected alert state after HandleConsumeSuccess(), want %v, got %v", tt.expectedAlert, c.Alert) + } + }) + } +} + +func TestConsumer_HandleConsumeFailure(t *testing.T) { + tests := []struct { + name string + title string + err error + sample *Sample + want *Alert + }{ + { + "basic", + "test", + errors.New("test"), + &Sample{ + Label: "test", + Value: "test", + Color: nil, + }, + &Alert{ + Title: "TEST", + Text: "test", + Color: nil, + Recoverable: true, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := NewConsumer() + c.HandleConsumeFailure(tt.title, tt.err, tt.sample) + got := <-c.AlertChannel + if !reflect.DeepEqual(tt.want, got) { + t.Errorf("unexpected alert state after HandleConsumeFailure(), want %v, got %v", got, tt.want) + } + }) + } +} + +func TestNewConsumer(t *testing.T) { + tests := []struct { + name string + checkFunc func(c *Consumer) bool + }{ + { + "initialized after creation", + func(c *Consumer) bool { + return c != nil + }, + }, + { + "alert is nil after creation", + func(c *Consumer) bool { + return c.Alert == nil + }, + }, + { + "command channel is initialized after creation", + func(c *Consumer) bool { + return c.CommandChannel != nil + }, + }, + { + "alert channel is initialized after creation", + func(c *Consumer) bool { + return c.AlertChannel != nil + }, + }, + { + "sample channel is initialized after creation", + func(c *Consumer) bool { + return c.SampleChannel != nil + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewConsumer(); !tt.checkFunc(got) { + t.Errorf("unexpected consumer state after NewConsumer() = %v", got) + } + }) + } +}