aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/DevHive.Tests/DevHive.Data.Tests/CommentRepository.Tests.cs98
-rw-r--r--src/DevHive.Tests/DevHive.Data.Tests/PostRepository.Tests.cs119
-rw-r--r--src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs3
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