Skip to content

Commit

Permalink
Merge pull request #53 from wayfair-incubator/more_tests
Browse files Browse the repository at this point in the history
More tests surrounding types registered through ServiceCollectionExtentions
  • Loading branch information
aforloney authored Oct 14, 2022
2 parents 1f6cf90 + 612c924 commit 5d401db
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/DataAccess/BigQueryClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace sharpbq.DataAccess;

public class BigQueryClientFactory : IBigQueryClientFactory
{
private readonly BigQueryProjectSettings _config;
private readonly IOptions<BigQueryProjectSettings> _config;

public BigQueryClientFactory(IOptions<BigQueryProjectSettings> config)
{
_config = config.Value;
_config = config;
}

public ISharpBQClient Create() => new SharpBQClient(_config);
Expand Down
5 changes: 3 additions & 2 deletions src/DataAccess/Clients/SharpBQClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Google.Apis.Auth.OAuth2;
using Google.Apis.Bigquery.v2.Data;
using Google.Cloud.BigQuery.V2;
using Microsoft.Extensions.Options;
using sharpbq.Configuration;

namespace sharpbq.DataAccess.Clients;
Expand All @@ -9,9 +10,9 @@ public class SharpBQClient : ISharpBQClient
{
private readonly BigQueryClient _client;

public SharpBQClient(BigQueryProjectSettings config)
public SharpBQClient(IOptions<BigQueryProjectSettings> config)
{
_client = BigQueryClient.Create(config.ProjectId, GoogleCredential.FromJson(config.Credentials));
_client = BigQueryClient.Create(config.Value.ProjectId, GoogleCredential.FromJson(config.Value.Credentials));
}

public BigQueryResults ExecuteQuery(string sql, IEnumerable<BigQueryParameter> parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using sharpbq.Configuration;
using sharpbq.Configuration;
using Shouldly;
using System.Collections.Generic;
using Xunit;

namespace sharpbq.UnitTests.Configuration;
Expand All @@ -22,31 +18,5 @@ public void DefaultSettings_ShouldMatch()
Subject.ProjectId.ShouldBeNull();
Subject.Credentials.ShouldBeNull();
}

[Fact]
public void IOptions_ConfigSettings_ShouldMatch()
{
// Arrange
var projectId = "99999999999";
var credentials = "khsgfkjhfksgskdhgdkjfhdfgkdh";
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(
new Dictionary<string, string>
{
{ "BigQuery:ProjectId", "99999999999" },
{ "BigQuery:Credentials", "khsgfkjhfksgskdhgdkjfhdfgkdh" }
})
.Build();
var services = new ServiceCollection();
services.Configure<BigQueryProjectSettings>(options => configuration.GetSection("BigQuery").Bind(options));
var provider = services.BuildServiceProvider();

// Act
var result = provider.GetRequiredService<IOptions<BigQueryProjectSettings>>().Value;

// Assert
result.ProjectId.ShouldBe(projectId);
result.Credentials.ShouldBe(credentials);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using Microsoft.Extensions.Options;
using sharpbq.Configuration;
using sharpbq.DataAccess;
using sharpbq.DataAccess.Clients;
using sharpbq.Extensions;
using Shouldly;
using System.Collections.Generic;
using System.Text.Json;
using Xunit;

namespace sharpbq.UnitTests.Extensions;
Expand All @@ -17,7 +19,7 @@ public void AddBigQueryClient_Types_ShouldMatch()
{
// Arrange
var projectId = "1";
var credentials = "abcdef";
var credentials = GetCredentials();
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(
Expand All @@ -36,14 +38,16 @@ public void AddBigQueryClient_Types_ShouldMatch()

// Assert
provider.GetRequiredService<IBigQueryClientFactory>().ShouldBeOfType<BigQueryClientFactory>();
provider.GetRequiredService<IDataStoreBase>().ShouldBeOfType<DataStoreBase>();
provider.GetRequiredService<ISharpBQClient>().ShouldBeOfType<SharpBQClient>();
}

[Fact]
public void AddBigQueryClient_BigQueryProjectSettings_ShouldMatch()
{
// Arrange
var projectId = "1";
var credentials = "abcdef";
var credentials = GetCredentials();
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(
Expand All @@ -65,5 +69,35 @@ public void AddBigQueryClient_BigQueryProjectSettings_ShouldMatch()
result.ProjectId.ShouldBe(projectId);
result.Credentials.ShouldBe(credentials);
}

private string GetCredentials()
=> JsonSerializer.Serialize(new Dictionary<string, object>
{
{ "type", "authorized_user" },
{ "project_id", "random" },
{ "quota_project_id", "random" },
{ "client_id", "random" },
{ "client_secret", "random" },
{ "client_email", "random" },
{ "private_key", "random" },
{ "private_key_id", "random" },
{ "refresh_token", "random" },
{ "audience", "random" },
{ "subject_token_type", "random" },
{ "token_url", "random" },
{ "service_account_impersonation_url", "random" },
{ "workforce_pool_user_project", "random" },
{ "credential_source", new Dictionary<string, object> {
{ "environment_id", "random" },
{ "region_url", "random" },
{ "url", "random" },
{ "regional_cred_verification_url", "random" },
{ "imdsv2_session_token_url", "random" },
{ "headers", new Dictionary<string, string>() },
{ "file", "random" },
{ "format", new { type = "", subject_token_field_name = "" } }
}
}
});
}

0 comments on commit 5d401db

Please sign in to comment.