test(data): add tests for data/consumer

ref #57
This commit is contained in:
simross 2019-10-07 21:17:09 +03:00
parent aadceceed2
commit de973dfb75
1 changed files with 111 additions and 0 deletions

111
data/consumer_test.go Normal file
View File

@ -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)
}
})
}
}