Add tests for not repeating notifications
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package notify
|
package notify
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/analogj/scrutiny/webapp/backend/pkg"
|
"github.com/analogj/scrutiny/webapp/backend/pkg"
|
||||||
|
"github.com/analogj/scrutiny/webapp/backend/pkg/database"
|
||||||
mock_database "github.com/analogj/scrutiny/webapp/backend/pkg/database/mock"
|
mock_database "github.com/analogj/scrutiny/webapp/backend/pkg/database/mock"
|
||||||
"github.com/analogj/scrutiny/webapp/backend/pkg/models"
|
"github.com/analogj/scrutiny/webapp/backend/pkg/models"
|
||||||
"github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
|
"github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
|
||||||
@@ -190,6 +192,71 @@ func TestShouldNotify_MetricsStatusFilterAttributesCritical_MetricsStatusThresho
|
|||||||
//assert
|
//assert
|
||||||
require.False(t, ShouldNotify(logrus.StandardLogger(), device, smartAttrs, statusThreshold, notifyFilterAttributes, true, &gin.Context{}, fakeDatabase))
|
require.False(t, ShouldNotify(logrus.StandardLogger(), device, smartAttrs, statusThreshold, notifyFilterAttributes, true, &gin.Context{}, fakeDatabase))
|
||||||
}
|
}
|
||||||
|
func TestShouldNotify_NoRepeat_DatabaseFailure(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
//setup
|
||||||
|
device := models.Device{
|
||||||
|
DeviceStatus: pkg.DeviceStatusFailedScrutiny,
|
||||||
|
}
|
||||||
|
smartAttrs := measurements.Smart{Attributes: map[string]measurements.SmartAttribute{
|
||||||
|
"5": &measurements.SmartAtaAttribute{
|
||||||
|
Status: pkg.AttributeStatusFailedScrutiny,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
statusThreshold := pkg.MetricsStatusThresholdBoth
|
||||||
|
notifyFilterAttributes := pkg.MetricsStatusFilterAttributesAll
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
fakeDatabase := mock_database.NewMockDeviceRepo(mockCtrl)
|
||||||
|
fakeDatabase.EXPECT().GetSmartAttributeHistory(&gin.Context{}, "", database.DURATION_KEY_FOREVER, 1, 1, []string{"5"}).Return([]measurements.Smart{}, errors.New("")).Times(1)
|
||||||
|
|
||||||
|
//assert
|
||||||
|
require.True(t, ShouldNotify(logrus.StandardLogger(), device, smartAttrs, statusThreshold, notifyFilterAttributes, false, &gin.Context{}, fakeDatabase))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShouldNotify_NoRepeat_NoDatabaseData(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
//setup
|
||||||
|
device := models.Device{
|
||||||
|
DeviceStatus: pkg.DeviceStatusFailedScrutiny,
|
||||||
|
}
|
||||||
|
smartAttrs := measurements.Smart{Attributes: map[string]measurements.SmartAttribute{
|
||||||
|
"5": &measurements.SmartAtaAttribute{
|
||||||
|
Status: pkg.AttributeStatusFailedScrutiny,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
statusThreshold := pkg.MetricsStatusThresholdBoth
|
||||||
|
notifyFilterAttributes := pkg.MetricsStatusFilterAttributesAll
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
fakeDatabase := mock_database.NewMockDeviceRepo(mockCtrl)
|
||||||
|
fakeDatabase.EXPECT().GetSmartAttributeHistory(&gin.Context{}, "", database.DURATION_KEY_FOREVER, 1, 1, []string{"5"}).Return([]measurements.Smart{}, nil).Times(1)
|
||||||
|
|
||||||
|
//assert
|
||||||
|
require.True(t, ShouldNotify(logrus.StandardLogger(), device, smartAttrs, statusThreshold, notifyFilterAttributes, false, &gin.Context{}, fakeDatabase))
|
||||||
|
}
|
||||||
|
func TestShouldNotify_NoRepeat(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
//setup
|
||||||
|
device := models.Device{
|
||||||
|
DeviceStatus: pkg.DeviceStatusFailedScrutiny,
|
||||||
|
}
|
||||||
|
smartAttrs := measurements.Smart{Attributes: map[string]measurements.SmartAttribute{
|
||||||
|
"5": &measurements.SmartAtaAttribute{
|
||||||
|
Status: pkg.AttributeStatusFailedScrutiny,
|
||||||
|
TransformedValue: 0,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
statusThreshold := pkg.MetricsStatusThresholdBoth
|
||||||
|
notifyFilterAttributes := pkg.MetricsStatusFilterAttributesAll
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
fakeDatabase := mock_database.NewMockDeviceRepo(mockCtrl)
|
||||||
|
fakeDatabase.EXPECT().GetSmartAttributeHistory(&gin.Context{}, "", database.DURATION_KEY_FOREVER, 1, 1, []string{"5"}).Return([]measurements.Smart{smartAttrs}, nil).Times(1)
|
||||||
|
|
||||||
|
//assert
|
||||||
|
require.False(t, ShouldNotify(logrus.StandardLogger(), device, smartAttrs, statusThreshold, notifyFilterAttributes, false, &gin.Context{}, fakeDatabase))
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewPayload(t *testing.T) {
|
func TestNewPayload(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|||||||
Reference in New Issue
Block a user