Skip to content

Commit 72f7b1d

Browse files
committed
Refactor message entity and add validation
1 parent 2c36513 commit 72f7b1d

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/Application/tests/RazorPagesTestSample.Tests/UnitTests/DataAccessLayerTest.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.EntityFrameworkCore;
55
using Xunit;
66
using RazorPagesTestSample.Data;
7+
using System.ComponentModel.DataAnnotations;
78

89
namespace RazorPagesTestSample.Tests.UnitTests
910
{
@@ -25,7 +26,7 @@ public async Task GetMessagesAsync_MessagesAreReturned()
2526
// Assert
2627
var actualMessages = Assert.IsAssignableFrom<List<Message>>(result);
2728
Assert.Equal(
28-
expectedMessages.OrderBy(m => m.Id).Select(m => m.Text),
29+
expectedMessages.OrderBy(m => m.Id).Select(m => m.Text),
2930
actualMessages.OrderBy(m => m.Id).Select(m => m.Text));
3031
}
3132
}
@@ -77,7 +78,7 @@ public async Task DeleteMessageAsync_MessageIsDeleted_WhenMessageIsFound()
7778
await db.AddRangeAsync(seedMessages);
7879
await db.SaveChangesAsync();
7980
var recId = 1;
80-
var expectedMessages =
81+
var expectedMessages =
8182
seedMessages.Where(message => message.Id != recId).ToList();
8283
#endregion
8384

@@ -90,7 +91,7 @@ public async Task DeleteMessageAsync_MessageIsDeleted_WhenMessageIsFound()
9091
// Assert
9192
var actualMessages = await db.Messages.AsNoTracking().ToListAsync();
9293
Assert.Equal(
93-
expectedMessages.OrderBy(m => m.Id).Select(m => m.Text),
94+
expectedMessages.OrderBy(m => m.Id).Select(m => m.Text),
9495
actualMessages.OrderBy(m => m.Id).Select(m => m.Text));
9596
#endregion
9697
}
@@ -121,10 +122,36 @@ public async Task DeleteMessageAsync_NoMessageIsDeleted_WhenMessageIsNotFound()
121122
// Assert
122123
var actualMessages = await db.Messages.AsNoTracking().ToListAsync();
123124
Assert.Equal(
124-
expectedMessages.OrderBy(m => m.Id).Select(m => m.Text),
125+
expectedMessages.OrderBy(m => m.Id).Select(m => m.Text),
125126
actualMessages.OrderBy(m => m.Id).Select(m => m.Text));
126127
}
127128
}
128129
#endregion
130+
131+
132+
[Theory]
133+
[InlineData(150, true)]
134+
[InlineData(199, true)]
135+
[InlineData(200, true)]
136+
[InlineData(201, true)]
137+
[InlineData(249, true)]
138+
[InlineData(250, true)]
139+
[InlineData(251, false)]
140+
[InlineData(300, false)]
141+
public async Task AddMessageAsync_TestMessageLength(int messageLength, bool expectedValidMessage)
142+
{
143+
using (var db = new AppDbContext(Utilities.TestDbContextOptions()))
144+
{
145+
// Arrange
146+
var recId = 10;
147+
var expectedMessage = new Message() { Id = recId, Text = new string('X', messageLength) };
148+
149+
// Act
150+
var isValidMessage = Validator.TryValidateObject(expectedMessage, new ValidationContext(expectedMessage), null, validateAllProperties: true);
151+
152+
// Assert
153+
Assert.Equal(expectedValidMessage, isValidMessage);
154+
}
155+
}
129156
}
130157
}

0 commit comments

Comments
 (0)