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;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
@ -10,13 +11,12 @@ namespace Retroactiune.Core.Entities
/// </summary> /// </summary>
public class Feedback public class Feedback
{ {
public Feedback() public Feedback()
{ {
Id = ObjectId.GenerateNewId().ToString(); Id = ObjectId.GenerateNewId().ToString();
CreatedAt = DateTime.UtcNow; CreatedAt = DateTime.UtcNow;
} }
[BsonId, JsonPropertyName("id")] [BsonId, JsonPropertyName("id")]
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } public string Id { get; set; }
@ -24,12 +24,31 @@ namespace Retroactiune.Core.Entities
[JsonPropertyName("feedback_receiver_id")] [JsonPropertyName("feedback_receiver_id")]
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
public string FeedbackReceiverId { get; set; } public string FeedbackReceiverId { get; set; }
[JsonPropertyName("rating")] [JsonPropertyName("rating")] public uint Rating { get; set; }
public uint Rating { get; set; }
[JsonPropertyName("description")] public string Description { get; set; } [JsonPropertyName("description")] public string Description { get; set; }
[JsonPropertyName("created_at")] public DateTime CreatedAt { 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

@ -435,15 +435,42 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var feedbacksCursor = await _mongoDb.FeedbacksCollection.FindAsync(FilterDefinition<Feedback>.Empty); var feedbacksCursor = await _mongoDb.FeedbacksCollection.FindAsync(FilterDefinition<Feedback>.Empty);
var feedbacks = await feedbacksCursor.ToListAsync(); var feedbacks = await feedbacksCursor.ToListAsync();
Assert.Equal("ok", feedbacks.ElementAt(0).Description); Assert.Equal("ok", feedbacks.ElementAt(0).Description);
Assert.Equal(4u, feedbacks.ElementAt(0).Rating); Assert.Equal(4u, feedbacks.ElementAt(0).Rating);
Assert.Equal(feedbackReceiver.Id, feedbacks.ElementAt(0).FeedbackReceiverId); Assert.Equal(feedbackReceiver.Id, feedbacks.ElementAt(0).FeedbackReceiverId);
var tokensCursor = await _mongoDb.TokensCollection.FindAsync(FilterDefinition<Token>.Empty); var tokensCursor = await _mongoDb.TokensCollection.FindAsync(FilterDefinition<Token>.Empty);
var tokens = await tokensCursor.ToListAsync(); var tokens = await tokensCursor.ToListAsync();
Assert.NotNull(tokens.ElementAt(0).TimeUsed); 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> /// </summary>
public class ListFeedbacksFiltersDto public class ListFeedbacksFiltersDto
{ {
public uint Rating { get; set; } public uint? Rating { get; set; }
public DateTime? CreatedAfter { get; set; } public DateTime? CreatedAfter { get; set; }
public DateTime? CreatedBefore { get; set; } public DateTime? CreatedBefore { get; set; }
} }