github.com/kuoss/venti@v0.2.20/pkg/service/alerting/types_test.go (about)

     1  package alerting
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestAlertStateString(t *testing.T) {
    10  	testCases := []struct {
    11  		alertState AlertState
    12  		want       string
    13  		wantPanic  string
    14  	}{
    15  		{StateInactive, "inactive", ""},
    16  		{StatePending, "pending", ""},
    17  		{StateFiring, "firing", ""},
    18  		{-1, "", "unknown alert state: -1"},
    19  	}
    20  	for _, tc := range testCases {
    21  		t.Run("", func(t *testing.T) {
    22  			if tc.wantPanic == "" {
    23  				got := tc.alertState.String()
    24  				require.Equal(t, tc.want, got)
    25  			} else {
    26  				require.PanicsWithError(t, tc.wantPanic, func() {
    27  					got := tc.alertState.String()
    28  					require.Equal(t, "", got)
    29  				})
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func TestAlertingRuleState(t *testing.T) {
    36  	testCases := []struct {
    37  		alerts map[uint64]*Alert
    38  		want   AlertState
    39  	}{
    40  		{
    41  			map[uint64]*Alert{},
    42  			StateInactive,
    43  		},
    44  		{
    45  			map[uint64]*Alert{0: {State: StateInactive}},
    46  			StateInactive,
    47  		},
    48  		{
    49  			map[uint64]*Alert{0: {State: StatePending}},
    50  			StatePending,
    51  		},
    52  		{
    53  			map[uint64]*Alert{0: {State: StateFiring}},
    54  			StateFiring,
    55  		},
    56  	}
    57  	for _, tc := range testCases {
    58  		t.Run("", func(t *testing.T) {
    59  			alertingRule := AlertingRule{Active: tc.alerts}
    60  			got := alertingRule.State()
    61  			require.Equal(t, tc.want, got)
    62  		})
    63  	}
    64  }