-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddNotificationTests.cs
More file actions
112 lines (92 loc) · 2.98 KB
/
Copy pathAddNotificationTests.cs
File metadata and controls
112 lines (92 loc) · 2.98 KB
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
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Notifications.Database;
using Notifications.Models;
namespace Notifications.API.Tests;
[TestFixture]
public class AddNotificationTests
{
private AppDbContext _dbContext;
[SetUp]
public async Task Setup()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(databaseName: "RepositoryTest")
.Options;
_dbContext = new AppDbContext(options);
await _dbContext.Database.EnsureCreatedAsync();
}
[TearDown]
public async Task TearDown()
{
await _dbContext.Database.EnsureDeletedAsync();
await _dbContext.DisposeAsync();
}
[Test]
public async Task HappyPath_ReturnsSuccess()
{
// Arrange
var resource = new CreateNotificationDto
{
Message = "Test message",
DisplayStart = DateTime.Now.AddDays(-1),
DisplayEnd = DateTime.Now.AddDays(1),
};
// Act
var result = await Repository.AddNotification(resource, _dbContext);
// Assert
using var scope = new AssertionScope();
result.Should().BeOfType<Ok<Notification>>();
var notification = ((Ok<Notification>)result).Value;
notification.Should().BeOfType<Notification>();
notification.Should().NotBeNull();
notification.Message.Should().Be(resource.Message);
var current = await Repository.GetCurrentNotificationsAsync(_dbContext);
current.Should().Contain(notification);
}
[Test]
public async Task EmptyMessage_ReturnsBadRequest()
{
// Arrange
var resource = new CreateNotificationDto
{
Message = "",
DisplayStart = DateTime.Now.AddDays(-1),
DisplayEnd = DateTime.Now.AddDays(1),
};
// Act
var result = await Repository.AddNotification(resource, _dbContext);
// Assert
result.Should().BeOfType<BadRequest<List<string>>>();
}
[Test]
public async Task InvalidDates_ReturnsBadRequest()
{
// Arrange
var resource = new CreateNotificationDto
{
Message = "Test message",
DisplayStart = DateTime.Now.AddDays(1),
DisplayEnd = DateTime.Now.AddDays(-1),
};
// Act
var result = await Repository.AddNotification(resource, _dbContext);
// Assert
result.Should().BeOfType<BadRequest<List<string>>>();
}
[Test]
public async Task EndDateInPast_ReturnsBadRequest()
{
// Arrange
var resource = new CreateNotificationDto
{
Message = "Test message",
DisplayStart = DateTime.Now.AddDays(-2),
DisplayEnd = DateTime.Now.AddDays(-1),
};
// Act
var result = await Repository.AddNotification(resource, _dbContext);
// Assert
result.Should().BeOfType<BadRequest<List<string>>>();
}
}