zitadel/internal/query/failed_events_test.go
Livio Spring 29441ce4b6
feat: save last occurrence of failed events and fix instance filtering (#4710)
* fix: filter failed events and current sequence correctly

* fix failed events sorting column

* feat: save last occurrence of failed event

* fix failedEvents query and update sql statements

* change sql statement to only create index

* fix linting

* fix linting

* Update internal/query/failed_events.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* update job name on test-docs to match the one from test-code

Co-authored-by: Silvan <silvan.reusser@gmail.com>
2022-11-18 13:49:38 +01:00

176 lines
4.2 KiB
Go

package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
)
func Test_FailedEventsPrepares(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareFailedEventsQuery no result",
prepare: prepareFailedEventsQuery,
want: want{
sqlExpectations: mockQueries(
regexp.QuoteMeta(`SELECT projections.failed_events.projection_name,`+
` projections.failed_events.failed_sequence,`+
` projections.failed_events.failure_count,`+
` projections.failed_events.last_failed,`+
` projections.failed_events.error,`+
` COUNT(*) OVER ()`+
` FROM projections.failed_events`),
nil,
nil,
),
},
object: &FailedEvents{FailedEvents: []*FailedEvent{}},
},
{
name: "prepareFailedEventsQuery one result",
prepare: prepareFailedEventsQuery,
want: want{
sqlExpectations: mockQueries(
regexp.QuoteMeta(`SELECT projections.failed_events.projection_name,`+
` projections.failed_events.failed_sequence,`+
` projections.failed_events.failure_count,`+
` projections.failed_events.last_failed,`+
` projections.failed_events.error,`+
` COUNT(*) OVER ()`+
` FROM projections.failed_events`),
[]string{
"projection_name",
"failed_sequence",
"failure_count",
"last_failed",
"error",
"count",
},
[][]driver.Value{
{
"projection-name",
uint64(20211108),
uint64(2),
testNow,
"error",
},
},
),
},
object: &FailedEvents{
SearchResponse: SearchResponse{
Count: 1,
},
FailedEvents: []*FailedEvent{
{
ProjectionName: "projection-name",
FailedSequence: 20211108,
FailureCount: 2,
LastFailed: testNow,
Error: "error",
},
},
},
},
{
name: "prepareFailedEventsQuery multiple result",
prepare: prepareFailedEventsQuery,
want: want{
sqlExpectations: mockQueries(
regexp.QuoteMeta(`SELECT projections.failed_events.projection_name,`+
` projections.failed_events.failed_sequence,`+
` projections.failed_events.failure_count,`+
` projections.failed_events.last_failed,`+
` projections.failed_events.error,`+
` COUNT(*) OVER ()`+
` FROM projections.failed_events`),
[]string{
"projection_name",
"failed_sequence",
"failure_count",
"last_failed",
"error",
"count",
},
[][]driver.Value{
{
"projection-name",
uint64(20211108),
2,
testNow,
"error",
},
{
"projection-name-2",
uint64(20211108),
2,
nil,
"error",
},
},
),
},
object: &FailedEvents{
SearchResponse: SearchResponse{
Count: 2,
},
FailedEvents: []*FailedEvent{
{
ProjectionName: "projection-name",
FailedSequence: 20211108,
FailureCount: 2,
LastFailed: testNow,
Error: "error",
},
{
ProjectionName: "projection-name-2",
FailedSequence: 20211108,
FailureCount: 2,
Error: "error",
},
},
},
},
{
name: "prepareFailedEventsQuery sql err",
prepare: prepareFailedEventsQuery,
want: want{
sqlExpectations: mockQueryErr(
regexp.QuoteMeta(`SELECT projections.failed_events.projection_name,`+
` projections.failed_events.failed_sequence,`+
` projections.failed_events.failure_count,`+
` projections.failed_events.last_failed,`+
` projections.failed_events.error,`+
` COUNT(*) OVER ()`+
` FROM projections.failed_events`),
sql.ErrConnDone,
),
err: func(err error) (error, bool) {
if !errors.Is(err, sql.ErrConnDone) {
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
}
return nil, true
},
},
object: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err)
})
}
}