-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReservationRepositoryTests.cs
223 lines (172 loc) · 8.8 KB
/
ReservationRepositoryTests.cs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using ICT_LAB_Web.Components.Entities;
using ICT_LAB_Web.Components.Services.Interfaces;
using Moq;
using NUnit.Framework;
namespace ICT_LAB_Web.Tests
{
[TestFixture]
public class ReservationRepositoryTests
{
private List<Reservation> TestData;
private Mock<IReservationRepository> ReservationRepository;
#region Constructor
public ReservationRepositoryTests()
{
this.InitializeData();
// Mock repo
ReservationRepository = new Mock<IReservationRepository>();
// Get all reservation
ReservationRepository.Setup(e => e.GetAll()).ReturnsAsync(TestData);
// Get reservation by user id
ReservationRepository.Setup(e => e.Get(It.IsAny<string>(), null, null)).ReturnsAsync((string id, DateTime? from, DateTime? till) => TestData.Where(q => q.UserId == id).ToList());
// Get reservation by user id, from and till date
ReservationRepository.Setup(e => e.Get(It.IsAny<string>(), It.IsAny<DateTime>(), It.IsAny<DateTime>())).ReturnsAsync((string id, DateTime from, DateTime till) => TestData.Where(q => q.UserId == id && q.StartTime >= from && q.EndTime <= till).ToList());
// Get reservation by id
ReservationRepository.Setup(e => e.GetById(It.IsAny<int>())).ReturnsAsync((int id) => TestData.FirstOrDefault(q => q.ReservationId == id));
// Get reservation by room
ReservationRepository.Setup(e => e.GetByRoom(It.IsAny<string>(), null, null)).ReturnsAsync((string room, DateTime from, DateTime till) => TestData.Where(q => q.RoomCode == room).ToList());
// Get reservation by room, from and till data
ReservationRepository.Setup(e => e.GetByRoom(It.IsAny<string>(), It.IsAny<DateTime>(), It.IsAny<DateTime>())).ReturnsAsync((string room, DateTime from, DateTime till) => TestData.Where(q => q.RoomCode == room && q.StartTime >= from && q.EndTime <= till).ToList());
// Get reservation by start date
ReservationRepository.Setup(e => e.GetByDate(It.IsAny<DateTime>())).ReturnsAsync((DateTime date) => TestData.Where(q => q.StartTime == date).ToList());
// Get reservation by room and start date
ReservationRepository.Setup(e => e.GetByStart(It.IsAny<string>(), It.IsAny<DateTime>())).ReturnsAsync((string id, DateTime start) => TestData.FirstOrDefault(q => q.UserId == id && q.StartTime >= start));
// Add reservation
ReservationRepository.Setup(e => e.Add(It.IsAny<Reservation>())).ReturnsAsync((Reservation reservation) => { TestData.Add(reservation); TestData.FirstOrDefault(q => q.ReservationId == reservation.ReservationId); return reservation; });
// Update reservation
ReservationRepository.Setup(e => e.Update(It.IsAny<Reservation>())).ReturnsAsync((Reservation reservation) => { var original = TestData.FirstOrDefault(q => q.ReservationId == reservation.ReservationId); original = reservation; return reservation; });
// Delete reservation
ReservationRepository.Setup(e => e.Delete(It.IsAny<int>())).ReturnsAsync((int id) => { TestData.Remove(TestData.FirstOrDefault(q => q.ReservationId == id)); return TestData.FirstOrDefault(q => q.ReservationId == id) == null ? true : false; });
}
#endregion
#region Tests
[Test]
[Order(1)]
public async Task Get_All()
{
// Arrange
var repo = ReservationRepository.Object;
// Act
var result = await repo.GetAll();
// Assert
var reservations = result.Should().BeAssignableTo<List<Reservation>>().Subject;
reservations.Should().NotBeNull();
reservations.Count().Should().Be(5);
}
[Test]
[Order(2)]
public async Task Get_By_UserId()
{
// Arrange
var repo = ReservationRepository.Object;
var userId = "jansmit";
// Act
var result = await repo.Get(userId, null, null);
// Assert
var reservations = result.Should().BeOfType<List<Reservation>>().Subject;
reservations.Should().NotBeNull();
reservations.Count().Should().Be(1);
}
[Test]
[Order(3)]
public async Task Get_By_UserId_And_Dates()
{
// Arrange
var repo = ReservationRepository.Object;
var userId = "jansmit";
var from = DateTime.ParseExact("2018-06-12 09:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
var till = from.AddDays(1);
// Act
var result = await repo.Get(userId, from, till);
// Assert
var reservations = result.Should().BeOfType<List<Reservation>>().Subject;
reservations.Should().NotBeNull();
reservations.Count().Should().Be(1);
}
[Test]
[Order(4)]
public async Task Add()
{
// Arrange
var repo = ReservationRepository.Object;
var newReservation = new Reservation
{
ReservationId = 6,
RoomCode = "H.4.308",
UserId = "barackobama",
StartTime = DateTime.Now,
EndTime = DateTime.Now.AddHours(4),
Description = "Work on assignment"
};
// Act
var result = await repo.Add(newReservation);
// Assert
var reservation = result.Should().BeOfType<Reservation>().Subject;
reservation.Should().NotBeNull();
reservation.RoomCode.Should().Be("H.4.308");
reservation.UserId.Should().Be("barackobama");
reservation.ReservationId.Should().Be(6);
TestData.Count.Should().Be(6);
}
[Test]
[Order(5)]
public async Task Update()
{
// Arrange
var repo = ReservationRepository.Object;
var originalReservation = TestData.FirstOrDefault(q => q.RoomCode == "H.4.308");
var reservationToUpdate = new Reservation
{
ReservationId = 6,
RoomCode = "H.4.308",
UserId = "barackobama",
StartTime = DateTime.Now,
EndTime = DateTime.Now.AddHours(3),
Description = "Work on assignment"
};
// Act
var result = await repo.Update(reservationToUpdate);
// Assert
var reservation = result.Should().BeOfType<Reservation>().Subject;
reservation.Should().NotBeNull();
reservation.RoomCode.Should().Be("H.4.308");
reservation.UserId.Should().Be("barackobama");
reservation.ReservationId.Should().Be(6);
reservation.Should().NotBeSameAs(originalReservation);
}
[Test]
[Order(6)]
public async Task Delete()
{
// Arrange
var repo = ReservationRepository.Object;
var reservationToDelete = TestData[0];
// Act
var result = await repo.Delete(reservationToDelete.ReservationId);
// Assert
result.Should().BeTrue();
TestData.Count.Should().Be(5);
}
#endregion
#region Setup Data
private void InitializeData()
{
var reservations = new List<Reservation>
{
new Reservation { ReservationId = 1, UserId = "bobdylan", RoomCode = "H.5.314", Description = "Meeting", StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(3) },
new Reservation { ReservationId = 2, UserId = "barackobama", RoomCode = "WD.01.016", Description = "Meeting with administrators", StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(1) },
new Reservation { ReservationId = 3, UserId = "spongebobsquarepants", RoomCode = "H.4.312", Description = "Working with team on project", StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(4) },
new Reservation { ReservationId = 4, UserId = "adminhenk", RoomCode = "H.2.203", Description = "Hiding from boss", StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(8) },
new Reservation { ReservationId = 5, UserId = "jansmit", RoomCode = "WD.04.002", Description = "Secret karaoke", StartTime = DateTime.ParseExact("2018-06-12 11:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), EndTime = DateTime.ParseExact("2018-06-12 13:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) },
};
this.TestData = reservations;
}
#endregion
}
}