Skip to content

Commit 04da50c

Browse files
anusudarsanebyhr
authored andcommitted
Randomize object names in tests
Randomize to run tests outside of non-dockerized env (like AstraDB) and avoid conflicts Co-authored by: Mayank Vadariya <[email protected]>
1 parent 0373ebb commit 04da50c

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

plugin/trino-cassandra/src/test/java/io/trino/plugin/cassandra/TestCassandraConnectorTest.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,18 @@ public void testCreateTableAs()
676676
@Test
677677
public void testIdentifiers()
678678
{
679-
String quotedKeyspaceNameStartingWithUnderscore = "\"_keyspace\"";
679+
String keyspaceName = "_keyspace" + randomNameSuffix();
680+
String quotedKeyspaceNameStartingWithUnderscore = "\"%s\"".formatted(keyspaceName);
680681
dropKeyspace(quotedKeyspaceNameStartingWithUnderscore);
681682
createKeyspace(quotedKeyspaceNameStartingWithUnderscore);
682683
assertContainsEventually(() -> computeActual("SHOW SCHEMAS FROM cassandra"), resultBuilder(getSession(), createUnboundedVarcharType())
683-
.row("_keyspace")
684+
.row(keyspaceName)
684685
.build(), new Duration(1, MINUTES));
685686

686-
assertUpdate("CREATE TABLE _keyspace._table AS SELECT 1 AS \"_col\", 2 AS \"2col\"", 1);
687-
assertQuery("SHOW TABLES FROM cassandra._keyspace", "VALUES ('_table')");
688-
assertQuery("SELECT * FROM cassandra._keyspace._table", "VALUES (1, 2)");
689-
assertUpdate("DROP TABLE cassandra._keyspace._table");
687+
assertUpdate("CREATE TABLE %s._table AS SELECT 1 AS \"_col\", 2 AS \"2col\"".formatted(keyspaceName), 1);
688+
assertQuery("SHOW TABLES FROM cassandra." + keyspaceName, "VALUES ('_table')");
689+
assertQuery("SELECT * FROM cassandra.%s._table".formatted(keyspaceName), "VALUES (1, 2)");
690+
assertUpdate("DROP TABLE cassandra.%s._table".formatted(keyspaceName));
690691

691692
dropKeyspace(quotedKeyspaceNameStartingWithUnderscore);
692693
}
@@ -1000,9 +1001,10 @@ public void testUpperCaseNameUnescapedInCassandra()
10001001
*
10011002
* http://docs.datastax.com/en/cql/3.1/cql/cql_reference/ucase-lcase_r.html
10021003
*/
1003-
String uppercaseKeyspaceName = "KEYSPACE_1";
1004-
createKeyspace(uppercaseKeyspaceName);
1004+
String uppercaseKeyspaceName = "KEYSPACE_1" + randomNameSuffix().toUpperCase(ENGLISH);
10051005
String lowercaseKeyspaceName = uppercaseKeyspaceName.toLowerCase(ENGLISH);
1006+
createKeyspace(uppercaseKeyspaceName);
1007+
10061008
assertContainsEventually(() -> computeActual("SHOW SCHEMAS FROM cassandra"), resultBuilder(getSession(), createUnboundedVarcharType())
10071009
.row(lowercaseKeyspaceName)
10081010
.build(), new Duration(1, MINUTES));
@@ -1032,24 +1034,26 @@ public void testUppercaseNameEscaped()
10321034
*
10331035
* http://docs.datastax.com/en/cql/3.1/cql/cql_reference/ucase-lcase_r.html
10341036
*/
1035-
String quotedUppercaseKeyspaceName = "\"KEYSPACE_2\"";
1037+
String uppercaseKeyspaceName = "KEYSPACE_2%s".formatted(randomNameSuffix().toUpperCase(ENGLISH));
1038+
String quotedUppercaseKeyspaceName = "\"%s\"".formatted(uppercaseKeyspaceName);
10361039
createKeyspace(quotedUppercaseKeyspaceName);
1040+
String lowerCaseKeyspaceName = uppercaseKeyspaceName.toLowerCase(ENGLISH);
10371041
assertContainsEventually(() -> computeActual("SHOW SCHEMAS FROM cassandra"), resultBuilder(getSession(), createUnboundedVarcharType())
1038-
.row("keyspace_2")
1042+
.row(lowerCaseKeyspaceName)
10391043
.build(), new Duration(1, MINUTES));
10401044

