Add delete unit and integration tests for FeedbackReceiversController.DeleteMany

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-20 14:53:34 +03:00
parent 5f5b983f82
commit 69017d915a
3 changed files with 55 additions and 3 deletions

View file

@ -4,7 +4,6 @@ using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoFixture; using AutoFixture;
@ -14,11 +13,13 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
using Newtonsoft.Json;
using Retroactiune.Database; using Retroactiune.Database;
using Retroactiune.DataTransferObjects; using Retroactiune.DataTransferObjects;
using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures; using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures;
using Retroactiune.Models; using Retroactiune.Models;
using Xunit; using Xunit;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
{ {
@ -150,6 +151,40 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
Assert.Equal(0L, docs); Assert.Equal(0L, docs);
} }
[Theory, AutoData]
public async Task Test_DeleteMany_OK(IEnumerable<FeedbackReceiver> items)
{
// Arrange
await _mongoDb.DropAsync();
var guids = new List<string>();
await _mongoDb.DropAsync();
byte index = 0;
var feedbackReceivers = items as FeedbackReceiver[] ?? items.ToArray();
foreach (var i in feedbackReceivers)
{
i.Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, index, 4, 5, 6, 7, 8, 9, index, 11, 14}))
.ToString();
index += 1;
guids.Add(i.Id);
}
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers);
// Test
var request = new HttpRequestMessage {
Method = HttpMethod.Delete,
RequestUri = new Uri($"{_client.BaseAddress.AbsoluteUri}api/v1/FeedbackReceivers"),
Content = new StringContent(JsonConvert.SerializeObject(guids), Encoding.UTF8, "application/json")
};
var httpResponse = await _client.SendAsync(request);
Assert.Equal(HttpStatusCode.NoContent, httpResponse.StatusCode);
// Assert
var docs = await _mongoDb.FeedbackReceiverCollection.CountDocumentsAsync(FilterDefinition<FeedbackReceiver>
.Empty);
Assert.Equal(0L, docs);
}
[Fact] [Fact]
public async Task Test_Get_Ok() public async Task Test_Get_Ok()
{ {

View file

@ -63,6 +63,24 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
Times.Once); Times.Once);
} }
[Fact]
public async Task DeleteMany_Successful()
{
// Arrange
var mapper = TestUtils.GetMapper();
var mockService = new Mock<IFeedbackReceiverService>();
// Test
var controller = new FeedbackReceiversController(mockService.Object, mapper, null);
var items = new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt", "2", "3"};
var result = await controller.DeleteMany(items);
// Assert
Assert.IsType<NoContentResult>(result);
mockService.Verify(s => s.DeleteManyAsync(items),
Times.Once);
}
[Fact] [Fact]
public async Task Get_Successful() public async Task Get_Successful()
{ {

View file

@ -143,9 +143,8 @@ namespace Retroactiune.Controllers
[HttpDelete] [HttpDelete]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(BasicResponse),StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(BasicResponse),StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteTokens([Required] IEnumerable<string> ids) public async Task<IActionResult> DeleteMany([Required] IEnumerable<string> ids)
{ {
// TODO: Unit test, integration test.
try try
{ {
await _service.DeleteManyAsync(ids); await _service.DeleteManyAsync(ids);