1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
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.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()
{
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)
{
User creator = new User { Id = Guid.NewGuid() };
Post post = new Post
{
Message = POST_MESSAGE,
Id = Guid.NewGuid(),
Creator = creator,
TimeCreated = DateTime.Now
};
this.Context.Posts.Add(post);
await this.Context.SaveChangesAsync();
return post;
}
#endregion
}
}
|