10411045
session.execute("CREATE TABLE " + quotedUppercaseKeyspaceName + ".\"TABLE_2\" (\"COLUMN_2\" bigint PRIMARY KEY)");
1042-
assertContainsEventually(() -> computeActual("SHOW TABLES FROM cassandra.keyspace_2"), resultBuilder(getSession(), createUnboundedVarcharType())
1046+
assertContainsEventually(() -> computeActual("SHOW TABLES FROM cassandra." + lowerCaseKeyspaceName), resultBuilder(getSession(), createUnboundedVarcharType())
10431047
.row("table_2")
10441048
.build(), new Duration(1, MINUTES));
1045-
assertContains(computeActual("SHOW COLUMNS FROM cassandra.keyspace_2.table_2"), resultBuilder(getSession(), createUnboundedVarcharType(), createUnboundedVarcharType(), createUnboundedVarcharType(), createUnboundedVarcharType())
1049+
assertContains(computeActual("SHOW COLUMNS FROM cassandra.%s.table_2".formatted(lowerCaseKeyspaceName)), resultBuilder(getSession(), createUnboundedVarcharType(), createUnboundedVarcharType(), createUnboundedVarcharType(), createUnboundedVarcharType())
10461050
.row("column_2", "bigint", "", "")
10471051
.build());
10481052

10491053
assertUpdate("INSERT INTO " + quotedUppercaseKeyspaceName + ".\"TABLE_2\" (\"COLUMN_2\") VALUES (1)", 1);
10501054

1051-
assertThat(computeActual("SELECT column_2 FROM cassandra.keyspace_2.table_2").getRowCount()).isEqualTo(1);
1052-
assertUpdate("DROP TABLE cassandra.keyspace_2.table_2");
1055+
assertThat(computeActual("SELECT column_2 FROM cassandra.%s.table_2".formatted(lowerCaseKeyspaceName)).getRowCount()).isEqualTo(1);
1056+
assertUpdate("DROP TABLE cassandra.%s.table_2".formatted(lowerCaseKeyspaceName));
10531057

10541058
// when an identifier is unquoted the lowercase and uppercase spelling may be used interchangeable
10551059
dropKeyspace(quotedUppercaseKeyspaceName);
@@ -1062,28 +1066,35 @@ public void testKeyspaceNameAmbiguity()
10621066
executeExclusively(() -> {
10631067
// Identifiers enclosed in double quotes are stored in Cassandra verbatim. It is possible to create 2 keyspaces with names
10641068
// that have differences only in letters case.
1065-
String quotedKeyspaceMixedCaseName1 = "\"KeYsPaCe_3\"";
1069+
String randomNameSuffix = randomNameSuffix();
1070+
String mixedCaseKeyspaceName1 = "KeYsPaCe_3%s".formatted(randomNameSuffix);
1071+
String mixedCaseKeyspaceName2 = "kEySpAcE_3%s".formatted(randomNameSuffix);
1072+
String lowercaseKeyspaceName = "keyspace_3%s".formatted(randomNameSuffix);
1073+
1074+
String quotedKeyspaceMixedCaseName1 = "\"%s\"".formatted(mixedCaseKeyspaceName1);
1075+
String quotedKeyspaceMixedCaseName2 = "\"%s\"".formatted(mixedCaseKeyspaceName2);
1076+
10661077
createKeyspace(quotedKeyspaceMixedCaseName1);
1067-
String quotedKeyspaceMixedCaseName2 = "\"kEySpAcE_3\"";
10681078
createKeyspace(quotedKeyspaceMixedCaseName2);
10691079

10701080
// Although in Trino all the schema and table names are always displayed as lowercase
10711081
assertContainsEventually(() -> computeActual("SHOW SCHEMAS FROM cassandra"), resultBuilder(getSession(), createUnboundedVarcharType())
1072-
.row("keyspace_3")
1073-
.row("keyspace_3")
1082+
.row(lowercaseKeyspaceName)
1083+
.row(lowercaseKeyspaceName)
10741084
.build(), new Duration(1, MINUTES));
10751085

10761086
// There is no way to figure out what the exactly keyspace we want to retrieve tables from
10771087
assertQueryFailsEventually(
1078-
"SHOW TABLES FROM cassandra.keyspace_3",
1079-
"Error listing tables for catalog cassandra: More than one keyspace has been found for the case insensitive schema name: keyspace_3 -> \\(KeYsPaCe_3, kEySpAcE_3\\)",
1088+
"SHOW TABLES FROM cassandra.%s".formatted(lowercaseKeyspaceName),
1089+
"Error listing tables for catalog cassandra: More than one keyspace has been found for the case insensitive schema name: %s -> \\(%s, %s\\)"
1090+
.formatted(lowercaseKeyspaceName, mixedCaseKeyspaceName1, mixedCaseKeyspaceName2),
10801091
new Duration(1, MINUTES));
10811092

10821093
dropKeyspace(quotedKeyspaceMixedCaseName1);
10831094
dropKeyspace(quotedKeyspaceMixedCaseName2);
10841095
// Wait until the schema becomes invisible to Trino. Otherwise, testSelectInformationSchemaColumns may fail due to ambiguous schema names.
10851096
assertEventually(() -> assertThat(computeActual("SHOW SCHEMAS FROM cassandra").getOnlyColumnAsSet())
1086-
.doesNotContain("keyspace_3"));
1097+
.doesNotContain(lowercaseKeyspaceName));
10871098
});
10881099
}
10891100

