Skip to content

Commit

Permalink
resolved: lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tharun0064 committed Jan 15, 2025
1 parent 3d056e5 commit 3e4368b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (
)

func TestPopulateBlockingMetrics(t *testing.T) {

conn, mock := connection.CreateMockSQL(t)
pgIntegration, _ := integration.New("test", "1.0.0")
args := args.ArgumentList{QueryCountThreshold: 10}
databaseName := "testdb"
version := uint64(13)
expectedQuery := queries.BlockingQueriesForV12AndV13
version := uint64(14)
validationQueryStatStatements := fmt.Sprintf("SELECT count(*) FROM pg_extension WHERE extname = '%s'", "pg_stat_statements")
mock.ExpectQuery(regexp.QuoteMeta(validationQueryStatStatements)).WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
expectedQuery := queries.BlockingQueriesForV14AndAbove
query := fmt.Sprintf(expectedQuery, databaseName, min(args.QueryCountThreshold, commonutils.MaxQueryThreshold))
mock.ExpectQuery(regexp.QuoteMeta(query)).WillReturnRows(sqlmock.NewRows([]string{
"newrelic", "blocked_pid", "blocked_query", "blocked_query_id", "blocked_query_start", "database_name",
Expand All @@ -37,17 +38,6 @@ func TestPopulateBlockingMetrics(t *testing.T) {
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestPopulateBlockingMetricsUnSupportedVersion(t *testing.T) {
conn, mock := connection.CreateMockSQL(t)
pgIntegration, _ := integration.New("test", "1.0.0")
args := args.ArgumentList{QueryCountThreshold: 10}
databaseName := "testdb"
version := uint64(11)
err := performancemetrics.PopulateBlockingMetrics(conn, pgIntegration, args, databaseName, version)
assert.EqualError(t, err, commonutils.ErrNotEligible.Error())
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestPopulateBlockingMetricsSupportedVersionExtensionNotRequired(t *testing.T) {
conn, mock := connection.CreateMockSQL(t)
pgIntegration, _ := integration.New("test", "1.0.0")
Expand All @@ -68,6 +58,17 @@ func TestPopulateBlockingMetricsSupportedVersionExtensionNotRequired(t *testing.
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestPopulateBlockingMetricsUnSupportedVersion(t *testing.T) {
conn, mock := connection.CreateMockSQL(t)
pgIntegration, _ := integration.New("test", "1.0.0")
args := args.ArgumentList{QueryCountThreshold: 10}
databaseName := "testdb"
version := uint64(11)
err := performancemetrics.PopulateBlockingMetrics(conn, pgIntegration, args, databaseName, version)
assert.EqualError(t, err, commonutils.ErrNotEligible.Error())
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestPopulateBlockingMetricsExtensionsNotEnabled(t *testing.T) {
conn, mock := connection.CreateMockSQL(t)
pgIntegration, _ := integration.New("test", "1.0.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,12 @@ func TestPopulateSlowMetricsInEligibility(t *testing.T) {
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestGetSlowRunningMetrics(t *testing.T) {

func runSlowQueryTest(t *testing.T, query string, version uint64, expectedLength int) {
conn, mock := connection.CreateMockSQL(t)
args := args.ArgumentList{QueryCountThreshold: 10}
databaseName := "testdb"
version := uint64(13)

expectedQuery := queries.SlowQueriesForV13AndAbove
query := fmt.Sprintf(expectedQuery, "testdb", min(args.QueryCountThreshold, commonutils.MaxQueryThreshold))
query = fmt.Sprintf(query, "testdb", min(args.QueryCountThreshold, commonutils.MaxQueryThreshold))
mock.ExpectQuery(regexp.QuoteMeta(query)).WillReturnRows(sqlmock.NewRows([]string{
"newrelic", "query_id", "query_text", "database_name", "schema_name", "execution_count",
"avg_elapsed_time_ms", "avg_disk_reads", "avg_disk_writes", "statement_type", "collection_timestamp",
Expand All @@ -75,17 +72,23 @@ func TestGetSlowRunningMetrics(t *testing.T) {
slowQueryList, _, err := performancemetrics.GetSlowRunningMetrics(conn, args, databaseName, version)

assert.NoError(t, err)
assert.Len(t, slowQueryList, 1)
assert.Len(t, slowQueryList, expectedLength)
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestGetSlowRunningEmptyMetrics(t *testing.T) {
func TestGetSlowRunningMetrics(t *testing.T) {
runSlowQueryTest(t, queries.SlowQueriesForV13AndAbove, 13, 1)
}

func TestGetSlowRunningMetricsV12(t *testing.T) {
runSlowQueryTest(t, queries.SlowQueriesForV12, 12, 1)
}

func TestGetSlowRunningEmptyMetrics(t *testing.T) {
conn, mock := connection.CreateMockSQL(t)
args := args.ArgumentList{QueryCountThreshold: 10}
databaseName := "testdb"
version := uint64(13)

expectedQuery := queries.SlowQueriesForV13AndAbove
query := fmt.Sprintf(expectedQuery, "testdb", min(args.QueryCountThreshold, commonutils.MaxQueryThreshold))
mock.ExpectQuery(regexp.QuoteMeta(query)).WillReturnRows(sqlmock.NewRows([]string{
Expand All @@ -99,29 +102,6 @@ func TestGetSlowRunningEmptyMetrics(t *testing.T) {
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestGetSlowRunningMetricsV12(t *testing.T) {

conn, mock := connection.CreateMockSQL(t)
args := args.ArgumentList{QueryCountThreshold: 10}
databaseName := "testdb"
version := uint64(12)

expectedQuery := queries.SlowQueriesForV12
query := fmt.Sprintf(expectedQuery, "testdb", min(args.QueryCountThreshold, commonutils.MaxQueryThreshold))
mock.ExpectQuery(regexp.QuoteMeta(query)).WillReturnRows(sqlmock.NewRows([]string{
"newrelic", "query_id", "query_text", "database_name", "schema_name", "execution_count",
"avg_elapsed_time_ms", "avg_disk_reads", "avg_disk_writes", "statement_type", "collection_timestamp",
}).AddRow(
"newrelic", "queryid1", "SELECT 1", "testdb", "public", 10,
15.0, 5.0, 2.0, "SELECT", "2023-01-01T00:00:00Z",
))
slowQueryList, _, err := performancemetrics.GetSlowRunningMetrics(conn, args, databaseName, version)

assert.NoError(t, err)
assert.Len(t, slowQueryList, 1)
assert.NoError(t, mock.ExpectationsWereMet())
}

func TestGetSlowRunningMetricsUnsupportedVersion(t *testing.T) {

conn, mock := connection.CreateMockSQL(t)
Expand Down

0 comments on commit 3e4368b

Please sign in to comment.