|
| 1 | +package postgres_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/pganalyze/collector/input/postgres" |
| 8 | + "github.com/pganalyze/collector/state" |
| 9 | + "github.com/pganalyze/collector/util" |
| 10 | +) |
| 11 | + |
| 12 | +type collectionHelperTestpair struct { |
| 13 | + Name string |
| 14 | + InputTypes []string |
| 15 | + ExpectedExists bool |
| 16 | + ExpectedReturnType string |
| 17 | +} |
| 18 | + |
| 19 | +var collectionHelperTests = []collectionHelperTestpair{ |
| 20 | + { |
| 21 | + "explain_analyze", |
| 22 | + []string{"text", "text[]", "text[]", "text[]"}, |
| 23 | + true, |
| 24 | + "text", |
| 25 | + }, |
| 26 | + { |
| 27 | + "get_stat_statements", |
| 28 | + []string{"boolean"}, |
| 29 | + true, |
| 30 | + "SETOF pg_stat_statements", |
| 31 | + }, |
| 32 | + // Verify we don't match on a shorter set of arguments |
| 33 | + { |
| 34 | + "explain_analyze", |
| 35 | + []string{"text", "text[]", "text[]"}, |
| 36 | + false, |
| 37 | + "", |
| 38 | + }, |
| 39 | + // Verify we don't match on a longer set of arguments |
| 40 | + { |
| 41 | + "explain_analyze", |
| 42 | + []string{"text", "text[]", "text[]", "text[]", "text[]"}, |
| 43 | + false, |
| 44 | + "", |
| 45 | + }, |
| 46 | + // Verify we don't match on different argument types |
| 47 | + { |
| 48 | + "explain_analyze", |
| 49 | + []string{"text", "text[]", "text[]", "float"}, |
| 50 | + false, |
| 51 | + "", |
| 52 | + }, |
| 53 | + // Verify we allow default arguments to be ommitted |
| 54 | + { |
| 55 | + "get_stat_statements", |
| 56 | + []string{}, |
| 57 | + true, |
| 58 | + "SETOF pg_stat_statements", |
| 59 | + }, |
| 60 | +} |
| 61 | + |
| 62 | +func TestCollectionFindHelper(t *testing.T) { |
| 63 | + db := setupTest(t) |
| 64 | + defer db.Close() |
| 65 | + |
| 66 | + _, err := db.Exec("CREATE EXTENSION IF NOT EXISTS pg_stat_statements") |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Could not create pg_stat_statements extension: %s", err) |
| 69 | + } |
| 70 | + |
| 71 | + _, err = db.Exec(util.GetStatStatementsHelper) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("Could not load get_stat_statements helper: %s", err) |
| 74 | + } |
| 75 | + |
| 76 | + ctx := context.Background() |
| 77 | + logger := &util.Logger{} |
| 78 | + server := &state.Server{} |
| 79 | + opts := state.CollectionOpts{} |
| 80 | + |
| 81 | + collection, err := postgres.NewCollection(ctx, logger, server, opts, db) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("Could not initialize collection struct: %s", err) |
| 84 | + } |
| 85 | + |
| 86 | + for _, pair := range collectionHelperTests { |
| 87 | + helperExists := collection.HelperExists(pair.Name, pair.InputTypes) |
| 88 | + if helperExists != pair.ExpectedExists { |
| 89 | + t.Errorf("Incorrect exists state for helper %s(%+v):\n got: %+v\n expected: %+v", pair.Name, pair.InputTypes, helperExists, pair.ExpectedExists) |
| 90 | + } |
| 91 | + |
| 92 | + helperReturnType := collection.HelperReturnType(pair.Name, pair.InputTypes) |
| 93 | + if helperReturnType != pair.ExpectedReturnType { |
| 94 | + t.Errorf("Incorrect return type for helper %s(%+v):\n got: %s\n expected: %s", pair.Name, pair.InputTypes, helperReturnType, pair.ExpectedReturnType) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments