@@ -5,15 +5,15 @@ import (
5
5
"regexp"
6
6
"testing"
7
7
8
- commonutils "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-utils"
9
- "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/queries"
10
- "gopkg.in/DATA-DOG/go-sqlmock.v1"
11
-
12
8
"github.com/newrelic/infra-integrations-sdk/v3/integration"
13
9
"github.com/newrelic/nri-postgresql/src/args"
14
10
"github.com/newrelic/nri-postgresql/src/connection"
11
+ commonutils "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-utils"
12
+ global_variables "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/global-variables"
15
13
performancemetrics "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/performance-metrics"
14
+ "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/queries"
16
15
"github.com/stretchr/testify/assert"
16
+ "gopkg.in/DATA-DOG/go-sqlmock.v1"
17
17
)
18
18
19
19
func TestPopulateBlockingMetrics (t * testing.T ) {
@@ -22,6 +22,7 @@ func TestPopulateBlockingMetrics(t *testing.T) {
22
22
args := args.ArgumentList {QueryCountThreshold : 10 }
23
23
databaseName := "testdb"
24
24
version := uint64 (14 )
25
+ gv := global_variables .SetGlobalVariables (args , version , databaseName )
25
26
validationQueryStatStatements := fmt .Sprintf ("SELECT count(*) FROM pg_extension WHERE extname = '%s'" , "pg_stat_statements" )
26
27
mock .ExpectQuery (regexp .QuoteMeta (validationQueryStatStatements )).WillReturnRows (sqlmock .NewRows ([]string {"count" }).AddRow (1 ))
27
28
expectedQuery := queries .BlockingQueriesForV14AndAbove
@@ -34,7 +35,7 @@ func TestPopulateBlockingMetrics(t *testing.T) {
34
35
456 , "SELECT 2" , 4566 , "2023-01-01 00:00:00" ,
35
36
))
36
37
37
- err := performancemetrics .PopulateBlockingMetrics (conn , pgIntegration , args , databaseName , version )
38
+ err := performancemetrics .PopulateBlockingMetrics (conn , pgIntegration , gv )
38
39
assert .NoError (t , err )
39
40
assert .NoError (t , mock .ExpectationsWereMet ())
40
41
}
@@ -45,6 +46,7 @@ func TestPopulateBlockingMetricsSupportedVersionExtensionNotRequired(t *testing.
45
46
args := args.ArgumentList {QueryCountThreshold : 10 }
46
47
databaseName := "testdb"
47
48
version := uint64 (12 )
49
+ gv := global_variables .SetGlobalVariables (args , version , databaseName )
48
50
expectedQuery := queries .BlockingQueriesForV12AndV13
49
51
query := fmt .Sprintf (expectedQuery , databaseName , min (args .QueryCountThreshold , commonutils .MaxQueryThreshold ))
50
52
mock .ExpectQuery (regexp .QuoteMeta (query )).WillReturnRows (sqlmock .NewRows ([]string {
@@ -54,7 +56,7 @@ func TestPopulateBlockingMetricsSupportedVersionExtensionNotRequired(t *testing.
54
56
"newrelic_value" , 123 , "SELECT 1" , 1233444 , "2023-01-01 00:00:00" , "testdb" ,
55
57
456 , "SELECT 2" , 4566 , "2023-01-01 00:00:00" ,
56
58
))
57
- err := performancemetrics .PopulateBlockingMetrics (conn , pgIntegration , args , databaseName , version )
59
+ err := performancemetrics .PopulateBlockingMetrics (conn , pgIntegration , gv )
58
60
assert .NoError (t , err )
59
61
assert .NoError (t , mock .ExpectationsWereMet ())
60
62
}
@@ -65,7 +67,8 @@ func TestPopulateBlockingMetricsUnSupportedVersion(t *testing.T) {
65
67
args := args.ArgumentList {QueryCountThreshold : 10 }
66
68
databaseName := "testdb"
67
69
version := uint64 (11 )
68
- err := performancemetrics .PopulateBlockingMetrics (conn , pgIntegration , args , databaseName , version )
70
+ gv := global_variables .SetGlobalVariables (args , version , databaseName )
71
+ err := performancemetrics .PopulateBlockingMetrics (conn , pgIntegration , gv )
69
72
assert .EqualError (t , err , commonutils .ErrNotEligible .Error ())
70
73
assert .NoError (t , mock .ExpectationsWereMet ())
71
74
}
@@ -76,9 +79,10 @@ func TestPopulateBlockingMetricsExtensionsNotEnabled(t *testing.T) {
76
79
args := args.ArgumentList {QueryCountThreshold : 10 }
77
80
databaseName := "testdb"
78
81
version := uint64 (14 )
82
+ gv := global_variables .SetGlobalVariables (args , version , databaseName )
79
83
validationQueryStatStatements := fmt .Sprintf ("SELECT count(*) FROM pg_extension WHERE extname = '%s'" , "pg_stat_statements" )
80
84
mock .ExpectQuery (regexp .QuoteMeta (validationQueryStatStatements )).WillReturnRows (sqlmock .NewRows ([]string {"count" }).AddRow (0 ))
81
- err := performancemetrics .PopulateBlockingMetrics (conn , pgIntegration , args , databaseName , version )
85
+ err := performancemetrics .PopulateBlockingMetrics (conn , pgIntegration , gv )
82
86
assert .EqualError (t , err , commonutils .ErrNotEligible .Error ())
83
87
assert .NoError (t , mock .ExpectationsWereMet ())
84
88
}
@@ -88,6 +92,8 @@ func TestGetBlockingMetrics(t *testing.T) {
88
92
args := args.ArgumentList {QueryCountThreshold : 10 }
89
93
databaseName := "testdb"
90
94
version := uint64 (13 )
95
+ gv := global_variables .SetGlobalVariables (args , version , databaseName )
96
+
91
97
expectedQuery := queries .BlockingQueriesForV12AndV13
92
98
query := fmt .Sprintf (expectedQuery , databaseName , min (args .QueryCountThreshold , commonutils .MaxQueryThreshold ))
93
99
mock .ExpectQuery (regexp .QuoteMeta (query )).WillReturnRows (sqlmock .NewRows ([]string {
@@ -97,8 +103,7 @@ func TestGetBlockingMetrics(t *testing.T) {
97
103
"newrelic_value" , 123 , "SELECT 1" , 1233444 , "2023-01-01 00:00:00" , "testdb" ,
98
104
456 , "SELECT 2" , 4566 , "2023-01-01 00:00:00" ,
99
105
))
100
- blockingQueriesMetricsList , err := performancemetrics .GetBlockingMetrics (conn , args , databaseName , version )
101
-
106
+ blockingQueriesMetricsList , err := performancemetrics .GetBlockingMetrics (conn , gv )
102
107
assert .NoError (t , err )
103
108
assert .Len (t , blockingQueriesMetricsList , 1 )
104
109
assert .NoError (t , mock .ExpectationsWereMet ())
0 commit comments