forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigQueryContainerTest.cs
More file actions
79 lines (63 loc) · 3.15 KB
/
BigQueryContainerTest.cs
File metadata and controls
79 lines (63 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
namespace Testcontainers.BigQuery;
public sealed class BigQueryContainerTest : IAsyncLifetime
{
private readonly BigQueryContainer _bigQueryContainer = new BigQueryBuilder().Build();
public async ValueTask InitializeAsync()
{
await _bigQueryContainer.StartAsync()
.ConfigureAwait(false);
}
public ValueTask DisposeAsync()
{
return _bigQueryContainer.DisposeAsync();
}
[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(TestContext.Current.CancellationToken)
.ConfigureAwait(true);
var dataset = await bigQueryClient.GetOrCreateDatasetAsync("mydata", cancellationToken: TestContext.Current.CancellationToken)
.ConfigureAwait(true);
// When
var table = await dataset.CreateTableAsync("scores", tableSchema, cancellationToken: TestContext.Current.CancellationToken)
.ConfigureAwait(true);
_ = await table.InsertRowAsync(expectedRow, cancellationToken: TestContext.Current.CancellationToken)
.ConfigureAwait(true);
var results = await bigQueryClient.ExecuteQueryAsync($"SELECT * FROM {table}", null, cancellationToken: TestContext.Current.CancellationToken)
.ConfigureAwait(true);
// 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);
}
}
}