diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-26 18:50:41 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-26 18:50:41 +0200 |
| commit | b97a24a4d84f795e615926a162fab3531c1b9ab3 (patch) | |
| tree | 30fba7000d04770f1d5cd9537f4c49f07da3fd74 /src/DevHive.Tests/DevHive.Data.Tests | |
| parent | c73d806b0a16be208402c5922eb3242c469e86b3 (diff) | |
| parent | 7bd33f55cd044fd232f4b574d54ddeacfd6b5003 (diff) | |
| download | DevHive-b97a24a4d84f795e615926a162fab3531c1b9ab3.tar DevHive-b97a24a4d84f795e615926a162fab3531c1b9ab3.tar.gz DevHive-b97a24a4d84f795e615926a162fab3531c1b9ab3.zip | |
Merge branch 'dev' of github.com:Team-Kaleidoscope/DevHive into dev
Diffstat (limited to 'src/DevHive.Tests/DevHive.Data.Tests')
3 files changed, 219 insertions, 1 deletions
diff --git a/src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs new file mode 100644 index 0000000..2a8bb1f --- /dev/null +++ b/src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs @@ -0,0 +1,98 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using Microsoft.EntityFrameworkCore; +using NUnit.Framework; + +namespace DevHive.Data.Tests +{ + [TestFixture] + public class CommentRepositoryTests + { + private const string COMMENT_MESSAGE = "Comment message"; + + protected DevHiveContext Context { get; set; } + + protected CommentRepository CommentRepository { get; set; } + + #region Setups + [SetUp] + public void Setup() + { + var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>() + .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); + + this.Context = new DevHiveContext(optionsBuilder.Options); + + CommentRepository = new CommentRepository(Context); + } + + [TearDown] + public void TearDown() + { + this.Context.Database.EnsureDeleted(); + } + #endregion + + #region GetCommentByIssuerAndTimeCreatedAsync + [Test] + public async Task GetCommentByCreatorAndTimeCreatedAsync_ReturnsTheCorrectComment_IfItExists() + { + Comment comment = await this.AddEntity(); + + Comment resultComment = await this.CommentRepository.GetCommentByIssuerAndTimeCreatedAsync(comment.CreatorId, comment.TimeCreated); + + Assert.AreEqual(comment.Id, resultComment.Id, "GetCommentByIssuerAndTimeCreatedAsync does not return the corect comment when it exists"); + } + + [Test] + public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist() + { + Comment comment = await this.AddEntity(); + + Comment resultComment = await this.CommentRepository.GetCommentByIssuerAndTimeCreatedAsync(Guid.Empty, DateTime.Now); + + Assert.IsNull(resultComment, "GetCommentByIssuerAndTimeCreatedAsync does not return null when the comment does not exist"); + } + #endregion + + #region DoesCommentExist + [Test] + public async Task DoesCommentExist_ReturnsTrue_WhenTheCommentExists() + { + Comment comment = await this.AddEntity(); + + bool result = await this.CommentRepository.DoesCommentExist(comment.Id); + + Assert.IsTrue(result, "DoesCommentExist does not return true whenm the Comment exists"); + } + + [Test] + public async Task DoesCommentExist_ReturnsFalse_WhenTheCommentDoesNotExist() + { + bool result = await this.CommentRepository.DoesCommentExist(Guid.Empty); + + Assert.IsFalse(result, "DoesCommentExist does not return false whenm the Comment" + + " does not exist"); + } + #endregion + + #region HelperMethods + private async Task<Comment> AddEntity(string name = COMMENT_MESSAGE) + { + Comment comment = new Comment + { + Message = COMMENT_MESSAGE, + CreatorId = Guid.NewGuid(), + TimeCreated = DateTime.Now + }; + + this.Context.Comments.Add(comment); + await this.Context.SaveChangesAsync(); + + return comment; + } + #endregion + } +} diff --git a/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs new file mode 100644 index 0000000..27b7c32 --- /dev/null +++ b/src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs @@ -0,0 +1,119 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using Microsoft.EntityFrameworkCore; +using NUnit.Framework; + +namespace DevHive.Data.Tests +{ + [TestFixture] + public class PostRepositoryTests + { + private const string POST_MESSAGE = "Post test message"; + + protected DevHiveContext Context { get; set; } + + protected PostRepository PostRepository { get; set; } + + #region Setups + [SetUp] + public void Setup() + { + var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>() + .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); + + this.Context = new DevHiveContext(optionsBuilder.Options); + + PostRepository = new PostRepository(Context); + } + + [TearDown] + public void TearDown() + { + this.Context.Database.EnsureDeleted(); + } + #endregion + + #region GetByIdAsync + [Test] + public async Task GetByNameAsync_ReturnsTheCorrectPost_IfItExists() + { + Post post = await AddEntity(); + + Post resultTechnology = await this.PostRepository.GetByIdAsync(post.Id); + + Assert.AreEqual(post.Id, resultTechnology.Id, "GetByIdAsync does not return the correct post"); + } + + [Test] + public async Task GetByIdAsync_ReturnsNull_IfTechnologyDoesNotExists() + { + Post resultPost = await this.PostRepository.GetByIdAsync(Guid.NewGuid()); + + Assert.IsNull(resultPost); + } + #endregion + + #region GetPostByCreatorAndTimeCreatedAsync + [Test] + public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsTheCorrectPost_IfItExists() + { + Post post = await this.AddEntity(); + + Post resultPost = await this.PostRepository.GetPostByCreatorAndTimeCreatedAsync(post.CreatorId, post.TimeCreated); + + Assert.AreEqual(post.Id, resultPost.Id, "GetPostByCreatorAndTimeCreatedAsync does not return the corect post when it exists"); + } + + [Test] + public async Task GetPostByCreatorAndTimeCreatedAsync_ReturnsNull_IfThePostDoesNotExist() + { + Post post = await this.AddEntity(); + + Post resutPost = await this.PostRepository.GetPostByCreatorAndTimeCreatedAsync(Guid.Empty, DateTime.Now); + + Assert.IsNull(resutPost, "GetPostByCreatorAndTimeCreatedAsync does not return null when the post does not exist"); + } + #endregion + + #region DoesPostExist + [Test] + public async Task DoesPostExist_ReturnsTrue_WhenThePostExists() + { + Post post = await this.AddEntity(); + + bool result = await this.PostRepository.DoesPostExist(post.Id); + + Assert.IsTrue(result, "DoesPostExist does not return true whenm the Post exists"); + } + + [Test] + public async Task DoesPostExist_ReturnsFalse_WhenThePostDoesNotExist() + { + bool result = await this.PostRepository.DoesPostExist(Guid.Empty); + + Assert.IsFalse(result, "DoesPostExist does not return false whenm the Post does not exist"); + } + #endregion + + #region HelperMethods + private async Task<Post> AddEntity(string name = POST_MESSAGE) + { + Post post = new Post + { + Message = POST_MESSAGE, + Id = Guid.NewGuid(), + CreatorId = Guid.NewGuid(), + TimeCreated = DateTime.Now + }; + + this.Context.Posts.Add(post); + await this.Context.SaveChangesAsync(); + + return post; + } + #endregion + } +} diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index 5201af2..fea26be 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -110,7 +110,8 @@ namespace DevHive.Data.Tests Name = name }; - await this.TechnologyRepository.AddAsync(technology); + this.Context.Technologies.Add(technology); + await this.Context.SaveChangesAsync(); } #endregion |
