-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BigQueryEmulatorContainer (#7324)
- Loading branch information
1 parent
bd70f5b
commit 8465795
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
modules/gcloud/src/main/java/org/testcontainers/containers/BigQueryEmulatorContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...les/gcloud/src/test/java/org/testcontainers/containers/BigQueryEmulatorContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |