Add ResultApiTests with comprehensive test coverage
Introduced the `ResultApiTests` class to validate the behavior of the `Results` API client. Added test cases to ensure proper dependency injection, handle various API operations (`GetAsync`, `GetDynamicAsync`, `CreateAsync`, `UpdateAsync`, `DeleteAsync`), and verify expected outcomes such as exceptions, payload handling, and dynamic responses. Included necessary `using` directives to support the new tests.
This commit is contained in:
132
tests/ReC.Tests/Client/ResultApiTests.cs
Normal file
132
tests/ReC.Tests/Client/ResultApiTests.cs
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Text.Json;
|
||||||
|
using ReC.Application.Common.Dto;
|
||||||
|
using ReC.Application.Common.Procedures.UpdateProcedure.Dto;
|
||||||
|
using ReC.Application.RecActions.Commands;
|
||||||
|
using ReC.Application.Results.Commands;
|
||||||
|
using ReC.Client;
|
||||||
|
using ReC.Domain.Constants;
|
||||||
|
using DomainResultType = ReC.Domain.Constants.ResultType;
|
||||||
|
|
||||||
|
namespace ReC.Tests.Client;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
public class ResultApiTests : RecClientTestBase
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void ReCClient_results_api_is_resolvable_through_dependency_injection()
|
||||||
|
{
|
||||||
|
var (client, scope) = CreateScopedClient();
|
||||||
|
using var _ = scope;
|
||||||
|
|
||||||
|
Assert.That(client, Is.Not.Null);
|
||||||
|
Assert.That(client.Results, Is.Not.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAsync_with_unknown_filter_throws_not_found()
|
||||||
|
{
|
||||||
|
var (client, scope) = CreateScopedClient();
|
||||||
|
using var _ = scope;
|
||||||
|
|
||||||
|
var ex = Assert.ThrowsAsync<ReCApiException>(async () =>
|
||||||
|
await client.Results.GetAsync<ResultViewDto[]>(id: long.MaxValue));
|
||||||
|
|
||||||
|
Assert.That(ex, Is.Not.Null);
|
||||||
|
Assert.That(ex!.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||||
|
Assert.That(ex.Method, Is.EqualTo("GET"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetDynamicAsync_without_filters_returns_dynamic_payload_or_throws_not_found()
|
||||||
|
{
|
||||||
|
var (client, scope) = CreateScopedClient();
|
||||||
|
using var _ = scope;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
dynamic results = await client.Results.GetDynamicAsync();
|
||||||
|
Assert.That(results, Is.Not.Null);
|
||||||
|
Assert.That(results, Is.TypeOf<JsonElement>());
|
||||||
|
}
|
||||||
|
catch (ReCApiException ex)
|
||||||
|
{
|
||||||
|
Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||||
|
Assert.That(ex.Method, Is.EqualTo("GET"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CreateAsync_with_invalid_action_reference_throws_or_completes()
|
||||||
|
{
|
||||||
|
var (client, scope) = CreateScopedClient();
|
||||||
|
using var _ = scope;
|
||||||
|
|
||||||
|
var payload = new InsertResultCommand
|
||||||
|
{
|
||||||
|
ActionId = long.MaxValue,
|
||||||
|
Status = RecStatus.Error,
|
||||||
|
Type = DomainResultType.Main,
|
||||||
|
References = new InvokeReferences { BatchId = "test-batch" },
|
||||||
|
Error = "integration-test"
|
||||||
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Results.CreateAsync(payload).GetAwaiter().GetResult();
|
||||||
|
Assert.Pass("Create completed.");
|
||||||
|
}
|
||||||
|
catch (ReCApiException ex)
|
||||||
|
{
|
||||||
|
Assert.That(ex.Method, Is.EqualTo("POST"));
|
||||||
|
Assert.That(ex.RequestUri!.AbsolutePath, Does.EndWith("api/Result"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateAsync_with_unknown_id_throws_or_completes()
|
||||||
|
{
|
||||||
|
var (client, scope) = CreateScopedClient();
|
||||||
|
using var _ = scope;
|
||||||
|
|
||||||
|
var payload = new UpdateResultDto
|
||||||
|
{
|
||||||
|
Error = "updated-from-test"
|
||||||
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Results.UpdateAsync(long.MaxValue, payload).GetAwaiter().GetResult();
|
||||||
|
Assert.Pass("Update completed.");
|
||||||
|
}
|
||||||
|
catch (ReCApiException ex)
|
||||||
|
{
|
||||||
|
Assert.That(ex.Method, Is.EqualTo("PUT"));
|
||||||
|
Assert.That(ex.RequestUri!.AbsolutePath, Does.EndWith($"api/Result/{long.MaxValue}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteAsync_sends_payload_as_query_string()
|
||||||
|
{
|
||||||
|
var (client, scope) = CreateScopedClient();
|
||||||
|
using var _ = scope;
|
||||||
|
|
||||||
|
var payload = new DeleteResultCommand
|
||||||
|
{
|
||||||
|
Start = long.MaxValue - 1,
|
||||||
|
End = long.MaxValue,
|
||||||
|
Force = false
|
||||||
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Results.DeleteAsync(payload).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
catch (ReCApiException ex)
|
||||||
|
{
|
||||||
|
Assert.That(ex.Method, Is.EqualTo("DELETE"));
|
||||||
|
Assert.That(ex.RequestUri!.Query, Does.Contain("Start=").And.Contains("End=").And.Contains("Force="));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user