Skip to content
Permalink
Browse files

Fix metadata test to use explicit PK/FK names (#1442)

Changes testCrossReference() to validate the retrieved primary and foreign key columns
against their expected values. Previously it would have accepted them in either order or
the same value repeated twice for each.

Also checks to ensure that the expected number of rows are actually returned. Previously
it would have skipped all the checks if no rows were returned.
  • Loading branch information...
sehrope authored and vlsi committed Mar 16, 2019
1 parent 35df0dd commit 42d6bfa51929a333c3d2d8c098997a9f01514eac
Showing with 9 additions and 4 deletions.
  1. +9 −4 pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
@@ -213,30 +213,33 @@ public void testTables() throws Exception {
public void testCrossReference() throws Exception {
Connection con1 = TestUtil.openDB();

TestUtil.createTable(con1, "vv", "a int not null, b int not null, primary key ( a, b )");
TestUtil.createTable(con1, "vv", "a int not null, b int not null, constraint vv_pkey primary key ( a, b )");

TestUtil.createTable(con1, "ww",
"m int not null, n int not null, primary key ( m, n ), foreign key ( m, n ) references vv ( a, b )");
"m int not null, n int not null, constraint m_pkey primary key ( m, n ), constraint ww_m_fkey foreign key ( m, n ) references vv ( a, b )");


DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);

ResultSet rs = dbmd.getCrossReference(null, "", "vv", null, "", "ww");
String[] expectedPkColumnNames = new String[]{"a", "b"};
String[] expectedFkColumnNames = new String[]{"m", "n"};
int numRows = 0;

for (int j = 1; rs.next(); j++) {

String pkTableName = rs.getString("PKTABLE_NAME");
assertEquals("vv", pkTableName);

String pkColumnName = rs.getString("PKCOLUMN_NAME");
assertTrue(pkColumnName.equals("a") || pkColumnName.equals("b"));
assertEquals(expectedPkColumnNames[j - 1], pkColumnName);

String fkTableName = rs.getString("FKTABLE_NAME");
assertEquals("ww", fkTableName);

String fkColumnName = rs.getString("FKCOLUMN_NAME");
assertTrue(fkColumnName.equals("m") || fkColumnName.equals("n"));
assertEquals(expectedFkColumnNames[j - 1], fkColumnName);

String fkName = rs.getString("FK_NAME");
assertEquals("ww_m_fkey", fkName);
@@ -246,7 +249,9 @@ public void testCrossReference() throws Exception {

int keySeq = rs.getInt("KEY_SEQ");
assertEquals(j, keySeq);
numRows += 1;
}
assertEquals(2, numRows);


TestUtil.dropTable(con1, "vv");

0 comments on commit 42d6bfa

Please sign in to comment.
You can’t perform that action at this time.