(t *testing.T)
| 3851 | {"multiline", "SELECT pid\nFROM pg_stat_activity\nWHERE state = 'active'", true}, |
| 3852 | {"tab before table", "SELECT * FROM\tpg_stat_activity", true}, |
| 3853 | {"multiple spaces", "SELECT * FROM pg_stat_activity", true}, |
| 3854 | |
| 3855 | // Should not match |
| 3856 | {"no FROM", "SELECT 'pg_stat_activity'", false}, |
| 3857 | {"in string literal", "SELECT 'text pg_stat_activity text'", false}, |
| 3858 | {"different table", "SELECT * FROM pg_stat_statements", false}, |
| 3859 | {"substring match", "SELECT * FROM pg_stat_activity_detail", false}, |
| 3860 | {"prefix only", "SELECT * FROM my_pg_stat_activity", false}, |
| 3861 | {"empty query", "", false}, |
| 3862 | {"INSERT", "INSERT INTO pg_stat_activity VALUES (1)", false}, |
| 3863 | {"CREATE VIEW", "CREATE VIEW v AS SELECT * FROM pg_stat_activity", true}, // this references FROM, so it matches |
| 3864 | } |
| 3865 | |
| 3866 | for _, tt := range tests { |
| 3867 | t.Run(tt.name, func(t *testing.T) { |
| 3868 | got := matchPgStatActivityQuery(tt.query) |
| 3869 | if got != tt.want { |
| 3870 | t.Errorf("matchPgStatActivityQuery(%q) = %v, want %v", tt.query, got, tt.want) |
| 3871 | } |
| 3872 | }) |
| 3873 | } |
| 3874 | } |
| 3875 | |
| 3876 | func TestPgStatActivityColumnsAndOIDs(t *testing.T) { |
| 3877 | // Verify that pgStatActivityColumns and pgStatActivityTypeOIDs are consistent |
| 3878 | if len(pgStatActivityTypeOIDs) != len(pgStatActivityColumns) { |
| 3879 | t.Fatalf("pgStatActivityTypeOIDs length %d != pgStatActivityColumns length %d", |
| 3880 | len(pgStatActivityTypeOIDs), len(pgStatActivityColumns)) |
| 3881 | } |
| 3882 | |
| 3883 | for i, col := range pgStatActivityColumns { |
| 3884 | if pgStatActivityTypeOIDs[i] != col.oid { |
| 3885 | t.Errorf("pgStatActivityTypeOIDs[%d] = %d, want %d (column %s)", |
| 3886 | i, pgStatActivityTypeOIDs[i], col.oid, col.name) |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | // Verify expected columns are present |
| 3891 | expectedColumns := map[string]bool{ |
| 3892 | "datid": false, "datname": false, "pid": false, "usesysid": false, |
| 3893 | "usename": false, "application_name": false, "client_addr": false, |
| 3894 | "client_port": false, "backend_start": false, "state": false, |
| 3895 | "query": false, "backend_type": false, "worker_id": false, |
| 3896 | } |
| 3897 | for _, col := range pgStatActivityColumns { |
| 3898 | if _, ok := expectedColumns[col.name]; ok { |
| 3899 | expectedColumns[col.name] = true |
| 3900 | } |
| 3901 | } |
| 3902 | for name, found := range expectedColumns { |
| 3903 | if !found { |
| 3904 | t.Errorf("expected column %q not found in pgStatActivityColumns", name) |
| 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | |
| 3909 | func TestProjectReaderPgStatActivityCTECannotSeeOtherConnections(t *testing.T) { |
nothing calls this directly
no test coverage detected