SAB-E02/Blog.DAL.Tests/RepositoryTests.cs
2021-04-06 18:04:43 +02:00

96 lines
2.9 KiB
C#

using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using Blog.DAL.Infrastructure;
using Blog.DAL.Model;
using Blog.DAL.Repository;
using System.Diagnostics;
using TDD.DbTestHelpers.Yaml;
using TDD.DbTestHelpers.Core;
using NUnit.Framework;
namespace Blog.DAL.Tests
{
[TestFixture]
public class RepositoryTests// : DbBaseTest<BlogFixtures>
{
[Test]
public void GetAllPost_OnePostInDb_ReturnTwoPosts()
{
// arrange
var context = new BlogContext();
context.Database.CreateIfNotExists();
var repository = new BlogRepository();
context.Posts.ToList().ForEach(x => context.Posts.Remove(x));
context.Posts.Add(new Post { Author = "me", Content = "lorem ipsum" });
context.SaveChanges();
// act
var result = repository.GetAllPosts();
// assert
Assert.AreEqual(1, result.Count());
}
[Test]
public void AddInvalidPost_TwoPostsInDb_ReturnThreePosts()
{
// arrange
var context = new BlogContext();
context.Database.CreateIfNotExists();
var repository = new BlogRepository();
// act
Assert.Throws<System.Data.Entity.Validation.DbEntityValidationException>(() => { repository.add(new Post { Author = null, Content = "lorem ipsum" }); });
}
[Test]
public void AddPost_TwoPostsInDb_ReturnThreePosts()
{
// arrange
var context = new BlogContext();
context.Database.CreateIfNotExists();
var repository = new BlogRepository();
// act
var post = repository.add(new Post { Id = 3, Author = "me", Content = "lorem ipsum" });
var result = repository.GetAllPosts();
// assert
Assert.AreEqual(3, result.Count());
}
[Test]
public void GetComments_TwoPostsInDbWithOneComment_ReturnValidCount()
{
// arrange
var context = new BlogContext();
context.Database.CreateIfNotExists();
var repository = new CommentRepository();
// assert
Assert.AreEqual(1, repository.GetAllCommentsOf(1).Count());
Assert.AreEqual(0, repository.GetAllCommentsOf(2).Count());
}
[Test]
public void AddComments_TwoPostsInDbWithOneComment_ReturnValidCount()
{
// arrange
var context = new BlogContext();
context.Database.CreateIfNotExists();
var repository = new CommentRepository();
repository.add(new Comment { PostId = 2, Author = "Test", Content = "Lorem ipsum" });
// assert
Assert.AreEqual(1, repository.GetAllCommentsOf(1).Count());
Assert.AreEqual(1, repository.GetAllCommentsOf(2).Count());
}
}
}