Best Testcontainers-java code snippet using org.testcontainers.containers.SpannerEmulatorContainerTest.createDatabase
Source:SpannerEmulatorContainerTest.java
...38 .setProjectId(PROJECT_NAME)39 .build();40 Spanner spanner = options.getService();41 InstanceId instanceId = createInstance(spanner);42 createDatabase(spanner);43 DatabaseId databaseId = DatabaseId.of(instanceId, DATABASE_NAME);44 DatabaseClient dbClient = spanner.getDatabaseClient(databaseId);45 dbClient.readWriteTransaction()46 .run(tx -> {47 String sql1 = "Delete from TestTable where 1=1";48 tx.executeUpdate(Statement.of(sql1));49 String sql = "INSERT INTO TestTable (Key, Value) VALUES (1, 'Java'), (2, 'Go')";50 tx.executeUpdate(Statement.of(sql));51 return null;52 });53 ResultSet resultSet = dbClient.readOnlyTransaction()54 .executeQuery(Statement.of("select * from TestTable order by Key"));55 resultSet.next();56 assertThat(resultSet.getLong(0)).isEqualTo(1);57 assertThat(resultSet.getString(1)).isEqualTo("Java");58 }59 // }60 // createDatabase {61 private void createDatabase(Spanner spanner) throws InterruptedException, ExecutionException {62 DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();63 Database database = dbAdminClient.createDatabase(INSTANCE_NAME, DATABASE_NAME, Arrays.asList("CREATE TABLE TestTable (Key INT64, Value STRING(MAX)) PRIMARY KEY (Key)")).get();64 }65 // }66 // createInstance {67 private InstanceId createInstance(Spanner spanner) throws InterruptedException, ExecutionException {68 InstanceConfigId instanceConfig = InstanceConfigId.of(PROJECT_NAME, "emulator-config");69 InstanceId instanceId = InstanceId.of(PROJECT_NAME, INSTANCE_NAME);70 InstanceAdminClient insAdminClient = spanner.getInstanceAdminClient();71 Instance instance = insAdminClient.createInstance(InstanceInfo.newBuilder(instanceId).setNodeCount(1).setDisplayName("Test instance").setInstanceConfigId(instanceConfig).build()).get();72 return instanceId;73 }74 // }75}...
createDatabase
Using AI Code Generation
1SpannerEmulatorContainerTest spannerEmulatorContainerTest = new SpannerEmulatorContainerTest();2spannerEmulatorContainerTest.createDatabase();3public static void main(String[] args) throws Exception {4 SpannerEmulatorContainerTest spannerEmulatorContainerTest = new SpannerEmulatorContainerTest();5 spannerEmulatorContainerTest.createDatabase();6}7SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer();8spannerEmulatorContainer.start();9SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer();10spannerEmulatorContainer.start();11SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer();12spannerEmulatorContainer.start();13SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer();14spannerEmulatorContainer.start();
createDatabase
Using AI Code Generation
1 public void createDatabase() {2 try (SpannerEmulatorContainer spannerEmulator = new SpannerEmulatorContainer()) {3 spannerEmulator.start();4 SpannerOptions options = SpannerOptions.newBuilder()5 .setCredentials(NoCredentials.getInstance())6 .build();7 Spanner spanner = options.getService();8 DatabaseId databaseId = DatabaseId.of(options.getProjectId(), "test-instance", "test-database");9 DatabaseClient databaseClient = spanner.getDatabaseClient(databaseId);10 databaseClient.writeAtLeastOnce(Arrays.asList(11 Mutation.newInsertOrUpdateBuilder("Users")12 .set("UserId").to(1)13 .set("Name").to("John")14 .build()15 ));16 ResultSet resultSet = databaseClient.singleUse().executeQuery(Statement.of("SELECT * FROM Users"));17 assertThat(resultSet.next()).isTrue();18 assertThat(resultSet.getLong(0)).isEqualTo(1);19 assertThat(resultSet.getString(1)).isEqualTo("John");20 assertThat(resultSet.next()).isFalse();21 }22 }23}
createDatabase
Using AI Code Generation
1package org.testcontainers.containers;2import com.google.cloud.spanner.Database;3import com.google.cloud.spanner.DatabaseAdminClient;4import com.google.cloud.spanner.DatabaseId;5import com.google.cloud.spanner.Spanner;6import com.google.cloud.spanner.SpannerOptions;7import com.google.cloud.spanner.testing.RemoteSpannerHelper;8import org.junit.After;9import org.junit.Before;10import org.junit.Rule;11import org.junit.Test;12import java.util.Collections;13import static org.junit.Assert.assertEquals;14import static org.junit.Assert.assertNotNull;15public class SpannerEmulatorContainerTest {16 public SpannerEmulatorContainer spanner = new SpannerEmulatorContainer();17 private DatabaseAdminClient databaseAdminClient;18 public void setUp() {19 SpannerOptions options = SpannerOptions.newBuilder()20 .setHost(spanner.getEmulatorEndpoint())21 .setProjectId(RemoteSpannerHelper.getOptions().getProjectId())22 .build();23 Spanner spanner = options.getService();24 databaseAdminClient = spanner.getDatabaseAdminClient();25 }26 public void tearDown() {27 databaseAdminClient.close();28 }29 public void testCreateDatabase() {30 DatabaseId databaseId = DatabaseId.of(spanner.getProjectId(), spanner.getInstanceId(), spanner.getDatabaseId());31 Database database = createDatabase(databaseAdminClient, databaseId);32 assertNotNull(database);33 assertEquals(databaseId.getDatabase(), database.getId().getDatabase());34 }35 static Database createDatabase(DatabaseAdminClient databaseAdminClient, DatabaseId databaseId) {36 return databaseAdminClient.createDatabase(databaseId.getInstanceId().getInstance(),37 databaseId.getDatabase(),38 Collections.emptyList()).get();39 }40}41package org.testcontainers.containers;42import com.google.cloud.spanner.Database;43import com.google.cloud.spanner.DatabaseAdminClient;44import com.google.cloud.spanner.DatabaseId;45import com.google.cloud.spanner.Spanner;46import com.google.cloud.spanner.SpannerOptions;47import com.google.cloud.spanner.testing.RemoteSpannerHelper;48import org.junit.After;49import org.junit.Before;50import org.junit.Rule;51import org.junit.Test;52import java.util.Collections;53import static org.junit.Assert.assertEquals;54import static org.junit.Assert.assertNotNull;
createDatabase
Using AI Code Generation
1public class SpannerEmulatorContainerTest {2 private static final String PROJECT_ID = "test-project";3 private static final String INSTANCE_ID = "test-instance";4 private static final String DATABASE_ID = "test-database";5 private static final String SPANNER_EMULATOR_HOST = "localhost:9010";6 private static SpannerEmulatorContainer spannerEmulatorContainer;7 public static void setUp() {8 spannerEmulatorContainer = new SpannerEmulatorContainer();9 spannerEmulatorContainer.start();10 }11 public static void tearDown() {12 spannerEmulatorContainer.stop();13 }14 public void createDatabase() throws IOException {15 String host = spannerEmulatorContainer.getEmulatorHost();16 Spanner spanner = SpannerOptions.newBuilder().setHost(host).build().getService();17 DatabaseId db = DatabaseId.of(PROJECT_ID, INSTANCE_ID, DATABASE_ID);18 .getDatabaseAdminClient()19 .createDatabase(20 db.getInstanceId().getInstance(),21 db.getDatabase(),22 ImmutableList.of(23 "CREATE TABLE Singers (\n"24 + " FirstName STRING(1024),\n"25 + " LastName STRING(1024),\n"26 + " SingerInfo BYTES(MAX)\n"27 + ") PRIMARY KEY (SingerId)",28 "CREATE TABLE Albums (\n"29 + " AlbumTitle STRING(MAX)\n"30 + ") PRIMARY KEY (SingerId, AlbumId),\n"31 + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"));32 try {33 op.get();34 } catch (ExecutionException e) {35 throw (SpannerException) e.getCause();36 } catch (InterruptedException e) {37 Thread.currentThread().interrupt
createDatabase
Using AI Code Generation
1SpannerEmulatorContainer emulator = new SpannerEmulatorContainer()2emulator.start()3SpannerEmulatorContainerTest test = new SpannerEmulatorContainerTest()4test.createDatabase(emulator)5SpannerEmulatorContainer emulator = new SpannerEmulatorContainer()6emulator.start()7SpannerEmulatorContainerTest test = new SpannerEmulatorContainerTest()8test.createDatabaseClient(emulator)9SpannerEmulatorContainer emulator = new SpannerEmulatorContainer()10emulator.start()11SpannerOptions options = SpannerOptions.newBuilder()12 .setHost(emulator.getEmulatorEndpoint())13 .setProjectId(emulator.getProjectId())14 .setCredentials(NoCredentials.getInstance())15 .build()16Spanner spanner = options.getService()17DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), emulator.getInstanceId(), emulator.getDatabaseId()))18SpannerEmulatorContainer emulator = new SpannerEmulatorContainer()19emulator.start()20SpannerEmulatorContainerTest test = new SpannerEmulatorContainerTest()21test.createDatabaseClient(emulator)22SpannerEmulatorContainer emulator = new SpannerEmulatorContainer()23emulator.start()24SpannerOptions options = SpannerOptions.newBuilder()25 .setHost(emulator.getEmulatorEndpoint())26 .setProjectId(emulator.getProjectId())27 .setCredentials(NoCredentials.getInstance())28 .build()29Spanner spanner = options.getService()30DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), emulator.getInstanceId(), emulator.getDatabaseId()))31SpannerEmulatorContainer emulator = new SpannerEmulatorContainer()32emulator.start()33SpannerOptions options = SpannerOptions.newBuilder()34 .setHost(emulator.getEmulatorEndpoint())35 .setProjectId(emulator.getProjectId())36 .setCredentials(NoCredentials.getInstance())37 .build()38Spanner spanner = options.getService()39DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), emulator.getInstanceId(), emulator.getDatabaseId()))40SpannerEmulatorContainer emulator = new SpannerEmulatorContainer()41emulator.start()
createDatabase
Using AI Code Generation
1package org.testcontainers.containers;2import com.google.cloud.spanner.DatabaseId;3import com.google.cloud.spanner.Spanner;4import com.google.cloud.spanner.SpannerException;5import com.google.cloud.spanner.SpannerOptions;6import com.google.cloud.spanner.Statement;7import com.google.cloud.spanner.TimestampBound;8import com.google.cloud.spanner.TransactionContext;9import com.google.cloud.spanner.TransactionRunner;10import com.google.cloud.spanner.TransactionRunner.TransactionCallable;11import com.google.cloud.spanner.TransactionRunner.TransactionContextCallable;12import com.google.protobuf.ByteString;13import java.util.Arrays;14import java.util.Collections;15import java.util.List;16import java.util.Optional;17import org.junit.AfterClass;18import org.junit.BeforeClass;19import org.junit.Test;20public class SpannerEmulatorContainerTest {21 private static final String PROJECT_ID = "fake-project-id";22 private static final String INSTANCE_ID = "fake-instance-id";23 private static final String DATABASE_ID = "fake-database-id";
createDatabase
Using AI Code Generation
1package org.testcontainers.containers;2import com.google.cloud.spanner.Database;3import com.google.cloud.spanner.DatabaseClient;4import com.google.cloud.spanner.DatabaseId;5import com.google.cloud.spanner.DatabaseInfo;6import com.google.cloud.spanner.Mutation;7import com.google.cloud.spanner.ResultSet;8import com.google.cloud.spanner.Spanner;9import com.google.cloud.spanner.SpannerOptions;10import com.google.cloud.spanner.Statement;11import com.google.cloud.spanner.Struct;12import com.google.cloud.spanner.TimestampBound;13import java.util.Arrays;14import java.util.List;15import org.junit.jupiter.api.AfterAll;16import org.junit.jupiter.api.BeforeAll;17import org.junit.jupiter.api.Test;18import org.testcontainers.junit.jupiter.Testcontainers;19import static org.junit.jupiter.api.Assertions.assertEquals;20import static org.junit.jupiter.api.Assertions.assertTrue;21public class SpannerEmulatorContainerTest {22 private static final String DATABASE_NAME = "my-database";23 private static final String INSTANCE_NAME = "my-instance";24 private static final String TABLE_NAME = "my-table";25 private static final String COLUMN_NAME = "my-column";26 private static final String COLUMN_VALUE = "my-value";27 private static final String COLUMN_ID = "id";28 private static final String COLUMN_NAME_VALUE = "name";29 private static final String COLUMN_EMAIL = "email";
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!