Skip to content

Commit

Permalink
Merge pull request #48 from PinguApps/35-feat-add-additional-tests-to…
Browse files Browse the repository at this point in the history
…-cover-shared-lib-and-increase-code-coverage

Added fuller test coverage
  • Loading branch information
pingu2k4 authored Jul 7, 2024
2 parents d5e6558 + a4bb320 commit a229c62
Show file tree
Hide file tree
Showing 36 changed files with 1,094 additions and 187 deletions.
1 change: 1 addition & 0 deletions .github/workflows/_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
tag: "${{ github.run_number }}_${{ github.run_id }}"
customSettings: ""
toolpath: "reportgeneratortool"
assemblyfilters: "-PinguApps.Appwrite.Shared.Tests"

- name: Upload Combined Coverage XML
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,4 @@ FodyWeavers.xsd
*.sln.iml

**/*appsettings*.json
**/coverage-report/**
22 changes: 22 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project>
<Target Name="AddInternalsVisibleTo" BeforeTargets="BeforeCompile">
<!-- Add default suffix if there is no InternalsVisibleTo or InternalsVisibleToSuffix defined -->
<ItemGroup Condition="@(InternalsVisibleToSuffix->Count()) == 0 AND @(InternalsVisibleTo->Count()) == 0">
<InternalsVisibleToSuffix Include=".Tests" />
</ItemGroup>

<!-- Handle InternalsVisibleTo -->
<ItemGroup Condition="'@(InternalsVisibleTo->Count())' &gt; 0">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>%(InternalsVisibleTo.Identity)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<!-- Handle InternalsVisibleToSuffix -->
<ItemGroup Condition="@(InternalsVisibleToSuffix->Count()) &gt; 0">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName)%(InternalsVisibleToSuffix.Identity)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Target>
</Project>
2 changes: 1 addition & 1 deletion PinguApps.Appwrite.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PinguApps.Appwrite.Server.T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PinguApps.Appwrite.Client.Tests", "tests\PinguApps.Appwrite.Client.Tests\PinguApps.Appwrite.Client.Tests.csproj", "{71C1431E-8098-4B9B-9CFA-A49F77242C79}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PinguApps.Appwrite.Tests.Shared", "tests\PinguApps.Appwrite.Tests.Shared\PinguApps.Appwrite.Tests.Shared.csproj", "{5AED345C-1E3E-4397-807C-8EFC22B786FA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PinguApps.Appwrite.Shared.Tests", "tests\PinguApps.Appwrite.Shared.Tests\PinguApps.Appwrite.Shared.Tests.csproj", "{5AED345C-1E3E-4397-807C-8EFC22B786FA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
2 changes: 1 addition & 1 deletion src/PinguApps.Appwrite.Client/Utils/ResponseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static AppwriteResult<T> GetApiResponse<T>(this IApiResponse<T> result)
return new AppwriteResult<T>(result.Content);
}

if (result.Error?.Content is null)
if (result.Error?.Content is null || string.IsNullOrEmpty(result.Error.Content))
{
throw new Exception("Unknown error encountered.");
}
Expand Down
9 changes: 9 additions & 0 deletions src/PinguApps.Appwrite.Common/Responses/HashOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace PinguApps.Appwrite.Shared.Responses;
public record HashOptions(
[property: JsonPropertyName("type")] string Type,
[property: JsonPropertyName("memoryCost")] long MemoryCost,
[property: JsonPropertyName("timeCost")] int TimeCost,
[property: JsonPropertyName("threads")] int Threads
);
3 changes: 3 additions & 0 deletions src/PinguApps.Appwrite.Common/Responses/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public record User(
[property: JsonPropertyName("$createdAt")] DateTime CreatedAt,
[property: JsonPropertyName("$updatedAt")] DateTime UpdatedAt,
[property: JsonPropertyName("name")] string Name,
[property: JsonPropertyName("password")] string? Password,
[property: JsonPropertyName("hash")] string? Hash,
[property: JsonPropertyName("hashOptions")] HashOptions? HashOptions,
[property: JsonPropertyName("registration")] DateTime Registration,
[property: JsonPropertyName("status")] bool Status,
[property: JsonPropertyName("labels")] IReadOnlyList<string> Labels,
Expand Down
2 changes: 1 addition & 1 deletion src/PinguApps.Appwrite.Server/Utils/ResponseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static AppwriteResult<T> GetApiResponse<T>(this IApiResponse<T> result)
return new AppwriteResult<T>(result.Content);
}

if (result.Error?.Content is null)
if (result.Error?.Content is null || string.IsNullOrEmpty(result.Error.Content))
{
throw new Exception("Unknown error encountered.");
}
Expand Down
135 changes: 0 additions & 135 deletions tests/PinguApps.Appwrite.Client.Tests/AccountTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
public partial class AccountClientTests
{
[Fact]
public async Task Create_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new CreateAccountRequest()
{
Email = "[email protected]",
Password = "password",
Name = "name"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.UserResponse);

// Act
var result = await _appwriteClient.Account.Create(request);

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task Create_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new CreateAccountRequest()
{
Email = "[email protected]",
Password = "password",
Name = "name"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

// Act
var result = await _appwriteClient.Account.Create(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task Create_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new CreateAccountRequest()
{
Email = "[email protected]",
Password = "password",
Name = "name"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account")
.ExpectedHeaders()
.WithJsonContent(request)
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Account.Create(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Net;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
public partial class AccountClientTests
{
[Fact]
public async Task Get_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account")
.ExpectedHeaders(true)
.Respond(Constants.AppJson, Constants.UserResponse);

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.Get();

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task Get_ShouldHandleException_WhenApiCallFails()
{
// Arrange
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account")
.ExpectedHeaders(true)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.Get();

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task Get_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account")
.ExpectedHeaders(true)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.Get();

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
Loading

0 comments on commit a229c62

Please sign in to comment.