From 3a0e59604bbe3dd008c13be94c780e6a0fe62148 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 24 Jul 2021 19:04:07 +0300 Subject: [PATCH] Add unit tests for GET feedbacks --- Retroactiune.Core/Entities/Feedback.cs | 29 +++++++++++++--- .../TestFeedbackReceiversController.cs | 33 +++++++++++++++++-- .../ListFeedbacksFiltersDto.cs | 2 +- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/Retroactiune.Core/Entities/Feedback.cs b/Retroactiune.Core/Entities/Feedback.cs index 3475e58..4f64911 100644 --- a/Retroactiune.Core/Entities/Feedback.cs +++ b/Retroactiune.Core/Entities/Feedback.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; @@ -10,13 +11,12 @@ namespace Retroactiune.Core.Entities /// public class Feedback { - public Feedback() { Id = ObjectId.GenerateNewId().ToString(); CreatedAt = DateTime.UtcNow; } - + [BsonId, JsonPropertyName("id")] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } @@ -24,12 +24,31 @@ namespace Retroactiune.Core.Entities [JsonPropertyName("feedback_receiver_id")] [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); + } } } \ No newline at end of file diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiversController.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiversController.cs index ea0f32c..524b0df 100644 --- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiversController.cs +++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiversController.cs @@ -435,15 +435,42 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers Assert.Equal(HttpStatusCode.OK, response.StatusCode); var feedbacksCursor = await _mongoDb.FeedbacksCollection.FindAsync(FilterDefinition.Empty); var feedbacks = await feedbacksCursor.ToListAsync(); - + Assert.Equal("ok", feedbacks.ElementAt(0).Description); Assert.Equal(4u, feedbacks.ElementAt(0).Rating); Assert.Equal(feedbackReceiver.Id, feedbacks.ElementAt(0).FeedbackReceiverId); - + var tokensCursor = await _mongoDb.TokensCollection.FindAsync(FilterDefinition.Empty); var tokens = await tokensCursor.ToListAsync(); - + Assert.NotNull(tokens.ElementAt(0).TimeUsed); } + + [Theory, AutoData] + public async Task Test_GetFeedbacks(IEnumerable 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>(await response.Content.ReadAsStringAsync()); + Assert.Equal(selectedFeedbacksSeed,feedbacksResponse); + } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/DataTransferObjects/ListFeedbacksFiltersDto.cs b/Retroactiune.WebAPI/DataTransferObjects/ListFeedbacksFiltersDto.cs index 87a1317..ac39787 100644 --- a/Retroactiune.WebAPI/DataTransferObjects/ListFeedbacksFiltersDto.cs +++ b/Retroactiune.WebAPI/DataTransferObjects/ListFeedbacksFiltersDto.cs @@ -7,7 +7,7 @@ namespace Retroactiune.DataTransferObjects /// public class ListFeedbacksFiltersDto { - public uint Rating { get; set; } + public uint? Rating { get; set; } public DateTime? CreatedAfter { get; set; } public DateTime? CreatedBefore { get; set; } }