Skip to content

Commit

Permalink
Add BigQueryEmulatorContainer (#7324)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Jul 24, 2023
1 parent bd70f5b commit 8465795
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
11 changes: 10 additions & 1 deletion docs/modules/gcloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

Testcontainers module for the Google Cloud Platform's [Cloud SDK](https://cloud.google.com/sdk/).

Currently, the module supports `Bigtable`, `Datastore`, `Firestore`, `Spanner`, and `Pub/Sub` emulators. In order to use it, you should use the following classes:
Currently, the module supports `BigQuery`, `Bigtable`, `Datastore`, `Firestore`, `Spanner`, and `Pub/Sub` emulators. In order to use it, you should use the following classes:

Class | Container Image
-|-
BigQueryEmulatorContainer | ["ghcr.io/goccy/bigquery-emulator"]("ghcr.io/goccy/bigquery-emulator")
BigtableEmulatorContainer | [gcr.io/google.com/cloudsdktool/cloud-sdk:emulators](https://gcr.io/google.com/cloudsdktool/cloud-sdk)
DatastoreEmulatorContainer | [gcr.io/google.com/cloudsdktool/cloud-sdk:emulators](https://gcr.io/google.com/cloudsdktool/cloud-sdk)
FirestoreEmulatorContainer | [gcr.io/google.com/cloudsdktool/cloud-sdk:emulators](https://gcr.io/google.com/cloudsdktool/cloud-sdk)
Expand All @@ -17,6 +18,14 @@ PubSubEmulatorContainer | [gcr.io/google.com/cloudsdktool/cloud-sdk:emulators](h

## Usage example

### BigQuery

Start BigQuery Emulator during a test:

<!--codeinclude-->
[Starting a BigQuery Emulator container](../../modules/gcloud/src/test/java/org/testcontainers/containers/BigQueryEmulatorContainerTest.java) inside_block:emulatorContainer
<!--/codeinclude-->

### Bigtable

Start Bigtable Emulator during a test:
Expand Down
1 change: 1 addition & 0 deletions modules/gcloud/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
api project(':testcontainers')

testImplementation platform("com.google.cloud:libraries-bom:26.18.0")
testImplementation 'com.google.cloud:google-cloud-bigquery'
testImplementation 'com.google.cloud:google-cloud-datastore'
testImplementation 'com.google.cloud:google-cloud-firestore'
testImplementation 'com.google.cloud:google-cloud-pubsub'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.testcontainers.containers;

import org.testcontainers.utility.DockerImageName;

/**
* Testcontainers implementation for BigQuery.
*/
public class BigQueryEmulatorContainer extends GenericContainer<BigQueryEmulatorContainer> {

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("ghcr.io/goccy/bigquery-emulator");

private static final int HTTP_PORT = 9050;

private static final int GRPC_PORT = 9060;

private static final String PROJECT_ID = "test-project";

public BigQueryEmulatorContainer(String image) {
this(DockerImageName.parse(image));
}

public BigQueryEmulatorContainer(DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
addExposedPorts(HTTP_PORT, GRPC_PORT);
withCommand("--project", PROJECT_ID);
}

public String getEmulatorHttpEndpoint() {
return String.format("http://%s:%d", getHost(), getMappedPort(HTTP_PORT));
}

public String getProjectId() {
return PROJECT_ID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.testcontainers.containers;

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

public class BigQueryEmulatorContainerTest {

@Test
public void test() throws Exception {
try (
// emulatorContainer {
BigQueryEmulatorContainer container = new BigQueryEmulatorContainer("ghcr.io/goccy/bigquery-emulator:0.4.3")
// }
) {
container.start();

String url = container.getEmulatorHttpEndpoint();
BigQueryOptions options = BigQueryOptions
.newBuilder()
.setProjectId(container.getProjectId())
.setHost(url)
.setLocation(url)
.build();
BigQuery bigQuery = options.getService();

String fn =
"CREATE FUNCTION testr(arr ARRAY<STRUCT<name STRING, val INT64>>) AS ((SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem))";

bigQuery.query(QueryJobConfiguration.newBuilder(fn).build());

String sql =
"SELECT testr([STRUCT<name STRING, val INT64>(\"foo\", 10), STRUCT<name STRING, val INT64>(\"bar\", 40), STRUCT<name STRING, val INT64>(\"foo\", 20)])";
TableResult result = bigQuery.query(QueryJobConfiguration.newBuilder(sql).build());
List<BigDecimal> values = result
.streamValues()
.map(fieldValues -> fieldValues.get(0).getNumericValue())
.collect(Collectors.toList());
assertThat(values).containsOnly(BigDecimal.valueOf(30));
}
}
}

0 comments on commit 8465795

Please sign in to comment.