-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gunpal Jain
committed
Feb 16, 2025
1 parent
c5fcc02
commit 9eea7e3
Showing
15 changed files
with
483 additions
and
31 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
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
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
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
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
163 changes: 163 additions & 0 deletions
163
tests/GenerativeAI.Tests/Platforms/GooglAIAdapter_Initialization_Tests.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,163 @@ | ||
using System; | ||
using System.Net.Http; | ||
using GenerativeAI; | ||
using GenerativeAI.Core; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
public class GoogleAIPlatformAdapterTests | ||
{ | ||
[Fact] | ||
public void Constructor_WithNullApiKey_ShouldThrowException() | ||
{ | ||
// Arrange / Act / Assert | ||
var ex = Should.Throw<Exception>(() => | ||
{ | ||
// Passing null for the API key (and environment variable checks are skipped here) | ||
_ = new GoogleAIPlatformAdapter(null!); | ||
}); | ||
|
||
ex.Message.ShouldContain("API Key is required"); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_WithValidApiKey_ShouldInitializeCredentialsProperly() | ||
{ | ||
// Arrange | ||
const string testApiKey = "TEST_API_KEY"; | ||
|
||
// Act | ||
var adapter = new GoogleAIPlatformAdapter(testApiKey); | ||
|
||
// Assert | ||
adapter.Credentials.ShouldNotBeNull(); | ||
adapter.Credentials.ApiKey.ShouldBe(testApiKey); | ||
adapter.Credentials.AuthToken.ShouldBeNull("No access token was passed, so AuthToken should be null."); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_WithApiKeyAndAccessToken_ShouldPopulateCredentialsAndToken() | ||
{ | ||
// Arrange | ||
const string testApiKey = "TEST_API_KEY"; | ||
const string testAccessToken = "TEST_ACCESS_TOKEN"; | ||
|
||
// Act | ||
var adapter = new GoogleAIPlatformAdapter(testApiKey, accessToken: testAccessToken); | ||
|
||
// Assert | ||
adapter.Credentials.ShouldNotBeNull(); | ||
adapter.Credentials.ApiKey.ShouldBe(testApiKey); | ||
adapter.Credentials.AuthToken.ShouldNotBeNull(); | ||
adapter.Credentials.AuthToken!.AccessToken.ShouldBe(testAccessToken); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_WithCustomApiVersion_ShouldSetApiVersionProperty() | ||
{ | ||
// Arrange | ||
const string customVersion = "v2Test"; | ||
|
||
// Act | ||
var adapter = new GoogleAIPlatformAdapter("TEST_API_KEY", apiVersion: customVersion); | ||
|
||
// Assert | ||
adapter.ApiVersion.ShouldBe(customVersion); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_DefaultBaseUrl_ShouldBeSetToGoogleGenerativeAI() | ||
{ | ||
// Arrange | ||
const string testApiKey = "TEST_API_KEY"; | ||
|
||
// Act | ||
var adapter = new GoogleAIPlatformAdapter(testApiKey); | ||
|
||
// Assert | ||
adapter.BaseUrl.ShouldNotBeNullOrEmpty(); | ||
// Use whichever default value you expect it to have: | ||
// For example, "https://generativelanguage.googleapis.com" | ||
// adapter.BaseUrl.ShouldBe("https://generativelanguage.googleapis.com"); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_ValidateAccessToken_ShouldBeTrueByDefault() | ||
{ | ||
// Arrange / Act | ||
var adapter = new GoogleAIPlatformAdapter("TEST_API_KEY"); | ||
|
||
// Assert | ||
// Using reflection or a test accessor to ensure the internal property | ||
// is set to true (the code snippet shows "bool ValidateAccessToken = true;") | ||
adapter.ShouldSatisfyAllConditions( | ||
() => adapter.ShouldNotBeNull(), | ||
() => adapter.GetType().GetProperty("ValidateAccessToken", | ||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) | ||
.ShouldNotBeNull(), | ||
() => adapter.GetType().GetProperty("ValidateAccessToken", | ||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)! | ||
.GetValue(adapter) | ||
.ShouldBeOfType<bool>() | ||
.ShouldBeTrue() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_WithLogger_ShouldInitializeLogger() | ||
{ | ||
// Arrange | ||
var loggerMock = new Mock<ILogger>(); | ||
|
||
// Act | ||
var adapter = new GoogleAIPlatformAdapter("TEST_API_KEY", logger: loggerMock.Object); | ||
|
||
// Assert | ||
// Using reflection because Logger is not public: | ||
var loggerProperty = adapter.GetType().GetProperty("Logger", | ||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
loggerProperty.ShouldNotBeNull("Logger property should exist."); | ||
|
||
var actualLogger = loggerProperty!.GetValue(adapter) as ILogger; | ||
actualLogger.ShouldNotBeNull(); | ||
actualLogger.ShouldBe(loggerMock.Object); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_WithAuthenticator_ShouldSetAuthenticator() | ||
{ | ||
// Arrange | ||
var mockAuthenticator = new Mock<IGoogleAuthenticator>(); | ||
|
||
// Act | ||
var adapter = new GoogleAIPlatformAdapter("TEST_API_KEY", authenticator: mockAuthenticator.Object); | ||
|
||
// Assert | ||
var authenticatorProperty = adapter.GetType().GetProperty("Authenticator", | ||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
|
||
authenticatorProperty.ShouldNotBeNull("Authenticator property should exist."); | ||
var actualAuthenticator = authenticatorProperty!.GetValue(adapter) as IGoogleAuthenticator; | ||
actualAuthenticator.ShouldBe(mockAuthenticator.Object); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_WithCustomValidateAccessToken_ShouldSetProperly() | ||
{ | ||
// Arrange | ||
const bool testFlag = false; | ||
|
||
// Act | ||
var adapter = new GoogleAIPlatformAdapter("TEST_API_KEY", validateAccessToken: testFlag); | ||
|
||
// Assert | ||
var validateAccessTokenProperty = adapter.GetType().GetProperty("ValidateAccessToken", | ||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
|
||
validateAccessTokenProperty.ShouldNotBeNull("ValidateAccessToken property should exist."); | ||
var actualValue = (bool)validateAccessTokenProperty!.GetValue(adapter)!; | ||
actualValue.ShouldBeFalse(); | ||
} | ||
} |
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
Oops, something went wrong.