aboutsummaryrefslogtreecommitdiff
path: root/src/Data/DevHive.Data.Tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/DevHive.Data.Tests')
-rw-r--r--src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs99
-rw-r--r--src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj29
-rw-r--r--src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs119
-rw-r--r--src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs116
-rw-r--r--src/Data/DevHive.Data.Tests/PostRepository.Tests.cs145
-rw-r--r--src/Data/DevHive.Data.Tests/RoleRepository.Tests.cs119
-rw-r--r--src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs118
-rw-r--r--src/Data/DevHive.Data.Tests/UserRepositoryTests.cs350
8 files changed, 1095 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..9cbb43b
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs
@@ -0,0 +1,99 @@
+using System;
+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";
+
+ protected DevHiveContext Context { get; set; }
+
+ protected CommentRepository CommentRepository { get; set; }
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this.Context = new DevHiveContext(optionsBuilder.Options);
+
+ CommentRepository = new CommentRepository(Context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this.Context.Database.EnsureDeleted();
+ }
+ #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 comment = await this.AddEntity();
+
+ 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 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(string name = COMMENT_MESSAGE)
+ {
+ User creator = new User { Id = Guid.NewGuid() };
+ Comment comment = new Comment
+ {
+ 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..42a3273
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/DevHive.Data.Tests.csproj
@@ -0,0 +1,29 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net5.0</TargetFramework>
+
+ <IsPackable>false</IsPackable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.1" />
+ <PackageReference Include="Moq" Version="4.16.0" />
+ <PackageReference Include="NUnit" Version="3.13.0" />
+ <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Data\DevHive.Data.csproj" />
+
+ <ProjectReference Include="..\DevHive.Data.Models\DevHive.Data.Models.csproj" />
+
+ <ProjectReference Include="..\..\Common\DevHive.Common.Models\DevHive.Common.csproj" />
+ </ItemGroup>
+
+ <PropertyGroup>
+ <EnableNETAnalyzers>true</EnableNETAnalyzers>
+ <AnalysisLevel>latest</AnalysisLevel>
+ </PropertyGroup>
+</Project>
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..f134bf3
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs
@@ -0,0 +1,119 @@
+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
+ {
+ protected DevHiveContext Context { get; set; }
+
+ protected FeedRepository FeedRepository { get; set; }
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this.Context = new DevHiveContext(optionsBuilder.Options);
+
+ FeedRepository = new FeedRepository(Context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this.Context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetFriendsPosts
+ [Test]
+ public async Task GetFriendsPosts_ReturnsListOfPosts_WhenTheyExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+ List<User> friendsList = new List<User>();
+ friendsList.Add(dummyUser);
+
+ DateTime dateTime = new DateTime(3000, 05, 09, 9, 15, 0);
+ Console.WriteLine(dateTime.ToFileTime());
+
+ Post dummyPost = this.CreateDummyPost(dummyUser);
+ Post anotherDummnyPost = this.CreateDummyPost(dummyUser);
+
+ const int PAGE_NUMBER = 1;
+ const int PAGE_SIZE = 10;
+
+ List<Post> resultList = await this.FeedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.GreaterOrEqual(2, resultList.Count, "GetFriendsPosts does not return all correct posts");
+ }
+
+ [Test]
+ public async Task GetFriendsPosts_ReturnsNull_WhenNoSuitablePostsExist()
+ {
+ User dummyUser = this.CreateDummyUser();
+ List<User> friendsList = new List<User>();
+ friendsList.Add(dummyUser);
+
+ DateTime dateTime = new DateTime(3000, 05, 09, 9, 15, 0);
+
+ const int PAGE_NUMBER = 1;
+ const int PAGE_SIZE = 10;
+
+ List<Post> resultList = await this.FeedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE);
+
+ Assert.LessOrEqual(0, resultList.Count, "GetFriendsPosts does not return all correct posts");
+ }
+ #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 Post CreateDummyPost(User poster)
+ {
+ const string POST_MESSAGE = "random message";
+ Guid id = Guid.NewGuid();
+ Post post = new Post
+ {
+ Id = id,
+ Message = POST_MESSAGE,
+ Creator = poster,
+ TimeCreated = new DateTime(2000, 05, 09, 9, 15, 0)
+ };
+
+ this.Context.Posts.Add(post);
+ this.Context.SaveChanges();
+
+ return post;
+ }
+ #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..f02a1e4
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs
@@ -0,0 +1,116 @@
+using System;
+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";
+ protected DevHiveContext Context { get; set; }
+ protected LanguageRepository LanguageRepository { get; set; }
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this.Context = new DevHiveContext(optionsBuilder.Options);
+
+ LanguageRepository = new LanguageRepository(Context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this.Context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByNameAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheCorrectLanguage_IfItExists()
+ {
+ await AddEntity();
+
+ Language language = this.Context.Languages.Where(x => x.Name == LANGUAGE_NAME).ToList().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 DoesLanguageExistAsync
+ [Test]
+ public async Task DoesLanguageExist_ReturnsTrue_IfIdExists()
+ {
+ await AddEntity();
+ Language language = this.Context.Languages.Where(x => x.Name == LANGUAGE_NAME).ToList().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 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 Language
+ {
+ Name = name
+ };
+
+ await this.LanguageRepository.AddAsync(language);
+ }
+ #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..6dacf0b
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs
@@ -0,0 +1,145 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Data.RelationModels;
+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 { get; set; }
+
+ private Mock<IUserRepository> UserRepository { get; set; }
+
+ private PostRepository PostRepository { get; set; }
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this.Context = new DevHiveContext(optionsBuilder.Options);
+
+ this.UserRepository = new Mock<IUserRepository>();
+
+ PostRepository = new PostRepository(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 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()
+ {
+ Post post = 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(string name = POST_MESSAGE)
+ {
+ User creator = new User { Id = Guid.NewGuid() };
+ await this.Context.Users.AddAsync(creator);
+ Post post = new Post
+ {
+ 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/RoleRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RoleRepository.Tests.cs
new file mode 100644
index 0000000..7f62c24
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/RoleRepository.Tests.cs
@@ -0,0 +1,119 @@
+using System;
+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 RoleRepositoryTests
+ {
+ private const string ROLE_NAME = "Role test name";
+
+ protected DevHiveContext Context { get; set; }
+
+ protected RoleRepository RoleRepository { get; set; }
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this.Context = new DevHiveContext(optionsBuilder.Options);
+
+ RoleRepository = new RoleRepository(Context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this.Context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByNameAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheRole_WhenItExists()
+ {
+ Role role = await this.AddEntity();
+
+ Role resultRole = await this.RoleRepository.GetByNameAsync(role.Name);
+
+ Assert.AreEqual(role.Id, resultRole.Id, "GetByNameAsync does not return the correct role");
+ }
+
+ [Test]
+ public async Task GetByNameAsync_ReturnsNull_WhenTheRoleDoesNotExist()
+ {
+ Role resultRole = await this.RoleRepository.GetByNameAsync(ROLE_NAME);
+
+ Assert.IsNull(resultRole, "GetByNameAsync does not return when the role does not exist");
+ }
+ #endregion
+
+ #region DoesNameExist
+ [Test]
+ public async Task DoesNameExist_ReturnsTrue_WhenTheNameExists()
+ {
+ Role role = await this.AddEntity();
+
+ bool result = await this.RoleRepository.DoesNameExist(role.Name);
+
+ Assert.IsTrue(result, "DoesNameExist returns false when the role name exist");
+ }
+
+ [Test]
+ public async Task DoesNameExist_ReturnsFalse_WhenTheNameDoesNotExist()
+ {
+ bool result = await this.RoleRepository.DoesNameExist(ROLE_NAME);
+
+ Assert.IsFalse(result, "DoesNameExist returns false when the role name exist");
+ }
+ #endregion
+
+ #region DoesRoleExist
+ [Test]
+ public async Task DoesRoleExist_ReturnsTrue_IfIdExists()
+ {
+ await AddEntity();
+ Role role = this.Context.Roles.Where(x => x.Name == ROLE_NAME).ToList().FirstOrDefault();
+ Guid id = role.Id;
+
+ bool result = await this.RoleRepository.DoesRoleExist(id);
+
+ Assert.IsTrue(result, "DoesRoleExistAsync returns flase when role exists");
+ }
+
+ [Test]
+ public async Task DoesRoleExist_ReturnsFalse_IfIdDoesNotExists()
+ {
+ Guid id = Guid.NewGuid();
+
+ bool result = await this.RoleRepository.DoesRoleExist(id);
+
+ Assert.IsFalse(result, "DoesRoleExist returns true when role does not exist");
+ }
+ #endregion
+
+ #region HelperMethods
+ private async Task<Role> AddEntity(string name = ROLE_NAME)
+ {
+ Role role = new Role
+ {
+ Id = Guid.NewGuid(),
+ Name = name
+ };
+
+ this.Context.Roles.Add(role);
+ await this.Context.SaveChangesAsync();
+
+ return role;
+ }
+ #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..d25fd3b
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs
@@ -0,0 +1,118 @@
+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";
+
+ protected DevHiveContext Context { get; set; }
+
+ protected TechnologyRepository TechnologyRepository { get; set; }
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase(databaseName: "DevHive_Test_Database");
+
+ this.Context = new DevHiveContext(optionsBuilder.Options);
+
+ TechnologyRepository = new TechnologyRepository(Context);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this.Context.Database.EnsureDeleted();
+ }
+ #endregion
+
+ #region GetByNameAsync
+ [Test]
+ public async Task GetByNameAsync_ReturnsTheCorrectTechnology_IfItExists()
+ {
+ await AddEntity();
+
+ Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().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 AddEntity();
+ Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().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 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 Technology
+ {
+ Name = name
+ };
+
+ this.Context.Technologies.Add(technology);
+ await this.Context.SaveChangesAsync();
+ }
+ #endregion
+ }
+}
diff --git a/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs b/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs
new file mode 100644
index 0000000..43e9a36
--- /dev/null
+++ b/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs
@@ -0,0 +1,350 @@
+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 UserRepositoryTests
+ {
+ private DevHiveContext _context;
+ private UserRepository _userRepository;
+
+ #region Setups
+ [SetUp]
+ public void Setup()
+ {
+ var options = new DbContextOptionsBuilder<DevHiveContext>()
+ .UseInMemoryDatabase("DevHive_UserRepository_Database");
+
+ this._context = new DevHiveContext(options.Options);
+ this._userRepository = new UserRepository(_context);
+ }
+
+ [TearDown]
+ public async Task Teardown()
+ {
+ await this._context.Database.EnsureDeletedAsync();
+ }
+ #endregion
+
+ #region QueryAll
+ // [Test]
+ // public async Task QueryAll_ShouldReturnAllUsersFromDatabase_WhenTheyExist()
+ // {
+ // //Arrange
+ // User dummyUserOne = CreateDummyUser();
+ // User dummyUserTwo = CreateAnotherDummyUser();
+ //
+ // await this._userRepository.AddAsync(dummyUserOne);
+ // await this._userRepository.AddAsync(dummyUserTwo);
+ //
+ // //Act
+ // IEnumerable<User> users = this._userRepository.QueryAll();
+ //
+ // //Assert
+ // Assert.AreEqual(2, users.Count(), "Method doesn't return all instances of user");
+ // }
+
+ // [Test]
+ // public void QueryAll_ReturnsNull_WhenNoUsersExist()
+ // {
+ // IEnumerable<User> users = this._userRepository.QueryAll();
+ //
+ // Assert.AreEqual(0, users.Count(), "Method returns Users when there are non");
+ // }
+ #endregion
+
+ #region EditAsync
+ [Test]
+ public async Task EditAsync_ReturnsTrue_WhenUserIsUpdatedSuccessfully()
+ {
+ User oldUser = this.CreateDummyUser();
+ this._context.Users.Add(oldUser);
+ await this._context.SaveChangesAsync();
+
+ oldUser.UserName = "SuperSecretUserName";
+ bool result = await this._userRepository.EditAsync(oldUser.Id, oldUser);
+
+ Assert.IsTrue(result, "EditAsync does not return true when User is updated successfully");
+ }
+ #endregion
+
+ #region GetByIdAsync
+ [Test]
+ public async Task GetByIdAsync_ReturnsTheUse_WhenItExists()
+ {
+ User dummyUserOne = CreateDummyUser();
+ await this._userRepository.AddAsync(dummyUserOne);
+
+ User resultUser = await this._userRepository.GetByIdAsync(dummyUserOne.Id);
+
+ Assert.AreEqual(dummyUserOne.UserName, resultUser.UserName);
+ }
+
+ [Test]
+ public async Task GetByIdAsync_ReturnsNull_WhenUserDoesNotExist()
+ {
+ Guid id = Guid.NewGuid();
+
+ User resultUser = await this._userRepository.GetByIdAsync(id);
+
+ Assert.IsNull(resultUser);
+ }
+ #endregion
+
+ #region GetByUsernameAsync
+ [Test]
+ public async Task GetByUsernameAsync_ReturnsUserFromDatabase_WhenItExists()
+ {
+ //Arrange
+ User dummyUser = CreateDummyUser();
+ await this._userRepository.AddAsync(dummyUser);
+ string username = dummyUser.UserName;
+
+ //Act
+ User user = await this._userRepository.GetByUsernameAsync(username);
+
+ //Assert
+ Assert.AreEqual(dummyUser.Id, user.Id, "Method doesn't get the proper user from database");
+ }
+
+ [Test]
+ public async Task GetByUsernameAsync_ReturnsNull_WhenUserDoesNotExist()
+ {
+ //Act
+ User user = await this._userRepository.GetByUsernameAsync(null);
+
+ //Assert
+ Assert.IsNull(user, "Method returns user when it does not exist");
+ }
+ #endregion
+
+ #region DoesUserExistAsync
+ [Test]
+ public async Task DoesUserExistAsync_ReturnsTrue_WhenUserExists()
+ {
+ User dummyUser = this.CreateDummyUser();
+ this._context.Users.Add(dummyUser);
+ await this._context.SaveChangesAsync();
+
+ bool result = await this._userRepository.DoesUserExistAsync(dummyUser.Id);
+
+ Assert.IsTrue(result, "DoesUserExistAsync does not return true when user exists");
+ }
+
+ [Test]
+ public async Task DoesUserExistAsync_ReturnsFalse_WhenUserDoesNotExist()
+ {
+ Guid id = Guid.NewGuid();
+
+ bool result = await this._userRepository.DoesUserExistAsync(id);
+
+ Assert.IsFalse(result, "DoesUserExistAsync does not return false when user does not exist");
+ }
+ #endregion
+
+ #region DoesUserNameExistAsync
+ [Test]
+ public async Task DoesUsernameExistAsync_ReturnsTrue_WhenUserWithTheNameExists()
+ {
+ User dummyUser = this.CreateDummyUser();
+ this._context.Users.Add(dummyUser);
+ await this._context.SaveChangesAsync();
+
+ bool result = await this._userRepository.DoesUsernameExistAsync(dummyUser.UserName);
+
+ Assert.IsTrue(result, "DoesUserNameExistAsync does not return true when username exists");
+ }
+
+ [Test]
+ public async Task DoesUsernameExistAsync_ReturnsFalse_WhenUserWithTheNameDoesNotExist()
+ {
+ string userName = "Fake name";
+
+ bool result = await this._userRepository.DoesUsernameExistAsync(userName);
+
+ Assert.IsFalse(result, "DoesUserNameExistAsync does not return false when username does not exist");
+ }
+ #endregion
+
+ #region DoesEmailExistAsync
+ [Test]
+ public async Task DoesEmailExistAsync_ReturnsTrue_WhenUserWithTheEmailExists()
+ {
+ User dummyUser = this.CreateDummyUser();
+ this._context.Users.Add(dummyUser);
+ await this._context.SaveChangesAsync();
+
+ bool result = await this._userRepository.DoesEmailExistAsync(dummyUser.Email);
+
+ Assert.IsTrue(result, "DoesUserNameExistAsync does not return true when email exists");
+ }
+
+ [Test]
+ public async Task DoesEmailExistAsync_ReturnsFalse_WhenUserWithTheEmailDoesNotExist()
+ {
+ string email = "Fake email";
+
+ bool result = await this._userRepository.DoesUsernameExistAsync(email);
+
+ Assert.IsFalse(result, "DoesUserNameExistAsync does not return false when email does not exist");
+ }
+ #endregion
+
+ #region DoesUserHaveThisFriendAsync
+ //[Test]
+ //public async Task DoesUserHaveThisFriendAsync_ReturnsTrue_WhenUserHasTheGivenFriend()
+ //{
+ // User dummyUser = this.CreateDummyUser();
+ // User anotherDummyUser = this.CreateAnotherDummyUser();
+ // HashSet<User> friends = new HashSet<User>
+ // {
+ // anotherDummyUser
+ // };
+ // dummyUser.Friends = friends;
+
+ // this._context.Users.Add(dummyUser);
+ // this._context.Users.Add(anotherDummyUser);
+ // await this._context.SaveChangesAsync();
+
+ // bool result = await this._userRepository.DoesUserHaveThisFriendAsync(dummyUser.Id, anotherDummyUser.Id);
+
+ // Assert.IsTrue(result, "DoesUserHaveThisFriendAsync does not return true when user has the given friend");
+ //}
+
+ // [Test]
+ // public async Task DoesUserHaveThisFriendAsync_ReturnsFalse_WhenUserDoesNotHaveTheGivenFriend()
+ // {
+ // User dummyUser = this.CreateDummyUser();
+ // User anotherDummyUser = this.CreateAnotherDummyUser();
+ //
+ // this._context.Users.Add(dummyUser);
+ // this._context.Users.Add(anotherDummyUser);
+ // await this._context.SaveChangesAsync();
+ //
+ // bool result = await this._userRepository.DoesUserHaveThisFriendAsync(dummyUser.Id, anotherDummyUser.Id);
+ //
+ // Assert.IsFalse(result, "DoesUserHaveThisFriendAsync does not return false when user des not have the given friend");
+ // }
+ #endregion
+
+ #region DoesUserHaveThisUsername
+ [Test]
+ public async Task DoesUserHaveThisUsername_ReturnsTrue_WhenUserHasTheGivenUsername()
+ {
+ User dummyUser = this.CreateDummyUser();
+ this._context.Users.Add(dummyUser);
+ await this._context.SaveChangesAsync();
+
+ bool result = this._userRepository.DoesUserHaveThisUsername(dummyUser.Id, dummyUser.UserName);
+
+ Assert.IsTrue(result, "DoesUserHaveThisUsername does not return true when the user has the given name");
+ }
+
+ [Test]
+ public async Task DoesUserHaveThisUsername_ReturnsFalse_WhenUserDoesntHaveTheGivenUsername()
+ {
+ string username = "Fake username";
+ User dummyUser = this.CreateDummyUser();
+ this._context.Users.Add(dummyUser);
+ await this._context.SaveChangesAsync();
+
+ bool result = this._userRepository.DoesUserHaveThisUsername(dummyUser.Id, username);
+
+ Assert.IsFalse(result, "DoesUserNameExistAsync does not return false when user doesnt have the given name");
+ }
+ #endregion
+
+ #region HelperMethods
+ private User CreateDummyUser()
+ {
+ HashSet<Language> languages = new()
+ {
+ new Language()
+ {
+ Id = Guid.NewGuid(),
+ Name = "csharp"
+ },
+ };
+
+ HashSet<Technology> technologies = new()
+ {
+ new Technology()
+ {
+ Id = Guid.NewGuid(),
+ Name = "ASP.NET Core"
+ },
+ };
+
+ HashSet<Role> roles = new()
+ {
+ new Role()
+ {
+ Id = Guid.NewGuid(),
+ Name = Role.DefaultRole
+ },
+ };
+
+ return new()
+ {
+ Id = Guid.NewGuid(),
+ UserName = "dummyUser",
+ FirstName = "Spas",
+ LastName = "Spasov",
+ Email = "abv@abv.bg",
+ Languages = languages,
+ Technologies = technologies,
+ Roles = roles
+ };
+ }
+
+ private User CreateAnotherDummyUser()
+ {
+ HashSet<Language> languages = new()
+ {
+ new Language()
+ {
+ Id = Guid.NewGuid(),
+ Name = "typescript"
+ },
+ };
+
+ HashSet<Technology> technologies = new()
+ {
+ new Technology()
+ {
+ Id = Guid.NewGuid(),
+ Name = "Angular"
+ },
+ };
+
+ HashSet<Role> roles = new()
+ {
+ new Role()
+ {
+ Id = Guid.NewGuid(),
+ Name = Role.DefaultRole
+ },
+ };
+
+ return new()
+ {
+ Id = Guid.NewGuid(),
+ UserName = "anotherDummyUser",
+ FirstName = "Alex",
+ LastName = "Spiridonov",
+ Email = "a_spiridonov@abv.bg",
+ Languages = languages,
+ Technologies = technologies,
+ Roles = roles
+ };
+ }
+ #endregion
+ }
+}