aboutsummaryrefslogtreecommitdiff
path: root/src/Data/DevHive.Data.Tests
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
parentf4a70c6430db923af9fa9958a11c2d6612cb52cc (diff)
parenta992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff)
downloadDevHive-0.2.tar
DevHive-0.2.tar.gz
DevHive-0.2.zip
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/Data/DevHive.Data.Tests')
-rw-r--r--src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs168
-rw-r--r--src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj21
-rw-r--r--src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs140
-rw-r--r--src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs133
-rw-r--r--src/Data/DevHive.Data.Tests/PostRepository.Tests.cs142
-rw-r--r--src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs188
-rw-r--r--src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs116
7 files changed, 908 insertions, 0 deletions
diff --git a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs
new file mode 100644
index 0000000..004d418
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs
@@ -0,0 +1,168 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class CommentRepositoryTests
+ {
+ private const string COMMENT_MESSAGE = "Comment message";
+ private DevHiveContext _context;
+ private CommentRepository _commentRepository;
+
+ #region SetUp
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._commentRepository = new CommentRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByIdAsync
+ [Test]
+ public async Task GetByIdAsync_ReturnsTheCorrectComment_IfItExists()
+ {
+ Comment comment = await this.AddEntity();
+
+ Comment resultComment = await this._commentRepository.GetByIdAsync(comment.Id);
+
+ Assert.AreEqual(comment.Id, resultComment.Id, "GetByIdAsync does not return the correct comment when it exists.");
+ }
+
+ [Test]
+ public async Task GetByIdAsync_ReturnsNull_IfCommentDoesNotExist()
+ {
+ Comment resultComment = await this._commentRepository.GetByIdAsync(Guid.Empty);
+
+ Assert.IsNull(resultComment, "GetByIdAsync does not return null when the comment does not exist");
+ }
+ #endregion
+
+ #region GetPostComments
+ [Test]
+ public async Task GetPostComments_ReturnsAllCommentsForPost_IfAnyExist()
+ {
+ List<Comment> comments = new List<Comment>
+ {
+ new Comment(),
+ new Comment(),
+ new Comment()
+ };
+ Post post = new Post
+ {
+ Id = Guid.NewGuid(),
+ Comments = comments
+ };
+
+ this._context.Posts.Add(post);
+ await this._context.SaveChangesAsync();
+
+ List<Comment> resultComments = await this._commentRepository.GetPostComments(post.Id);
+
+ Assert.AreEqual(comments.Count, resultComments.Count, "GetPostComments does not return the comments for a given post correctly");
+ }
+
+ [Test]
+ public async Task GetPostComments_ReturnsEmptyList_WhenPostDoesNotExist()
+ {
+ List<Comment> resultComments = await this._commentRepository.GetPostComments(Guid.Empty);
+
+ Assert.IsEmpty(resultComments, "GetPostComments does not return empty string when post does not exist");
+ }
+ #endregion
+
+ #region GetCommentByIssuerAndTimeCreatedAsync
+ [Test]
+ public async Task GetCommentByCreatorAndTimeCreatedAsync_ReturnsTheCorrectComment_IfItExists()
+ {
+ Comment comment = await this.AddEntity();
+
+ Comment resultComment = await this._commentRepository.GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated);
+
+ Assert.AreEqual(comment.Id, resultComment.Id, "GetCommentByIssuerAndTimeCreatedAsync does not return the corect comment when it exists");
+ }
+
+ [Test]
+ public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist()
+ {
+ Comment resultComment = await this._commentRepository.GetCommentByIssuerAndTimeCreatedAsync(Guid.Empty, DateTime.Now);
+
+ Assert.IsNull(resultComment, "GetCommentByIssuerAndTimeCreatedAsync does not return null when the comment does not exist");
+ }
+ #endregion
+
+ #region EditAsync
+ [Test]
+ public async Task EditAsync_ReturnsTrue_WhenCommentIsUpdatedSuccessfully()
+ {
+ string newMessage = "New message!";
+ Comment comment = await this.AddEntity();
+ Comment updatedComment = new Comment
+ {
+ Id = comment.Id,
+ Message = newMessage
+ };
+
+ bool result = await this._commentRepository.EditAsync(comment.Id, updatedComment);
+
+ Assert.IsTrue(result, "EditAsync does not return true when comment is updated successfully");
+ }
+ #endregion
+
+ #region DoesCommentExist
+ [Test]
+ public async Task DoesCommentExist_ReturnsTrue_WhenTheCommentExists()
+ {
+ Comment comment = await this.AddEntity();
+
+ bool result = await this._commentRepository.DoesCommentExist(comment.Id);
+
+ Assert.IsTrue(result, "DoesCommentExist does not return true whenm the Comment exists");
+ }
+
+ [Test]
+ public async Task DoesCommentExist_ReturnsFalse_WhenTheCommentDoesNotExist()
+ {
+ bool result = await this._commentRepository.DoesCommentExist(Guid.Empty);
+
+ Assert.IsFalse(result, "DoesCommentExist does not return false whenm the Comment" +
+ " does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task<Comment> AddEntity()
+ {
+ User creator = new() { Id = Guid.NewGuid() };
+ Comment comment = new()
+ {
+ Id = Guid.NewGuid(),
+ Message = COMMENT_MESSAGE,
+ Creator = creator,
+ TimeCreated = DateTime.Now
+ };
+
+ this._context.Comments.Add(comment);
+ await this._context.SaveChangesAsync();
+
+ return comment;
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj b/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj
new file mode 100644
index 0000000..e9b33e5
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj
@@ -0,0 +1,21 @@
+<Project Sdk="Microsoft.NET.Sdk">
+ <PropertyGroup>
+ <TargetFramework>net5.0</TargetFramework>
+ <IsPackable>false</IsPackable>
+ </PropertyGroup>
+ <ItemGroup>
+ <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.4"/>
+ <PackageReference Include="Moq" Version="4.16.1"/>
+ <PackageReference Include="NUnit" Version="3.13.1"/>
+ <PackageReference Include="NUnit3TestAdapter" Version="3.17.0"/>
+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1"/>
+ <PackageReference Include="SonarAnalyzer.CSharp" Version="8.20.0.28934"/>
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Data\DevHive.Data.csproj"/>
+ </ItemGroup>
+ <PropertyGroup>
+ <EnableNETAnalyzers>true</EnableNETAnalyzers>
+ <AnalysisLevel>latest</AnalysisLevel>
+ </PropertyGroup>
+</Project> \ No newline at end of file
diff --git a/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs
new file mode 100644
index 0000000..5d66bfc
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs
@@ -0,0 +1,140 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class FeedRepositoryTests
+ {
+ private const int PAGE_NUMBER = 1;
+ private const int PAGE_SIZE = 10;
+
+ private DevHiveContext _context;
+ private FeedRepository _feedRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._feedRepository = new FeedRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetFriendsPosts
+ [Test]
+ public async Task GetFriendsPosts_ReturnsListOfPosts_WhenTheyExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+ dummyUser.Posts = await this.CreateDummyPosts(dummyUser);
+ List<User> friendsList = new()
+ {
+ dummyUser
+ };
+
+ DateTime dateTime = DateTime.Now;
+
+ List<Post> resultList = await this._feedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.GreaterOrEqual(resultList.Count, dummyUser.Posts.Count, "GetFriendsPosts does not return the posts corrtrectly");
+ }
+
+ [Test]
+ public async Task GetFriendsPosts_ReturnsEmptyList_WhenNoSuitablePostsExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+ List<User> friendsList = new()
+ {
+ dummyUser
+ };
+
+ DateTime dateTime = DateTime.Now;
+
+ List<Post> resultList = await this._feedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.LessOrEqual(resultList.Count, 0, "GetFriendsPosts does not return all correct posts");
+ }
+ #endregion
+
+ #region GetUsersPosts
+ [Test]
+ public async Task GetUsersPosts_ReturnsAllPostsOfTheUser_IfAnyExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+ HashSet<Post> posts = await this.CreateDummyPosts(dummyUser);
+
+ DateTime dateTime = DateTime.Now;
+
+ List<Post> resultList = await this._feedRepository.GetUsersPosts(dummyUser, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.GreaterOrEqual(resultList.Count, posts.Count, "GetUsersPosts does not return the posts corrtrectly");
+ }
+
+ [Test]
+ public async Task GetUsersPosts_ReturnsEmptyList_WhenNoSuitablePostsExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+
+ DateTime dateTime = DateTime.Now;
+
+ List<Post> resultList = await this._feedRepository.GetUsersPosts(dummyUser, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.LessOrEqual(resultList.Count, 0, "GetUsersPosts does not return empty list when no suitable posts exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private User CreateDummyUser()
+ {
+ HashSet<Role> roles = new()
+ {
+ new Role()
+ {
+ Id = Guid.NewGuid(),
+ Name = Role.DefaultRole
+ },
+ };
+
+ return new()
+ {
+ Id = Guid.NewGuid(),
+ UserName = "pioneer10",
+ FirstName = "Spas",
+ LastName = "Spasov",
+ Email = "abv@abv.bg",
+ Roles = roles
+ };
+ }
+
+ private async Task<HashSet<Post>> CreateDummyPosts(User user)
+ {
+ HashSet<Post> posts = new HashSet<Post>
+ {
+ new Post { Creator = user, TimeCreated = DateTime.Now },
+ new Post{ Creator = user, TimeCreated = DateTime.Now },
+ new Post{ Creator = user, TimeCreated = DateTime.Now }
+ };
+
+ await this._context.Posts.AddRangeAsync(posts);
+ await this._context.SaveChangesAsync();
+
+ return posts;
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs
new file mode 100644
index 0000000..c7d4dc7
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs
@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using DevHive.Data.Models;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class LenguageRepositoryTests
+ {
+ private const string LANGUAGE_NAME = "Language test name";
+ private DevHiveContext _context;
+ private LanguageRepository _languageRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._languageRepository = new LanguageRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByNameAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheCorrectLanguage_IfItExists()
+ {
+ await this.AddEntity();
+
+ Language language = this._context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault();
+
+ Language languageResult = await this._languageRepository.GetByNameAsync(LANGUAGE_NAME);
+
+ Assert.AreEqual(language.Id, languageResult.Id);
+ }
+
+ [Test]
+ public async Task GetByNameAsync_ReturnsNull_IfTechnologyDoesNotExists()
+ {
+ Language languageResult = await this._languageRepository.GetByNameAsync(LANGUAGE_NAME);
+
+ Assert.IsNull(languageResult);
+ }
+ #endregion
+
+ #region GetLanguages
+ [Test]
+ public async Task GetLanguages_ReturnsAllLanguages()
+ {
+ await this.AddEntity();
+ await this.AddEntity("secondLanguage");
+ await this.AddEntity("thirdLanguage");
+
+ HashSet<Language> languages = this._languageRepository.GetLanguages();
+
+ Assert.GreaterOrEqual(languages.Count, 3, "GetLanguages does not get all Languages");
+ }
+ #endregion
+
+ #region DoesLanguageExistAsync
+ [Test]
+ public async Task DoesLanguageExist_ReturnsTrue_IfIdExists()
+ {
+ await this.AddEntity();
+ Language language = this._context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault();
+
+ Guid id = language.Id;
+
+ bool result = await this._languageRepository.DoesLanguageExistAsync(id);
+
+ Assert.IsTrue(result, "DoesLanguageExistAsync returns flase when language exists");
+ }
+
+ [Test]
+ public async Task DoesLanguageExist_ReturnsFalse_IfIdDoesNotExists()
+ {
+ Guid id = Guid.NewGuid();
+
+ bool result = await this._languageRepository.DoesLanguageExistAsync(id);
+
+ Assert.IsFalse(result, "DoesLanguageExistAsync returns true when language does not exist");
+ }
+ #endregion
+
+ #region DoesTechnologyNameExistAsync
+ [Test]
+ public async Task DoesLanguageNameExist_ReturnsTrue_IfLanguageExists()
+ {
+ await this.AddEntity();
+
+ bool result = await this._languageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME);
+
+ Assert.IsTrue(result, "DoesLanguageNameExists returns true when language name does not exist");
+ }
+
+ [Test]
+ public async Task DoesLanguageNameExist_ReturnsFalse_IfLanguageDoesNotExists()
+ {
+ bool result = await this._languageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME);
+
+ Assert.False(result, "DoesTechnologyNameExistAsync returns true when language name does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task AddEntity(string name = LANGUAGE_NAME)
+ {
+ Language language = new()
+ {
+ Id = Guid.NewGuid(),
+ Name = name
+ };
+
+ await this._context.Languages.AddAsync(language);
+ await this._context.SaveChangesAsync();
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs
new file mode 100644
index 0000000..005769f
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs
@@ -0,0 +1,142 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using DevHive.Data.Models.Relational;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using Moq;
+using NUnit.Framework;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class PostRepositoryTests
+ {
+ private const string POST_MESSAGE = "Post test message";
+ private DevHiveContext _context;
+ private Mock<IUserRepository> _userRepository;
+ private PostRepository _postRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._userRepository = new Mock<IUserRepository>();
+
+ this._postRepository = new PostRepository(this._context, this._userRepository.Object);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region AddNewPostToCreator
+ // [Test]
+ // public async Task AddNewPostToCreator_ReturnsTrue_WhenNewPostIsAddedToCreator()
+ // {
+ // Post post = await this.AddEntity();
+ // User user = new User { Id = Guid.NewGuid() };
+ //
+ // this.UserRepository.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
+ //
+ // bool result = await this.PostRepository.AddNewPostToCreator(user.Id, post);
+ //
+ // Assert.IsTrue(result, "AddNewPostToCreator does not return true when Post Is Added To Creator successfully");
+ // }
+ #endregion
+
+ #region GetByIdAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheCorrectPost_IfItExists()
+ {
+ Post post = await this.AddEntity();
+
+ Post resultTechnology = await this._postRepository.GetByIdAsync(post.Id);
+
+ Assert.AreEqual(post.Id, resultTechnology.Id, "GetByIdAsync does not return the correct post");
+ }
+
+ [Test]
+ public async Task GetByIdAsync_ReturnsNull_IfTechnologyDoesNotExists()
+ {
+ Post resultPost = await this._postRepository.GetByIdAsync(Guid.NewGuid());
+
+ Assert.IsNull(resultPost);
+ }
+ #endregion
+
+ #region GetPostByCreatorAndTimeCreatedAsync
+ [Test]
+ public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsTheCorrectPost_IfItExists()
+ {
+ Post post = await this.AddEntity();
+
+ Post resultPost = await this._postRepository.GetPostByCreatorAndTimeCreatedAsync(post.Creator.Id, post.TimeCreated);
+
+ Assert.AreEqual(post.Id, resultPost.Id, "GetPostByCreatorAndTimeCreatedAsync does not return the corect post when it exists");
+ }
+
+ [Test]
+ public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist()
+ {
+ await this.AddEntity();
+
+ Post resutPost = await this._postRepository.GetPostByCreatorAndTimeCreatedAsync(Guid.Empty, DateTime.Now);
+
+ Assert.IsNull(resutPost, "GetPostByCreatorAndTimeCreatedAsync does not return null when the post does not exist");
+ }
+ #endregion
+
+ #region DoesPostExist
+ [Test]
+ public async Task DoesPostExist_ReturnsTrue_WhenThePostExists()
+ {
+ Post post = await this.AddEntity();
+
+ bool result = await this._postRepository.DoesPostExist(post.Id);
+
+ Assert.IsTrue(result, "DoesPostExist does not return true whenm the Post exists");
+ }
+
+ [Test]
+ public async Task DoesPostExist_ReturnsFalse_WhenThePostDoesNotExist()
+ {
+ bool result = await this._postRepository.DoesPostExist(Guid.Empty);
+
+ Assert.IsFalse(result, "DoesPostExist does not return false whenm the Post does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task<Post> AddEntity()
+ {
+ User creator = new() { Id = Guid.NewGuid() };
+ await this._context.Users.AddAsync(creator);
+ Post post = new()
+ {
+ Message = POST_MESSAGE,
+ Id = Guid.NewGuid(),
+ Creator = creator,
+ TimeCreated = DateTime.Now,
+ Attachments = new List<PostAttachments> { new PostAttachments { FileUrl = "kur" }, new PostAttachments { FileUrl = "za" }, new PostAttachments { FileUrl = "tva" } },
+ Comments = new List<Comment>()
+ };
+
+ await this._context.Posts.AddAsync(post);
+ await this._context.SaveChangesAsync();
+
+ return post;
+ }
+ #endregion
+ }
+}
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
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs
new file mode 100644
index 0000000..d268777
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs
@@ -0,0 +1,116 @@
+using DevHive.Data.Models;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using NUnit.Framework;
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace DevHive.Data.Tests
+{
+ [TestFixture]
+ public class TechnologyRepositoryTests
+ {
+ private const string TECHNOLOGY_NAME = "Technology test name";
+ private DevHiveContext _context;
+ private TechnologyRepository _technologyRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this._context = new DevHiveContext(optionsBuilder.Options);
+
+ this._technologyRepository = new TechnologyRepository(this._context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this._context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByNameAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheCorrectTechnology_IfItExists()
+ {
+ await this.AddEntity();
+
+ Technology technology = this._context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault();
+
+ Technology resultTechnology = await this._technologyRepository.GetByNameAsync(TECHNOLOGY_NAME);
+
+ Assert.AreEqual(technology.Id, resultTechnology.Id);
+ }
+
+ [Test]
+ public async Task GetByNameAsync_ReturnsNull_IfTechnologyDoesNotExists()
+ {
+ Technology resultTechnology = await this._technologyRepository.GetByNameAsync(TECHNOLOGY_NAME);
+
+ Assert.IsNull(resultTechnology);
+ }
+ #endregion
+
+ #region DoesTechnologyExistAsync
+ [Test]
+ public async Task DoesTechnologyExist_ReturnsTrue_IfIdExists()
+ {
+ await this.AddEntity();
+ Technology technology = this._context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault();
+ Guid id = technology.Id;
+
+ bool result = await this._technologyRepository.DoesTechnologyExistAsync(id);
+
+ Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists");
+ }
+
+ [Test]
+ public async Task DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists()
+ {
+ Guid id = Guid.NewGuid();
+
+ bool result = await this._technologyRepository.DoesTechnologyExistAsync(id);
+
+ Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist");
+ }
+ #endregion
+
+ #region DoesTechnologyNameExistAsync
+ [Test]
+ public async Task DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists()
+ {
+ await this.AddEntity();
+
+ bool result = await this._technologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME);
+
+ Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist");
+ }
+
+ [Test]
+ public async Task DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists()
+ {
+ bool result = await this._technologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME);
+
+ Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task AddEntity(string name = TECHNOLOGY_NAME)
+ {
+ Technology technology = new()
+ {
+ Name = name
+ };
+
+ this._context.Technologies.Add(technology);
+ await this._context.SaveChangesAsync();
+ }
+ #endregion
+ }
+}