Add integration tests for FeedbackReceiver.cs

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-07 22:49:43 +03:00
parent 13e1ffa3d6
commit 422533bb5a
3 changed files with 181 additions and 9 deletions

View file

@ -9,6 +9,7 @@ using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.Xunit2;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
@ -152,7 +153,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
{
Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3",
Description = "someting",
Description = "something",
CreatedAt = DateTime.Parse("2020-02-01")
};
@ -195,7 +196,146 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
// Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
}
// TODO test list
[Fact]
public async Task Test_List_Ok()
{
// Arrange
await _mongoDb.DropAsync();
var feedbackReceivers = new List<FeedbackReceiver>
{
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3",
Description = "something",
CreatedAt = DateTime.UnixEpoch
},
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3_Two",
Description = "something",
CreatedAt = DateTime.UnixEpoch
},
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3_Three",
Description = "something",
CreatedAt = DateTime.UnixEpoch
}
};
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers);
// Test
var httpResponse = await _client.GetAsync("/api/v1/FeedbackReceivers/", CancellationToken.None);
var items = JsonSerializer.Deserialize<List<FeedbackReceiver>>(
await httpResponse.Content.ReadAsStringAsync());
// Assert
Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
for (var i = 0; i < feedbackReceivers.Count; i++)
{
Assert.Equal(feedbackReceivers[i], items[i]);
}
}
[Fact]
public async Task Test_List_Ok_Filter()
{
// Arrange
await _mongoDb.DropAsync();
var feedbackReceivers = new List<FeedbackReceiver>
{
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3",
Description = "something",
CreatedAt = DateTime.UnixEpoch
},
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3_Two",
Description = "something",
CreatedAt = DateTime.UnixEpoch
},
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3_Three",
Description = "something",
CreatedAt = DateTime.UnixEpoch
}
};
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers);
// Test
var qb = new QueryBuilder {{"filter", new[] {feedbackReceivers[0].Id, feedbackReceivers[1].Id}}};
var httpResponse =
await _client.GetAsync("/api/v1/FeedbackReceivers/" + qb,
CancellationToken.None);
var items = JsonSerializer.Deserialize<List<FeedbackReceiver>>(
await httpResponse.Content.ReadAsStringAsync());
// Assert
Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
Assert.Equal(2, items.Count);
for (var i = 0; i < 2; i++)
{
Assert.Equal(feedbackReceivers[i], items[i]);
}
}
[Fact]
public async Task Test_List_Ok_LimitOffset()
{
// Arrange
await _mongoDb.DropAsync();
var feedbackReceivers = new List<FeedbackReceiver>
{
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3",
Description = "something",
CreatedAt = DateTime.UnixEpoch
},
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3_Two",
Description = "something",
CreatedAt = DateTime.UnixEpoch
},
new FeedbackReceiver
{
Id = new BsonObjectId(new ObjectId(new byte[] {3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(),
Name = "N4m3_Three",
Description = "something",
CreatedAt = DateTime.UnixEpoch
}
};
await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers);
// Test
var qb = new QueryBuilder {{"offset", "1"}, {"limit", "1"}};
var httpResponse = await _client.GetAsync("/api/v1/FeedbackReceivers/" + qb, CancellationToken.None);
var items = JsonSerializer.Deserialize<List<FeedbackReceiver>>(
await httpResponse.Content.ReadAsStringAsync());
// Assert
Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
Assert.Single(items);
Assert.Equal(feedbackReceivers[1], items[0]);
}
}
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoFixture.Xunit2;
using Microsoft.AspNetCore.Mvc;
@ -96,7 +97,21 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
mockService.Verify(s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}, null, null),
Times.Once);
}
// TODO: test list
[Theory, AutoData]
public async Task List_Ok(IEnumerable<string> filter, int offset, int limit)
{
// Arrange
var mapper = TestUtils.GetMapper();
var mockService = new Mock<IFeedbackReceiverService>();
var filterArr = filter as string[] ?? filter.ToArray();
// Test
var controller = new FeedbackReceiversController(mockService.Object, mapper, null);
var result = await controller.List(filterArr, offset, limit);
Assert.IsType<OkObjectResult>(result);
mockService.Verify(s => s.FindAsync(filterArr, offset, limit), Times.Once);
}
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
@ -9,14 +10,30 @@ namespace Retroactiune.Models
/// </summary>
public class FeedbackReceiver
{
[BsonId]
[BsonId, JsonPropertyName("id")]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name { get; set; }
[JsonPropertyName("name")] public string Name { get; set; }
public string Description { get; set; }
[JsonPropertyName("description")] public string Description { get; set; }
public DateTime CreatedAt { get; set; }
[JsonPropertyName("createdAt")] public DateTime CreatedAt { get; set; }
public override bool Equals(object obj)
{
if (!(obj is FeedbackReceiver convertedObj))
{
return false;
}
return Id.Equals(convertedObj.Id) && Name.Equals(convertedObj.Name) &&
Description.Equals(convertedObj.Description) && CreatedAt.Equals(convertedObj.CreatedAt);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}