Skip to content

Commit

Permalink
test: materialized view privileges (#2209)
Browse files Browse the repository at this point in the history
* test: add and drop a materialized view

Add to TestUtil and also to DatabaseMetaData setup and teardown

fixes #2060

* test: materialized view privileges

make "matviewtest" all lowercase so it can be found in pg catalog, which lowercases all names

fixes #2060

Co-authored-by: Dave Cramer <davecramer@gmail.com>
  • Loading branch information
mgrobaker and davecramer committed Nov 12, 2021
1 parent 0b49f42 commit 7d2a21e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
27 changes: 23 additions & 4 deletions pgjdbc/src/test/java/org/postgresql/test/TestUtil.java
Expand Up @@ -512,16 +512,28 @@ public static void createUnloggedTable(Connection con, String table, String colu
*/
public static void createView(Connection con, String viewName, String query)
throws SQLException {
Statement st = con.createStatement();
try {
try ( Statement st = con.createStatement() ) {
// Drop the view
dropView(con, viewName);

String sql = "CREATE VIEW " + viewName + " AS " + query;

st.executeUpdate(sql);
} finally {
closeQuietly(st);
}
}

/*
* Helper - creates a materialized view
*/
public static void createMaterializedView(Connection con, String matViewName, String query)
throws SQLException {
try ( Statement st = con.createStatement() ) {
// Drop the view
dropMaterializedView(con, matViewName);

String sql = "CREATE MATERIALIZED VIEW " + matViewName + " AS " + query;

st.executeUpdate(sql);
}
}

Expand Down Expand Up @@ -628,6 +640,13 @@ public static void dropView(Connection con, String view) throws SQLException {
dropObject(con, "VIEW", view);
}

/*
* Helper - drops a materialized view
*/
public static void dropMaterializedView(Connection con, String matView) throws SQLException {
dropObject(con, "MATERIALIZED VIEW", matView);
}

/*
* Helper - drops a type
*/
Expand Down
Expand Up @@ -82,6 +82,7 @@ public void setUp() throws Exception {
TestUtil.createTable(con, "arraytable", "a numeric(5,2)[], b varchar(100)[]");
TestUtil.createTable(con, "intarraytable", "a int4[], b int4[][]");
TestUtil.createView(con, "viewtest", "SELECT id, quest FROM metadatatest");
TestUtil.createMaterializedView(con, "matviewtest", "SELECT id, quest FROM metadatatest");
TestUtil.dropType(con, "custom");
TestUtil.dropType(con, "_custom");
TestUtil.createCompositeType(con, "custom", "i int", false);
Expand Down Expand Up @@ -144,6 +145,7 @@ public void tearDown() throws Exception {
TestUtil.dropTable(con, "duplicate");

TestUtil.dropView(con, "viewtest");
TestUtil.dropMaterializedView(con, "matviewtest");
TestUtil.dropTable(con, "metadatatest");
TestUtil.dropTable(con, "sercoltest");
TestUtil.dropSequence(con, "sercoltest_b_seq");
Expand Down Expand Up @@ -631,11 +633,16 @@ public void testColumnPrivileges() throws SQLException {
rs.close();
}

@Test
public void testTablePrivileges() throws SQLException {
/*
* Helper function - this logic is used several times to test relation privileges
*/
public void relationPrivilegesHelper(String relationName) throws SQLException {
// Query PG catalog for privileges
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getTablePrivileges(null, null, "metadatatest");
ResultSet rs = dbmd.getTablePrivileges(null, null, relationName);

// Parse result to check if table/view owner has select privileges
boolean foundSelect = false;
while (rs.next()) {
if (rs.getString("GRANTEE").equals(TestUtil.getUser())
Expand All @@ -644,9 +651,26 @@ public void testTablePrivileges() throws SQLException {
}
}
rs.close();
// Test that the table owner has select priv
assertTrue("Couldn't find SELECT priv on table metadatatest for " + TestUtil.getUser(),
foundSelect);

// Check test condition
assertTrue("Couldn't find SELECT priv on relation "
+ relationName + " for " + TestUtil.getUser(),
foundSelect);
}

@Test
public void testTablePrivileges() throws SQLException {
relationPrivilegesHelper("metadatatest");
}

@Test
public void testViewPrivileges() throws SQLException {
relationPrivilegesHelper("viewtest");
}

@Test
public void testMaterializedViewPrivileges() throws SQLException {
relationPrivilegesHelper("matviewtest");
}

@Test
Expand All @@ -659,24 +683,6 @@ public void testNoTablePrivileges() throws SQLException {
assertTrue(!rs.next());
}

@Test
public void testViewPrivileges() throws SQLException {
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getTablePrivileges(null, null, "viewtest");
boolean foundSelect = false;
while (rs.next()) {
if (rs.getString("GRANTEE").equals(TestUtil.getUser())
&& rs.getString("PRIVILEGE").equals("SELECT")) {
foundSelect = true;
}
}
rs.close();
// Test that the view owner has select priv
assertTrue("Couldn't find SELECT priv on table metadatatest for " + TestUtil.getUser(),
foundSelect);
}

@Test
public void testPrimaryKeys() throws SQLException {
// At the moment just test that no exceptions are thrown KJ
Expand Down

0 comments on commit 7d2a21e

Please sign in to comment.