From e82ec81155b4230123df9706b9590af2dd4ef9eb Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 13 Mar 2021 10:03:54 +0200 Subject: Added tests for rating service --- .../DevHive.Services.Tests/RatingService.Tests.cs | 370 +++++++++++++++++++++ 1 file changed, 370 insertions(+) create mode 100644 src/Services/DevHive.Services.Tests/RatingService.Tests.cs diff --git a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs new file mode 100644 index 0000000..89c1a2e --- /dev/null +++ b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs @@ -0,0 +1,370 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Services.Models.Rating; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class RatingServiceTests + { + private Mock PostRepositoryMock { get; set; } + private Mock RatingRepositoryMock { get; set; } + private Mock UserRepositoryMock { get; set; } + private Mock MapperMock { get; set; } + private RatingService RatingService { get; set; } + + #region SetUps + [SetUp] + public void SetUp() + { + this.PostRepositoryMock = new Mock(); + this.RatingRepositoryMock = new Mock(); + this.UserRepositoryMock = new Mock(); + this.MapperMock = new Mock(); + this.RatingService = new RatingService(this.PostRepositoryMock.Object, this.RatingRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); + } + #endregion + + #region Create + [Test] + public async Task RatePost_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + { + bool isLike = true; + Guid id = Guid.NewGuid(); + Guid postId = Guid.NewGuid(); + Guid userId = Guid.NewGuid(); + CreateRatingServiceModel createRatingServiceModel = new CreateRatingServiceModel + { + PostId = postId, + UserId = userId, + IsLike = isLike + }; + Rating rating = new Rating + { + Id = id, + IsLike = isLike + }; + User user = new User + { + Id = userId + }; + Post post = new Post + { + Id = postId + }; + + this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); + this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); + this.RatingRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(rating); + + Guid result = await this.RatingService.RatePost(createRatingServiceModel); + + Assert.AreEqual(id, result); + } + + [Test] + public async Task RatePost_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + { + bool isLike = true; + Guid id = Guid.NewGuid(); + Guid postId = Guid.NewGuid(); + Guid userId = Guid.NewGuid(); + CreateRatingServiceModel createRatingServiceModel = new CreateRatingServiceModel + { + PostId = postId, + UserId = userId, + IsLike = isLike + }; + Rating rating = new Rating + { + Id = id, + IsLike = isLike + }; + User user = new User + { + Id = userId + }; + Post post = new Post + { + Id = postId + }; + + this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); + this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); + this.RatingRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(rating); + + Guid result = await this.RatingService.RatePost(createRatingServiceModel); + + Assert.AreEqual(result, Guid.Empty); + } + #endregion + + #region Read + [Test] + public async Task GetRatingById_ReturnsTheRating_WhenItExists() + { + Guid id = Guid.NewGuid(); + bool isLike = true; + User user = new User + { + Id = Guid.NewGuid() + }; + Rating rating = new Rating + { + Id = id, + IsLike = isLike, + User = user + }; + ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel + { + Id = id, + IsLike = isLike + }; + + this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(rating)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + + ReadRatingServiceModel result = await this.RatingService.GetRatingById(id); + + Assert.AreEqual(isLike, result.IsLike); + } + + [Test] + public void GetRatingById_ThrowsException_WhenRatingDoesNotExist() + { + string exceptionMessage = "The rating does not exist"; + this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + + Exception ex = Assert.ThrowsAsync(() => this.RatingService.GetRatingById(Guid.Empty)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + + [Test] + public async Task GetRatingByPostAndUser_ReturnsTheRating_WhenItExists() + { + Guid id = Guid.NewGuid(); + bool isLike = true; + User user = new User + { + Id = Guid.NewGuid() + }; + Rating rating = new Rating + { + Id = id, + IsLike = isLike, + User = user + }; + ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel + { + Id = id, + IsLike = isLike + }; + + this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + + ReadRatingServiceModel result = await this.RatingService.GetRatingByPostAndUser(user.Id, Guid.Empty); + + Assert.AreEqual(isLike, result.IsLike); + } + + [Test] + public void GetRatingByPostAndUser_ThrowsException_WhenRatingDoesNotExist() + { + string exceptionMessage = "The rating does not exist"; + this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + + Exception ex = Assert.ThrowsAsync(() => this.RatingService.GetRatingById(Guid.Empty)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region Update + [Test] + public async Task UpdateRating_ReturnsObject_WhenRatingIsUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + bool isLike = true; + UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel + { + Id = id, + IsLike = isLike + }; + User user = new User + { + Id = Guid.NewGuid() + }; + Rating rating = new Rating + { + Id = id, + IsLike = isLike, + User = user + }; + ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel + { + Id = id, + IsLike = isLike + }; + + this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); + this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + this.RatingRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + + ReadRatingServiceModel result = await this.RatingService.UpdateRating(updateRatingServiceModel); + + Assert.AreEqual(result, readRatingServiceModel); + } + + [Test] + public async Task UpdateRating_ReturnsNull_WhenRatingIsNotUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + bool isLike = true; + UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel + { + Id = id, + IsLike = isLike + }; + User user = new User + { + Id = Guid.NewGuid() + }; + Rating rating = new Rating + { + Id = id, + IsLike = isLike, + User = user + }; + ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel + { + Id = id, + IsLike = isLike + }; + + this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); + this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + this.RatingRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + + ReadRatingServiceModel result = await this.RatingService.UpdateRating(updateRatingServiceModel); + + Assert.IsNull(result); + } + + [Test] + public void UpdateRating_ThrowsException_WhenRatingDoesNotExists() + { + string exceptionMessage = "Rating does not exist!"; + UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel + { + Id = Guid.Empty, + IsLike = true + }; + + this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + + Exception ex = Assert.ThrowsAsync(() => this.RatingService.UpdateRating(updateRatingServiceModel)); + + Assert.AreEqual(ex.Message, exceptionMessage); + } + + [Test] + public void UpdateRating_ThrowsException_WhenUserHasNotRatedPost() + { + string exceptionMessage = "User has not rated the post!"; + Guid id = Guid.NewGuid(); + bool isLike = true; + UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel + { + Id = id, + IsLike = isLike + }; + User user = new User + { + Id = Guid.NewGuid() + }; + Rating rating = new Rating + { + Id = id, + IsLike = isLike, + User = user + }; + + this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); + this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + + Exception ex = Assert.ThrowsAsync(() => this.RatingService.UpdateRating(updateRatingServiceModel)); + + Assert.AreEqual(ex.Message, exceptionMessage); + } + #endregion + + #region Delete + [Test] + public async Task DeleteRating_ReturnsTrue_WhenRatingIsDeletedSuccessfully() + { + Guid ratingId = Guid.NewGuid(); + Rating rating = new Rating + { + Id = ratingId + }; + + this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(true)); + this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + this.RatingRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(true)); + + bool result = await this.RatingService.DeleteRating(ratingId); + + Assert.IsTrue(result); + } + + [Test] + public async Task DeleteRating_ReturnsFalse_WhenRatingIsNotDeletedSuccessfully() + { + Guid ratingId = Guid.NewGuid(); + Rating rating = new Rating + { + Id = ratingId + }; + + this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(true)); + this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + this.RatingRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(false)); + + bool result = await this.RatingService.DeleteRating(ratingId); + + Assert.IsFalse(result); + } + + [Test] + public void DeleteRating_ThrowsException_WhenRatingDoesNotExist() + { + string exceptionMessage = "Rating does not exist!"; + + this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(false)); + + Exception ex = Assert.ThrowsAsync(() => this.RatingService.DeleteRating(Guid.Empty)); + + Assert.AreEqual(ex.Message, exceptionMessage); + } + #endregion + } +} -- cgit v1.2.3