From 98e17766b203734a1817eed94338e2d25f4395f7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 13 Feb 2021 16:20:18 +0200 Subject: Project Restructure P.1 --- .../DevHive.Data.Tests/CommentRepository.Tests.cs | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs (limited to 'src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs') 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() + .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 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 + } +} -- cgit v1.2.3 From 503a23c04355624b133161c9356b139f2e4500f6 Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 22 Feb 2021 21:51:41 +0200 Subject: Code cleanup of UserTests --- src/.editorconfig | 8 +- .../DevHive.Data.Tests/CommentRepository.Tests.cs | 18 +- .../DevHive.Data.Tests/FeedRepository.Tests.cs | 49 +- .../DevHive.Data.Tests/LenguageRepository.Tests.cs | 20 +- .../DevHive.Data.Tests/PostRepository.Tests.cs | 30 +- .../DevHive.Data.Tests/RoleRepository.Tests.cs | 39 +- .../TechnologyRepository.Tests.cs | 22 +- src/Data/DevHive.Data.Tests/UserRepositoryTests.cs | 183 +--- .../Migrations/20210216152915_Inital.cs | 1145 ++++++++++---------- .../DevHive.Services.Tests/CommentService.Tests.cs | 100 +- .../DevHive.Services.Tests/UserService.Tests.cs | 393 ++++--- 11 files changed, 980 insertions(+), 1027 deletions(-) (limited to 'src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs') diff --git a/src/.editorconfig b/src/.editorconfig index 7fa9b2a..757e49d 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -45,10 +45,10 @@ dotnet_diagnostic.IDE0055.severity = warning dotnet_sort_system_directives_first = true dotnet_separate_import_directive_groups = false # Avoid "this." and "Me." if not necessary -dotnet_style_qualification_for_field = false:refactoring -dotnet_style_qualification_for_property = false:refactoring -dotnet_style_qualification_for_method = false:refactoring -dotnet_style_qualification_for_event = false:refactoring +dotnet_style_qualification_for_field = true:refactoring +dotnet_style_qualification_for_property = true:refactoring +dotnet_style_qualification_for_method = true:refactoring +dotnet_style_qualification_for_event = true:refactoring # Use language keywords instead of framework type names for type references dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion diff --git a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs index 9cbb43b..1fd8ad0 100644 --- a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs @@ -20,18 +20,18 @@ namespace DevHive.Data.Tests [SetUp] public void Setup() { - var optionsBuilder = new DbContextOptionsBuilder() + DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); this.Context = new DevHiveContext(optionsBuilder.Options); - CommentRepository = new CommentRepository(Context); + this.CommentRepository = new CommentRepository(this.Context); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + _ = this.Context.Database.EnsureDeleted(); } #endregion @@ -49,7 +49,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist() { - Comment comment = await this.AddEntity(); + _ = await this.AddEntity(); Comment resultComment = await this.CommentRepository.GetCommentByIssuerAndTimeCreatedAsync(Guid.Empty, DateTime.Now); @@ -79,18 +79,18 @@ namespace DevHive.Data.Tests #endregion #region HelperMethods - private async Task AddEntity(string name = COMMENT_MESSAGE) + private async Task AddEntity() { - User creator = new User { Id = Guid.NewGuid() }; - Comment comment = new Comment + User creator = new() { Id = Guid.NewGuid() }; + Comment comment = new() { Message = COMMENT_MESSAGE, Creator = creator, TimeCreated = DateTime.Now }; - this.Context.Comments.Add(comment); - await this.Context.SaveChangesAsync(); + _ = this.Context.Comments.Add(comment); + _ = await this.Context.SaveChangesAsync(); return comment; } diff --git a/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs index f134bf3..eb5bce0 100644 --- a/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs @@ -19,18 +19,18 @@ namespace DevHive.Data.Tests [SetUp] public void Setup() { - var optionsBuilder = new DbContextOptionsBuilder() + DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); this.Context = new DevHiveContext(optionsBuilder.Options); - FeedRepository = new FeedRepository(Context); + this.FeedRepository = new FeedRepository(this.Context); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + _ = this.Context.Database.EnsureDeleted(); } #endregion @@ -38,16 +38,15 @@ namespace DevHive.Data.Tests [Test] public async Task GetFriendsPosts_ReturnsListOfPosts_WhenTheyExist() { - User dummyUser = this.CreateDummyUser(); - List friendsList = new List(); - friendsList.Add(dummyUser); + User dummyUser = CreateDummyUser(); + List friendsList = new() + { + dummyUser + }; - DateTime dateTime = new DateTime(3000, 05, 09, 9, 15, 0); + DateTime dateTime = new(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; @@ -59,11 +58,13 @@ namespace DevHive.Data.Tests [Test] public async Task GetFriendsPosts_ReturnsNull_WhenNoSuitablePostsExist() { - User dummyUser = this.CreateDummyUser(); - List friendsList = new List(); - friendsList.Add(dummyUser); + User dummyUser = CreateDummyUser(); + List friendsList = new() + { + dummyUser + }; - DateTime dateTime = new DateTime(3000, 05, 09, 9, 15, 0); + DateTime dateTime = new(3000, 05, 09, 9, 15, 0); const int PAGE_NUMBER = 1; const int PAGE_SIZE = 10; @@ -75,7 +76,7 @@ namespace DevHive.Data.Tests #endregion #region HelperMethods - private User CreateDummyUser() + private static User CreateDummyUser() { HashSet roles = new() { @@ -96,24 +97,6 @@ namespace DevHive.Data.Tests 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 index f02a1e4..18d5959 100644 --- a/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs @@ -19,18 +19,18 @@ namespace DevHive.Data.Tests [SetUp] public void Setup() { - var optionsBuilder = new DbContextOptionsBuilder() + DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); this.Context = new DevHiveContext(optionsBuilder.Options); - LanguageRepository = new LanguageRepository(Context); + this.LanguageRepository = new LanguageRepository(this.Context); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + _ = this.Context.Database.EnsureDeleted(); } #endregion @@ -38,9 +38,9 @@ namespace DevHive.Data.Tests [Test] public async Task GetByNameAsync_ReturnsTheCorrectLanguage_IfItExists() { - await AddEntity(); + await this.AddEntity(); - Language language = this.Context.Languages.Where(x => x.Name == LANGUAGE_NAME).ToList().FirstOrDefault(); + Language language = this.Context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault(); Language languageResult = await this.LanguageRepository.GetByNameAsync(LANGUAGE_NAME); @@ -60,8 +60,8 @@ namespace DevHive.Data.Tests [Test] public async Task DoesLanguageExist_ReturnsTrue_IfIdExists() { - await AddEntity(); - Language language = this.Context.Languages.Where(x => x.Name == LANGUAGE_NAME).ToList().FirstOrDefault(); + await this.AddEntity(); + Language language = this.Context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault(); Guid id = language.Id; @@ -85,7 +85,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesLanguageNameExist_ReturnsTrue_IfLanguageExists() { - await AddEntity(); + await this.AddEntity(); bool result = await this.LanguageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME); @@ -104,12 +104,12 @@ namespace DevHive.Data.Tests #region HelperMethods private async Task AddEntity(string name = LANGUAGE_NAME) { - Language language = new Language + Language language = new() { Name = name }; - await this.LanguageRepository.AddAsync(language); + _ = 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 index 6a0cccd..c57844e 100644 --- a/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs @@ -11,7 +11,7 @@ using NUnit.Framework; namespace DevHive.Data.Tests { - [TestFixture] + [TestFixture] public class PostRepositoryTests { private const string POST_MESSAGE = "Post test message"; @@ -26,20 +26,20 @@ namespace DevHive.Data.Tests [SetUp] public void Setup() { - var optionsBuilder = new DbContextOptionsBuilder() + DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); this.Context = new DevHiveContext(optionsBuilder.Options); this.UserRepository = new Mock(); - PostRepository = new PostRepository(Context, this.UserRepository.Object); + this.PostRepository = new PostRepository(this.Context, this.UserRepository.Object); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + _ = this.Context.Database.EnsureDeleted(); } #endregion @@ -49,11 +49,11 @@ namespace DevHive.Data.Tests // { // Post post = await this.AddEntity(); // User user = new User { Id = Guid.NewGuid() }; - // + // // this.UserRepository.Setup(p => p.GetByIdAsync(It.IsAny())).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 @@ -62,7 +62,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetByNameAsync_ReturnsTheCorrectPost_IfItExists() { - Post post = await AddEntity(); + Post post = await this.AddEntity(); Post resultTechnology = await this.PostRepository.GetByIdAsync(post.Id); @@ -92,7 +92,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist() { - Post post = await this.AddEntity(); + _ = await this.AddEntity(); Post resutPost = await this.PostRepository.GetPostByCreatorAndTimeCreatedAsync(Guid.Empty, DateTime.Now); @@ -121,11 +121,11 @@ namespace DevHive.Data.Tests #endregion #region HelperMethods - private async Task AddEntity(string name = POST_MESSAGE) + private async Task AddEntity() { - User creator = new User { Id = Guid.NewGuid() }; - await this.Context.Users.AddAsync(creator); - Post post = new Post + User creator = new() { Id = Guid.NewGuid() }; + _ = await this.Context.Users.AddAsync(creator); + Post post = new() { Message = POST_MESSAGE, Id = Guid.NewGuid(), @@ -135,8 +135,8 @@ namespace DevHive.Data.Tests Comments = new List() }; - await this.Context.Posts.AddAsync(post); - await this.Context.SaveChangesAsync(); + _ = await this.Context.Posts.AddAsync(post); + _ = await this.Context.SaveChangesAsync(); return post; } diff --git a/src/Data/DevHive.Data.Tests/RoleRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RoleRepository.Tests.cs index 7f62c24..2e43cf7 100644 --- a/src/Data/DevHive.Data.Tests/RoleRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/RoleRepository.Tests.cs @@ -3,7 +3,9 @@ using System.Linq; using System.Threading.Tasks; using DevHive.Data.Models; using DevHive.Data.Repositories; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; +using Moq; using NUnit.Framework; namespace DevHive.Data.Tests @@ -12,27 +14,26 @@ namespace DevHive.Data.Tests public class RoleRepositoryTests { private const string ROLE_NAME = "Role test name"; - - protected DevHiveContext Context { get; set; } - - protected RoleRepository RoleRepository { get; set; } + private DevHiveContext _context; + private RoleRepository _roleRepository; #region Setups [SetUp] public void Setup() { - var optionsBuilder = new DbContextOptionsBuilder() + DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); + this._context = new DevHiveContext(optionsBuilder.Options); - RoleRepository = new RoleRepository(Context); + Mock> roleManagerMock = new(); + this._roleRepository = new(this._context, roleManagerMock.Object); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + _ = this._context.Database.EnsureDeleted(); } #endregion @@ -42,7 +43,7 @@ namespace DevHive.Data.Tests { Role role = await this.AddEntity(); - Role resultRole = await this.RoleRepository.GetByNameAsync(role.Name); + Role resultRole = await this._roleRepository.GetByNameAsync(role.Name); Assert.AreEqual(role.Id, resultRole.Id, "GetByNameAsync does not return the correct role"); } @@ -50,7 +51,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetByNameAsync_ReturnsNull_WhenTheRoleDoesNotExist() { - Role resultRole = await this.RoleRepository.GetByNameAsync(ROLE_NAME); + Role resultRole = await this._roleRepository.GetByNameAsync(ROLE_NAME); Assert.IsNull(resultRole, "GetByNameAsync does not return when the role does not exist"); } @@ -62,7 +63,7 @@ namespace DevHive.Data.Tests { Role role = await this.AddEntity(); - bool result = await this.RoleRepository.DoesNameExist(role.Name); + bool result = await this._roleRepository.DoesNameExist(role.Name); Assert.IsTrue(result, "DoesNameExist returns false when the role name exist"); } @@ -70,7 +71,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesNameExist_ReturnsFalse_WhenTheNameDoesNotExist() { - bool result = await this.RoleRepository.DoesNameExist(ROLE_NAME); + bool result = await this._roleRepository.DoesNameExist(ROLE_NAME); Assert.IsFalse(result, "DoesNameExist returns false when the role name exist"); } @@ -80,11 +81,11 @@ namespace DevHive.Data.Tests [Test] public async Task DoesRoleExist_ReturnsTrue_IfIdExists() { - await AddEntity(); - Role role = this.Context.Roles.Where(x => x.Name == ROLE_NAME).ToList().FirstOrDefault(); + _ = await this.AddEntity(); + Role role = this._context.Roles.Where(x => x.Name == ROLE_NAME).AsEnumerable().FirstOrDefault(); Guid id = role.Id; - bool result = await this.RoleRepository.DoesRoleExist(id); + bool result = await this._roleRepository.DoesRoleExist(id); Assert.IsTrue(result, "DoesRoleExistAsync returns flase when role exists"); } @@ -94,7 +95,7 @@ namespace DevHive.Data.Tests { Guid id = Guid.NewGuid(); - bool result = await this.RoleRepository.DoesRoleExist(id); + bool result = await this._roleRepository.DoesRoleExist(id); Assert.IsFalse(result, "DoesRoleExist returns true when role does not exist"); } @@ -103,14 +104,14 @@ namespace DevHive.Data.Tests #region HelperMethods private async Task AddEntity(string name = ROLE_NAME) { - Role role = new Role + Role role = new() { Id = Guid.NewGuid(), Name = name }; - this.Context.Roles.Add(role); - await this.Context.SaveChangesAsync(); + _ = this._context.Roles.Add(role); + _ = await this._context.SaveChangesAsync(); return role; } diff --git a/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs index d25fd3b..f7fbacb 100644 --- a/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -21,18 +21,18 @@ namespace DevHive.Data.Tests [SetUp] public void Setup() { - var optionsBuilder = new DbContextOptionsBuilder() + DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); this.Context = new DevHiveContext(optionsBuilder.Options); - TechnologyRepository = new TechnologyRepository(Context); + this.TechnologyRepository = new TechnologyRepository(this.Context); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + _ = this.Context.Database.EnsureDeleted(); } #endregion @@ -40,9 +40,9 @@ namespace DevHive.Data.Tests [Test] public async Task GetByNameAsync_ReturnsTheCorrectTechnology_IfItExists() { - await AddEntity(); + await this.AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault(); Technology resultTechnology = await this.TechnologyRepository.GetByNameAsync(TECHNOLOGY_NAME); @@ -62,8 +62,8 @@ namespace DevHive.Data.Tests [Test] public async Task DoesTechnologyExist_ReturnsTrue_IfIdExists() { - await AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + 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); @@ -86,7 +86,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() { - await AddEntity(); + await this.AddEntity(); bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); @@ -105,13 +105,13 @@ namespace DevHive.Data.Tests #region HelperMethods private async Task AddEntity(string name = TECHNOLOGY_NAME) { - Technology technology = new Technology + Technology technology = new() { Name = name }; - this.Context.Technologies.Add(technology); - await this.Context.SaveChangesAsync(); + _ = 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 index 5f84e34..4ff5ef7 100644 --- a/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; -using System.Linq; +using System.Threading; using System.Threading.Tasks; using DevHive.Data.Models; using DevHive.Data.Repositories; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; +using Moq; using NUnit.Framework; namespace DevHive.Data.Tests @@ -19,54 +21,46 @@ namespace DevHive.Data.Tests [SetUp] public void Setup() { - var options = new DbContextOptionsBuilder() + DbContextOptionsBuilder options = new DbContextOptionsBuilder() .UseInMemoryDatabase("DevHive_UserRepository_Database"); - this._context = new DevHiveContext(options.Options); - this._userRepository = new UserRepository(_context); + + Guid userId = Guid.NewGuid(); + Mock> userStore = new(); + userStore.Setup(x => x.FindByIdAsync(userId.ToString(), CancellationToken.None)) + .ReturnsAsync(new User() + { + Id = userId, + UserName = "test", + }); + Mock> userManagerMock = new(userStore.Object, null, null, null, null, null, null, null, null); + + Guid roleId = Guid.NewGuid(); + Mock> roleStore = new(); + roleStore.Setup(x => x.FindByIdAsync(roleId.ToString(), CancellationToken.None)) + .ReturnsAsync(new Role() + { + Id = roleId, + Name = "test", + }); + Mock> roleManagerMock = new(roleStore.Object, null, null, null, null); + this._userRepository = new(this._context, userManagerMock.Object, roleManagerMock.Object); } [TearDown] public async Task Teardown() { - await this._context.Database.EnsureDeletedAsync(); + _ = 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 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 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(); + User oldUser = CreateDummyUser(); + _ = this._context.Users.Add(oldUser); + _ = await this._context.SaveChangesAsync(); oldUser.UserName = "SuperSecretUserName"; bool result = await this._userRepository.EditAsync(oldUser.Id, oldUser); @@ -80,7 +74,7 @@ namespace DevHive.Data.Tests public async Task GetByIdAsync_ReturnsTheUse_WhenItExists() { User dummyUserOne = CreateDummyUser(); - await this._userRepository.AddAsync(dummyUserOne); + _ = await this._userRepository.AddAsync(dummyUserOne); User resultUser = await this._userRepository.GetByIdAsync(dummyUserOne.Id); @@ -104,7 +98,7 @@ namespace DevHive.Data.Tests { //Arrange User dummyUser = CreateDummyUser(); - await this._userRepository.AddAsync(dummyUser); + _ = await this._userRepository.AddAsync(dummyUser); string username = dummyUser.UserName; //Act @@ -129,9 +123,9 @@ namespace DevHive.Data.Tests [Test] public async Task DoesUserExistAsync_ReturnsTrue_WhenUserExists() { - User dummyUser = this.CreateDummyUser(); - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); + User dummyUser = CreateDummyUser(); + _ = this._context.Users.Add(dummyUser); + _ = await this._context.SaveChangesAsync(); bool result = await this._userRepository.DoesUserExistAsync(dummyUser.Id); @@ -153,9 +147,9 @@ namespace DevHive.Data.Tests [Test] public async Task DoesUsernameExistAsync_ReturnsTrue_WhenUserWithTheNameExists() { - User dummyUser = this.CreateDummyUser(); - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); + User dummyUser = CreateDummyUser(); + _ = this._context.Users.Add(dummyUser); + _ = await this._context.SaveChangesAsync(); bool result = await this._userRepository.DoesUsernameExistAsync(dummyUser.UserName); @@ -177,9 +171,9 @@ namespace DevHive.Data.Tests [Test] public async Task DoesEmailExistAsync_ReturnsTrue_WhenUserWithTheEmailExists() { - User dummyUser = this.CreateDummyUser(); - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); + User dummyUser = CreateDummyUser(); + _ = this._context.Users.Add(dummyUser); + _ = await this._context.SaveChangesAsync(); bool result = await this._userRepository.DoesEmailExistAsync(dummyUser.Email); @@ -197,52 +191,15 @@ namespace DevHive.Data.Tests } #endregion - #region DoesUserHaveThisFriendAsync - //[Test] - //public async Task DoesUserHaveThisFriendAsync_ReturnsTrue_WhenUserHasTheGivenFriend() - //{ - // User dummyUser = this.CreateDummyUser(); - // User anotherDummyUser = this.CreateAnotherDummyUser(); - // HashSet friends = new HashSet - // { - // 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 DoesUserHaveThisUsernameAsync [Test] public async Task DoesUserHaveThisUsername_ReturnsTrue_WhenUserHasTheGivenUsername() { - User dummyUser = this.CreateDummyUser(); - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); + User dummyUser = CreateDummyUser(); + _ = this._context.Users.Add(dummyUser); + _ = await this._context.SaveChangesAsync(); - bool result = this._userRepository.DoesUserHaveThisUsernameAsync(dummyUser.Id, dummyUser.UserName); + bool result = await this._userRepository.DoesUserHaveThisUsernameAsync(dummyUser.Id, dummyUser.UserName); Assert.IsTrue(result, "DoesUserHaveThisUsernameAsync does not return true when the user has the given name"); } @@ -251,18 +208,18 @@ namespace DevHive.Data.Tests public async Task DoesUserHaveThisUsername_ReturnsFalse_WhenUserDoesntHaveTheGivenUsername() { string username = "Fake username"; - User dummyUser = this.CreateDummyUser(); - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); + User dummyUser = CreateDummyUser(); + _ = this._context.Users.Add(dummyUser); + _ = await this._context.SaveChangesAsync(); - bool result = this._userRepository.DoesUserHaveThisUsernameAsync(dummyUser.Id, username); + bool result = await this._userRepository.DoesUserHaveThisUsernameAsync(dummyUser.Id, username); Assert.IsFalse(result, "DoesUserNameExistAsync does not return false when user doesnt have the given name"); } #endregion #region HelperMethods - private User CreateDummyUser() + private static User CreateDummyUser() { HashSet languages = new() { @@ -303,48 +260,6 @@ namespace DevHive.Data.Tests Roles = roles }; } - - private User CreateAnotherDummyUser() - { - HashSet languages = new() - { - new Language() - { - Id = Guid.NewGuid(), - Name = "typescript" - }, - }; - - HashSet technologies = new() - { - new Technology() - { - Id = Guid.NewGuid(), - Name = "Angular" - }, - }; - - HashSet 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 } } diff --git a/src/Data/DevHive.Data/Migrations/20210216152915_Inital.cs b/src/Data/DevHive.Data/Migrations/20210216152915_Inital.cs index 131501e..4c5b982 100644 --- a/src/Data/DevHive.Data/Migrations/20210216152915_Inital.cs +++ b/src/Data/DevHive.Data/Migrations/20210216152915_Inital.cs @@ -1,586 +1,587 @@ -using System; +// +using System; using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DevHive.Data.Migrations { - public partial class Inital : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "AspNetRoles", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - ConcurrencyStamp = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetRoles", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "AspNetUsers", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - FirstName = table.Column(type: "text", nullable: true), - LastName = table.Column(type: "text", nullable: true), - UserId = table.Column(type: "uuid", nullable: true), - UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - EmailConfirmed = table.Column(type: "boolean", nullable: false), - PasswordHash = table.Column(type: "text", nullable: true), - SecurityStamp = table.Column(type: "text", nullable: true), - ConcurrencyStamp = table.Column(type: "text", nullable: true), - PhoneNumber = table.Column(type: "text", nullable: true), - PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), - TwoFactorEnabled = table.Column(type: "boolean", nullable: false), - LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), - LockoutEnabled = table.Column(type: "boolean", nullable: false), - AccessFailedCount = table.Column(type: "integer", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUsers", x => x.Id); - table.ForeignKey( - name: "FK_AspNetUsers_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "Languages", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Languages", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Technologies", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Technologies", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "AspNetRoleClaims", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - RoleId = table.Column(type: "uuid", nullable: false), - ClaimType = table.Column(type: "text", nullable: true), - ClaimValue = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); - table.ForeignKey( - name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", - column: x => x.RoleId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserClaims", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - UserId = table.Column(type: "uuid", nullable: false), - ClaimType = table.Column(type: "text", nullable: true), - ClaimValue = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); - table.ForeignKey( - name: "FK_AspNetUserClaims_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserLogins", - columns: table => new - { - LoginProvider = table.Column(type: "text", nullable: false), - ProviderKey = table.Column(type: "text", nullable: false), - ProviderDisplayName = table.Column(type: "text", nullable: true), - UserId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); - table.ForeignKey( - name: "FK_AspNetUserLogins_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserRoles", - columns: table => new - { - UserId = table.Column(type: "uuid", nullable: false), - RoleId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); - table.ForeignKey( - name: "FK_AspNetUserRoles_AspNetRoles_RoleId", - column: x => x.RoleId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_AspNetUserRoles_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserTokens", - columns: table => new - { - UserId = table.Column(type: "uuid", nullable: false), - LoginProvider = table.Column(type: "text", nullable: false), - Name = table.Column(type: "text", nullable: false), - Value = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); - table.ForeignKey( - name: "FK_AspNetUserTokens_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Posts", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - CreatorId = table.Column(type: "uuid", nullable: true), - Message = table.Column(type: "text", nullable: true), - TimeCreated = table.Column(type: "timestamp without time zone", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Posts", x => x.Id); - table.ForeignKey( - name: "FK_Posts_AspNetUsers_CreatorId", - column: x => x.CreatorId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "ProfilePicture", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - UserId = table.Column(type: "uuid", nullable: false), - PictureURL = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_ProfilePicture", x => x.Id); - table.ForeignKey( - name: "FK_ProfilePicture_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "RoleUser", - columns: table => new - { - RolesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_RoleUser", x => new { x.RolesId, x.UsersId }); - table.ForeignKey( - name: "FK_RoleUser_AspNetRoles_RolesId", - column: x => x.RolesId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_RoleUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "LanguageUser", - columns: table => new - { - LanguagesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_LanguageUser", x => new { x.LanguagesId, x.UsersId }); - table.ForeignKey( - name: "FK_LanguageUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_LanguageUser_Languages_LanguagesId", - column: x => x.LanguagesId, - principalTable: "Languages", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "TechnologyUser", - columns: table => new - { - TechnologiesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_TechnologyUser", x => new { x.TechnologiesId, x.UsersId }); - table.ForeignKey( - name: "FK_TechnologyUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_TechnologyUser_Technologies_TechnologiesId", - column: x => x.TechnologiesId, - principalTable: "Technologies", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Comments", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - PostId = table.Column(type: "uuid", nullable: true), - CreatorId = table.Column(type: "uuid", nullable: true), - Message = table.Column(type: "text", nullable: true), - TimeCreated = table.Column(type: "timestamp without time zone", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Comments", x => x.Id); - table.ForeignKey( - name: "FK_Comments_AspNetUsers_CreatorId", - column: x => x.CreatorId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_Comments_Posts_PostId", - column: x => x.PostId, - principalTable: "Posts", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "PostAttachments", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - PostId = table.Column(type: "uuid", nullable: true), - FileUrl = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_PostAttachments", x => x.Id); - table.ForeignKey( - name: "FK_PostAttachments_Posts_PostId", - column: x => x.PostId, - principalTable: "Posts", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "RatedPosts", - columns: table => new - { - UserId = table.Column(type: "uuid", nullable: false), - PostId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_RatedPosts", x => new { x.UserId, x.PostId }); - table.ForeignKey( - name: "FK_RatedPosts_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_RatedPosts_Posts_PostId", - column: x => x.PostId, - principalTable: "Posts", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Rating", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - PostId = table.Column(type: "uuid", nullable: false), - Rate = table.Column(type: "integer", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Rating", x => x.Id); - table.ForeignKey( - name: "FK_Rating_Posts_PostId", - column: x => x.PostId, - principalTable: "Posts", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "UserRates", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - UserId = table.Column(type: "uuid", nullable: true), - Liked = table.Column(type: "boolean", nullable: false), - PostId = table.Column(type: "uuid", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_UserRates", x => x.Id); - table.ForeignKey( - name: "FK_UserRates_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_UserRates_Posts_PostId", - column: x => x.PostId, - principalTable: "Posts", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateIndex( - name: "IX_AspNetRoleClaims_RoleId", - table: "AspNetRoleClaims", - column: "RoleId"); - - migrationBuilder.CreateIndex( - name: "RoleNameIndex", - table: "AspNetRoles", - column: "NormalizedName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserClaims_UserId", - table: "AspNetUserClaims", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserLogins_UserId", - table: "AspNetUserLogins", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserRoles_RoleId", - table: "AspNetUserRoles", - column: "RoleId"); - - migrationBuilder.CreateIndex( - name: "EmailIndex", - table: "AspNetUsers", - column: "NormalizedEmail"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUsers_UserId", - table: "AspNetUsers", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUsers_UserName", - table: "AspNetUsers", - column: "UserName", - unique: true); - - migrationBuilder.CreateIndex( - name: "UserNameIndex", - table: "AspNetUsers", - column: "NormalizedUserName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_Comments_CreatorId", - table: "Comments", - column: "CreatorId"); - - migrationBuilder.CreateIndex( - name: "IX_Comments_PostId", - table: "Comments", - column: "PostId"); - - migrationBuilder.CreateIndex( - name: "IX_LanguageUser_UsersId", - table: "LanguageUser", - column: "UsersId"); - - migrationBuilder.CreateIndex( - name: "IX_PostAttachments_PostId", - table: "PostAttachments", - column: "PostId"); - - migrationBuilder.CreateIndex( - name: "IX_Posts_CreatorId", - table: "Posts", - column: "CreatorId"); - - migrationBuilder.CreateIndex( - name: "IX_ProfilePicture_UserId", - table: "ProfilePicture", - column: "UserId", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_RatedPosts_PostId", - table: "RatedPosts", - column: "PostId"); - - migrationBuilder.CreateIndex( - name: "IX_Rating_PostId", - table: "Rating", - column: "PostId", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_RoleUser_UsersId", - table: "RoleUser", - column: "UsersId"); - - migrationBuilder.CreateIndex( - name: "IX_TechnologyUser_UsersId", - table: "TechnologyUser", - column: "UsersId"); - - migrationBuilder.CreateIndex( - name: "IX_UserRates_PostId", - table: "UserRates", - column: "PostId"); - - migrationBuilder.CreateIndex( - name: "IX_UserRates_UserId", - table: "UserRates", - column: "UserId"); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "AspNetRoleClaims"); - - migrationBuilder.DropTable( - name: "AspNetUserClaims"); - - migrationBuilder.DropTable( - name: "AspNetUserLogins"); - - migrationBuilder.DropTable( - name: "AspNetUserRoles"); - - migrationBuilder.DropTable( - name: "AspNetUserTokens"); - - migrationBuilder.DropTable( - name: "Comments"); - - migrationBuilder.DropTable( - name: "LanguageUser"); - - migrationBuilder.DropTable( - name: "PostAttachments"); - - migrationBuilder.DropTable( - name: "ProfilePicture"); - - migrationBuilder.DropTable( - name: "RatedPosts"); - - migrationBuilder.DropTable( - name: "Rating"); + public partial class Inital : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AspNetRoles", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetUsers", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + FirstName = table.Column(type: "text", nullable: true), + LastName = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "uuid", nullable: true), + UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + EmailConfirmed = table.Column(type: "boolean", nullable: false), + PasswordHash = table.Column(type: "text", nullable: true), + SecurityStamp = table.Column(type: "text", nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true), + PhoneNumber = table.Column(type: "text", nullable: true), + PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), + TwoFactorEnabled = table.Column(type: "boolean", nullable: false), + LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), + LockoutEnabled = table.Column(type: "boolean", nullable: false), + AccessFailedCount = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUsers", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUsers_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "Languages", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Languages", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Technologies", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Technologies", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetRoleClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RoleId = table.Column(type: "uuid", nullable: false), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + UserId = table.Column(type: "uuid", nullable: false), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUserClaims_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserLogins", + columns: table => new + { + LoginProvider = table.Column(type: "text", nullable: false), + ProviderKey = table.Column(type: "text", nullable: false), + ProviderDisplayName = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); + table.ForeignKey( + name: "FK_AspNetUserLogins_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserRoles", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + RoleId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserTokens", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + LoginProvider = table.Column(type: "text", nullable: false), + Name = table.Column(type: "text", nullable: false), + Value = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); + table.ForeignKey( + name: "FK_AspNetUserTokens_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Posts", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + CreatorId = table.Column(type: "uuid", nullable: true), + Message = table.Column(type: "text", nullable: true), + TimeCreated = table.Column(type: "timestamp without time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Posts", x => x.Id); + table.ForeignKey( + name: "FK_Posts_AspNetUsers_CreatorId", + column: x => x.CreatorId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "ProfilePicture", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + PictureURL = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ProfilePicture", x => x.Id); + table.ForeignKey( + name: "FK_ProfilePicture_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "RoleUser", + columns: table => new + { + RolesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RoleUser", x => new { x.RolesId, x.UsersId }); + table.ForeignKey( + name: "FK_RoleUser_AspNetRoles_RolesId", + column: x => x.RolesId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RoleUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "LanguageUser", + columns: table => new + { + LanguagesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_LanguageUser", x => new { x.LanguagesId, x.UsersId }); + table.ForeignKey( + name: "FK_LanguageUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LanguageUser_Languages_LanguagesId", + column: x => x.LanguagesId, + principalTable: "Languages", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "TechnologyUser", + columns: table => new + { + TechnologiesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TechnologyUser", x => new { x.TechnologiesId, x.UsersId }); + table.ForeignKey( + name: "FK_TechnologyUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_TechnologyUser_Technologies_TechnologiesId", + column: x => x.TechnologiesId, + principalTable: "Technologies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Comments", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: true), + CreatorId = table.Column(type: "uuid", nullable: true), + Message = table.Column(type: "text", nullable: true), + TimeCreated = table.Column(type: "timestamp without time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Comments", x => x.Id); + table.ForeignKey( + name: "FK_Comments_AspNetUsers_CreatorId", + column: x => x.CreatorId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_Comments_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "PostAttachments", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: true), + FileUrl = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_PostAttachments", x => x.Id); + table.ForeignKey( + name: "FK_PostAttachments_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "RatedPosts", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RatedPosts", x => new { x.UserId, x.PostId }); + table.ForeignKey( + name: "FK_RatedPosts_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RatedPosts_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Rating", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: false), + Rate = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Rating", x => x.Id); + table.ForeignKey( + name: "FK_Rating_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "UserRates", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: true), + Liked = table.Column(type: "boolean", nullable: false), + PostId = table.Column(type: "uuid", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_UserRates", x => x.Id); + table.ForeignKey( + name: "FK_UserRates_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_UserRates_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_AspNetRoleClaims_RoleId", + table: "AspNetRoleClaims", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "RoleNameIndex", + table: "AspNetRoles", + column: "NormalizedName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserClaims_UserId", + table: "AspNetUserClaims", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserLogins_UserId", + table: "AspNetUserLogins", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserRoles_RoleId", + table: "AspNetUserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "EmailIndex", + table: "AspNetUsers", + column: "NormalizedEmail"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_UserId", + table: "AspNetUsers", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_UserName", + table: "AspNetUsers", + column: "UserName", + unique: true); + + migrationBuilder.CreateIndex( + name: "UserNameIndex", + table: "AspNetUsers", + column: "NormalizedUserName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Comments_CreatorId", + table: "Comments", + column: "CreatorId"); + + migrationBuilder.CreateIndex( + name: "IX_Comments_PostId", + table: "Comments", + column: "PostId"); + + migrationBuilder.CreateIndex( + name: "IX_LanguageUser_UsersId", + table: "LanguageUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_PostAttachments_PostId", + table: "PostAttachments", + column: "PostId"); + + migrationBuilder.CreateIndex( + name: "IX_Posts_CreatorId", + table: "Posts", + column: "CreatorId"); + + migrationBuilder.CreateIndex( + name: "IX_ProfilePicture_UserId", + table: "ProfilePicture", + column: "UserId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_RatedPosts_PostId", + table: "RatedPosts", + column: "PostId"); + + migrationBuilder.CreateIndex( + name: "IX_Rating_PostId", + table: "Rating", + column: "PostId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_RoleUser_UsersId", + table: "RoleUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_TechnologyUser_UsersId", + table: "TechnologyUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_UserRates_PostId", + table: "UserRates", + column: "PostId"); + + migrationBuilder.CreateIndex( + name: "IX_UserRates_UserId", + table: "UserRates", + column: "UserId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AspNetRoleClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserLogins"); + + migrationBuilder.DropTable( + name: "AspNetUserRoles"); + + migrationBuilder.DropTable( + name: "AspNetUserTokens"); + + migrationBuilder.DropTable( + name: "Comments"); + + migrationBuilder.DropTable( + name: "LanguageUser"); + + migrationBuilder.DropTable( + name: "PostAttachments"); + + migrationBuilder.DropTable( + name: "ProfilePicture"); + + migrationBuilder.DropTable( + name: "RatedPosts"); + + migrationBuilder.DropTable( + name: "Rating"); - migrationBuilder.DropTable( - name: "RoleUser"); + migrationBuilder.DropTable( + name: "RoleUser"); - migrationBuilder.DropTable( - name: "TechnologyUser"); + migrationBuilder.DropTable( + name: "TechnologyUser"); - migrationBuilder.DropTable( - name: "UserRates"); + migrationBuilder.DropTable( + name: "UserRates"); - migrationBuilder.DropTable( - name: "Languages"); + migrationBuilder.DropTable( + name: "Languages"); - migrationBuilder.DropTable( - name: "AspNetRoles"); + migrationBuilder.DropTable( + name: "AspNetRoles"); - migrationBuilder.DropTable( - name: "Technologies"); + migrationBuilder.DropTable( + name: "Technologies"); - migrationBuilder.DropTable( - name: "Posts"); + migrationBuilder.DropTable( + name: "Posts"); - migrationBuilder.DropTable( - name: "AspNetUsers"); - } - } + migrationBuilder.DropTable( + name: "AspNetUsers"); + } + } } diff --git a/src/Services/DevHive.Services.Tests/CommentService.Tests.cs b/src/Services/DevHive.Services.Tests/CommentService.Tests.cs index d511739..d843375 100644 --- a/src/Services/DevHive.Services.Tests/CommentService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/CommentService.Tests.cs @@ -37,22 +37,22 @@ namespace DevHive.Services.Tests public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() { Guid id = Guid.NewGuid(); - User creator = new User { Id = Guid.NewGuid() }; - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + User creator = new() { Id = Guid.NewGuid() }; + CreateCommentServiceModel createCommentServiceModel = new() { Message = MESSAGE }; - Comment comment = new Comment + Comment comment = new() { Message = MESSAGE, Id = id, }; - this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(comment)); - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(creator)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + _ = this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); + _ = this.CommentRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(comment)); + _ = this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); + _ = this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(creator)); + _ = this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); Guid result = await this.CommentService.AddComment(createCommentServiceModel); @@ -62,18 +62,18 @@ namespace DevHive.Services.Tests [Test] public async Task AddComment_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() { - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + CreateCommentServiceModel createCommentServiceModel = new() { Message = MESSAGE }; - Comment comment = new Comment + Comment comment = new() { Message = MESSAGE, }; - this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + _ = this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); + _ = this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); + _ = this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); Guid result = await this.CommentService.AddComment(createCommentServiceModel); @@ -85,7 +85,7 @@ namespace DevHive.Services.Tests { const string EXCEPTION_MESSAGE = "Post does not exist!"; - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + CreateCommentServiceModel createCommentServiceModel = new() { Message = MESSAGE }; @@ -100,25 +100,25 @@ namespace DevHive.Services.Tests [Test] public async Task GetCommentById_ReturnsTheComment_WhenItExists() { - Guid creatorId = new Guid(); - User creator = new User { Id = creatorId }; - Comment comment = new Comment + Guid creatorId = new(); + User creator = new() { Id = creatorId }; + Comment comment = new() { Message = MESSAGE, Creator = creator }; - ReadCommentServiceModel commentServiceModel = new ReadCommentServiceModel + ReadCommentServiceModel commentServiceModel = new() { Message = MESSAGE }; - User user = new User + User user = new() { Id = creatorId, }; - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentServiceModel); + _ = this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + _ = this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + _ = this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentServiceModel); ReadCommentServiceModel result = await this.CommentService.GetCommentById(new Guid()); @@ -129,15 +129,15 @@ namespace DevHive.Services.Tests public void GetCommentById_ThorwsException_WhenTheUserDoesNotExist() { const string EXCEPTION_MESSAGE = "The user does not exist"; - Guid creatorId = new Guid(); - User creator = new User { Id = creatorId }; - Comment comment = new Comment + Guid creatorId = new(); + User creator = new() { Id = creatorId }; + Comment comment = new() { Message = MESSAGE, Creator = creator }; - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + _ = this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); Exception ex = Assert.ThrowsAsync(() => this.CommentService.GetCommentById(new Guid()), "GetCommentById does not throw exception when the user does not exist"); @@ -148,14 +148,14 @@ namespace DevHive.Services.Tests public void GetCommentById_ThrowsException_WhenCommentDoesNotExist() { string exceptionMessage = "The comment does not exist"; - Guid creatorId = new Guid(); - User user = new User + Guid creatorId = new(); + User user = new() { Id = creatorId, }; - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + _ = this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + _ = this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); Exception ex = Assert.ThrowsAsync(() => this.CommentService.GetCommentById(new Guid())); @@ -168,21 +168,21 @@ namespace DevHive.Services.Tests public async Task UpdateComment_ReturnsTheIdOfTheComment_WhenUpdatedSuccessfully() { Guid id = Guid.NewGuid(); - Comment comment = new Comment + Comment comment = new() { Id = id, Message = MESSAGE }; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + UpdateCommentServiceModel updateCommentServiceModel = new() { CommentId = id, NewMessage = MESSAGE }; - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + _ = this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); + _ = this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + _ = this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + _ = this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); Guid result = await this.CommentService.UpdateComment(updateCommentServiceModel); @@ -192,19 +192,19 @@ namespace DevHive.Services.Tests [Test] public async Task UpdateComment_ReturnsEmptyId_WhenTheCommentIsNotUpdatedSuccessfully() { - Comment comment = new Comment + Comment comment = new() { Message = MESSAGE }; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + UpdateCommentServiceModel updateCommentServiceModel = new() { CommentId = Guid.NewGuid(), NewMessage = MESSAGE }; - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + _ = this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); + _ = this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + _ = this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); Guid result = await this.CommentService.UpdateComment(updateCommentServiceModel); @@ -215,11 +215,11 @@ namespace DevHive.Services.Tests public void UpdateComment_ThrowsArgumentException_WhenCommentDoesNotExist() { string exceptionMessage = "Comment does not exist!"; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + UpdateCommentServiceModel updateCommentServiceModel = new() { }; - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); + _ = this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this.CommentService.UpdateComment(updateCommentServiceModel)); @@ -233,12 +233,12 @@ namespace DevHive.Services.Tests [TestCase(false)] public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass) { - Guid id = new Guid(); - Comment comment = new Comment(); + Guid id = new(); + Comment comment = new(); - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); - this.CommentRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + _ = this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); + _ = this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + _ = this.CommentRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); bool result = await this.CommentService.DeleteComment(id); @@ -249,9 +249,9 @@ namespace DevHive.Services.Tests public void DeleteComment_ThrowsException_WhenCommentDoesNotExist() { string exceptionMessage = "Comment does not exist!"; - Guid id = new Guid(); + Guid id = new(); - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); + _ = this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this.CommentService.DeleteComment(id)); diff --git a/src/Services/DevHive.Services.Tests/UserService.Tests.cs b/src/Services/DevHive.Services.Tests/UserService.Tests.cs index 550106f..8642733 100644 --- a/src/Services/DevHive.Services.Tests/UserService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/UserService.Tests.cs @@ -20,31 +20,37 @@ using NUnit.Framework; namespace DevHive.Services.Tests { - [TestFixture] + [TestFixture] public class UserServiceTests { - private Mock CloudServiceMock { get; set; } - private Mock UserRepositoryMock { get; set; } - private Mock RoleRepositoryMock { get; set; } - private Mock LanguageRepositoryMock { get; set; } - private Mock TechnologyRepositoryMock { get; set; } - private Mock MapperMock { get; set; } - private JwtOptions JwtOptions { get; set; } - private UserService UserService { get; set; } + private Mock _cloudServiceMock; + private Mock _userRepositoryMock; + private Mock _roleRepositoryMock; + private Mock _languageRepositoryMock; + private Mock _technologyRepositoryMock; + private Mock _mapperMock; + private JwtOptions _jwtOptions; + private UserService _userService; #region SetUps [SetUp] public void Setup() { - this.UserRepositoryMock = new Mock(); - this.RoleRepositoryMock = new Mock(); - this.CloudServiceMock = new Mock(); - this.LanguageRepositoryMock = new Mock(); - this.TechnologyRepositoryMock = new Mock(); - this.JwtOptions = new JwtOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm"); - this.MapperMock = new Mock(); - // TODO: give actual UserManager and RoleManager to UserService - this.UserService = new UserService(this.UserRepositoryMock.Object, this.LanguageRepositoryMock.Object, this.RoleRepositoryMock.Object, this.TechnologyRepositoryMock.Object, null, null, this.MapperMock.Object, this.JwtOptions, this.CloudServiceMock.Object); + this._userRepositoryMock = new Mock(); + this._roleRepositoryMock = new Mock(); + this._cloudServiceMock = new Mock(); + this._languageRepositoryMock = new Mock(); + this._technologyRepositoryMock = new Mock(); + this._jwtOptions = new JwtOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm"); + this._mapperMock = new Mock(); + this._userService = new UserService( + this._userRepositoryMock.Object, + this._languageRepositoryMock.Object, + this._roleRepositoryMock.Object, + this._technologyRepositoryMock.Object, + this._mapperMock.Object, + this._jwtOptions, + this._cloudServiceMock.Object); } #endregion @@ -52,132 +58,149 @@ namespace DevHive.Services.Tests [Test] public async Task LoginUser_ReturnsTokenModel_WhenLoggingUserIn() { - string somePassword = "GoshoTrapovImaGolemChep"; - const string name = "GoshoTrapov"; - string hashedPassword = PasswordModifications.GeneratePasswordHash(somePassword); - LoginServiceModel loginServiceModel = new LoginServiceModel + string somePassword = "I'm_Nigga"; + + LoginServiceModel loginServiceModel = new() { Password = somePassword }; - User user = new User + User user = new() { Id = Guid.NewGuid(), - PasswordHash = hashedPassword, - UserName = name + PasswordHash = somePassword, + UserName = "g_trapov" }; - this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny())).Returns(Task.FromResult(user)); - - string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, user.Roles); + this._userRepositoryMock.Setup(p => + p.DoesUsernameExistAsync(It.IsAny())) + .Returns(Task.FromResult(true)); + this._userRepositoryMock.Setup(p => + p.GetByUsernameAsync(It.IsAny())) + .Returns(Task.FromResult(user)); - TokenModel tokenModel = await this.UserService.LoginUser(loginServiceModel); + string jwtSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, user.Roles); + TokenModel tokenModel = await this._userService.LoginUser(loginServiceModel); - Assert.AreEqual(JWTSecurityToken, tokenModel.Token, "LoginUser does not return the correct token"); + Assert.AreEqual(jwtSecurityToken, tokenModel.Token, "LoginUser does not return the correct token"); } [Test] public void LoginUser_ThrowsException_WhenUserNameDoesNotExist() { - const string EXCEPTION_MESSAGE = "Invalid username!"; - LoginServiceModel loginServiceModel = new LoginServiceModel - { - }; + LoginServiceModel loginServiceModel = new(); - this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this._userRepositoryMock.Setup(p => + p.DoesUsernameExistAsync(It.IsAny())) + .Returns(Task.FromResult(false)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.LoginUser(loginServiceModel)); + Exception ex = Assert.ThrowsAsync( + () => this._userService.LoginUser(loginServiceModel)); - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message"); + Assert.AreEqual("Invalid username!", ex.Message, "Incorrect Exception message"); } [Test] - public void LoginUser_ThroiwsException_WhenPasswordIsIncorect() + public void LoginUser_ThrowsException_WhenPasswordIsIncorrect() { - const string EXCEPTION_MESSAGE = "Incorrect password!"; - string somePassword = "GoshoTrapovImaGolemChep"; - LoginServiceModel loginServiceModel = new LoginServiceModel + string somePassword = "I'm_Nigga"; + + LoginServiceModel loginServiceModel = new() { Password = somePassword }; - User user = new User + User user = new() { Id = Guid.NewGuid(), - PasswordHash = "InvalidPasswordHas" }; - this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny())).Returns(Task.FromResult(user)); + this._userRepositoryMock.Setup(p => + p.DoesUsernameExistAsync(It.IsAny())) + .Returns(Task.FromResult(true)); + this._userRepositoryMock.Setup(p => + p.GetByUsernameAsync(It.IsAny())) + .Returns(Task.FromResult(user)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.LoginUser(loginServiceModel)); + Exception ex = Assert.ThrowsAsync(() => this._userService.LoginUser(loginServiceModel)); - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message"); + Assert.AreEqual("Incorrect password!", ex.Message, "Incorrect Exception message"); } #endregion #region RegisterUser - // [Test] - // public async Task RegisterUser_ReturnsTokenModel_WhenUserIsSuccessfull() - // { - // string somePassword = "GoshoTrapovImaGolemChep"; - // const string name = "GoshoTrapov"; - // RegisterServiceModel registerServiceModel = new RegisterServiceModel - // { - // Password = somePassword - // }; - // User user = new User - // { - // Id = Guid.NewGuid(), - // UserName = name - // }; - // Role role = new Role { Name = Role.DefaultRole }; - // HashSet roles = new HashSet { role }; - // - // this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - // this.UserRepositoryMock.Setup(p => p.DoesEmailExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - // this.RoleRepositoryMock.Setup(p => p.DoesNameExist(It.IsAny())).Returns(Task.FromResult(true)); - // this.RoleRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny())).Returns(Task.FromResult(role)); - // this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(user); - // this.UserRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Verifiable(); - // - // string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, roles); - // - // TokenModel tokenModel = await this.UserService.RegisterUser(registerServiceModel); - // - // Mock.Verify(); - // Assert.AreEqual(JWTSecurityToken, tokenModel.Token, "RegisterUser does not return the correct token"); - // } + [Test] + public async Task RegisterUser_ReturnsTokenModel_WhenUserIsSuccessfull() + { + Guid userId = Guid.NewGuid(); + RegisterServiceModel registerServiceModel = new() + { + Password = "ImNigga" + }; + User user = new() + { + Id = userId, + UserName = "g_trapov" + }; + Role role = new() { Name = Role.DefaultRole }; + HashSet roles = new() { role }; + + this._userRepositoryMock.Setup(p => + p.DoesUsernameExistAsync(It.IsAny())) + .Returns(Task.FromResult(false)); + this._userRepositoryMock.Setup(p => + p.DoesEmailExistAsync(It.IsAny())) + .Returns(Task.FromResult(false)); + this._userRepositoryMock.Setup(p => + p.AddAsync(It.IsAny())) + .ReturnsAsync(true); + + this._roleRepositoryMock.Setup(p => + p.DoesNameExist(It.IsAny())) + .Returns(Task.FromResult(true)); + this._roleRepositoryMock.Setup(p => + p.GetByNameAsync(It.IsAny())) + .Returns(Task.FromResult(role)); + + this._mapperMock.Setup(p => + p.Map(It.IsAny())) + .Returns(user); + + string jwtSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, roles); + TokenModel tokenModel = await this._userService.RegisterUser(registerServiceModel); + + Assert.AreEqual(jwtSecurityToken, tokenModel.Token, "RegisterUser does not return the correct token"); + } [Test] public void RegisterUser_ThrowsException_WhenUsernameAlreadyExists() { const string EXCEPTION_MESSAGE = "Username already exists!"; - RegisterServiceModel registerServiceModel = new RegisterServiceModel - { - }; + RegisterServiceModel registerServiceModel = new(); - this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this._userRepositoryMock.Setup(p => + p.DoesUsernameExistAsync(It.IsAny())) + .Returns(Task.FromResult(true)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.RegisterUser(registerServiceModel)); + Exception ex = Assert.ThrowsAsync( + () => this._userService.RegisterUser(registerServiceModel)); - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message"); + Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorrect Exception message"); } [Test] public void RegisterUser_ThrowsException_WhenEmailAlreadyExists() { - const string EXCEPTION_MESSAGE = "Email already exists!"; - - RegisterServiceModel registerServiceModel = new RegisterServiceModel - { - }; + RegisterServiceModel registerServiceModel = new(); - this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.UserRepositoryMock.Setup(p => p.DoesEmailExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this._userRepositoryMock.Setup(p => + p.DoesUsernameExistAsync(It.IsAny())) + .Returns(Task.FromResult(false)); + this._userRepositoryMock.Setup(p => + p.DoesEmailExistAsync(It.IsAny())) + .Returns(Task.FromResult(true)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.RegisterUser(registerServiceModel)); + Exception ex = Assert.ThrowsAsync(() => this._userService.RegisterUser(registerServiceModel)); - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorect Exception message"); + Assert.AreEqual("Email already exists!", ex.Message, "Incorrect Exception message"); } #endregion @@ -185,34 +208,37 @@ namespace DevHive.Services.Tests [Test] public async Task GetUserById_ReturnsTheUser_WhenItExists() { - Guid id = new Guid(); - string name = "Gosho Trapov"; - User user = new() - { - }; - UserServiceModel userServiceModel = new UserServiceModel + Guid id = new(); + string username = "g_trapov"; + User user = new(); + UserServiceModel userServiceModel = new() { - UserName = name + UserName = username }; - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(userServiceModel); + this._userRepositoryMock.Setup(p => + p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(user)); + this._mapperMock.Setup(p => + p.Map(It.IsAny())) + .Returns(userServiceModel); - UserServiceModel result = await this.UserService.GetUserById(id); + UserServiceModel result = await this._userService.GetUserById(id); - Assert.AreEqual(name, result.UserName); + Assert.AreEqual(username, result.UserName); } [Test] public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist() { - const string EXCEPTION_MESSEGE = "User does not exist!"; - Guid id = new Guid(); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + Guid id = new(); + this._userRepositoryMock.Setup(p => + p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(null)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.GetUserById(id)); + Exception ex = Assert.ThrowsAsync(() => this._userService.GetUserById(id)); - Assert.AreEqual(EXCEPTION_MESSEGE, ex.Message, "Incorecct exception message"); + Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message"); } #endregion @@ -220,32 +246,37 @@ namespace DevHive.Services.Tests [Test] public async Task GetUserByUsername_ReturnsTheCorrectUser_IfItExists() { - string username = "Gosho Trapov"; - User user = new User(); - UserServiceModel userServiceModel = new UserServiceModel + string username = "g_trapov"; + User user = new(); + UserServiceModel userServiceModel = new() { UserName = username }; - this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(userServiceModel); + this._userRepositoryMock.Setup(p => + p.GetByUsernameAsync(It.IsAny())) + .Returns(Task.FromResult(user)); + this._mapperMock.Setup(p => + p.Map(It.IsAny())) + .Returns(userServiceModel); - UserServiceModel result = await this.UserService.GetUserByUsername(username); + UserServiceModel result = await this._userService.GetUserByUsername(username); Assert.AreEqual(username, result.UserName, "GetUserByUsername does not return the correct user"); } [Test] - public async Task GetUserByUsername_ThrowsException_IfUserDoesNotExist() + public void GetUserByUsername_ThrowsException_IfUserDoesNotExist() { - string username = "Gosho Trapov"; - const string EXCEPTION_MESSEGE = "User does not exist!"; + string username = "g_trapov"; - this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny())).Returns(Task.FromResult(null)); + this._userRepositoryMock.Setup(p => + p.GetByUsernameAsync(It.IsAny())) + .Returns(Task.FromResult(null)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.GetUserByUsername(username)); + Exception ex = Assert.ThrowsAsync(() => this._userService.GetUserByUsername(username)); - Assert.AreEqual(EXCEPTION_MESSEGE, ex.Message, "Incorecct exception message"); + Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message"); } #endregion @@ -255,76 +286,90 @@ namespace DevHive.Services.Tests // [TestCase(false)] // public async Task UpdateUser_ReturnsIfUpdateIsSuccessfull_WhenUserExistsy(bool shouldPass) // { - // string name = "Gosho Trapov"; + // string username = "g_trapov"; // Guid id = Guid.NewGuid(); // User user = new User // { - // UserName = name, + // UserName = username, // Id = id, // }; // UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel // { - // UserName = name, + // UserName = username, // }; // UserServiceModel userServiceModel = new UserServiceModel // { - // UserName = name, + // UserName = username, // }; // Role role = new Role { }; - // - // this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - // this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - // this.UserRepositoryMock.Setup(p => p.DoesUserHaveThisUsernameAsync(It.IsAny(), It.IsAny())).Returns(true); - // this.UserRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(shouldPass)); - // this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - // this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(user); - // this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(userServiceModel); - // + // + // this._userRepositoryMock.Setup(p => + // p.DoesUserExistAsync(It.IsAny())) + // .Returns(Task.FromResult(true)); + // this._userRepositoryMock.Setup(p => + // p.DoesUsernameExistAsync(It.IsAny())) + // .Returns(Task.FromResult(false)); + // this._userRepositoryMock.Setup(p => + // p.DoesUserHaveThisUsernameAsync(It.IsAny(), It.IsAny())) + // .Returns(true); + // this._userRepositoryMock.Setup(p => + // p.EditAsync(It.IsAny(), It.IsAny())) + // .Returns(Task.FromResult(shouldPass)); + // this._userRepositoryMock.Setup(p => + // p.GetByIdAsync(It.IsAny())) + // .Returns(Task.FromResult(user)); + // this._mapperMock.Setup(p => + // p.Map(It.IsAny())) + // .Returns(user); + // this._mapperMock.Setup(p => + // p.Map(It.IsAny())) + // .Returns(userServiceModel); + // // if (shouldPass) // { - // UserServiceModel result = await this.UserService.UpdateUser(updateUserServiceModel); - // + // UserServiceModel result = await this._userService.UpdateUser(updateUserServiceModel); + // // Assert.AreEqual(updateUserServiceModel.UserName, result.UserName); // } // else // { // const string EXCEPTION_MESSAGE = "Unable to edit user!"; - // - // Exception ex = Assert.ThrowsAsync(() => this.UserService.UpdateUser(updateUserServiceModel)); - // - // Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message"); + // + // Exception ex = Assert.ThrowsAsync(() => this._userService.UpdateUser(updateUserServiceModel)); + // + // Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorrect exception message"); // } // } [Test] public void UpdateUser_ThrowsException_WhenUserDoesNotExist() { - const string EXCEPTION_MESSAGE = "User does not exist!"; - UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel - { - }; + UpdateUserServiceModel updateUserServiceModel = new(); - this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this._userRepositoryMock.Setup(p => + p.DoesUserExistAsync(It.IsAny())) + .Returns(Task.FromResult(false)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.UpdateUser(updateUserServiceModel)); + Exception ex = Assert.ThrowsAsync(() => this._userService.UpdateUser(updateUserServiceModel)); - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message"); + Assert.AreEqual("User does not exist!", ex.Message, "Incorrect exception message"); } [Test] public void UpdateUser_ThrowsException_WhenUserNameAlreadyExists() { - const string EXCEPTION_MESSAGE = "Username already exists!"; - UpdateUserServiceModel updateUserServiceModel = new UpdateUserServiceModel - { - }; + UpdateUserServiceModel updateUserServiceModel = new(); - this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this._userRepositoryMock.Setup(p => + p.DoesUserExistAsync(It.IsAny())) + .Returns(Task.FromResult(true)); + this._userRepositoryMock.Setup(p => + p.DoesUsernameExistAsync(It.IsAny())) + .Returns(Task.FromResult(true)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.UpdateUser(updateUserServiceModel)); + Exception ex = Assert.ThrowsAsync(() => this._userService.UpdateUser(updateUserServiceModel)); - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message"); + Assert.AreEqual("Username already exists!", ex.Message, "Incorrect exception message"); } #endregion @@ -337,41 +382,49 @@ namespace DevHive.Services.Tests // { // Guid id = new Guid(); // User user = new User(); - // - // this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - // this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - // this.UserRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - // - // bool result = await this.UserService.DeleteUser(id); - // + // + // this._userRepositoryMock.Setup(p => + // p.DoesUserExistAsync(It.IsAny())) + // .Returns(Task.FromResult(true)); + // this._userRepositoryMock.Setup(p => + // p.GetByIdAsync(It.IsAny())) + // .Returns(Task.FromResult(user)); + // this._userRepositoryMock.Setup(p => + // p.DeleteAsync(It.IsAny())) + // .Returns(Task.FromResult(shouldPass)); + // + // bool result = await this._userService.DeleteUser(id); + // // Assert.AreEqual(shouldPass, result); // } - // + // [Test] public void DeleteUser_ThrowsException_WhenUserDoesNotExist() { string exceptionMessage = "User does not exist!"; - Guid id = new Guid(); + Guid id = new(); - this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this._userRepositoryMock.Setup(p => + p.DoesUserExistAsync(It.IsAny())) + .Returns(Task.FromResult(false)); - Exception ex = Assert.ThrowsAsync(() => this.UserService.DeleteUser(id)); + Exception ex = Assert.ThrowsAsync(() => this._userService.DeleteUser(id)); - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + Assert.AreEqual(exceptionMessage, ex.Message, "Incorrect exception message"); } #endregion #region HelperMethods private string WriteJWTSecurityToken(Guid userId, string username, HashSet roles) { - byte[] signingKey = Encoding.ASCII.GetBytes(this.JwtOptions.Secret); + byte[] signingKey = Encoding.ASCII.GetBytes(this._jwtOptions.Secret); HashSet claims = new() { new Claim("ID", $"{userId}"), new Claim("Username", username), }; - foreach (var role in roles) + foreach (Role role in roles) { claims.Add(new Claim(ClaimTypes.Role, role.Name)); } -- cgit v1.2.3 From 039909bd4d1a49773e6261c110cac4495f3a12fb Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 14 Mar 2021 11:07:04 +0200 Subject: Made properties into variables, fixed spacing and made protected members private, all in Data layer tests --- .../DevHive.Data.Tests/CommentRepository.Tests.cs | 24 +++++++-------- .../DevHive.Data.Tests/FeedRepository.Tests.cs | 15 +++++----- .../DevHive.Data.Tests/LenguageRepository.Tests.cs | 28 ++++++++--------- .../DevHive.Data.Tests/PostRepository.Tests.cs | 35 ++++++++++------------ .../TechnologyRepository.Tests.cs | 32 ++++++++++---------- 5 files changed, 63 insertions(+), 71 deletions(-) (limited to 'src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs') diff --git a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs index 1fd8ad0..1e5390b 100644 --- a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs @@ -11,10 +11,8 @@ namespace DevHive.Data.Tests public class CommentRepositoryTests { private const string COMMENT_MESSAGE = "Comment message"; - - protected DevHiveContext Context { get; set; } - - protected CommentRepository CommentRepository { get; set; } + private DevHiveContext _context; + private CommentRepository _commentRepository; #region Setups [SetUp] @@ -23,15 +21,15 @@ namespace DevHive.Data.Tests DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); + this._context = new DevHiveContext(optionsBuilder.Options); - this.CommentRepository = new CommentRepository(this.Context); + this._commentRepository = new CommentRepository(this._context); } [TearDown] public void TearDown() { - _ = this.Context.Database.EnsureDeleted(); + _ = this._context.Database.EnsureDeleted(); } #endregion @@ -41,7 +39,7 @@ namespace DevHive.Data.Tests { Comment comment = await this.AddEntity(); - Comment resultComment = await this.CommentRepository.GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated); + 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"); } @@ -51,7 +49,7 @@ namespace DevHive.Data.Tests { _ = await this.AddEntity(); - Comment resultComment = await this.CommentRepository.GetCommentByIssuerAndTimeCreatedAsync(Guid.Empty, DateTime.Now); + Comment resultComment = await this._commentRepository.GetCommentByIssuerAndTimeCreatedAsync(Guid.Empty, DateTime.Now); Assert.IsNull(resultComment, "GetCommentByIssuerAndTimeCreatedAsync does not return null when the comment does not exist"); } @@ -63,7 +61,7 @@ namespace DevHive.Data.Tests { Comment comment = await this.AddEntity(); - bool result = await this.CommentRepository.DoesCommentExist(comment.Id); + bool result = await this._commentRepository.DoesCommentExist(comment.Id); Assert.IsTrue(result, "DoesCommentExist does not return true whenm the Comment exists"); } @@ -71,7 +69,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesCommentExist_ReturnsFalse_WhenTheCommentDoesNotExist() { - bool result = await this.CommentRepository.DoesCommentExist(Guid.Empty); + bool result = await this._commentRepository.DoesCommentExist(Guid.Empty); Assert.IsFalse(result, "DoesCommentExist does not return false whenm the Comment" + " does not exist"); @@ -89,8 +87,8 @@ namespace DevHive.Data.Tests TimeCreated = DateTime.Now }; - _ = this.Context.Comments.Add(comment); - _ = await this.Context.SaveChangesAsync(); + _ = this._context.Comments.Add(comment); + _ = await this._context.SaveChangesAsync(); return comment; } diff --git a/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs index eb5bce0..24fcb51 100644 --- a/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs @@ -11,9 +11,8 @@ namespace DevHive.Data.Tests [TestFixture] public class FeedRepositoryTests { - protected DevHiveContext Context { get; set; } - - protected FeedRepository FeedRepository { get; set; } + private DevHiveContext _context; + private FeedRepository _feedRepository; #region Setups [SetUp] @@ -22,15 +21,15 @@ namespace DevHive.Data.Tests DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); + this._context = new DevHiveContext(optionsBuilder.Options); - this.FeedRepository = new FeedRepository(this.Context); + this._feedRepository = new FeedRepository(this._context); } [TearDown] public void TearDown() { - _ = this.Context.Database.EnsureDeleted(); + _ = this._context.Database.EnsureDeleted(); } #endregion @@ -50,7 +49,7 @@ namespace DevHive.Data.Tests const int PAGE_NUMBER = 1; const int PAGE_SIZE = 10; - List resultList = await this.FeedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE); + List resultList = await this._feedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE); Assert.GreaterOrEqual(2, resultList.Count, "GetFriendsPosts does not return all correct posts"); } @@ -69,7 +68,7 @@ namespace DevHive.Data.Tests const int PAGE_NUMBER = 1; const int PAGE_SIZE = 10; - List resultList = await this.FeedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE); + List resultList = await this._feedRepository.GetFriendsPosts(friendsList, dateTime, PAGE_NUMBER, PAGE_SIZE); Assert.LessOrEqual(0, resultList.Count, "GetFriendsPosts does not return all correct posts"); } diff --git a/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs index 18d5959..b7bb2e3 100644 --- a/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs @@ -12,8 +12,8 @@ namespace DevHive.Data.Tests public class LenguageRepositoryTests { private const string LANGUAGE_NAME = "Language test name"; - protected DevHiveContext Context { get; set; } - protected LanguageRepository LanguageRepository { get; set; } + private DevHiveContext _context; + private LanguageRepository _languageRepository; #region Setups [SetUp] @@ -22,15 +22,15 @@ namespace DevHive.Data.Tests DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); + this._context = new DevHiveContext(optionsBuilder.Options); - this.LanguageRepository = new LanguageRepository(this.Context); + this._languageRepository = new LanguageRepository(this._context); } [TearDown] public void TearDown() { - _ = this.Context.Database.EnsureDeleted(); + _ = this._context.Database.EnsureDeleted(); } #endregion @@ -40,9 +40,9 @@ namespace DevHive.Data.Tests { await this.AddEntity(); - Language language = this.Context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault(); + Language language = this._context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault(); - Language languageResult = await this.LanguageRepository.GetByNameAsync(LANGUAGE_NAME); + Language languageResult = await this._languageRepository.GetByNameAsync(LANGUAGE_NAME); Assert.AreEqual(language.Id, languageResult.Id); } @@ -50,7 +50,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetByNameAsync_ReturnsNull_IfTechnologyDoesNotExists() { - Language languageResult = await this.LanguageRepository.GetByNameAsync(LANGUAGE_NAME); + Language languageResult = await this._languageRepository.GetByNameAsync(LANGUAGE_NAME); Assert.IsNull(languageResult); } @@ -61,11 +61,11 @@ namespace DevHive.Data.Tests public async Task DoesLanguageExist_ReturnsTrue_IfIdExists() { await this.AddEntity(); - Language language = this.Context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault(); + Language language = this._context.Languages.Where(x => x.Name == LANGUAGE_NAME).AsEnumerable().FirstOrDefault(); Guid id = language.Id; - bool result = await this.LanguageRepository.DoesLanguageExistAsync(id); + bool result = await this._languageRepository.DoesLanguageExistAsync(id); Assert.IsTrue(result, "DoesLanguageExistAsync returns flase when language exists"); } @@ -75,7 +75,7 @@ namespace DevHive.Data.Tests { Guid id = Guid.NewGuid(); - bool result = await this.LanguageRepository.DoesLanguageExistAsync(id); + bool result = await this._languageRepository.DoesLanguageExistAsync(id); Assert.IsFalse(result, "DoesLanguageExistAsync returns true when language does not exist"); } @@ -87,7 +87,7 @@ namespace DevHive.Data.Tests { await this.AddEntity(); - bool result = await this.LanguageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME); + bool result = await this._languageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME); Assert.IsTrue(result, "DoesLanguageNameExists returns true when language name does not exist"); } @@ -95,7 +95,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesLanguageNameExist_ReturnsFalse_IfLanguageDoesNotExists() { - bool result = await this.LanguageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME); + bool result = await this._languageRepository.DoesLanguageNameExistAsync(LANGUAGE_NAME); Assert.False(result, "DoesTechnologyNameExistAsync returns true when language name does not exist"); } @@ -109,7 +109,7 @@ namespace DevHive.Data.Tests Name = name }; - _ = await this.LanguageRepository.AddAsync(language); + _ = 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 index c57844e..374befd 100644 --- a/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs @@ -15,12 +15,9 @@ namespace DevHive.Data.Tests public class PostRepositoryTests { private const string POST_MESSAGE = "Post test message"; - - private DevHiveContext Context { get; set; } - - private Mock UserRepository { get; set; } - - private PostRepository PostRepository { get; set; } + private DevHiveContext _context; + private Mock _userRepository; + private PostRepository _postRepository; #region Setups [SetUp] @@ -29,17 +26,17 @@ namespace DevHive.Data.Tests DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); + this._context = new DevHiveContext(optionsBuilder.Options); - this.UserRepository = new Mock(); + this._userRepository = new Mock(); - this.PostRepository = new PostRepository(this.Context, this.UserRepository.Object); + this._postRepository = new PostRepository(this._context, this._userRepository.Object); } [TearDown] public void TearDown() { - _ = this.Context.Database.EnsureDeleted(); + _ = this._context.Database.EnsureDeleted(); } #endregion @@ -64,7 +61,7 @@ namespace DevHive.Data.Tests { Post post = await this.AddEntity(); - Post resultTechnology = await this.PostRepository.GetByIdAsync(post.Id); + Post resultTechnology = await this._postRepository.GetByIdAsync(post.Id); Assert.AreEqual(post.Id, resultTechnology.Id, "GetByIdAsync does not return the correct post"); } @@ -72,7 +69,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetByIdAsync_ReturnsNull_IfTechnologyDoesNotExists() { - Post resultPost = await this.PostRepository.GetByIdAsync(Guid.NewGuid()); + Post resultPost = await this._postRepository.GetByIdAsync(Guid.NewGuid()); Assert.IsNull(resultPost); } @@ -84,7 +81,7 @@ namespace DevHive.Data.Tests { Post post = await this.AddEntity(); - Post resultPost = await this.PostRepository.GetPostByCreatorAndTimeCreatedAsync(post.Creator.Id, post.TimeCreated); + 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"); } @@ -94,7 +91,7 @@ namespace DevHive.Data.Tests { _ = await this.AddEntity(); - Post resutPost = await this.PostRepository.GetPostByCreatorAndTimeCreatedAsync(Guid.Empty, DateTime.Now); + Post resutPost = await this._postRepository.GetPostByCreatorAndTimeCreatedAsync(Guid.Empty, DateTime.Now); Assert.IsNull(resutPost, "GetPostByCreatorAndTimeCreatedAsync does not return null when the post does not exist"); } @@ -106,7 +103,7 @@ namespace DevHive.Data.Tests { Post post = await this.AddEntity(); - bool result = await this.PostRepository.DoesPostExist(post.Id); + bool result = await this._postRepository.DoesPostExist(post.Id); Assert.IsTrue(result, "DoesPostExist does not return true whenm the Post exists"); } @@ -114,7 +111,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesPostExist_ReturnsFalse_WhenThePostDoesNotExist() { - bool result = await this.PostRepository.DoesPostExist(Guid.Empty); + bool result = await this._postRepository.DoesPostExist(Guid.Empty); Assert.IsFalse(result, "DoesPostExist does not return false whenm the Post does not exist"); } @@ -124,7 +121,7 @@ namespace DevHive.Data.Tests private async Task AddEntity() { User creator = new() { Id = Guid.NewGuid() }; - _ = await this.Context.Users.AddAsync(creator); + _ = await this._context.Users.AddAsync(creator); Post post = new() { Message = POST_MESSAGE, @@ -135,8 +132,8 @@ namespace DevHive.Data.Tests Comments = new List() }; - _ = await this.Context.Posts.AddAsync(post); - _ = await this.Context.SaveChangesAsync(); + _ = await this._context.Posts.AddAsync(post); + _ = await this._context.SaveChangesAsync(); return post; } diff --git a/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs index f7fbacb..c12a38e 100644 --- a/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -12,10 +12,8 @@ namespace DevHive.Data.Tests public class TechnologyRepositoryTests { private const string TECHNOLOGY_NAME = "Technology test name"; - - protected DevHiveContext Context { get; set; } - - protected TechnologyRepository TechnologyRepository { get; set; } + private DevHiveContext _context; + private TechnologyRepository _technologyRepository; #region Setups [SetUp] @@ -24,15 +22,15 @@ namespace DevHive.Data.Tests DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); + this._context = new DevHiveContext(optionsBuilder.Options); - this.TechnologyRepository = new TechnologyRepository(this.Context); + this._technologyRepository = new TechnologyRepository(this._context); } [TearDown] public void TearDown() { - _ = this.Context.Database.EnsureDeleted(); + _ = this._context.Database.EnsureDeleted(); } #endregion @@ -42,9 +40,9 @@ namespace DevHive.Data.Tests { await this.AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault(); + Technology technology = this._context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault(); - Technology resultTechnology = await this.TechnologyRepository.GetByNameAsync(TECHNOLOGY_NAME); + Technology resultTechnology = await this._technologyRepository.GetByNameAsync(TECHNOLOGY_NAME); Assert.AreEqual(technology.Id, resultTechnology.Id); } @@ -52,7 +50,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetByNameAsync_ReturnsNull_IfTechnologyDoesNotExists() { - Technology resultTechnology = await this.TechnologyRepository.GetByNameAsync(TECHNOLOGY_NAME); + Technology resultTechnology = await this._technologyRepository.GetByNameAsync(TECHNOLOGY_NAME); Assert.IsNull(resultTechnology); } @@ -63,10 +61,10 @@ namespace DevHive.Data.Tests public async Task DoesTechnologyExist_ReturnsTrue_IfIdExists() { await this.AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault(); + Technology technology = this._context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).AsEnumerable().FirstOrDefault(); Guid id = technology.Id; - bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); + bool result = await this._technologyRepository.DoesTechnologyExistAsync(id); Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists"); } @@ -76,7 +74,7 @@ namespace DevHive.Data.Tests { Guid id = Guid.NewGuid(); - bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); + bool result = await this._technologyRepository.DoesTechnologyExistAsync(id); Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist"); } @@ -88,7 +86,7 @@ namespace DevHive.Data.Tests { await this.AddEntity(); - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + bool result = await this._technologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); } @@ -96,7 +94,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() { - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + bool result = await this._technologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); } @@ -110,8 +108,8 @@ namespace DevHive.Data.Tests Name = name }; - _ = this.Context.Technologies.Add(technology); - _ = await this.Context.SaveChangesAsync(); + _ = this._context.Technologies.Add(technology); + _ = await this._context.SaveChangesAsync(); } #endregion } -- cgit v1.2.3 From e24ae7d4f9567ee0d3fdb1dd596ee2e85094c676 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 14 Mar 2021 11:44:45 +0200 Subject: Removed all discards from Data layer tests --- src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs | 8 ++++---- src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs | 2 +- src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs | 4 ++-- src/Data/DevHive.Data.Tests/PostRepository.Tests.cs | 10 +++++----- src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) (limited to 'src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs') diff --git a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs index 1e5390b..0aa22bc 100644 --- a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs @@ -29,7 +29,7 @@ namespace DevHive.Data.Tests [TearDown] public void TearDown() { - _ = this._context.Database.EnsureDeleted(); + this._context.Database.EnsureDeleted(); } #endregion @@ -47,7 +47,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist() { - _ = await this.AddEntity(); + await this.AddEntity(); Comment resultComment = await this._commentRepository.GetCommentByIssuerAndTimeCreatedAsync(Guid.Empty, DateTime.Now); @@ -87,8 +87,8 @@ namespace DevHive.Data.Tests TimeCreated = DateTime.Now }; - _ = this._context.Comments.Add(comment); - _ = await this._context.SaveChangesAsync(); + this._context.Comments.Add(comment); + await this._context.SaveChangesAsync(); return comment; } diff --git a/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs index 24fcb51..f54e89d 100644 --- a/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/FeedRepository.Tests.cs @@ -29,7 +29,7 @@ namespace DevHive.Data.Tests [TearDown] public void TearDown() { - _ = this._context.Database.EnsureDeleted(); + this._context.Database.EnsureDeleted(); } #endregion diff --git a/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs index b7bb2e3..3bb9400 100644 --- a/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/LenguageRepository.Tests.cs @@ -30,7 +30,7 @@ namespace DevHive.Data.Tests [TearDown] public void TearDown() { - _ = this._context.Database.EnsureDeleted(); + this._context.Database.EnsureDeleted(); } #endregion @@ -109,7 +109,7 @@ namespace DevHive.Data.Tests Name = name }; - _ = await this._languageRepository.AddAsync(language); + 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 index 374befd..005769f 100644 --- a/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs @@ -36,7 +36,7 @@ namespace DevHive.Data.Tests [TearDown] public void TearDown() { - _ = this._context.Database.EnsureDeleted(); + this._context.Database.EnsureDeleted(); } #endregion @@ -89,7 +89,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist() { - _ = await this.AddEntity(); + await this.AddEntity(); Post resutPost = await this._postRepository.GetPostByCreatorAndTimeCreatedAsync(Guid.Empty, DateTime.Now); @@ -121,7 +121,7 @@ namespace DevHive.Data.Tests private async Task AddEntity() { User creator = new() { Id = Guid.NewGuid() }; - _ = await this._context.Users.AddAsync(creator); + await this._context.Users.AddAsync(creator); Post post = new() { Message = POST_MESSAGE, @@ -132,8 +132,8 @@ namespace DevHive.Data.Tests Comments = new List() }; - _ = await this._context.Posts.AddAsync(post); - _ = await this._context.SaveChangesAsync(); + await this._context.Posts.AddAsync(post); + await this._context.SaveChangesAsync(); return post; } diff --git a/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs index c12a38e..d268777 100644 --- a/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -30,7 +30,7 @@ namespace DevHive.Data.Tests [TearDown] public void TearDown() { - _ = this._context.Database.EnsureDeleted(); + this._context.Database.EnsureDeleted(); } #endregion @@ -108,8 +108,8 @@ namespace DevHive.Data.Tests Name = name }; - _ = this._context.Technologies.Add(technology); - _ = await this._context.SaveChangesAsync(); + this._context.Technologies.Add(technology); + await this._context.SaveChangesAsync(); } #endregion } -- cgit v1.2.3 From 35479f659d2cd04a025025aa5543a6e7961cf99d Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 18 Mar 2021 20:44:57 +0200 Subject: Added tests for untested methods in CommentRepository --- .../DevHive.Data.Tests/CommentRepository.Tests.cs | 77 +++++++++++++++++++++- .../DevHive.Data.Tests/RatingRepository.Tests.cs | 2 +- .../DevHive.Services.Tests/PostService.Tests.cs | 3 +- 3 files changed, 77 insertions(+), 5 deletions(-) (limited to 'src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs') diff --git a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs index 0aa22bc..004d418 100644 --- a/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/CommentRepository.Tests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; using DevHive.Data.Repositories; @@ -14,7 +15,7 @@ namespace DevHive.Data.Tests private DevHiveContext _context; private CommentRepository _commentRepository; - #region Setups + #region SetUp [SetUp] public void Setup() { @@ -33,6 +34,59 @@ namespace DevHive.Data.Tests } #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 comments = new List + { + 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 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 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() @@ -47,14 +101,30 @@ namespace DevHive.Data.Tests [Test] public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist() { - 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 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() @@ -82,6 +152,7 @@ namespace DevHive.Data.Tests User creator = new() { Id = Guid.NewGuid() }; Comment comment = new() { + Id = Guid.NewGuid(), Message = COMMENT_MESSAGE, Creator = creator, TimeCreated = DateTime.Now diff --git a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs index 2da90d9..29091d7 100644 --- a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs @@ -9,7 +9,7 @@ using System.Collections.Generic; namespace DevHive.Data.Tests { - [TestFixture] + [TestFixture] public class RatingRepositoryTests { private DevHiveContext _context; diff --git a/src/Services/DevHive.Services.Tests/PostService.Tests.cs b/src/Services/DevHive.Services.Tests/PostService.Tests.cs index 058485e..feab3be 100644 --- a/src/Services/DevHive.Services.Tests/PostService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/PostService.Tests.cs @@ -124,7 +124,8 @@ namespace DevHive.Services.Tests Post post = new Post { Message = MESSAGE, - Creator = creator + Creator = creator, + Ratings = new List() }; ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel { -- cgit v1.2.3