From a0d819bc3ad1f83c0bfa4294fc63679fb9d9f4d9 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Feb 2021 21:05:29 +0200 Subject: za viko --- .../DevHive.Data.Tests/PostRepository.Tests.cs | 36 ++++++++++++++++---- .../DevHive.Data.Tests/UserRepositoryTests.cs | 38 +++++++++++----------- 2 files changed, 49 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs index 113780b..7d80f1f 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs @@ -1,9 +1,12 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Data.Repositories; using Microsoft.EntityFrameworkCore; +using Moq; using NUnit.Framework; namespace DevHive.Data.Tests @@ -13,9 +16,11 @@ namespace DevHive.Data.Tests { private const string POST_MESSAGE = "Post test message"; - protected DevHiveContext Context { get; set; } + private DevHiveContext Context { get; set; } - protected PostRepository PostRepository { get; set; } + private Mock UserRepository { get; set; } + + private PostRepository PostRepository { get; set; } #region Setups [SetUp] @@ -26,7 +31,9 @@ namespace DevHive.Data.Tests this.Context = new DevHiveContext(optionsBuilder.Options); - PostRepository = new PostRepository(Context); + this.UserRepository = new Mock(); + + PostRepository = new PostRepository(Context, this.UserRepository.Object); } [TearDown] @@ -36,6 +43,21 @@ namespace DevHive.Data.Tests } #endregion + #region AddNewPostToCreator + [Test] + public async Task AddNewPostToCreator_ReturnsTrue_WhenNewPostIsAddedToCreator() + { + Post post = await this.AddEntity(); + User user = new User { Id = Guid.NewGuid() }; + + this.UserRepository.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); + + bool result = await this.PostRepository.AddNewPostToCreator(user.Id, post); + + Assert.IsTrue(result, "AddNewPostToCreator does not return true when Post Is Added To Creator successfully"); + } + #endregion + #region GetByIdAsync [Test] public async Task GetByNameAsync_ReturnsTheCorrectPost_IfItExists() @@ -91,7 +113,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesPostExist_ReturnsFalse_WhenThePostDoesNotExist() - { + { bool result = await this.PostRepository.DoesPostExist(Guid.Empty); Assert.IsFalse(result, "DoesPostExist does not return false whenm the Post does not exist"); @@ -107,10 +129,12 @@ namespace DevHive.Data.Tests Message = POST_MESSAGE, Id = Guid.NewGuid(), Creator = creator, - TimeCreated = DateTime.Now + TimeCreated = DateTime.Now, + FileUrls = new List(), + Comments = new List() }; - this.Context.Posts.Add(post); + await this.Context.Posts.AddAsync(post); await this.Context.SaveChangesAsync(); return post; diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index 657369d..aaa189a 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -198,25 +198,25 @@ 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_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() -- cgit v1.2.3