-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostgresIntrospectionProviderTest.java
More file actions
454 lines (371 loc) · 19.5 KB
/
Copy pathPostgresIntrospectionProviderTest.java
File metadata and controls
454 lines (371 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package com.dbaagent.provider.postgres;
import com.dbaagent.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.sql.*;
import java.util.List;
import org.mockito.ArgumentCaptor;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class PostgresIntrospectionProviderTest {
private PostgresIntrospectionProvider provider;
@Mock
private Connection connection;
@Mock
private Statement statement;
@Mock
private PreparedStatement preparedStatement;
@Mock
private ResultSet resultSet;
@BeforeEach
void setUp() {
provider = new PostgresIntrospectionProvider();
}
@Test
void getDatabaseType_returnsPostgres() {
assertEquals("postgres", provider.getDatabaseType());
}
@Test
void getDatabaseObjects_returnsTables() throws SQLException {
when(connection.createStatement()).thenReturn(statement);
when(statement.executeQuery(anyString())).thenReturn(resultSet);
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
// First call returns one table, second call returns false
when(resultSet.next())
.thenReturn(true) // First table
.thenReturn(false) // End of tables
.thenReturn(false) // No columns
.thenReturn(false) // No functions
.thenReturn(false); // No procedures
when(resultSet.getString("schema_name")).thenReturn("public");
when(resultSet.getString("name")).thenReturn("users");
when(resultSet.getString("type")).thenReturn("table");
when(resultSet.getObject("row_count")).thenReturn(100L);
List<DatabaseObject> objects = provider.getDatabaseObjects(connection, "test_db");
assertNotNull(objects);
assertEquals(1, objects.size());
assertEquals("users", objects.get(0).getName());
assertEquals("table", objects.get(0).getType());
assertEquals("public", objects.get(0).getSchema());
}
@Test
void getTableColumns_returnsColumns() throws SQLException {
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next())
.thenReturn(true)
.thenReturn(true)
.thenReturn(false);
when(resultSet.getString("column_name"))
.thenReturn("id")
.thenReturn("name");
when(resultSet.getString("data_type"))
.thenReturn("integer")
.thenReturn("character varying");
when(resultSet.getString("is_nullable"))
.thenReturn("NO")
.thenReturn("YES");
when(resultSet.getBoolean("is_primary_key"))
.thenReturn(true)
.thenReturn(false);
when(resultSet.getString("column_default"))
.thenReturn("nextval('users_id_seq')")
.thenReturn(null);
List<ColumnInfo> columns = provider.getTableColumns(connection, "public", "users");
assertNotNull(columns);
assertEquals(2, columns.size());
assertEquals("id", columns.get(0).getName());
assertEquals("integer", columns.get(0).getDataType());
assertFalse(columns.get(0).getNullable());
assertTrue(columns.get(0).getPrimaryKey());
assertEquals("name", columns.get(1).getName());
assertEquals("character varying", columns.get(1).getDataType());
assertTrue(columns.get(1).getNullable());
assertFalse(columns.get(1).getPrimaryKey());
}
@Test
void getTableIndexes_returnsIndexes() throws SQLException {
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next())
.thenReturn(true)
.thenReturn(true)
.thenReturn(false);
when(resultSet.getString("index_name"))
.thenReturn("users_pkey")
.thenReturn("users_pkey");
when(resultSet.getString("column_name"))
.thenReturn("id")
.thenReturn("id");
when(resultSet.getBoolean("is_unique"))
.thenReturn(true);
when(resultSet.getBoolean("is_primary"))
.thenReturn(true);
when(resultSet.getString("index_type"))
.thenReturn("btree");
List<TableIndex> indexes = provider.getTableIndexes(connection, "public", "users");
assertNotNull(indexes);
assertEquals(1, indexes.size());
assertEquals("users_pkey", indexes.get(0).getName());
assertTrue(indexes.get(0).isUnique());
assertTrue(indexes.get(0).isPrimary());
assertEquals("btree", indexes.get(0).getType());
}
@Test
void getTableStats_returnsStats() throws SQLException {
PreparedStatement rowCountStatement = mock(PreparedStatement.class);
ResultSet rowCountResultSet = mock(ResultSet.class);
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement, rowCountStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(rowCountStatement.executeQuery()).thenReturn(rowCountResultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString("comment")).thenReturn("User accounts table");
when(resultSet.getLong("data_bytes")).thenReturn(81920L);
when(resultSet.getLong("index_bytes")).thenReturn(40960L);
when(resultSet.getLong("total_bytes")).thenReturn(122880L);
when(rowCountResultSet.next()).thenReturn(true);
when(rowCountResultSet.getObject("row_count")).thenReturn(1000L);
TableStats stats = provider.getTableStats(connection, "public", "users");
assertNotNull(stats);
assertEquals("users", stats.getTableName());
assertEquals("PostgreSQL", stats.getEngine());
assertEquals(1000L, stats.getRowCount());
assertEquals(81920L, stats.getDataSize());
assertEquals(40960L, stats.getIndexSize());
assertEquals(122880L, stats.getSizeBytes());
}
@Test
void getTableStats_bindsEveryPlaceholderInTheStatsQuery() throws SQLException {
// The stats query carried NINE `?` placeholders (the two size subtractions use
// two each) while the binding loop ran `i <= 7`, so parameters 8 and 9 were
// never set and Postgres rejected every call with
// No value specified for parameter 8
// silently killing table-growth snapshots for every table.
//
// getTableStats_returnsStats above passed throughout, because a mocked
// PreparedStatement does not enforce that placeholders are bound. This test
// compares the two directly, so the count can never drift again.
PreparedStatement rowCountStatement = mock(PreparedStatement.class);
ResultSet rowCountResultSet = mock(ResultSet.class);
ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement, rowCountStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(rowCountStatement.executeQuery()).thenReturn(rowCountResultSet);
when(resultSet.next()).thenReturn(true);
when(rowCountResultSet.next()).thenReturn(true);
when(rowCountResultSet.getObject("row_count")).thenReturn(1000L);
provider.getTableStats(connection, "public", "users");
verify(connection, atLeastOnce()).prepareStatement(sqlCaptor.capture());
String statsQuery = sqlCaptor.getAllValues().get(0);
int placeholders = (int) statsQuery.chars().filter(c -> c == '?').count();
assertTrue(placeholders > 0, "stats query should still be parameterised");
verify(preparedStatement, times(placeholders)).setString(anyInt(), eq("users"));
}
@Test
void scanSchema_returnsSchemaMetadata() throws SQLException {
when(connection.createStatement()).thenReturn(statement);
when(statement.executeQuery(anyString())).thenReturn(resultSet);
when(resultSet.next())
.thenReturn(true)
.thenReturn(false);
when(resultSet.getString("schemaname")).thenReturn("public");
when(resultSet.getString("tablename")).thenReturn("users");
when(resultSet.getString("type")).thenReturn("table");
when(resultSet.getObject("row_count")).thenReturn(100L);
when(resultSet.getLong("size_bytes")).thenReturn(8192L);
SchemaMetadata schema = provider.scanSchema(connection, "test_db");
assertNotNull(schema);
assertEquals("test_db", schema.getDatabaseName());
assertNotNull(schema.getTables());
assertEquals(1, schema.getTables().size());
assertEquals("users", schema.getTables().get(0).getName());
assertEquals("public", schema.getTables().get(0).getSchema());
}
@Test
void getForeignKeys_returnsRelationships() throws SQLException {
when(connection.createStatement()).thenReturn(statement);
when(statement.executeQuery(anyString())).thenReturn(resultSet);
when(resultSet.next())
.thenReturn(true)
.thenReturn(false);
when(resultSet.getString("constraint_name")).thenReturn("fk_orders_user");
when(resultSet.getString("source_schema")).thenReturn("public");
when(resultSet.getString("source_table")).thenReturn("orders");
when(resultSet.getString("source_column")).thenReturn("user_id");
when(resultSet.getString("target_schema")).thenReturn("public");
when(resultSet.getString("target_table")).thenReturn("users");
when(resultSet.getString("target_column")).thenReturn("id");
List<RelationshipMetadata> relationships = provider.getForeignKeys(connection, "test_db");
assertNotNull(relationships);
assertEquals(1, relationships.size());
assertEquals("fk_orders_user", relationships.get(0).getConstraintName());
// qualifyForConsumers returns just the table name for "public" schema
assertEquals("orders", relationships.get(0).getFromTable());
assertEquals("user_id", relationships.get(0).getFromColumn());
assertEquals("users", relationships.get(0).getToTable());
assertEquals("id", relationships.get(0).getToColumn());
}
@Test
void getTableRowCount_returnsCount() throws SQLException {
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getObject("row_count")).thenReturn(500L);
Long count = provider.getTableRowCount(connection, "public", "users");
assertEquals(500L, count);
}
@Test
void scanSchema_fallsBackToExactCountWhenEstimateMissing() throws SQLException {
Statement exactCountStatement = mock(Statement.class);
Statement columnsStatement = mock(Statement.class);
Statement indexesStatement = mock(Statement.class);
Statement foreignKeysStatement = mock(Statement.class);
ResultSet exactCountResultSet = mock(ResultSet.class);
ResultSet columnsResultSet = mock(ResultSet.class);
ResultSet indexesResultSet = mock(ResultSet.class);
ResultSet foreignKeysResultSet = mock(ResultSet.class);
when(connection.createStatement()).thenReturn(
statement,
exactCountStatement,
columnsStatement,
indexesStatement,
foreignKeysStatement
);
when(statement.executeQuery(anyString())).thenReturn(resultSet);
when(exactCountStatement.executeQuery(anyString())).thenReturn(exactCountResultSet);
when(columnsStatement.executeQuery(anyString())).thenReturn(columnsResultSet);
when(indexesStatement.executeQuery(anyString())).thenReturn(indexesResultSet);
when(foreignKeysStatement.executeQuery(anyString())).thenReturn(foreignKeysResultSet);
when(resultSet.next()).thenReturn(true).thenReturn(false);
when(resultSet.getString("schemaname")).thenReturn("public");
when(resultSet.getString("tablename")).thenReturn("dim_route");
when(resultSet.getString("type")).thenReturn("table");
when(resultSet.getObject("row_count")).thenReturn(null);
when(resultSet.getLong("size_bytes")).thenReturn(8192L);
when(exactCountResultSet.next()).thenReturn(true);
when(exactCountResultSet.getLong("row_count")).thenReturn(40L);
when(columnsResultSet.next()).thenReturn(false);
when(indexesResultSet.next()).thenReturn(false);
when(foreignKeysResultSet.next()).thenReturn(false);
SchemaMetadata schema = provider.scanSchema(connection, "test_db");
assertNotNull(schema);
assertEquals(1, schema.getTables().size());
assertEquals(40L, schema.getTables().get(0).getRowCount());
}
@Test
void getColumnDetails_returnsDetails() throws SQLException {
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next())
.thenReturn(true)
.thenReturn(false);
when(resultSet.getString("column_name")).thenReturn("email");
when(resultSet.getString("column_default")).thenReturn(null);
when(resultSet.getString("is_nullable")).thenReturn("NO");
when(resultSet.getString("data_type")).thenReturn("character varying");
when(resultSet.getLong("character_maximum_length")).thenReturn(255L);
when(resultSet.getInt("numeric_precision")).thenReturn(0);
when(resultSet.getInt("numeric_scale")).thenReturn(0);
when(resultSet.getString("udt_name")).thenReturn("varchar");
List<ColumnDetail> details = provider.getColumnDetails(connection, "public", "users");
assertNotNull(details);
assertEquals(1, details.size());
assertEquals("email", details.get(0).getColumnName());
assertEquals("character varying", details.get(0).getDataType());
assertFalse(details.get(0).getIsNullable());
}
// ─── pg_stat_statements extension view exclusion tests ───────────────────────
@Test
void excludeExtensionViewsPredicate_excludesPgStatStatements() {
String predicate = PostgresIntrospectionProvider.excludeExtensionViewsPredicate("v.viewname");
assertTrue(predicate.contains("pg_stat_statements"),
"Exclusion predicate should mention pg_stat_statements");
assertTrue(predicate.contains("pg_stat_statements_info"),
"Exclusion predicate should mention pg_stat_statements_info");
assertTrue(predicate.contains("pg_buffercache"),
"Exclusion predicate should mention pg_buffercache");
assertTrue(predicate.contains("NOT IN"),
"Exclusion predicate should use NOT IN clause");
}
@Test
void nonSystemSchemaPredicate_excludesPgCatalog() {
String predicate = PostgresIntrospectionProvider.nonSystemSchemaPredicate("t.schemaname");
assertTrue(predicate.contains("pg_catalog"),
"Schema predicate should exclude pg_catalog");
assertTrue(predicate.contains("information_schema"),
"Schema predicate should exclude information_schema");
assertTrue(predicate.contains("pg_toast"),
"Schema predicate should exclude pg_toast");
}
@Test
void getTablesAndViews_queryExcludesExtensionViews() throws SQLException {
// Capture all SQL queries and find the tables/views one
ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
when(connection.createStatement()).thenReturn(statement);
when(statement.executeQuery(sqlCaptor.capture())).thenReturn(resultSet);
when(resultSet.next()).thenReturn(false);
// getDatabaseObjects calls getTablesAndViews internally
List<DatabaseObject> objects = provider.getDatabaseObjects(connection, "test_db");
// Find the tables/views query among all executed queries
String tablesViewsQuery = sqlCaptor.getAllValues().stream()
.filter(q -> q.contains("pg_tables") || q.contains("pg_views"))
.findFirst()
.orElse(null);
assertNotNull(tablesViewsQuery, "Should have executed a query accessing pg_tables or pg_views");
// The query for views should contain the extension exclusion
assertTrue(tablesViewsQuery.contains("pg_views"),
"Query should access pg_views");
assertTrue(tablesViewsQuery.contains("pg_tables"),
"Query should access pg_tables");
// Verify schema exclusions are present
assertTrue(tablesViewsQuery.contains("NOT IN"),
"Query should have NOT IN clause for exclusions");
assertTrue(tablesViewsQuery.contains("pg_stat_statements"),
"Query should exclude pg_stat_statements extension views");
}
@Test
void extensionViewExclusion_isExactMatch() {
// Verify the exclusion predicate uses exact matches, not prefix matches
String predicate = PostgresIntrospectionProvider.EXCLUDED_EXTENSION_VIEWS_SQL;
// The excluded list should be specific, exact names only
assertTrue(predicate.contains("'pg_stat_statements'"),
"Predicate should exclude exactly 'pg_stat_statements'");
assertTrue(predicate.contains("'pg_stat_statements_info'"),
"Predicate should exclude exactly 'pg_stat_statements_info'");
assertTrue(predicate.contains("'pg_buffercache'"),
"Predicate should exclude exactly 'pg_buffercache'");
// Verify it's a NOT IN list (exact match semantics, not LIKE pattern)
assertTrue(predicate.startsWith("NOT IN"),
"Predicate should use NOT IN for exact matching");
}
@Test
void excludeExtensionFunctionsPredicate_excludesPgStatStatementsFunctions() {
String predicate = PostgresIntrospectionProvider.excludeExtensionFunctionsPredicate("p.proname");
assertTrue(predicate.contains("pg_stat_statements"),
"Exclusion predicate should mention pg_stat_statements");
assertTrue(predicate.contains("pg_stat_statements_info"),
"Exclusion predicate should mention pg_stat_statements_info");
assertTrue(predicate.contains("pg_stat_statements_reset"),
"Exclusion predicate should mention pg_stat_statements_reset");
assertTrue(predicate.contains("NOT IN"),
"Exclusion predicate should use NOT IN clause");
}
@Test
void extensionFunctionExclusion_isExactMatch() {
String predicate = PostgresIntrospectionProvider.EXCLUDED_EXTENSION_FUNCTIONS_SQL;
assertTrue(predicate.contains("'pg_stat_statements'"),
"Predicate should exclude exactly 'pg_stat_statements'");
assertTrue(predicate.contains("'pg_stat_statements_reset'"),
"Predicate should exclude exactly 'pg_stat_statements_reset'");
assertTrue(predicate.startsWith("NOT IN"),
"Predicate should use NOT IN for exact matching");
}
}