Skip to content

Commit a997bca

Browse files
Add unit tests for create file upload api
1 parent dd339be commit a997bca

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

Src/Notion.Client/Api/FileUploads/Create/Request/CreateFileUploadRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class CreateFileUploadRequest : ICreateFileUploadBodyParameters
44
{
55
public FileUploadMode Mode { get; set; }
66

7-
public string Filename { get; set; }
7+
public string FileName { get; set; }
88

99
public string ContentType { get; set; }
1010

Src/Notion.Client/Api/FileUploads/Create/Request/ICreateFileUploadBodyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface ICreateFileUploadBodyParameters
2020
/// from the content_type parameter.
2121
/// </summary>
2222
[JsonProperty("filename")]
23-
string Filename { get; }
23+
string FileName { get; }
2424

2525
/// <summary>
2626
/// MIME type of the file to be created. Recommended when sending the file in multiple parts.

Test/Notion.IntegrationTests/FIleUploadsClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public async Task CreateAsync()
1414
{
1515
Mode = FileUploadMode.ExternalUrl,
1616
ExternalUrl = "https://unsplash.com/photos/hOhlYhAiizc/download?ixid=M3wxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNzYwMTkxNzc3fA&force=true",
17-
Filename = "sample-image.jpg",
17+
FileName = "sample-image.jpg",
1818
};
1919

2020
// Act
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Moq;
6+
using Moq.AutoMock;
7+
using Newtonsoft.Json;
8+
using Notion.Client;
9+
using Xunit;
10+
11+
namespace Notion.UnitTests;
12+
13+
public class FileUploadClientTests
14+
{
15+
private readonly AutoMocker _mocker = new();
16+
private readonly FileUploadsClient _fileUploadClient;
17+
private readonly Mock<IRestClient> _restClientMock;
18+
19+
public FileUploadClientTests()
20+
{
21+
_restClientMock = _mocker.GetMock<IRestClient>();
22+
_fileUploadClient = _mocker.CreateInstance<FileUploadsClient>();
23+
}
24+
25+
[Fact]
26+
public async Task CreateAsync_ThrowsArgumentNullException_WhenRequestIsNull()
27+
{
28+
// Act & Assert
29+
var exception = await Assert.ThrowsAsync<ArgumentNullException>(() => _fileUploadClient.CreateAsync(null));
30+
Assert.Equal("fileUploadObjectRequest", exception.ParamName);
31+
Assert.Equal("Value cannot be null. (Parameter 'fileUploadObjectRequest')", exception.Message);
32+
}
33+
34+
[Fact]
35+
public async Task CreateAsync_CallsRestClientPostAsync_WithCorrectParameters()
36+
{
37+
// Arrange
38+
var request = new CreateFileUploadRequest
39+
{
40+
FileName = "testfile.txt",
41+
Mode = FileUploadMode.SinglePart,
42+
};
43+
44+
var expectedResponse = new CreateFileUploadResponse
45+
{
46+
UploadUrl = "https://example.com/upload",
47+
Id = Guid.NewGuid().ToString(),
48+
};
49+
50+
_restClientMock
51+
.Setup(client => client.PostAsync<CreateFileUploadResponse>(
52+
It.Is<string>(url => url == ApiEndpoints.FileUploadsApiUrls.Create()),
53+
It.IsAny<ICreateFileUploadBodyParameters>(),
54+
It.IsAny<IEnumerable<KeyValuePair<string, string>>>(),
55+
It.IsAny<IDictionary<string, string>>(),
56+
It.IsAny<JsonSerializerSettings>(),
57+
It.IsAny<IBasicAuthenticationParameters>(),
58+
It.IsAny<CancellationToken>()))
59+
.ReturnsAsync(expectedResponse);
60+
61+
// Act
62+
var response = await _fileUploadClient.CreateAsync(request);
63+
64+
// Assert
65+
Assert.Equal(expectedResponse.UploadUrl, response.UploadUrl);
66+
Assert.Equal(expectedResponse.Id, response.Id);
67+
68+
_restClientMock.VerifyAll();
69+
}
70+
}

0 commit comments

Comments
 (0)