From fd832c7167f47d56810e2954a76fe4819b98f5f6 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Fri, 18 Oct 2024 01:42:32 +0100 Subject: [PATCH] Rejigged tests and added tests for create teams --- .../Clients/Account/AccountClientTests.cs | 2 +- .../Clients/AppwriteClientTests.cs | 2 + ...Extensions.cs => ClientTestsExtensions.cs} | 4 +- .../Teams/TeamsClientTests.ListTeams.cs | 84 +++++++++++++++++++ .../Clients/Teams/TeamsClientTests.cs | 38 +++++++++ .../Teams/TeamsClientTests.ListTeams.cs | 62 ++++++++++++++ .../Clients/Teams/TeamsClientTests.cs | 39 +++++++++ 7 files changed, 228 insertions(+), 3 deletions(-) rename tests/PinguApps.Appwrite.Client.Tests/Clients/{Account/AccountTestsExtensions.cs => ClientTestsExtensions.cs} (90%) create mode 100644 tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.ListTeams.cs create mode 100644 tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.cs create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.ListTeams.cs create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.cs diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.cs index 968d22ad..6b41629f 100644 --- a/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.cs +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.cs @@ -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 }); diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/AppwriteClientTests.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/AppwriteClientTests.cs index 4f485339..780e65c0 100644 --- a/tests/PinguApps.Appwrite.Client.Tests/Clients/AppwriteClientTests.cs +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/AppwriteClientTests.cs @@ -42,6 +42,7 @@ public void SetSession_UpdatesSession() var mockAccountClient = new Mock(); var mockTeamsClient = new Mock(); mockAccountClient.As(); + mockTeamsClient.As(); var appwriteClient = new AppwriteClient(mockAccountClient.Object, mockTeamsClient.Object); // Act @@ -58,6 +59,7 @@ public void SetSession_UpdatesSessionInAccountClient() var mockAccountClient = new Mock(); var mockTeamsClient = new Mock(); mockAccountClient.As(); + mockTeamsClient.As(); var appwriteClient = new AppwriteClient(mockAccountClient.Object, mockTeamsClient.Object); // Act diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountTestsExtensions.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/ClientTestsExtensions.cs similarity index 90% rename from tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountTestsExtensions.cs rename to tests/PinguApps.Appwrite.Client.Tests/Clients/ClientTestsExtensions.cs index 8338d4af..17fccfb6 100644 --- a/tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountTestsExtensions.cs +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/ClientTestsExtensions.cs @@ -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) { diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.ListTeams.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.ListTeams.cs new file mode 100644 index 00000000..e7c14de2 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.ListTeams.cs @@ -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); + } +} diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.cs new file mode 100644 index 00000000..c991ab08 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.cs @@ -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(); + + _jsonSerializerOptions = new JsonSerializerOptions + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + + _jsonSerializerOptions.Converters.Add(new IgnoreSdkExcludedPropertiesConverterFactory()); + } +} diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.ListTeams.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.ListTeams.cs new file mode 100644 index 00000000..c5c9efbb --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.ListTeams.cs @@ -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); + } +} diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.cs new file mode 100644 index 00000000..9efd8b38 --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.cs @@ -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(); + + _jsonSerializerOptions = new JsonSerializerOptions + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + + _jsonSerializerOptions.Converters.Add(new IgnoreSdkExcludedPropertiesConverterFactory()); + } +}