aboutsummaryrefslogtreecommitdiff
path: root/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs
diff options
context:
space:
mode:
authorKamen Mladenov <kamen.d.mladenov@protonmail.com>2021-04-09 19:51:35 +0300
committerGitHub <noreply@github.com>2021-04-09 19:51:35 +0300
commit233f38915ba0079079233eff55434ef349c05c45 (patch)
tree6c5f69017865bcab87355e910c87339453da1406 /src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs
parentf4a70c6430db923af9fa9958a11c2d6612cb52cc (diff)
parenta992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff)
downloadDevHive-233f38915ba0079079233eff55434ef349c05c45.tar
DevHive-233f38915ba0079079233eff55434ef349c05c45.tar.gz
DevHive-233f38915ba0079079233eff55434ef349c05c45.zip
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs')
-rw-r--r--src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs188
1 files changed, 188 insertions, 0 deletions
diff --git a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs
new file mode 100644
index 0000000..b5299b0
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs
@@ -0,0 +1,188 @@
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using System.Linq;
+using System;
+using System.Collections.Generic;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class RatingRepositoryTests
+ {
+ private DevHiveContext _context;
+ private RatingRepository _ratingRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+ this._ratingRepository = new RatingRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetById
+ [Test]
+ public async Task GetByIdAsync_ReturnsTheCorrectRating_IfItExists()
+ {
+ Guid ratingId = Guid.NewGuid();
+ await AddDummyRating(ratingId);
+
+ Rating ratingResult = await this._ratingRepository.GetByIdAsync(ratingId);
+
+ Assert.AreEqual(ratingResult.Id, ratingId);
+ }
+
+ [Test]
+ public async Task GetByIdAsync_ReturnsNull_IfRatingDoesNotExist()
+ {
+ Rating ratingResult = await this._ratingRepository.GetByIdAsync(Guid.NewGuid());
+
+ Assert.IsNull(ratingResult);
+ }
+ #endregion
+
+ #region GetByPostId
+ [Test]
+ public async Task GetRatingsByPostId_ReturnsFilledListOfRatings_WhenTheyExist()
+ {
+ Guid postId = Guid.NewGuid();
+ await AddDummyPost(postId);
+ await AddDummyRating(Guid.NewGuid(), postId);
+ await AddDummyRating(Guid.NewGuid(), postId);
+
+ List<Rating> result = await this._ratingRepository.GetRatingsByPostId(postId);
+
+ Assert.IsNotEmpty(result);
+ }
+
+ [Test]
+ public async Task GetRatingsByPostId_ReturnsEmptyList_WhenThereAreNoRatings()
+ {
+ List<Rating> result = await this._ratingRepository.GetRatingsByPostId(Guid.NewGuid());
+
+ Assert.IsEmpty(result);
+ }
+ #endregion
+
+ #region GetByUserAndPostId
+ [Test]
+ public async Task GetRatingByUserAndPostId_ReturnsRating_WhenItExists()
+ {
+ Guid ratingId = Guid.NewGuid();
+ Guid postId = Guid.NewGuid();
+ Guid userId = Guid.NewGuid();
+ await AddDummyPost(postId);
+ await AddDummyUser(userId);
+ await AddDummyRating(ratingId, postId, userId);
+
+ Rating result = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId);
+
+ Assert.AreEqual(result.Id, ratingId);
+ }
+
+ [Test]
+ public async Task GetRatingByUserAndPostId_ReturnsNull_WhenRatingDoesNotExist()
+ {
+ Rating result = await this._ratingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid());
+
+ Assert.IsNull(result);
+ }
+ #endregion
+
+ #region UserRatedPost
+ [Test]
+ public async Task UserRatedPost_ReturnsTrue_WhenUserHasRatedPost()
+ {
+ Guid postId = Guid.NewGuid();
+ Guid userId = Guid.NewGuid();
+ await AddDummyPost(postId);
+ await AddDummyUser(userId);
+ await AddDummyRating(Guid.NewGuid(), postId, userId);
+
+ bool result = await this._ratingRepository.UserRatedPost(userId, postId);
+
+ Assert.IsTrue(result);
+ }
+
+ [Test]
+ public async Task UserRatedPost_ReturnsFalse_WhenUserHasNotRatedPost()
+ {
+ bool result = await this._ratingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid());
+
+ Assert.IsFalse(result);
+ }
+ #endregion
+
+ #region DoesRatingExist
+ [Test]
+ public async Task DoesRatingExist_ReturnsTrue_WhenItExists()
+ {
+ Guid ratingId = Guid.NewGuid();
+ await AddDummyRating(ratingId);
+
+ bool result = await this._ratingRepository.DoesRatingExist(ratingId);
+
+ Assert.IsTrue(result);
+ }
+
+ [Test]
+ public async Task DoesRatingExist_ReturnsFalse_WhenRatingDoesNotExist()
+ {
+ bool result = await this._ratingRepository.DoesRatingExist(Guid.NewGuid());
+
+ Assert.IsFalse(result);
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task AddDummyRating(Guid ratingId, Guid postId = default(Guid), Guid userId = default(Guid))
+ {
+ Rating rating = new Rating
+ {
+ Id = ratingId,
+ Post = this._context.Posts.FirstOrDefault(x => x.Id == postId),
+ User = this._context.Users.FirstOrDefault(x => x.Id == userId)
+ };
+
+ await this._context.Rating.AddAsync(rating);
+ await this._context.SaveChangesAsync();
+ }
+
+ private async Task AddDummyPost(Guid postId)
+ {
+ Post post = new Post()
+ {
+ Id = postId,
+ Message = "Never gonna give you up"
+ };
+
+ await this._context.Posts.AddAsync(post);
+ await this._context.SaveChangesAsync();
+ }
+
+ private async Task AddDummyUser(Guid userId)
+ {
+ User user = new User()
+ {
+ Id = userId
+ };
+
+ await this._context.Users.AddAsync(user);
+ await this._context.SaveChangesAsync();
+ }
+ #endregion
+ }
+}