|
19 | 19 | import static com.google.common.truth.Truth.assertThat;
|
20 | 20 | import static org.junit.Assert.assertEquals;
|
21 | 21 | import static org.junit.Assert.assertFalse;
|
| 22 | +import static org.junit.Assert.assertThrows; |
22 | 23 | import static org.junit.Assert.assertTrue;
|
23 | 24 | import static org.junit.Assert.fail;
|
24 | 25 |
|
25 | 26 | import com.google.api.core.ApiFuture;
|
26 | 27 | import com.google.api.core.ApiFutures;
|
27 | 28 | import com.google.cloud.spanner.AbortedException;
|
| 29 | +import com.google.cloud.spanner.Dialect; |
28 | 30 | import com.google.cloud.spanner.ErrorCode;
|
| 31 | +import com.google.cloud.spanner.MockSpannerServiceImpl; |
| 32 | +import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; |
29 | 33 | import com.google.cloud.spanner.ResultSet;
|
30 | 34 | import com.google.cloud.spanner.SpannerException;
|
31 | 35 | import com.google.cloud.spanner.SpannerOptions;
|
|
43 | 47 | import java.util.concurrent.TimeUnit;
|
44 | 48 | import java.util.concurrent.TimeoutException;
|
45 | 49 | import javax.annotation.Nonnull;
|
| 50 | +import org.junit.After; |
46 | 51 | import org.junit.AfterClass;
|
47 | 52 | import org.junit.Test;
|
48 | 53 | import org.junit.experimental.runners.Enclosed;
|
@@ -465,4 +470,48 @@ public void testShowSetRPCPriority() {
|
465 | 470 | }
|
466 | 471 | }
|
467 | 472 | }
|
| 473 | + |
| 474 | + public static class DialectDetectionTest extends AbstractMockServerTest { |
| 475 | + protected String getBaseUrl() { |
| 476 | + return super.getBaseUrl() + ";minSessions=1"; |
| 477 | + } |
| 478 | + |
| 479 | + @After |
| 480 | + public void reset() { |
| 481 | + // Reset dialect to default. |
| 482 | + mockSpanner.putStatementResult( |
| 483 | + MockSpannerServiceImpl.StatementResult.detectDialectResult(Dialect.GOOGLE_STANDARD_SQL)); |
| 484 | + mockSpanner.reset(); |
| 485 | + mockSpanner.removeAllExecutionTimes(); |
| 486 | + // Close all open Spanner instances to ensure that each test run gets a fresh instance. |
| 487 | + SpannerPool.closeSpannerPool(); |
| 488 | + } |
| 489 | + |
| 490 | + @Test |
| 491 | + public void testDefaultGetDialect() { |
| 492 | + try (Connection connection = createConnection()) { |
| 493 | + assertEquals(Dialect.GOOGLE_STANDARD_SQL, connection.getDialect()); |
| 494 | + } |
| 495 | + } |
| 496 | + |
| 497 | + @Test |
| 498 | + public void testPostgreSQLGetDialect() { |
| 499 | + mockSpanner.putStatementResult( |
| 500 | + MockSpannerServiceImpl.StatementResult.detectDialectResult(Dialect.POSTGRESQL)); |
| 501 | + try (Connection connection = createConnection()) { |
| 502 | + assertEquals(Dialect.POSTGRESQL, connection.getDialect()); |
| 503 | + } |
| 504 | + } |
| 505 | + |
| 506 | + @Test |
| 507 | + public void testGetDialect_DatabaseNotFound() throws Exception { |
| 508 | + mockSpanner.setBatchCreateSessionsExecutionTime( |
| 509 | + SimulatedExecutionTime.stickyDatabaseNotFoundException("invalid-database")); |
| 510 | + try (Connection connection = createConnection()) { |
| 511 | + SpannerException exception = assertThrows(SpannerException.class, connection::getDialect); |
| 512 | + assertEquals(ErrorCode.NOT_FOUND, exception.getErrorCode()); |
| 513 | + assertTrue(exception.getMessage().contains("Database with id invalid-database not found")); |
| 514 | + } |
| 515 | + } |
| 516 | + } |
468 | 517 | }
|
0 commit comments