Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: materialized view privileges #2209

Merged
merged 10 commits into from Nov 12, 2021
25 changes: 25 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/TestUtil.java
Expand Up @@ -525,6 +525,24 @@ public static void createView(Connection con, String viewName, String query)
}
}

/*
* Helper - creates a materialized view
*/
public static void createMaterializedView(Connection con, String matViewName, String query)
throws SQLException {
Statement st = con.createStatement();
davecramer marked this conversation as resolved.
Show resolved Hide resolved
try {
// Drop the view
dropView(con, matViewName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be dropMaterializedView(...).


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

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

/**
* Helper creates an enum type.
*
Expand Down Expand Up @@ -628,6 +646,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 @@ -73,7 +73,6 @@ public void setUp() throws Exception {
}
TestUtil.createTable(con, "metadatatest",
"id int4, name text, updated timestamptz, colour text, quest text");
TestUtil.createTable(con, "precision_test", "implicit_precision numeric");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might be basing this on an out of date branch. Rebase atop master as these changes should not be removing those tests.

TestUtil.dropSequence(con, "sercoltest_b_seq");
TestUtil.dropSequence(con, "sercoltest_c_seq");
TestUtil.createTable(con, "sercoltest", "a int, b serial, c bigserial");
Expand All @@ -82,6 +81,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 @@ -127,11 +127,11 @@ public void tearDown() throws Exception {
stmt.execute("DROP FUNCTION f4(int)");

TestUtil.dropView(con, "viewtest");
TestUtil.dropMaterializedView(con, "matviewtest");
TestUtil.dropTable(con, "metadatatest");
TestUtil.dropTable(con, "sercoltest");
TestUtil.dropSequence(con, "sercoltest_b_seq");
TestUtil.dropSequence(con, "sercoltest_c_seq");
TestUtil.dropTable(con, "precision_test");
TestUtil.dropTable(con, "\"a\\\"");
TestUtil.dropTable(con, "\"a'\"");
TestUtil.dropTable(con, "arraytable");
Expand Down Expand Up @@ -210,7 +210,7 @@ public void testTables() throws Exception {
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);

ResultSet rs = dbmd.getTables(null, null, "metadatates%", new String[]{"TABLE"});
ResultSet rs = dbmd.getTables(null, null, "metadatatest", new String[]{"TABLE"});
davecramer marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(rs.next());
String tableName = rs.getString("TABLE_NAME");
assertEquals("metadatatest", tableName);
Expand Down Expand Up @@ -497,16 +497,6 @@ public void testForeignKeys() throws Exception {
TestUtil.closeDB(con1);
}

@Test
public void testNumericPrecision() throws SQLException {
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getColumns(null, "public", "precision_test", "%");
assertTrue("It should have a row for the first column", rs.next());
assertEquals("The column size should be zero", 0, rs.getInt("COLUMN_SIZE"));
assertFalse("It should have a single column", rs.next());
}

@Test
public void testColumns() throws SQLException {
// At the moment just test that no exceptions are thrown KJ
Expand Down Expand Up @@ -612,11 +602,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 {
davecramer marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -625,9 +620,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 @@ -640,24 +652,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