@@ -1092,7 +1103,7 @@ public void testTableNameAmbiguity()
10921103
{
10931104
// This test creates tables with names that collide in a way not supported by the connector. Run it exclusively to prevent other tests from failing.
10941105
executeExclusively(() -> {
1095-
String keyspaceName = "keyspace_4";
1106+
String keyspaceName = "keyspace_4" + randomNameSuffix();
10961107
createKeyspace(keyspaceName);
10971108
assertContainsEventually(() -> computeActual("SHOW SCHEMAS FROM cassandra"), resultBuilder(getSession(), createUnboundedVarcharType())
10981109
.row(keyspaceName)
@@ -1127,7 +1138,7 @@ public void testColumnNameAmbiguity()
11271138
{
11281139
// This test creates columns with names that collide in a way not supported by the connector. Run it exclusively to prevent other tests from failing.
11291140
executeExclusively(() -> {
1130-
String keyspaceName = "keyspace_5";
1141+
String keyspaceName = "keyspace_5" + randomNameSuffix();
11311142
createKeyspace(keyspaceName);
11321143
assertContainsEventually(() -> computeActual("SHOW SCHEMAS FROM cassandra"), resultBuilder(getSession(), createUnboundedVarcharType())
11331144
.row(keyspaceName)
@@ -1337,7 +1348,7 @@ public void testEmptyTimestampClusteringKey()
13371348
@Test
13381349
public void testNestedCollectionType()
13391350
{
1340-
String keyspaceName = "keyspace_test_nested_collection";
1351+
String keyspaceName = "keyspace_test_nested_collection" + randomNameSuffix();
13411352
createKeyspace(keyspaceName);
13421353
assertContainsEventually(() -> computeActual("SHOW SCHEMAS FROM cassandra"), resultBuilder(getSession(), createUnboundedVarcharType())
13431354
.row(keyspaceName)

0 commit comments

Comments
 (0)