Add unit tests for GET feedbacks

This commit is contained in:
Denis-Cosmin Nutiu 2021-07-24 19:04:07 +03:00
parent bf431cb3e4
commit 3a0e59604b
3 changed files with 55 additions and 9 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
@ -10,7 +11,6 @@ namespace Retroactiune.Core.Entities
/// </summary>
public class Feedback
{
public Feedback()
{
Id = ObjectId.GenerateNewId().ToString();
@ -25,11 +25,30 @@ namespace Retroactiune.Core.Entities
[BsonRepresentation(BsonType.ObjectId)]
public string FeedbackReceiverId { get; set; }
[JsonPropertyName("rating")]
public uint Rating { get; set; }
[JsonPropertyName("rating")] public uint Rating { get; set; }
[JsonPropertyName("description")] public string Description { get; set; }
[JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; }
private bool Equals(Feedback other)
{
return Id == other.Id && FeedbackReceiverId == other.FeedbackReceiverId && Rating == other.Rating &&
Description == other.Description && CreatedAt - other.CreatedAt < TimeSpan.FromSeconds(1);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Feedback) obj);
}
[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")]
public override int GetHashCode()
{
return HashCode.Combine(Id, FeedbackReceiverId, Rating, Description, CreatedAt);
}
}
}

View file

@ -445,5 +445,32 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
Assert.NotNull(tokens.ElementAt(0).TimeUsed);
}
[Theory, AutoData]
public async Task Test_GetFeedbacks(IEnumerable<Feedback> feedbacksSeed)
{
// Setup
await _mongoDb.DropAsync();
var feedbackReceiverGuid = ObjectId.GenerateNewId().ToString();
var selectedFeedbacksSeed = feedbacksSeed.Select(i =>
{
i.Id = ObjectId.GenerateNewId().ToString();
i.CreatedAt = i.CreatedAt.ToUniversalTime();
i.FeedbackReceiverId = feedbackReceiverGuid;
return i;
}).ToList();
await _mongoDb.FeedbacksCollection.InsertManyAsync(selectedFeedbacksSeed);
// Test
var response = await _client.GetAsync($"/api/v1/feedback_receivers/{feedbackReceiverGuid}/feedbacks");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var feedbacksResponse =
JsonSerializer.Deserialize<List<Feedback>>(await response.Content.ReadAsStringAsync());
Assert.Equal(selectedFeedbacksSeed,feedbacksResponse);
}
}
}

View file

@ -7,7 +7,7 @@ namespace Retroactiune.DataTransferObjects
/// </summary>
public class ListFeedbacksFiltersDto
{
public uint Rating { get; set; }
public uint? Rating { get; set; }
public DateTime? CreatedAfter { get; set; }
public DateTime? CreatedBefore { get; set; }
}