Skip to content

Commit

Permalink
test: for controller and handler
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Dec 17, 2024
1 parent 855e1d6 commit 647615c
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 3 deletions.
4 changes: 2 additions & 2 deletions api/controller/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ func GetTriggerNoisiness(
triggerIDsWithEventsCount = []triggerIDWithEventsCount{}
}

if size <= total {
triggerIDsWithEventsCount = triggerIDsWithEventsCount[:total]
if size <= int64(len(triggerIDsWithEventsCount)) {
triggerIDsWithEventsCount = triggerIDsWithEventsCount[:size]
}
}

Expand Down
171 changes: 170 additions & 1 deletion api/controller/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ func TestGetTriggerNoisiness(t *testing.T) {
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(0))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(0))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID1, triggerID2}).
Return([]moira.TriggerCheck{trigger1, trigger2}, nil)
Return([]*moira.TriggerCheck{&trigger1, &trigger2}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, allEventsSize, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
Expand All @@ -1270,5 +1270,174 @@ func TestGetTriggerNoisiness(t *testing.T) {
Total: 2,
})
})

Convey("with triggers and some events", func() {
Convey("with asc sort order", func() {
dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID1, triggerID2}).
Return([]*moira.TriggerCheck{&trigger1, &trigger2}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, allEventsSize, defaultFrom, defaultTo, api.AscSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger1,
EventsCount: 1,
},
{
TriggerCheck: trigger2,
EventsCount: 2,
},
},
Page: zeroPage,
Size: allEventsSize,
Total: 2,
})
})

Convey("with desc sort order", func() {
dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID2, triggerID1}).
Return([]*moira.TriggerCheck{&trigger2, &trigger1}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, allEventsSize, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger2,
EventsCount: 2,
},
{
TriggerCheck: trigger1,
EventsCount: 1,
},
},
Page: zeroPage,
Size: allEventsSize,
Total: 2,
})
})
})

Convey("with paginating", func() {
Convey("with page >= 0, size >= 0", func() {
dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{}).Return([]*moira.TriggerCheck{}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, 0, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{},
Page: zeroPage,
Size: 0,
Total: 2,
})

dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID2}).
Return([]*moira.TriggerCheck{&trigger2}, nil)

triggerNoisinessList, err = GetTriggerNoisiness(dataBase, zeroPage, 1, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger2,
EventsCount: 2,
},
},
Page: zeroPage,
Size: 1,
Total: 2,
})

dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID1}).
Return([]*moira.TriggerCheck{&trigger1}, nil)

triggerNoisinessList, err = GetTriggerNoisiness(dataBase, 1, 1, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: trigger1,
EventsCount: 1,
},
},
Page: 1,
Size: 1,
Total: 2,
})
})

Convey("with page > 0, size < 0", func() {
dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, 1, -1, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{},
Page: 1,
Size: -1,
Total: 2,
})
})

Convey("with page < 0, size < 0", func() {
dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, -1, -1, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{},
Page: -1,
Size: -1,
Total: 2,
})
})

Convey("with page < 0, size > 0", func() {
dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, -1, 1, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldBeNil)
So(triggerNoisinessList, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{},
Page: -1,
Size: 1,
Total: 2,
})
})
})

Convey("error when len of fetched trigger checks != len of fetched ids", func() {
dataBase.EXPECT().GetAllTriggerIDs().Return([]string{triggerID1, triggerID2}, nil)
dataBase.EXPECT().GetNotificationEventCount(triggerID1, defaultFrom, defaultTo).Return(int64(1))
dataBase.EXPECT().GetNotificationEventCount(triggerID2, defaultFrom, defaultTo).Return(int64(2))
dataBase.EXPECT().GetTriggerChecks([]string{triggerID2, triggerID1}).
Return([]*moira.TriggerCheck{&trigger2}, nil)

triggerNoisinessList, err := GetTriggerNoisiness(dataBase, zeroPage, allEventsSize, defaultFrom, defaultTo, api.DescSortOrder)
So(err, ShouldResemble, api.ErrorInternalServer(fmt.Errorf("failed to fetch triggers for such range")))
So(triggerNoisinessList, ShouldBeNil)
})
})
}
88 changes: 88 additions & 0 deletions api/handler/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -886,3 +888,89 @@ func isTriggerCreated(response *http.Response) bool {

return actual.Message == expected
}

func TestGetTriggerNoisiness(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockDB := mock_moira_alert.NewMockDatabase(mockCtrl)
database = mockDB

getRequestTriggerNoisiness := func(from, to string) *http.Request {
request := httptest.NewRequest("GET", "/trigger/noisiness", nil)
request.Header.Add("content-type", "application/json")

request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "page", int64(0)))
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "size", int64(-1)))
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "from", from))
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "to", to))
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "sort", api.AscSortOrder))

return request
}

testTrigger := moira.TriggerCheck{
Trigger: moira.Trigger{
ID: "triggerID",
},
}

Convey("Test get trigger noisiness", t, func() {
now := time.Now()

from := strconv.FormatInt(now.Add(time.Second*-3).Unix(), 10)
to := strconv.FormatInt(now.Unix(), 10)

Convey("with ok", func() {
responseWriter := httptest.NewRecorder()

mockDB.EXPECT().GetAllTriggerIDs().Return([]string{testTrigger.ID}, nil)
mockDB.EXPECT().GetNotificationEventCount(testTrigger.ID, from, to).Return(int64(1))
mockDB.EXPECT().GetTriggerChecks([]string{testTrigger.ID}).Return([]*moira.TriggerCheck{&testTrigger}, nil)

getTriggerNoisiness(responseWriter, getRequestTriggerNoisiness(from, to))

response := responseWriter.Result()

Check failure on line 933 in api/handler/triggers_test.go

View workflow job for this annotation

GitHub Actions / lint

response body must be closed (bodyclose)

So(response.StatusCode, ShouldEqual, http.StatusOK)

contentBytes, err := io.ReadAll(response.Body)
So(err, ShouldBeNil)

var gotDTO dto.TriggerNoisinessList
err = json.Unmarshal(contentBytes, &gotDTO)
So(err, ShouldBeNil)
So(&gotDTO, ShouldResemble, &dto.TriggerNoisinessList{
List: []dto.TriggerNoisiness{
{
TriggerCheck: testTrigger,
EventsCount: 1,
},
},
Page: 0,
Size: -1,
Total: 1,
})
})

Convey("with error from db", func() {
responseWriter := httptest.NewRecorder()
errFromDB := errors.New("some DB error")

mockDB.EXPECT().GetAllTriggerIDs().Return(nil, errFromDB)

getTriggerNoisiness(responseWriter, getRequestTriggerNoisiness(from, to))

response := responseWriter.Result()

Check failure on line 964 in api/handler/triggers_test.go

View workflow job for this annotation

GitHub Actions / lint

response body must be closed (bodyclose)

So(response.StatusCode, ShouldEqual, http.StatusInternalServerError)

contentBytes, err := io.ReadAll(response.Body)
So(err, ShouldBeNil)

expectedContentBytes, err := json.Marshal(api.ErrorInternalServer(errFromDB))
So(err, ShouldBeNil)
So(string(contentBytes), ShouldResemble, string(expectedContentBytes)+"\n")
})
})
}

0 comments on commit 647615c

Please sign in to comment.