1
1
package performancemetrics
2
2
3
3
import (
4
+ "database/sql/driver"
4
5
"fmt"
5
6
"regexp"
6
7
"testing"
7
8
9
+ commonutils "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-utils"
10
+ "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/datamodels"
11
+
8
12
"github.com/newrelic/nri-postgresql/src/args"
9
13
"github.com/newrelic/nri-postgresql/src/connection"
10
14
common_parameters "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-parameters"
@@ -21,29 +25,50 @@ func TestGetBlockingMetrics(t *testing.T) {
21
25
cp := common_parameters .SetCommonParameters (args , version , databaseName )
22
26
expectedQuery := queries .BlockingQueriesForV12AndV13
23
27
query := fmt .Sprintf (expectedQuery , databaseName , args .QueryMonitoringCountThreshold )
24
- mock .ExpectQuery (regexp .QuoteMeta (query )).WillReturnRows (sqlmock .NewRows ([]string {
28
+ rowData := []driver.Value {
29
+ "newrelic_value" , int64 (123 ), "SELECT 1" , "1233444" , "2023-01-01 00:00:00" , "testdb" ,
30
+ int64 (456 ), "SELECT 2" , "4566" , "2023-01-01 00:00:00" ,
31
+ }
32
+ expectedRows := [][]driver.Value {
33
+ rowData , rowData ,
34
+ }
35
+ mockRows := sqlmock .NewRows ([]string {
25
36
"newrelic" , "blocked_pid" , "blocked_query" , "blocked_query_id" , "blocked_query_start" , "database_name" ,
26
37
"blocking_pid" , "blocking_query" , "blocking_query_id" , "blocking_query_start" ,
27
- }).AddRow (
28
- "newrelic_value" , 123 , "SELECT 1" , 1233444 , "2023-01-01 00:00:00" , "testdb" ,
29
- 456 , "SELECT 2" , 4566 , "2023-01-01 00:00:00" ,
30
- ).AddRow (
31
- "newrelic_value" , 123 , "SELECT 1" , 1233444 , "2023-01-01 00:00:00" , "testdb" ,
32
- 456 , "SELECT 2" , 4566 , "2023-01-01 00:00:00" ,
33
- ))
38
+ }).AddRow (rowData ... ).AddRow (rowData ... )
39
+ mock .ExpectQuery (regexp .QuoteMeta (query )).WillReturnRows (mockRows )
34
40
blockingQueriesMetricsList , err := getBlockingMetrics (conn , cp )
41
+ compareMockRowsWithMetrics (t , expectedRows , blockingQueriesMetricsList )
35
42
assert .NoError (t , err )
36
43
assert .Len (t , blockingQueriesMetricsList , 2 )
37
44
assert .NoError (t , mock .ExpectationsWereMet ())
38
45
}
39
46
47
+ func compareMockRowsWithMetrics (t * testing.T , expectedRows [][]driver.Value , blockingQueriesMetricsList []interface {}) {
48
+ assert .Equal (t , 2 , len (blockingQueriesMetricsList ))
49
+ for index := range blockingQueriesMetricsList {
50
+ anonymizeQuery := commonutils .AnonymizeQueryText (expectedRows [index ][2 ].(string ))
51
+ blockingSession := blockingQueriesMetricsList [index ].(datamodels.BlockingSessionMetrics )
52
+ assert .Equal (t , expectedRows [index ][0 ], * blockingSession .Newrelic )
53
+ assert .Equal (t , expectedRows [index ][1 ], * blockingSession .BlockedPid )
54
+ assert .Equal (t , anonymizeQuery , * blockingSession .BlockedQuery )
55
+ assert .Equal (t , expectedRows [index ][3 ], * blockingSession .BlockedQueryID )
56
+ assert .Equal (t , expectedRows [index ][4 ], * blockingSession .BlockedQueryStart )
57
+ assert .Equal (t , expectedRows [index ][5 ], * blockingSession .BlockedDatabase )
58
+ assert .Equal (t , expectedRows [index ][6 ], * blockingSession .BlockingPid )
59
+ assert .Equal (t , anonymizeQuery , * blockingSession .BlockingQuery )
60
+ assert .Equal (t , expectedRows [index ][8 ], * blockingSession .BlockingQueryID )
61
+ assert .Equal (t , expectedRows [index ][9 ], * blockingSession .BlockingQueryStart )
62
+ }
63
+ }
64
+
40
65
func TestGetBlockingMetricsErr (t * testing.T ) {
41
66
conn , mock := connection .CreateMockSQL (t )
42
67
args := args.ArgumentList {QueryMonitoringCountThreshold : 10 }
43
68
databaseName := "testdb"
44
69
version := uint64 (13 )
45
70
cp := common_parameters .SetCommonParameters (args , version , databaseName )
46
71
_ , err := getBlockingMetrics (conn , cp )
47
- assert .NotNil (t , err )
72
+ assert .EqualError (t , err , commonutils . ErrUnExpectedError . Error () )
48
73
assert .NoError (t , mock .ExpectationsWereMet ())
49
74
}
0 commit comments