Skip to content

Commit

Permalink
Rejigged tests and added tests for create teams
Browse files Browse the repository at this point in the history
  • Loading branch information
pingu2k4 committed Oct 18, 2024
1 parent d65376a commit fd832c7
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AccountClientTests()
_mockHttp = new MockHttpMessageHandler();
var services = new ServiceCollection();

services.AddAppwriteClientForServer("PROJECT_ID", TestConstants.Endpoint, new RefitSettings
services.AddAppwriteClientForServer(TestConstants.ProjectId, TestConstants.Endpoint, new RefitSettings
{
HttpMessageHandlerFactory = () => _mockHttp
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void SetSession_UpdatesSession()
var mockAccountClient = new Mock<IAccountClient>();
var mockTeamsClient = new Mock<ITeamsClient>();
mockAccountClient.As<ISessionAware>();
mockTeamsClient.As<ISessionAware>();
var appwriteClient = new AppwriteClient(mockAccountClient.Object, mockTeamsClient.Object);

// Act
Expand All @@ -58,6 +59,7 @@ public void SetSession_UpdatesSessionInAccountClient()
var mockAccountClient = new Mock<IAccountClient>();
var mockTeamsClient = new Mock<ITeamsClient>();
mockAccountClient.As<ISessionAware>();
mockTeamsClient.As<ISessionAware>();
var appwriteClient = new AppwriteClient(mockAccountClient.Object, mockTeamsClient.Object);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
namespace PinguApps.Appwrite.Client.Tests.Clients;

public static class AccountTestsExtensions
public static class ClientTestsExtensions
{
public static MockedRequest ExpectedHeaders(this MockedRequest request, bool addSessionHeaders = false)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Net;
using PinguApps.Appwrite.Client.Clients;
using PinguApps.Appwrite.Shared.Requests.Teams;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Teams;
public partial class TeamsClientTests
{
[Fact]
public async Task ListTeams_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new ListTeamsRequest();

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams")
.ExpectedHeaders(true)
.Respond(TestConstants.AppJson, TestConstants.TeamsListResponse);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Teams.ListTeams(request);

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

[Fact]
public async Task ListTeams_ShouldReturnError_WhenSessionIsNull()
{
// Arrange
var request = new ListTeamsRequest();

// Act
var result = await _appwriteClient.Teams.ListTeams(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsInternalError);
Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message);
}

[Fact]
public async Task ListTeams_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new ListTeamsRequest();

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams")
.ExpectedHeaders(true)
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Teams.ListTeams(request);

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

[Fact]
public async Task ListTeams_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new ListTeamsRequest();

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams")
.ExpectedHeaders(true)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Teams.ListTeams(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,38 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Shared.Converters;
using PinguApps.Appwrite.Shared.Tests;
using Refit;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Teams;
public partial class TeamsClientTests
{
private readonly MockHttpMessageHandler _mockHttp;
private readonly IAppwriteClient _appwriteClient;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public TeamsClientTests()
{
_mockHttp = new MockHttpMessageHandler();
var services = new ServiceCollection();

services.AddAppwriteClientForServer(TestConstants.ProjectId, TestConstants.Endpoint, new RefitSettings
{
HttpMessageHandlerFactory = () => _mockHttp
});

var serviceProvider = services.BuildServiceProvider();

_appwriteClient = serviceProvider.GetRequiredService<IAppwriteClient>();

_jsonSerializerOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

_jsonSerializerOptions.Converters.Add(new IgnoreSdkExcludedPropertiesConverterFactory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests.Teams;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Clients.Teams;
public partial class TeamsClientTests
{
[Fact]
public async Task ListTeams_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new ListTeamsRequest();

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams")
.ExpectedHeaders()
.Respond(TestConstants.AppJson, TestConstants.TeamsListResponse);

// Act
var result = await _appwriteClient.Teams.ListTeams(request);

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

[Fact]
public async Task ListTeams_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new ListTeamsRequest();

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams")
.ExpectedHeaders()
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

// Act
var result = await _appwriteClient.Teams.ListTeams(request);

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

[Fact]
public async Task ListTeams_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new ListTeamsRequest();

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams")
.ExpectedHeaders()
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Teams.ListTeams(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,39 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Clients;
using PinguApps.Appwrite.Shared.Converters;
using PinguApps.Appwrite.Shared.Tests;
using Refit;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Clients.Teams;
public partial class TeamsClientTests
{
private readonly MockHttpMessageHandler _mockHttp;
private readonly IAppwriteClient _appwriteClient;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public TeamsClientTests()
{
_mockHttp = new MockHttpMessageHandler();
var services = new ServiceCollection();

services.AddAppwriteServer(TestConstants.ProjectId, TestConstants.ApiKey, TestConstants.Endpoint, new RefitSettings
{
HttpMessageHandlerFactory = () => _mockHttp
});

var serviceProvider = services.BuildServiceProvider();

_appwriteClient = serviceProvider.GetRequiredService<IAppwriteClient>();

_jsonSerializerOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

_jsonSerializerOptions.Converters.Add(new IgnoreSdkExcludedPropertiesConverterFactory());
}
}

0 comments on commit fd832c7

Please sign in to comment.