Skip to content

Commit

Permalink
Remove usage of @test with exceptions in plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Damans227 authored and ebyhr committed Oct 17, 2021
1 parent eadea39 commit ede9164
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static io.trino.spi.type.VarcharType.VARCHAR;
import static java.lang.Float.floatToIntBits;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;

public class TestRow
Expand Down Expand Up @@ -67,10 +68,12 @@ public void testRow()
assertEquals(r2, r1);
}

@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "type is null")
@Test
public void testRowTypeIsNull()
{
Row r1 = new Row();
r1.addField(VARCHAR, null);
assertThatThrownBy(() -> r1.addField(VARCHAR, null))
.isInstanceOf(NullPointerException.class)
.hasMessage("type is null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import static io.trino.plugin.atop.LocalAtopQueryRunner.createQueryRunner;
import static io.trino.testing.TestingSession.testSessionBuilder;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestAtopSecurity
{
Expand Down Expand Up @@ -57,11 +58,13 @@ public void testAdminCanRead()
queryRunner.execute(admin, "SELECT * FROM disks");
}

@Test(expectedExceptions = AccessDeniedException.class)
@Test
public void testNonAdminCannotRead()
{
Session bob = getSession("bob");
queryRunner.execute(bob, "SELECT * FROM disks");
assertThatThrownBy(() -> queryRunner.execute(bob, "SELECT * FROM disks"))
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied:.*");
}

private Session getSession(String user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static io.trino.testing.assertions.Assert.assertEquals;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -117,12 +118,15 @@ protected TestTable createTableWithDefaultColumns()
throw new SkipException("Memory connector does not support column default values");
}

// it has to be RuntimeException as FailureInfo$FailureException is private
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "line 1:1: Destination table 'memory.default.nation' already exists")
@Test
public void testCreateTableWhenTableIsAlreadyCreated()
{
@Language("SQL") String createTableSql = "CREATE TABLE nation AS SELECT * FROM tpch.tiny.nation";
assertUpdate(createTableSql);

// it has to be RuntimeException as FailureInfo$FailureException is private
assertThatThrownBy(() -> assertUpdate(createTableSql))
.isInstanceOf(RuntimeException.class)
.hasMessage("line 1:1: Destination table 'memory.default.nation' already exists");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static io.trino.testing.TestingConnectorSession.SESSION;
import static io.trino.testing.assertions.TrinoExceptionAssert.assertTrinoExceptionThrownBy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNull;
Expand Down Expand Up @@ -163,7 +164,7 @@ public void testCreateSchema()
assertEquals(metadata.listTables(SESSION, Optional.of("default")), ImmutableList.of());
}

@Test(expectedExceptions = TrinoException.class, expectedExceptionsMessageRegExp = "View already exists: test\\.test_view")
@Test
public void testCreateViewWithoutReplace()
{
SchemaTableName test = new SchemaTableName("test", "test_view");
Expand All @@ -174,7 +175,9 @@ public void testCreateViewWithoutReplace()
catch (Exception e) {
fail("should have succeeded");
}
metadata.createView(SESSION, test, testingViewDefinition("test"), false);
assertThatThrownBy(() -> metadata.createView(SESSION, test, testingViewDefinition("test"), false))
.isInstanceOf(TrinoException.class)
.hasMessageMatching("View already exists: test\\.test_view");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.testing.TestingConnectorSession.SESSION;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -78,12 +79,14 @@ public void testReadFromUnknownTable()
pagesStore.getPages(0L, 0, 1, ImmutableList.of(0), 0, OptionalLong.empty(), OptionalDouble.empty());
}

@Test(expectedExceptions = TrinoException.class)
@Test
public void testTryToReadFromEmptyTable()
{
createTable(0L, 0L);
assertEquals(pagesStore.getPages(0L, 0, 1, ImmutableList.of(0), 0, OptionalLong.empty(), OptionalDouble.empty()), ImmutableList.of());
pagesStore.getPages(0L, 0, 1, ImmutableList.of(0), 42, OptionalLong.empty(), OptionalDouble.empty());
assertThatThrownBy(() -> pagesStore.getPages(0L, 0, 1, ImmutableList.of(0), 42, OptionalLong.empty(), OptionalDouble.empty()))
.isInstanceOf(TrinoException.class)
.hasMessageMatching("Expected to find.*");
}

@Test
Expand All @@ -110,12 +113,14 @@ public void testCleanUp()
assertTrue(pagesStore.contains(2L));
}

@Test(expectedExceptions = TrinoException.class)
@Test
public void testMemoryLimitExceeded()
{
createTable(0L, 0L);
insertToTable(0L, createOneMegaBytePage(), 0L);
insertToTable(0L, createOneMegaBytePage(), 0L);
assertThatThrownBy(() -> insertToTable(0L, createOneMegaBytePage(), 0L))
.isInstanceOf(TrinoException.class)
.hasMessageMatching("Memory limit.*");
}

private void insertToTable(long tableId, Long... activeTableIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.IntegerType.INTEGER;
import static java.util.Collections.unmodifiableList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
Expand Down Expand Up @@ -84,18 +85,22 @@ public void testReadBlockAllNonNullOption2()
assertBlockEquals(actual, list(2L, 7L, 1L, 3L, 8L, 4L, 5L));
}

@Test(expectedExceptions = IllegalArgumentException.class)
@Test
public void testReadBlockWrongActualType()
{
TrinoThriftBlock columnsData = integerData(new TrinoThriftInteger(null, null));
columnsData.toBlock(BIGINT);
assertThatThrownBy(() -> columnsData.toBlock(BIGINT))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("type doesn't match:.*");
}

@Test(expectedExceptions = IllegalArgumentException.class)
@Test
public void testReadBlockWrongDesiredType()
{
TrinoThriftBlock columnsData = longColumn(null, null);
columnsData.toBlock(INTEGER);
assertThatThrownBy(() -> columnsData.toBlock(INTEGER))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("type doesn't match:.*");
}

@Test
Expand Down

0 comments on commit ede9164

Please sign in to comment.