aboutsummaryrefslogtreecommitdiff
path: root/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs
blob: 2da90d94388c24032c2da1205e8f938c4226dd61 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using DevHive.Data.Repositories;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using System.Threading.Tasks;
using DevHive.Data.Models;
using System.Linq;
using System;
using System.Collections.Generic;

namespace DevHive.Data.Tests
{
    [TestFixture]
	public class RatingRepositoryTests
	{
		private DevHiveContext _context;
		private RatingRepository _ratingRepository;

		#region Setups
		[SetUp]
		public void Setup()
		{
			var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
				.UseInMemoryDatabase(databaseName: "DevHive_Test_Database");

			this._context = new DevHiveContext(optionsBuilder.Options);
			this._ratingRepository = new RatingRepository(this._context, null);
		}

		[TearDown]
		public void TearDown()
		{
			this._context.Database.EnsureDeleted();
		}
		#endregion

		#region GetById
		[Test]
		public async Task GetByIdAsync_ReturnsTheCorrectRating_IfItExists()
		{
			Guid ratingId = Guid.NewGuid();
			await AddDummyRating(ratingId);

			Rating ratingResult = await this._ratingRepository.GetByIdAsync(ratingId);

			Assert.AreEqual(ratingResult.Id, ratingId);
		}

		[Test]
		public async Task GetByIdAsync_ReturnsNull_IfRatingDoesNotExist()
		{
			Rating ratingResult = await this._ratingRepository.GetByIdAsync(Guid.NewGuid());

			Assert.IsNull(ratingResult);
		}
		#endregion

		#region GetByPostId
		[Test]
		public async Task GetRatingsByPostId_ReturnsFilledListOfRatings_WhenTheyExist()
		{
			Guid postId = Guid.NewGuid();
			await AddDummyPost(postId);
			await AddDummyRating(Guid.NewGuid(), postId);
			await AddDummyRating(Guid.NewGuid(), postId);

			List<Rating> result = await this._ratingRepository.GetRatingsByPostId(postId);

			Assert.IsNotEmpty(result);
		}

		[Test]
		public async Task GetRatingsByPostId_ReturnsEmptyList_WhenThereAreNoRatings()
		{
			List<Rating> result = await this._ratingRepository.GetRatingsByPostId(Guid.NewGuid());

			Assert.IsEmpty(result);
		}
		#endregion

		#region GetByUserAndPostId
		[Test]
		public async Task GetRatingByUserAndPostId_ReturnsRating_WhenItExists()
		{
			Guid ratingId = Guid.NewGuid();
			Guid postId = Guid.NewGuid();
			Guid userId = Guid.NewGuid();
			await AddDummyPost(postId);
			await AddDummyUser(userId);
			await AddDummyRating(ratingId, postId, userId);

			Rating result = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId);

			Assert.AreEqual(result.Id, ratingId);
		}

		[Test]
		public async Task GetRatingByUserAndPostId_ReturnsNull_WhenRatingDoesNotExist()
		{
			Rating result = await this._ratingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid());

			Assert.IsNull(result);
		}
		#endregion

		#region UserRatedPost
		[Test]
		public async Task UserRatedPost_ReturnsTrue_WhenUserHasRatedPost()
		{
			Guid postId = Guid.NewGuid();
			Guid userId = Guid.NewGuid();
			await AddDummyPost(postId);
			await AddDummyUser(userId);
			await AddDummyRating(Guid.NewGuid(), postId, userId);

			bool result = await this._ratingRepository.UserRatedPost(userId, postId);

			Assert.IsTrue(result);
		}

		[Test]
		public async Task UserRatedPost_ReturnsFalse_WhenUserHasNotRatedPost()
		{
			bool result = await this._ratingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid());

			Assert.IsFalse(result);
		}
		#endregion

		#region DoesRatingExist
		[Test]
		public async Task DoesRatingExist_ReturnsTrue_WhenItExists()
		{
			Guid ratingId = Guid.NewGuid();
			await AddDummyRating(ratingId);

			bool result = await this._ratingRepository.DoesRatingExist(ratingId);

			Assert.IsTrue(result);
		}

		[Test]
		public async Task DoesRatingExist_ReturnsFalse_WhenRatingDoesNotExist()
		{
			bool result = await this._ratingRepository.DoesRatingExist(Guid.NewGuid());

			Assert.IsFalse(result);
		}
		#endregion

		#region HelperMethods
		private async Task AddDummyRating(Guid ratingId, Guid postId = default(Guid), Guid userId = default(Guid))
		{
			Rating rating = new Rating
			{
				Id = ratingId,
				Post = this._context.Posts.FirstOrDefault(x => x.Id == postId),
				User = this._context.Users.FirstOrDefault(x => x.Id == userId)
			};

			await this._context.Rating.AddAsync(rating);
			await this._context.SaveChangesAsync();
		}

		private async Task AddDummyPost(Guid postId)
		{
			Post post = new Post()
			{
				Id = postId,
				Message = "Never gonna give you up"
			};

			await this._context.Posts.AddAsync(post);
			await this._context.SaveChangesAsync();
		}

		private async Task AddDummyUser(Guid userId)
		{
			User user = new User()
			{
				Id = userId
			};

			await this._context.Users.AddAsync(user);
			await this._context.SaveChangesAsync();
		}
		#endregion
	}
}