From 0f600e8181bacfbfab40f98118475bd2ad760822 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Tue, 10 Dec 2024 19:20:15 +0000 Subject: [PATCH] added tests for update document --- .../DatabasesClientTests.UpdateDocument.cs | 78 +++++++++++++++++++ .../DatabasesClientTests.UpdateDocument.cs | 78 +++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Client.Tests/Clients/Databases/DatabasesClientTests.UpdateDocument.cs create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Databases/DatabasesClientTests.UpdateDocument.cs diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Databases/DatabasesClientTests.UpdateDocument.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Databases/DatabasesClientTests.UpdateDocument.cs new file mode 100644 index 00000000..7cd92fac --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Databases/DatabasesClientTests.UpdateDocument.cs @@ -0,0 +1,78 @@ +using System.Net; +using PinguApps.Appwrite.Shared.Requests.Databases; +using PinguApps.Appwrite.Shared.Tests; +using PinguApps.Appwrite.Shared.Utils; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Client.Tests.Clients.Databases; +public partial class DatabasesClientTests +{ + [Fact] + public async Task UpdateDocument_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = UpdateDocumentRequest.CreateBuilder() + .WithDatabaseId(IdUtils.GenerateUniqueId()) + .WithCollectionId(IdUtils.GenerateUniqueId()) + .WithDocumentId(IdUtils.GenerateUniqueId()) + .AddField("test", "test") + .Build(); + + _mockHttp.Expect(HttpMethod.Patch, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents/{request.DocumentId}") + .ExpectedHeaders() + .Respond(TestConstants.AppJson, TestConstants.DocumentResponse); + + // Act + var result = await _appwriteClient.Databases.UpdateDocument(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task UpdateDocument_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = UpdateDocumentRequest.CreateBuilder() + .WithDatabaseId(IdUtils.GenerateUniqueId()) + .WithCollectionId(IdUtils.GenerateUniqueId()) + .WithDocumentId(IdUtils.GenerateUniqueId()) + .AddField("test", "test") + .Build(); + + _mockHttp.Expect(HttpMethod.Patch, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents/{request.DocumentId}") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); + + // Act + var result = await _appwriteClient.Databases.UpdateDocument(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task UpdateDocument_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = UpdateDocumentRequest.CreateBuilder() + .WithDatabaseId(IdUtils.GenerateUniqueId()) + .WithCollectionId(IdUtils.GenerateUniqueId()) + .WithDocumentId(IdUtils.GenerateUniqueId()) + .AddField("test", "test") + .Build(); + + _mockHttp.Expect(HttpMethod.Patch, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents/{request.DocumentId}") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Databases.UpdateDocument(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/Databases/DatabasesClientTests.UpdateDocument.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Databases/DatabasesClientTests.UpdateDocument.cs new file mode 100644 index 00000000..7eaf2f65 --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Databases/DatabasesClientTests.UpdateDocument.cs @@ -0,0 +1,78 @@ +using System.Net; +using PinguApps.Appwrite.Shared.Requests.Databases; +using PinguApps.Appwrite.Shared.Tests; +using PinguApps.Appwrite.Shared.Utils; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Server.Tests.Clients.Databases; +public partial class DatabasesClientTests +{ + [Fact] + public async Task UpdateDocument_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = UpdateDocumentRequest.CreateBuilder() + .WithDatabaseId(IdUtils.GenerateUniqueId()) + .WithCollectionId(IdUtils.GenerateUniqueId()) + .WithDocumentId(IdUtils.GenerateUniqueId()) + .AddField("test", "test") + .Build(); + + _mockHttp.Expect(HttpMethod.Patch, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents/{request.DocumentId}") + .ExpectedHeaders() + .Respond(TestConstants.AppJson, TestConstants.DocumentResponse); + + // Act + var result = await _appwriteClient.Databases.UpdateDocument(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task UpdateDocument_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = UpdateDocumentRequest.CreateBuilder() + .WithDatabaseId(IdUtils.GenerateUniqueId()) + .WithCollectionId(IdUtils.GenerateUniqueId()) + .WithDocumentId(IdUtils.GenerateUniqueId()) + .AddField("test", "test") + .Build(); + + _mockHttp.Expect(HttpMethod.Patch, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents/{request.DocumentId}") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); + + // Act + var result = await _appwriteClient.Databases.UpdateDocument(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task UpdateDocument_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = UpdateDocumentRequest.CreateBuilder() + .WithDatabaseId(IdUtils.GenerateUniqueId()) + .WithCollectionId(IdUtils.GenerateUniqueId()) + .WithDocumentId(IdUtils.GenerateUniqueId()) + .AddField("test", "test") + .Build(); + + _mockHttp.Expect(HttpMethod.Patch, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents/{request.DocumentId}") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Databases.UpdateDocument(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +}