Skip to content

Commit

Permalink
Merge pull request #32 from bb-io/develop
Browse files Browse the repository at this point in the history
Added unit tests
  • Loading branch information
RiabushenkoA authored Feb 18, 2025
2 parents 1cf4031 + 59b2c7f commit 242b299
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,4 @@ healthchecksdb
*.env

/Apps.GoogleSheets/ApplicationConstants.cs
/Tests.GoogleSheets/appsettings.json
6 changes: 6 additions & 0 deletions Apps.GoogleSheets.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apps.GoogleSheets", "Apps.GoogleSheets\Apps.GoogleSheets.csproj", "{275D9CE1-A4E8-41E8-A01B-404B1A2D0AD2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.GoogleSheets", "Tests.GoogleSheets\Tests.GoogleSheets.csproj", "{B4C40A72-1266-484B-9B9D-A83E6D005044}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{275D9CE1-A4E8-41E8-A01B-404B1A2D0AD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{275D9CE1-A4E8-41E8-A01B-404B1A2D0AD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{275D9CE1-A4E8-41E8-A01B-404B1A2D0AD2}.Release|Any CPU.Build.0 = Release|Any CPU
{B4C40A72-1266-484B-9B9D-A83E6D005044}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4C40A72-1266-484B-9B9D-A83E6D005044}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4C40A72-1266-484B-9B9D-A83E6D005044}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4C40A72-1266-484B-9B9D-A83E6D005044}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 11 additions & 0 deletions Apps.GoogleSheets/Actions/SpreadsheetActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Blackbird.Applications.Sdk.Glossaries.Utils.Converters;
using Blackbird.Applications.Sdk.Glossaries.Utils.Dtos;
using Blackbird.Applications.Sdk.Common.Exceptions;
using Blackbird.Applications.Sdk.Common.Authentication;

namespace Apps.GoogleSheets.Actions
{
Expand Down Expand Up @@ -60,9 +61,19 @@ public async Task<CellDto> UpdateCell(
updateRequest.ValueInputOption = UpdateRequest.ValueInputOptionEnum.USERENTERED;
updateRequest.IncludeValuesInResponse = true;
var result = await ErrorHandler.ExecuteWithErrorHandlingAsync(async () => await updateRequest.ExecuteAsync());
if (result?.UpdatedData?.Values == null || result.UpdatedData.Values.Count == 0 || result.UpdatedData.Values[0].Count == 0)
{
throw new PluginApplicationException("No updated data was returned from the API. Please check your input and try again");
}
return new CellDto { Value = result?.UpdatedData.Values[0][0].ToString() };
}

[Action("DEBUG: Get auth data", Description = "Can be used only for debugging purposes.")]
public List<AuthenticationCredentialsProvider> GetAuthenticationCredentialsProviders()
{
return InvocationContext.AuthenticationCredentialsProviders.ToList();
}

[Action("Get sheet row", Description = "Get sheet row by address")]
public async Task<RowDto> GetRow(
[ActionParameter] SpreadsheetFileRequest spreadsheetFileRequest,
Expand Down
2 changes: 1 addition & 1 deletion Apps.GoogleSheets/Apps.GoogleSheets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<Product>Google Sheets</Product>
<Description>Create and edit online spreadsheets. Get insights together with secure sharing in real-time and from any device</Description>
<Version>1.4.4</Version>
<Version>1.4.5</Version>
<AssemblyName>Apps.GoogleSheets</AssemblyName>
</PropertyGroup>
<ItemGroup>
Expand Down
28 changes: 28 additions & 0 deletions Tests.GoogleSheets/Base/FileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Blackbird.Applications.Sdk.Common.Files;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;

namespace Tests.GoogleSheets.Base;

public class FileManager(string folderLocation) : IFileManagementClient
{
public Task<Stream> DownloadAsync(FileReference reference)
{
var path = Path.Combine(folderLocation, @$"Input\{reference.Name}");
var bytes = File.ReadAllBytes(path);

var stream = new MemoryStream(bytes);
return Task.FromResult((Stream)stream);
}

public Task<FileReference> UploadAsync(Stream stream, string contentType, string fileName)
{
var path = Path.Combine(folderLocation, @$"Output\{fileName}");
new FileInfo(path).Directory!.Create();
using (var fileStream = File.Create(path))
{
stream.CopyTo(fileStream);
}

return Task.FromResult(new FileReference { Name = fileName });
}
}
29 changes: 29 additions & 0 deletions Tests.GoogleSheets/Base/TestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Blackbird.Applications.Sdk.Common.Authentication;
using Blackbird.Applications.Sdk.Common.Invocation;
using Microsoft.Extensions.Configuration;

namespace Tests.GoogleSheets.Base;

public class TestBase
{
protected TestBase()
{
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
Creds = config.GetSection("ConnectionDefinition").GetChildren()
.Select(x => new AuthenticationCredentialsProvider(x.Key, x.Value!)).ToList();
var folderLocation = config.GetSection("TestFolder").Value!;

InvocationContext = new InvocationContext
{
AuthenticationCredentialsProviders = Creds,
};

FileManager = new FileManager(folderLocation);
}

protected IEnumerable<AuthenticationCredentialsProvider> Creds { get; set; }

public InvocationContext InvocationContext { get; set; }

public FileManager FileManager { get; set; }
}
15 changes: 15 additions & 0 deletions Tests.GoogleSheets/SpreadsheetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Tests.GoogleSheets.Base;

namespace Tests.CrowGoogleSheetsdin
{
[TestClass]
public class SpreadsheetTests : TestBase
{
[TestMethod]
public async Task Getrange_ReturnsSuccess()
{

}

}
}
40 changes: 40 additions & 0 deletions Tests.GoogleSheets/Tests.GoogleSheets.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="8.0.0-rc.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Apps.GoogleSheets\Apps.GoogleSheets.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Output\" />
<Folder Include="Output\" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions Tests.GoogleSheets/ValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Apps.GoogleSheets.Connections;
using Blackbird.Applications.Sdk.Common.Authentication;
using FluentAssertions;
using Tests.GoogleSheets.Base;

namespace Tests.GoogleSheets;

[TestClass]
public class ValidatorTests : TestBase
{
[TestMethod]
public async Task ValidateConnection_ValidEnterpriseConnection_ShouldBeSuccessful()
{
var validator = new ConnectionValidator();

var result = await validator.ValidateConnection(Creds, CancellationToken.None);
Console.WriteLine(result.Message);
result.IsValid.Should().Be(true);
}

[TestMethod]
public async Task ValidateConnection_InvalidConnection_ShouldFail()
{
var validator = new ConnectionValidator();

var newCredentials = Creds.Select(x => new AuthenticationCredentialsProvider(x.KeyName, x.Value + "_incorrect"));
var result = await validator.ValidateConnection(newCredentials, CancellationToken.None);
result.IsValid.Should().Be(false);
}
}
8 changes: 8 additions & 0 deletions Tests.GoogleSheets/appsettings.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ConnectionDefinition": {
"access_token": "",
"crowdin_plan": "",
"domain": ""
},
"TestFolder": "C:\\Users\\..."
}

0 comments on commit 242b299

Please sign in to comment.