From a997bca1bc70c86a888a8295df5aff752bfb277b Mon Sep 17 00:00:00 2001 From: Vedant Koditkar <18693839+KoditkarVedant@users.noreply.github.com> Date: Sun, 12 Oct 2025 19:30:49 +0530 Subject: [PATCH] Add unit tests for create file upload api --- .../Create/Request/CreateFileUploadRequest.cs | 2 +- .../ICreateFileUploadBodyParameters.cs | 2 +- .../FIleUploadsClientTests.cs | 2 +- .../Notion.UnitTests/FileUploadClientTests.cs | 70 +++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 Test/Notion.UnitTests/FileUploadClientTests.cs diff --git a/Src/Notion.Client/Api/FileUploads/Create/Request/CreateFileUploadRequest.cs b/Src/Notion.Client/Api/FileUploads/Create/Request/CreateFileUploadRequest.cs index 6b5e4776..e9145e68 100644 --- a/Src/Notion.Client/Api/FileUploads/Create/Request/CreateFileUploadRequest.cs +++ b/Src/Notion.Client/Api/FileUploads/Create/Request/CreateFileUploadRequest.cs @@ -4,7 +4,7 @@ public class CreateFileUploadRequest : ICreateFileUploadBodyParameters { public FileUploadMode Mode { get; set; } - public string Filename { get; set; } + public string FileName { get; set; } public string ContentType { get; set; } diff --git a/Src/Notion.Client/Api/FileUploads/Create/Request/ICreateFileUploadBodyParameters.cs b/Src/Notion.Client/Api/FileUploads/Create/Request/ICreateFileUploadBodyParameters.cs index 38b21532..c6317e07 100644 --- a/Src/Notion.Client/Api/FileUploads/Create/Request/ICreateFileUploadBodyParameters.cs +++ b/Src/Notion.Client/Api/FileUploads/Create/Request/ICreateFileUploadBodyParameters.cs @@ -20,7 +20,7 @@ public interface ICreateFileUploadBodyParameters /// from the content_type parameter. /// [JsonProperty("filename")] - string Filename { get; } + string FileName { get; } /// /// MIME type of the file to be created. Recommended when sending the file in multiple parts. diff --git a/Test/Notion.IntegrationTests/FIleUploadsClientTests.cs b/Test/Notion.IntegrationTests/FIleUploadsClientTests.cs index 306f550c..17972f7c 100644 --- a/Test/Notion.IntegrationTests/FIleUploadsClientTests.cs +++ b/Test/Notion.IntegrationTests/FIleUploadsClientTests.cs @@ -14,7 +14,7 @@ public async Task CreateAsync() { Mode = FileUploadMode.ExternalUrl, ExternalUrl = "https://unsplash.com/photos/hOhlYhAiizc/download?ixid=M3wxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNzYwMTkxNzc3fA&force=true", - Filename = "sample-image.jpg", + FileName = "sample-image.jpg", }; // Act diff --git a/Test/Notion.UnitTests/FileUploadClientTests.cs b/Test/Notion.UnitTests/FileUploadClientTests.cs new file mode 100644 index 00000000..53ebaa59 --- /dev/null +++ b/Test/Notion.UnitTests/FileUploadClientTests.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Moq; +using Moq.AutoMock; +using Newtonsoft.Json; +using Notion.Client; +using Xunit; + +namespace Notion.UnitTests; + +public class FileUploadClientTests +{ + private readonly AutoMocker _mocker = new(); + private readonly FileUploadsClient _fileUploadClient; + private readonly Mock _restClientMock; + + public FileUploadClientTests() + { + _restClientMock = _mocker.GetMock(); + _fileUploadClient = _mocker.CreateInstance(); + } + + [Fact] + public async Task CreateAsync_ThrowsArgumentNullException_WhenRequestIsNull() + { + // Act & Assert + var exception = await Assert.ThrowsAsync(() => _fileUploadClient.CreateAsync(null)); + Assert.Equal("fileUploadObjectRequest", exception.ParamName); + Assert.Equal("Value cannot be null. (Parameter 'fileUploadObjectRequest')", exception.Message); + } + + [Fact] + public async Task CreateAsync_CallsRestClientPostAsync_WithCorrectParameters() + { + // Arrange + var request = new CreateFileUploadRequest + { + FileName = "testfile.txt", + Mode = FileUploadMode.SinglePart, + }; + + var expectedResponse = new CreateFileUploadResponse + { + UploadUrl = "https://example.com/upload", + Id = Guid.NewGuid().ToString(), + }; + + _restClientMock + .Setup(client => client.PostAsync( + It.Is(url => url == ApiEndpoints.FileUploadsApiUrls.Create()), + It.IsAny(), + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expectedResponse); + + // Act + var response = await _fileUploadClient.CreateAsync(request); + + // Assert + Assert.Equal(expectedResponse.UploadUrl, response.UploadUrl); + Assert.Equal(expectedResponse.Id, response.Id); + + _restClientMock.VerifyAll(); + } +} \ No newline at end of file