From 8a0baf114397d0bd647fefe3964f0f22ccf13701 Mon Sep 17 00:00:00 2001 From: Anthony Forloney Date: Fri, 14 Oct 2022 18:08:25 -0400 Subject: [PATCH 1/2] removing redundant test as it's covered elsewhere where it's more fitting --- .../BigQueryProjectSettingsTest.cs | 32 +------------------ 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/test/sharpbq.UnitTests/Configuration/BigQueryProjectSettingsTest.cs b/test/sharpbq.UnitTests/Configuration/BigQueryProjectSettingsTest.cs index d7cc2b7..015b260 100644 --- a/test/sharpbq.UnitTests/Configuration/BigQueryProjectSettingsTest.cs +++ b/test/sharpbq.UnitTests/Configuration/BigQueryProjectSettingsTest.cs @@ -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; @@ -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 - { - { "BigQuery:ProjectId", "99999999999" }, - { "BigQuery:Credentials", "khsgfkjhfksgskdhgdkjfhdfgkdh" } - }) - .Build(); - var services = new ServiceCollection(); - services.Configure(options => configuration.GetSection("BigQuery").Bind(options)); - var provider = services.BuildServiceProvider(); - - // Act - var result = provider.GetRequiredService>().Value; - - // Assert - result.ProjectId.ShouldBe(projectId); - result.Credentials.ShouldBe(credentials); - } } From 338d62349826039eb618187e7d882f67120bbaf8 Mon Sep 17 00:00:00 2001 From: Anthony Forloney Date: Fri, 14 Oct 2022 19:49:50 -0400 Subject: [PATCH 2/2] adjusting files to adhere to IOptions pattern to simplify the testing to ensure proper types are registered --- src/DataAccess/BigQueryClientFactory.cs | 4 +- src/DataAccess/Clients/SharpBQClient.cs | 5 ++- .../ServiceCollectionExtensionsTests.cs | 38 ++++++++++++++++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/DataAccess/BigQueryClientFactory.cs b/src/DataAccess/BigQueryClientFactory.cs index 38826d3..e68e2cc 100644 --- a/src/DataAccess/BigQueryClientFactory.cs +++ b/src/DataAccess/BigQueryClientFactory.cs @@ -6,11 +6,11 @@ namespace sharpbq.DataAccess; public class BigQueryClientFactory : IBigQueryClientFactory { - private readonly BigQueryProjectSettings _config; + private readonly IOptions _config; public BigQueryClientFactory(IOptions config) { - _config = config.Value; + _config = config; } public ISharpBQClient Create() => new SharpBQClient(_config); diff --git a/src/DataAccess/Clients/SharpBQClient.cs b/src/DataAccess/Clients/SharpBQClient.cs index f0535e0..f5edbc2 100644 --- a/src/DataAccess/Clients/SharpBQClient.cs +++ b/src/DataAccess/Clients/SharpBQClient.cs @@ -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; @@ -9,9 +10,9 @@ public class SharpBQClient : ISharpBQClient { private readonly BigQueryClient _client; - public SharpBQClient(BigQueryProjectSettings config) + public SharpBQClient(IOptions 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 parameters, diff --git a/test/sharpbq.UnitTests/Extensions/ServiceCollectionExtensionsTests.cs b/test/sharpbq.UnitTests/Extensions/ServiceCollectionExtensionsTests.cs index 3258dd6..2069cc8 100644 --- a/test/sharpbq.UnitTests/Extensions/ServiceCollectionExtensionsTests.cs +++ b/test/sharpbq.UnitTests/Extensions/ServiceCollectionExtensionsTests.cs @@ -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; @@ -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( @@ -36,6 +38,8 @@ public void AddBigQueryClient_Types_ShouldMatch() // Assert provider.GetRequiredService().ShouldBeOfType(); + provider.GetRequiredService().ShouldBeOfType(); + provider.GetRequiredService().ShouldBeOfType(); } [Fact] @@ -43,7 +47,7 @@ public void AddBigQueryClient_BigQueryProjectSettings_ShouldMatch() { // Arrange var projectId = "1"; - var credentials = "abcdef"; + var credentials = GetCredentials(); var services = new ServiceCollection(); var configuration = new ConfigurationBuilder() .AddInMemoryCollection( @@ -65,5 +69,35 @@ public void AddBigQueryClient_BigQueryProjectSettings_ShouldMatch() result.ProjectId.ShouldBe(projectId); result.Credentials.ShouldBe(credentials); } + + private string GetCredentials() + => JsonSerializer.Serialize(new Dictionary + { + { "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 { + { "environment_id", "random" }, + { "region_url", "random" }, + { "url", "random" }, + { "regional_cred_verification_url", "random" }, + { "imdsv2_session_token_url", "random" }, + { "headers", new Dictionary() }, + { "file", "random" }, + { "format", new { type = "", subject_token_field_name = "" } } + } + } + }); }