Skip to content

Commit

Permalink
Added tests to each documents client method, to check that when neede…
Browse files Browse the repository at this point in the history
…d, session is added
  • Loading branch information
pingu2k4 committed Dec 10, 2024
1 parent 0f600e8 commit c3202a7
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public async Task CreateDocument_ShouldReturnSuccess_WhenApiCallSucceeds()
Assert.True(result.Success);
}

[Fact]
public async Task CreateDocument_ShouldIncludeSessionHeaders_WhenProvided()
{
// Arrange
var request = CreateDocumentRequest.CreateBuilder()
.WithDatabaseId(IdUtils.GenerateUniqueId())
.WithCollectionId(IdUtils.GenerateUniqueId())
.AddField("AttributeName", "MyValue")
.Build();

_mockHttp.Expect(HttpMethod.Post, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents")
.ExpectedHeaders()
.WithJsonContent(request, _jsonSerializerOptions)
.Respond(TestConstants.AppJson, TestConstants.DocumentResponse);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Databases.CreateDocument(request);

// Assert
_mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task CreateDocument_ShouldHandleException_WhenApiCallFails()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public async Task DeleteDocument_ShouldReturnSuccess_WhenApiCallSucceeds()
Assert.True(result.Success);
}

[Fact]
public async Task DeleteDocument_ShouldIncludeSessionHeaders_WhenProvided()
{
// Arrange
var request = new DeleteDocumentRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId(),
DocumentId = IdUtils.GenerateUniqueId()
};

_mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents/{request.DocumentId}")
.ExpectedHeaders()
.Respond(HttpStatusCode.NoContent);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Databases.DeleteDocument(request);

// Assert
_mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task DeleteDocument_ShouldHandleException_WhenApiCallFails()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public async Task GetDocument_ShouldReturnSuccess_WhenApiCallSucceeds()
Assert.True(result.Success);
}

[Fact]
public async Task GetDocument_ShouldIncludeSessionHeaders_WhenProvided()
{
// Arrange
var request = new GetDocumentRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId(),
DocumentId = IdUtils.GenerateUniqueId(),
};

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents/{request.DocumentId}")
.ExpectedHeaders()
.Respond(TestConstants.AppJson, TestConstants.DocumentResponse);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Databases.GetDocument(request);

// Assert
_mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task GetDocument_ShouldProvideQueryParams_WhenQueriesProvided()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ public async Task ListDocuments_ShouldReturnSuccess_WhenApiCallSucceeds()
Assert.True(result.Success);
}

[Fact]
public async Task ListDocuments_ShouldIncludeSessionHeaders_WhenProvided()
{
// Arrange
var request = new ListDocumentsRequest
{
DatabaseId = IdUtils.GenerateUniqueId(),
CollectionId = IdUtils.GenerateUniqueId()
};

_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}/collections/{request.CollectionId}/documents")
.ExpectedHeaders(true)
.Respond(TestConstants.AppJson, TestConstants.DocumentsListResponse);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Databases.ListDocuments(request);

// Assert
_mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task ListDocuments_ShouldProvideQueryParams_WhenQueriesProvided()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public async Task UpdateDocument_ShouldReturnSuccess_WhenApiCallSucceeds()
Assert.True(result.Success);
}

[Fact]
public async Task UpdateDocument_ShouldIncludeSessionHeaders_WhenProvided()
{
// 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(true)
.Respond(TestConstants.AppJson, TestConstants.DocumentResponse);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Databases.UpdateDocument(request);

// Assert
_mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task UpdateDocument_ShouldHandleException_WhenApiCallFails()
{
Expand Down

0 comments on commit c3202a7

Please sign in to comment.