forked from sqshq/sampler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sqshq#69 from simross/data_tests
test(data): add tests for data/consumer
- Loading branch information
Showing
1 changed file
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
} |