diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-04-09 19:51:35 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 19:51:35 +0300 |
| commit | 233f38915ba0079079233eff55434ef349c05c45 (patch) | |
| tree | 6c5f69017865bcab87355e910c87339453da1406 /src/Data/DevHive.Data.Tests/PostRepository.Tests.cs | |
| parent | f4a70c6430db923af9fa9958a11c2d6612cb52cc (diff) | |
| parent | a992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff) | |
| download | DevHive-main.tar DevHive-main.tar.gz DevHive-main.zip | |
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/Data/DevHive.Data.Tests/PostRepository.Tests.cs')
| -rw-r--r-- | src/Data/DevHive.Data.Tests/PostRepository.Tests.cs | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs new file mode 100644 index 0000000..005769f --- /dev/null +++ b/src/Data/DevHive.Data.Tests/PostRepository.Tests.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Data.Models.Relational; +using DevHive.Data.Repositories; +using Microsoft.EntityFrameworkCore; +using Moq; +using NUnit.Framework; + +namespace DevHive.Data.Tests +{ + [TestFixture] + public class PostRepositoryTests + { + private const string POST_MESSAGE = "Post test message"; + private DevHiveContext _context; + private Mock<IUserRepository> _userRepository; + private PostRepository _postRepository; + + #region Setups + [SetUp] + public void Setup() + { + DbContextOptionsBuilder<DevHiveContext> optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>() + .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); + + this._context = new DevHiveContext(optionsBuilder.Options); + + this._userRepository = new Mock<IUserRepository>(); + + this._postRepository = new PostRepository(this._context, this._userRepository.Object); + } + + [TearDown] + public void TearDown() + { + this._context.Database.EnsureDeleted(); + } + #endregion + + #region AddNewPostToCreator + // [Test] + // public async Task AddNewPostToCreator_ReturnsTrue_WhenNewPostIsAddedToCreator() + // { + // Post post = await this.AddEntity(); + // User user = new User { Id = Guid.NewGuid() }; + // + // this.UserRepository.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); + // + // bool result = await this.PostRepository.AddNewPostToCreator(user.Id, post); + // + // Assert.IsTrue(result, "AddNewPostToCreator does not return true when Post Is Added To Creator successfully"); + // } + #endregion + + #region GetByIdAsync + [Test] + public async Task GetByNameAsync_ReturnsTheCorrectPost_IfItExists() + { + Post post = await this.AddEntity(); + + Post resultTechnology = await this._postRepository.GetByIdAsync(post.Id); + + Assert.AreEqual(post.Id, resultTechnology.Id, "GetByIdAsync does not return the correct post"); + } + + [Test] + public async Task GetByIdAsync_ReturnsNull_IfTechnologyDoesNotExists() + { + Post resultPost = await this._postRepository.GetByIdAsync(Guid.NewGuid()); + + Assert.IsNull(resultPost); + } + #endregion + + #region GetPostByCreatorAndTimeCreatedAsync + [Test] + public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsTheCorrectPost_IfItExists() + { + Post post = await this.AddEntity(); + + Post resultPost = await this._postRepository.GetPostByCreatorAndTimeCreatedAsync(post.Creator.Id, post.TimeCreated); + + Assert.AreEqual(post.Id, resultPost.Id, "GetPostByCreatorAndTimeCreatedAsync does not return the corect post when it exists"); + } + + [Test] + public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist() + { + await this.AddEntity(); + + Post resutPost = await this._postRepository.GetPostByCreatorAndTimeCreatedAsync(Guid.Empty, DateTime.Now); + + Assert.IsNull(resutPost, "GetPostByCreatorAndTimeCreatedAsync does not return null when the post does not exist"); + } + #endregion + + #region DoesPostExist + [Test] + public async Task DoesPostExist_ReturnsTrue_WhenThePostExists() + { + Post post = await this.AddEntity(); + + bool result = await this._postRepository.DoesPostExist(post.Id); + + Assert.IsTrue(result, "DoesPostExist does not return true whenm the Post exists"); + } + + [Test] + public async Task DoesPostExist_ReturnsFalse_WhenThePostDoesNotExist() + { + bool result = await this._postRepository.DoesPostExist(Guid.Empty); + + Assert.IsFalse(result, "DoesPostExist does not return false whenm the Post does not exist"); + } + #endregion + + #region HelperMethods + private async Task<Post> AddEntity() + { + User creator = new() { Id = Guid.NewGuid() }; + await this._context.Users.AddAsync(creator); + Post post = new() + { + Message = POST_MESSAGE, + Id = Guid.NewGuid(), + Creator = creator, + TimeCreated = DateTime.Now, + Attachments = new List<PostAttachments> { new PostAttachments { FileUrl = "kur" }, new PostAttachments { FileUrl = "za" }, new PostAttachments { FileUrl = "tva" } }, + Comments = new List<Comment>() + }; + + await this._context.Posts.AddAsync(post); + await this._context.SaveChangesAsync(); + + return post; + } + #endregion + } +} |
