forked from newrelic/nri-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat : resolved review comments (#42)
resolved review comments
- Loading branch information
1 parent
28483e3
commit 948764b
Showing
21 changed files
with
766 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/query-performance-monitoring/common-utils/commmon-helpers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package commonutils_test | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
"time" | ||
|
||
"github.com/newrelic/nri-postgresql/src/collection" | ||
commonutils "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetQuotedStringFromArray(t *testing.T) { | ||
input := []string{"db1", "db2", "db3"} | ||
expected := "'db1','db2','db3'" | ||
result := commonutils.GetQuotedStringFromArray(input) | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestGetDatabaseListInString(t *testing.T) { | ||
dbListKeys := []string{"db1", "db2"} | ||
sort.Strings(dbListKeys) // Sort the keys to ensure consistent order | ||
dbList := collection.DatabaseList{} | ||
for _, key := range dbListKeys { | ||
dbList[key] = collection.SchemaList{} | ||
} | ||
expected := "'db1','db2'" | ||
result := commonutils.GetDatabaseListInString(dbList) | ||
assert.Equal(t, expected, result) | ||
|
||
// Test with empty database list | ||
dbList = collection.DatabaseList{} | ||
expected = "" | ||
result = commonutils.GetDatabaseListInString(dbList) | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestAnonymizeQueryText(t *testing.T) { | ||
query := "SELECT * FROM users WHERE id = 1 AND name = 'John'" | ||
expected := "SELECT * FROM users WHERE id = ? AND name = ?" | ||
result := commonutils.AnonymizeQueryText(query) | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestGeneratePlanID(t *testing.T) { | ||
queryID := "query123" | ||
result := commonutils.GeneratePlanID(queryID) | ||
assert.NotNil(t, result) | ||
assert.Contains(t, *result, queryID) | ||
assert.Contains(t, *result, "-") | ||
assert.Contains(t, *result, time.Now().Format(commonutils.TimeFormat)[:8]) // Check date part | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/query-performance-monitoring/common-utils/ingestion-helper_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package commonutils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/newrelic/infra-integrations-sdk/v3/integration" | ||
"github.com/newrelic/nri-postgresql/src/args" | ||
commonutils "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetMetric(t *testing.T) { | ||
pgIntegration, _ := integration.New("test", "1.0.0") | ||
entity, _ := pgIntegration.Entity("test-entity", "test-type") | ||
|
||
metricSet := entity.NewMetricSet("test-event") | ||
|
||
commonutils.SetMetric(metricSet, "testGauge", 123.0, "gauge") | ||
assert.Equal(t, 123.0, metricSet.Metrics["testGauge"]) | ||
|
||
commonutils.SetMetric(metricSet, "testAttribute", "value", "attribute") | ||
assert.Equal(t, "value", metricSet.Metrics["testAttribute"]) | ||
|
||
commonutils.SetMetric(metricSet, "testDefault", 456.0, "unknown") | ||
assert.Equal(t, 456.0, metricSet.Metrics["testDefault"]) | ||
} | ||
|
||
func TestIngestMetric(t *testing.T) { | ||
pgIntegration, _ := integration.New("test", "1.0.0") | ||
args := args.ArgumentList{ | ||
Hostname: "localhost", | ||
Port: "5432", | ||
} | ||
metricList := []interface{}{ | ||
struct { | ||
TestField int `metric_name:"testField" source_type:"gauge"` | ||
}{TestField: 123}, | ||
} | ||
|
||
commonutils.IngestMetric(metricList, "testEvent", pgIntegration, args) | ||
assert.NotEmpty(t, pgIntegration.Entities) | ||
} | ||
|
||
func TestCreateEntity(t *testing.T) { | ||
pgIntegration, _ := integration.New("test", "1.0.0") | ||
args := args.ArgumentList{ | ||
Hostname: "localhost", | ||
Port: "5432", | ||
} | ||
|
||
entity, err := commonutils.CreateEntity(pgIntegration, args) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, entity) | ||
assert.Equal(t, "localhost:5432", entity.Metadata.Name) | ||
} | ||
|
||
func TestProcessModel(t *testing.T) { | ||
pgIntegration, _ := integration.New("test", "1.0.0") | ||
entity, _ := pgIntegration.Entity("test-entity", "test-type") | ||
|
||
metricSet := entity.NewMetricSet("test-event") | ||
|
||
model := struct { | ||
TestField int `metric_name:"testField" source_type:"gauge"` | ||
}{TestField: 123} | ||
|
||
err := commonutils.ProcessModel(model, metricSet) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 123.0, metricSet.Metrics["testField"]) | ||
} | ||
|
||
func TestPublishMetrics(t *testing.T) { | ||
pgIntegration, _ := integration.New("test", "1.0.0") | ||
args := args.ArgumentList{ | ||
Hostname: "localhost", | ||
Port: "5432", | ||
} | ||
entity, _ := commonutils.CreateEntity(pgIntegration, args) | ||
|
||
err := commonutils.PublishMetrics(pgIntegration, &entity, args) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, entity) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/query-performance-monitoring/common-utils/query-fetch-helpers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package commonutils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
commonutils "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-utils" | ||
|
||
"github.com/newrelic/nri-postgresql/src/query-performance-monitoring/queries" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func runTestCases(t *testing.T, tests []struct { | ||
version uint64 | ||
expected string | ||
expectErr bool | ||
}, fetchFunc func(uint64) (string, error)) { | ||
for _, test := range tests { | ||
result, err := fetchFunc(test.version) | ||
if test.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func TestFetchVersionSpecificSlowQueries(t *testing.T) { | ||
tests := []struct { | ||
version uint64 | ||
expected string | ||
expectErr bool | ||
}{ | ||
{commonutils.PostgresVersion12, queries.SlowQueriesForV12, false}, | ||
{commonutils.PostgresVersion13, queries.SlowQueriesForV13AndAbove, false}, | ||
{commonutils.PostgresVersion11, "", true}, | ||
} | ||
|
||
runTestCases(t, tests, commonutils.FetchVersionSpecificSlowQueries) | ||
} | ||
|
||
func TestFetchVersionSpecificBlockingQueries(t *testing.T) { | ||
tests := []struct { | ||
version uint64 | ||
expected string | ||
expectErr bool | ||
}{ | ||
{commonutils.PostgresVersion12, queries.BlockingQueriesForV12AndV13, false}, | ||
{commonutils.PostgresVersion13, queries.BlockingQueriesForV12AndV13, false}, | ||
{commonutils.PostgresVersion14, queries.BlockingQueriesForV14AndAbove, false}, | ||
{commonutils.PostgresVersion11, "", true}, | ||
} | ||
|
||
runTestCases(t, tests, commonutils.FetchVersionSpecificBlockingQueries) | ||
} | ||
|
||
func TestFetchVersionSpecificIndividualQueries(t *testing.T) { | ||
tests := []struct { | ||
version uint64 | ||
expected string | ||
expectErr bool | ||
}{ | ||
{commonutils.PostgresVersion12, queries.IndividualQuerySearchV12, false}, | ||
{commonutils.PostgresVersion13, queries.IndividualQuerySearchV13AndAbove, false}, | ||
{commonutils.PostgresVersion14, queries.IndividualQuerySearchV13AndAbove, false}, | ||
{commonutils.PostgresVersion11, "", true}, | ||
} | ||
|
||
runTestCases(t, tests, commonutils.FetchVersionSpecificIndividualQueries) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.