2021-12-16 13:44:26 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
var (
|
2023-10-19 10:19:10 +00:00
|
|
|
prepareFailedEventsStmt = `SELECT` +
|
|
|
|
` projections.failed_events2.projection_name,` +
|
|
|
|
` projections.failed_events2.failed_sequence,` +
|
|
|
|
` projections.failed_events2.aggregate_type,` +
|
|
|
|
` projections.failed_events2.aggregate_id,` +
|
|
|
|
` projections.failed_events2.failure_count,` +
|
|
|
|
` projections.failed_events2.last_failed,` +
|
|
|
|
` projections.failed_events2.error,` +
|
2023-02-27 21:36:43 +00:00
|
|
|
` COUNT(*) OVER ()` +
|
2023-10-19 10:19:10 +00:00
|
|
|
` FROM projections.failed_events2` +
|
2023-02-27 21:36:43 +00:00
|
|
|
` AS OF SYSTEM TIME '-1 ms'`
|
|
|
|
|
|
|
|
prepareFailedEventsCols = []string{
|
|
|
|
"projection_name",
|
|
|
|
"failed_sequence",
|
2023-10-19 10:19:10 +00:00
|
|
|
"aggregate_type",
|
|
|
|
"aggregate_id",
|
2023-02-27 21:36:43 +00:00
|
|
|
"failure_count",
|
|
|
|
"last_failed",
|
|
|
|
"error",
|
|
|
|
"count",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-12-16 13:44:26 +00:00
|
|
|
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(
|
2023-02-27 21:36:43 +00:00
|
|
|
regexp.QuoteMeta(prepareFailedEventsStmt),
|
2021-12-16 13:44:26 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &FailedEvents{FailedEvents: []*FailedEvent{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareFailedEventsQuery one result",
|
|
|
|
prepare: prepareFailedEventsQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2023-02-27 21:36:43 +00:00
|
|
|
regexp.QuoteMeta(prepareFailedEventsStmt),
|
|
|
|
prepareFailedEventsCols,
|
2021-12-16 13:44:26 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
"projection-name",
|
|
|
|
uint64(20211108),
|
2023-10-19 10:19:10 +00:00
|
|
|
"agg-type",
|
|
|
|
"agg-id",
|
2021-12-16 13:44:26 +00:00
|
|
|
uint64(2),
|
2022-11-18 12:49:38 +00:00
|
|
|
testNow,
|
2021-12-16 13:44:26 +00:00
|
|
|
"error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &FailedEvents{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
FailedEvents: []*FailedEvent{
|
|
|
|
{
|
|
|
|
ProjectionName: "projection-name",
|
|
|
|
FailedSequence: 20211108,
|
|
|
|
FailureCount: 2,
|
2022-11-18 12:49:38 +00:00
|
|
|
LastFailed: testNow,
|
2021-12-16 13:44:26 +00:00
|
|
|
Error: "error",
|
2023-10-19 10:19:10 +00:00
|
|
|
AggregateType: "agg-type",
|
|
|
|
AggregateID: "agg-id",
|
2021-12-16 13:44:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareFailedEventsQuery multiple result",
|
|
|
|
prepare: prepareFailedEventsQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2023-02-27 21:36:43 +00:00
|
|
|
regexp.QuoteMeta(prepareFailedEventsStmt),
|
|
|
|
prepareFailedEventsCols,
|
2021-12-16 13:44:26 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
"projection-name",
|
|
|
|
uint64(20211108),
|
2023-10-19 10:19:10 +00:00
|
|
|
"agg-type",
|
|
|
|
"agg-id",
|
2021-12-16 13:44:26 +00:00
|
|
|
2,
|
2022-11-18 12:49:38 +00:00
|
|
|
testNow,
|
2021-12-16 13:44:26 +00:00
|
|
|
"error",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"projection-name-2",
|
|
|
|
uint64(20211108),
|
2023-10-19 10:19:10 +00:00
|
|
|
"agg-type",
|
|
|
|
"agg-id",
|
2021-12-16 13:44:26 +00:00
|
|
|
2,
|
2022-11-18 12:49:38 +00:00
|
|
|
nil,
|
2021-12-16 13:44:26 +00:00
|
|
|
"error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &FailedEvents{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 2,
|
|
|
|
},
|
|
|
|
FailedEvents: []*FailedEvent{
|
|
|
|
{
|
|
|
|
ProjectionName: "projection-name",
|
|
|
|
FailedSequence: 20211108,
|
|
|
|
FailureCount: 2,
|
2022-11-18 12:49:38 +00:00
|
|
|
LastFailed: testNow,
|
2021-12-16 13:44:26 +00:00
|
|
|
Error: "error",
|
2023-10-19 10:19:10 +00:00
|
|
|
AggregateType: "agg-type",
|
|
|
|
AggregateID: "agg-id",
|
2021-12-16 13:44:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ProjectionName: "projection-name-2",
|
|
|
|
FailedSequence: 20211108,
|
|
|
|
FailureCount: 2,
|
|
|
|
Error: "error",
|
2023-10-19 10:19:10 +00:00
|
|
|
AggregateType: "agg-type",
|
|
|
|
AggregateID: "agg-id",
|
2021-12-16 13:44:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareFailedEventsQuery sql err",
|
|
|
|
prepare: prepareFailedEventsQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
2023-02-27 21:36:43 +00:00
|
|
|
regexp.QuoteMeta(prepareFailedEventsStmt),
|
2021-12-16 13:44:26 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
},
|
2023-08-22 12:49:02 +00:00
|
|
|
object: (*FailedEvents)(nil),
|
2021-12-16 13:44:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-27 21:36:43 +00:00
|
|
|
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
|
2021-12-16 13:44:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|