-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get database #550
get database #550
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,9 +72,22 @@ public async Task<AppwriteResult> DeleteDatabase(DeleteDatabaseRequest request) | |
} | ||
} | ||
|
||
[ExcludeFromCodeCoverage] | ||
/// <inheritdoc/> | ||
public Task<AppwriteResult<Database>> GetDatabase(GetDatabaseRequest request) => throw new NotImplementedException(); | ||
public async Task<AppwriteResult<Database>> GetDatabase(GetDatabaseRequest request) | ||
{ | ||
try | ||
{ | ||
request.Validate(true); | ||
|
||
var result = await _databasesApi.GetDatabase(request.DatabaseId); | ||
|
||
return result.GetApiResponse(); | ||
} | ||
catch (Exception e) | ||
{ | ||
return e.GetExceptionResponse<Database>(); | ||
} | ||
Comment on lines
+86
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overly broad exception handling Tell me moreWhat is the issue? Why this matters |
||
} | ||
|
||
[ExcludeFromCodeCoverage] | ||
/// <inheritdoc/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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 GetDatabase_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new GetDatabaseRequest | ||
{ | ||
DatabaseId = IdUtils.GenerateUniqueId() | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}") | ||
.ExpectedHeaders() | ||
.Respond(TestConstants.AppJson, TestConstants.DatabaseResponse); | ||
|
||
// Act | ||
var result = await _appwriteClient.Databases.GetDatabase(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task GetDatabase_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new GetDatabaseRequest | ||
{ | ||
DatabaseId = IdUtils.GenerateUniqueId() | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}") | ||
.ExpectedHeaders() | ||
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteClient.Databases.GetDatabase(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task GetDatabase_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new GetDatabaseRequest | ||
{ | ||
DatabaseId = IdUtils.GenerateUniqueId() | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/databases/{request.DatabaseId}") | ||
.ExpectedHeaders() | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteClient.Databases.GetDatabase(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log exception before returning the exception response.
Tell me more
Consider logging the exception before returning the exception response. This will help with debugging and monitoring the application. You can add a logging statement like
_logger.LogError(e, "Error occurred while getting database");
before returning the exception response.