From 69017d915a4621e0d7a8ee686ec6a19cf58fa699 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sun, 20 Jun 2021 14:53:34 +0300 Subject: [PATCH] Add delete unit and integration tests for FeedbackReceiversController.DeleteMany --- .../Controllers/TestFeedbackReceiver.cs | 37 ++++++++++++++++++- .../TestFeedbackReceiverController.cs | 18 +++++++++ .../FeedbackReceiversController.cs | 3 +- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs index 0c5aba7..fd9ff3c 100644 --- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs +++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Text; -using System.Text.Json; using System.Threading; using System.Threading.Tasks; using AutoFixture; @@ -14,11 +13,13 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MongoDB.Bson; using MongoDB.Driver; +using Newtonsoft.Json; using Retroactiune.Database; using Retroactiune.DataTransferObjects; using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures; using Retroactiune.Models; using Xunit; +using JsonSerializer = System.Text.Json.JsonSerializer; namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers { @@ -149,6 +150,40 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers .Empty); Assert.Equal(0L, docs); } + + [Theory, AutoData] + public async Task Test_DeleteMany_OK(IEnumerable items) + { + // Arrange + await _mongoDb.DropAsync(); + var guids = new List(); + 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 + .Empty); + Assert.Equal(0L, docs); + } [Fact] public async Task Test_Get_Ok() diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs index d57896b..ed44e2e 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs @@ -62,6 +62,24 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers mockService.Verify(s => s.DeleteManyAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}), Times.Once); } + + [Fact] + public async Task DeleteMany_Successful() + { + // Arrange + var mapper = TestUtils.GetMapper(); + var mockService = new Mock(); + + // 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(result); + mockService.Verify(s => s.DeleteManyAsync(items), + Times.Once); + } [Fact] public async Task Get_Successful() diff --git a/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs index c50d56e..a9f1d32 100644 --- a/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs +++ b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs @@ -143,9 +143,8 @@ namespace Retroactiune.Controllers [HttpDelete] [ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(BasicResponse),StatusCodes.Status400BadRequest)] - public async Task DeleteTokens([Required] IEnumerable ids) + public async Task DeleteMany([Required] IEnumerable ids) { - // TODO: Unit test, integration test. try { await _service.DeleteManyAsync(ids);