-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com>
- Loading branch information
1 parent
ab2c7c9
commit 33ece8e
Showing
11 changed files
with
298 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
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,79 @@ | ||
namespace Testcontainers.BigQuery; | ||
|
||
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
[PublicAPI] | ||
public sealed class BigQueryBuilder : ContainerBuilder<BigQueryBuilder, BigQueryContainer, BigQueryConfiguration> | ||
{ | ||
public const string BigQueryImage = "ghcr.io/goccy/bigquery-emulator:0.4"; | ||
|
||
public const ushort BigQueryPort = 9050; | ||
|
||
public const string DefaultProjectId = "default"; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BigQueryBuilder" /> class. | ||
/// </summary> | ||
public BigQueryBuilder() | ||
: this(new BigQueryConfiguration()) | ||
{ | ||
DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BigQueryBuilder" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
private BigQueryBuilder(BigQueryConfiguration resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
DockerResourceConfiguration = resourceConfiguration; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override BigQueryConfiguration DockerResourceConfiguration { get; } | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="projectId"></param> | ||
/// <returns></returns> | ||
public BigQueryBuilder WithProject(string projectId) | ||
{ | ||
return WithCommand("--project", projectId); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override BigQueryContainer Build() | ||
{ | ||
Validate(); | ||
return new BigQueryContainer(DockerResourceConfiguration, TestcontainersSettings.Logger); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override BigQueryBuilder Init() | ||
{ | ||
return base.Init() | ||
.WithImage(BigQueryImage) | ||
.WithPortBinding(BigQueryPort, true) | ||
.WithProject(DefaultProjectId) | ||
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("(?s).*listening.*$")); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override BigQueryBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new BigQueryConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override BigQueryBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new BigQueryConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override BigQueryBuilder Merge(BigQueryConfiguration oldValue, BigQueryConfiguration newValue) | ||
{ | ||
return new BigQueryBuilder(new BigQueryConfiguration(oldValue, newValue)); | ||
} | ||
} |
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,53 @@ | ||
namespace Testcontainers.BigQuery; | ||
|
||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class BigQueryConfiguration : ContainerConfiguration | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BigQueryConfiguration" /> class. | ||
/// </summary> | ||
public BigQueryConfiguration() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BigQueryConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public BigQueryConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BigQueryConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public BigQueryConfiguration(IContainerConfiguration resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BigQueryConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public BigQueryConfiguration(BigQueryConfiguration resourceConfiguration) | ||
: this(new BigQueryConfiguration(), resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BigQueryConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="oldValue">The old Docker resource configuration.</param> | ||
/// <param name="newValue">The new Docker resource configuration.</param> | ||
public BigQueryConfiguration(BigQueryConfiguration oldValue, BigQueryConfiguration newValue) | ||
: base(oldValue, newValue) | ||
{ | ||
} | ||
} |
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,25 @@ | ||
namespace Testcontainers.BigQuery; | ||
|
||
/// <inheritdoc cref="DockerContainer" /> | ||
[PublicAPI] | ||
public sealed class BigQueryContainer : DockerContainer | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BigQueryContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
public BigQueryContainer(BigQueryConfiguration configuration, ILogger logger) | ||
: base(configuration, logger) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the BigQuery emulator endpoint. | ||
/// </summary> | ||
/// <returns>The BigQuery emulator endpoint.</returns> | ||
public string GetEmulatorEndpoint() | ||
{ | ||
return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(BigQueryBuilder.BigQueryPort)).ToString(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Testcontainers.BigQuery/Testcontainers.BigQuery.csproj
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/> | ||
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj"/> | ||
</ItemGroup> | ||
</Project> |
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,7 @@ | ||
global using System; | ||
global using Docker.DotNet.Models; | ||
global using DotNet.Testcontainers.Builders; | ||
global using DotNet.Testcontainers.Configurations; | ||
global using DotNet.Testcontainers.Containers; | ||
global using JetBrains.Annotations; | ||
global using Microsoft.Extensions.Logging; |
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 @@ | ||
root = true |
78 changes: 78 additions & 0 deletions
78
tests/Testcontainers.BigQuery.Tests/BigQueryContainerTest.cs
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,78 @@ | ||
namespace Testcontainers.BigQuery; | ||
|
||
public sealed class BigQueryContainerTest : IAsyncLifetime | ||
{ | ||
private readonly BigQueryContainer _bigQueryContainer = new BigQueryBuilder().Build(); | ||
|
||
public Task InitializeAsync() | ||
{ | ||
return _bigQueryContainer.StartAsync(); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return _bigQueryContainer.DisposeAsync().AsTask(); | ||
} | ||
|
||
[Fact] | ||
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] | ||
public async Task ExecuteQueryReturnsInsertRow() | ||
{ | ||
// Given | ||
var utcNow = DateTime.UtcNow; | ||
|
||
// Storing DateTime.UtcNow in BigQuery loses precision. The query result differs | ||
// in the last digit. Truncating milliseconds prevents running into this issue. | ||
var utcNowWithoutMilliseconds = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, utcNow.Hour, utcNow.Minute, utcNow.Second, DateTimeKind.Utc); | ||
|
||
var bigQueryClientBuilder = new BigQueryClientBuilder(); | ||
bigQueryClientBuilder.BaseUri = _bigQueryContainer.GetEmulatorEndpoint(); | ||
bigQueryClientBuilder.ProjectId = BigQueryBuilder.DefaultProjectId; | ||
bigQueryClientBuilder.Credential = new Credential(); | ||
|
||
var tableSchemaBuilder = new TableSchemaBuilder(); | ||
tableSchemaBuilder.Add("player", BigQueryDbType.String); | ||
tableSchemaBuilder.Add("gameStarted", BigQueryDbType.DateTime); | ||
tableSchemaBuilder.Add("score", BigQueryDbType.Int64); | ||
var tableSchema = tableSchemaBuilder.Build(); | ||
|
||
var expectedRow = new BigQueryInsertRow(); | ||
expectedRow.Add("player", "Bob"); | ||
expectedRow.Add("gameStarted", utcNowWithoutMilliseconds); | ||
expectedRow.Add("score", 85L); | ||
|
||
using var bigQueryClient = await bigQueryClientBuilder.BuildAsync() | ||
.ConfigureAwait(false); | ||
|
||
var dataset = await bigQueryClient.GetOrCreateDatasetAsync("mydata") | ||
.ConfigureAwait(false); | ||
|
||
// When | ||
var table = await dataset.CreateTableAsync("scores", tableSchema) | ||
.ConfigureAwait(false); | ||
|
||
_ = await table.InsertRowAsync(expectedRow) | ||
.ConfigureAwait(false); | ||
|
||
var results = await bigQueryClient.ExecuteQueryAsync($"SELECT * FROM {table}", null) | ||
.ConfigureAwait(false); | ||
|
||
// Then | ||
Assert.Single(results); | ||
Assert.Equal(expectedRow["player"], results.Single()["player"]); | ||
Assert.Equal(expectedRow["gameStarted"], results.Single()["gameStarted"]); | ||
Assert.Equal(expectedRow["score"], results.Single()["score"]); | ||
} | ||
|
||
private sealed class Credential : ICredential | ||
{ | ||
public void Initialize(ConfigurableHttpClient httpClient) | ||
{ | ||
} | ||
|
||
public Task<string> GetAccessTokenForRequestAsync(string authUri, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(string.Empty); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/Testcontainers.BigQuery.Tests/Testcontainers.BigQuery.Tests.csproj
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net6.0</TargetFrameworks> | ||
<IsPackable>false</IsPackable> | ||
<IsPublishable>false</IsPublishable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2"/> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0"/> | ||
<PackageReference Include="xunit" Version="2.5.0"/> | ||
<PackageReference Include="Google.Cloud.BigQuery.V2" Version="3.4.0"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers.BigQuery/Testcontainers.BigQuery.csproj"/> | ||
<ProjectReference Include="$(SolutionDir)tests/Testcontainers.Commons/Testcontainers.Commons.csproj"/> | ||
</ItemGroup> | ||
</Project> |
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,9 @@ | ||
global using System; | ||
global using System.Linq; | ||
global using System.Threading.Tasks; | ||
global using DotNet.Testcontainers.Commons; | ||
global using Google.Cloud.BigQuery.V2; | ||
global using Xunit; | ||
global using System.Threading; | ||
global using Google.Apis.Auth.OAuth2; | ||
global using Google.Apis.Http; |