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 (
|
|
|
|
currentSequenceStmt = `SELECT max(projections.current_sequences.current_sequence) as current_sequence,` +
|
|
|
|
` max(projections.current_sequences.timestamp) as timestamp,` +
|
|
|
|
` projections.current_sequences.projection_name,` +
|
|
|
|
` COUNT(*) OVER ()` +
|
|
|
|
` FROM projections.current_sequences` +
|
|
|
|
" AS OF SYSTEM TIME '-1 ms' " +
|
|
|
|
` GROUP BY projections.current_sequences.projection_name`
|
|
|
|
|
|
|
|
currentSequenceCols = []string{
|
|
|
|
"current_sequence",
|
|
|
|
"timestamp",
|
|
|
|
"projection_name",
|
|
|
|
"count",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-12-16 13:44:26 +00:00
|
|
|
func Test_CurrentSequencesPrepares(t *testing.T) {
|
|
|
|
type want struct {
|
|
|
|
sqlExpectations sqlExpectation
|
|
|
|
err checkErr
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
prepare interface{}
|
|
|
|
want want
|
|
|
|
object interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "prepareCurrentSequencesQuery no result",
|
|
|
|
prepare: prepareCurrentSequencesQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2023-02-27 21:36:43 +00:00
|
|
|
regexp.QuoteMeta(currentSequenceStmt),
|
2021-12-16 13:44:26 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &CurrentSequences{CurrentSequences: []*CurrentSequence{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareCurrentSequencesQuery one result",
|
|
|
|
prepare: prepareCurrentSequencesQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2023-02-27 21:36:43 +00:00
|
|
|
regexp.QuoteMeta(currentSequenceStmt),
|
|
|
|
currentSequenceCols,
|
2021-12-16 13:44:26 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
uint64(20211108),
|
|
|
|
testNow,
|
|
|
|
"projection-name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &CurrentSequences{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
CurrentSequences: []*CurrentSequence{
|
|
|
|
{
|
|
|
|
Timestamp: testNow,
|
|
|
|
CurrentSequence: 20211108,
|
|
|
|
ProjectionName: "projection-name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareCurrentSequencesQuery multiple result",
|
|
|
|
prepare: prepareCurrentSequencesQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2023-02-27 21:36:43 +00:00
|
|
|
regexp.QuoteMeta(currentSequenceStmt),
|
|
|
|
currentSequenceCols,
|
2021-12-16 13:44:26 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
uint64(20211108),
|
|
|
|
testNow,
|
|
|
|
"projection-name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
uint64(20211108),
|
|
|
|
testNow,
|
|
|
|
"projection-name-2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &CurrentSequences{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 2,
|
|
|
|
},
|
|
|
|
CurrentSequences: []*CurrentSequence{
|
|
|
|
{
|
|
|
|
Timestamp: testNow,
|
|
|
|
CurrentSequence: 20211108,
|
|
|
|
ProjectionName: "projection-name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Timestamp: testNow,
|
|
|
|
CurrentSequence: 20211108,
|
|
|
|
ProjectionName: "projection-name-2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareCurrentSequencesQuery sql err",
|
|
|
|
prepare: prepareCurrentSequencesQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
2023-02-27 21:36:43 +00:00
|
|
|
regexp.QuoteMeta(currentSequenceStmt),
|
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
|
|
|
|
},
|
|
|
|
},
|
|
|
|
object: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|