Skip to content

Commit 1aed13f

Browse files
authored
Merge pull request #7 from fatihdurgut/CR6
Refactor message entity and add validation
2 parents 46ecb87 + c097096 commit 1aed13f

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sarif-viewer.connectToGithubCodeScanning": "off"
3+
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
using System.ComponentModel.DataAnnotations;
22

3-
namespace RazorPagesTestSample.Data
3+
/// <summary>
4+
/// Represents a message entity with an ID and text content.
5+
/// </summary>
6+
namespace RazorPagesTestSample
47
{
5-
#region snippet1
68
public class Message
79
{
10+
/// <summary>
11+
/// Gets or sets the unique identifier for the message.
12+
/// </summary>
813
public int Id { get; set; }
914

15+
/// <summary>
16+
/// Gets or sets the text content of the message.
17+
/// </summary>
18+
/// <remarks>
19+
/// The text content is required and must be a string with a maximum length of 250 characters.
20+
/// </remarks>
1021
[Required]
1122
[DataType(DataType.Text)]
12-
[StringLength(200, ErrorMessage = "There's a 200 character limit on messages. Please shorten your message.")]
23+
[StringLength(250, ErrorMessage = "There's a 250 character limit on messages. Please shorten your message.")]
1324
public string Text { get; set; }
1425
}
15-
#endregion
16-
}
26+
27+
}

src/Application/tests/RazorPagesTestSample.Tests/RazorPagesTestSample.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
1515
<PackageReference Include="Moq" Version="4.20.70" />
16-
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
16+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1717
<PackageReference Include="System.Diagnostics.TraceSource" Version="4.3.0" />
1818
<PackageReference Include="System.Net.Http" Version="4.3.4" />
1919
<PackageReference Include="xunit" Version="2.7.0" />

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

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

89
namespace RazorPagesTestSample.Tests.UnitTests
@@ -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
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using RazorPagesTestSample.Pages;
1616
using RazorPagesTestSample.Data;
1717

18+
1819
namespace RazorPagesTestSample.Tests.UnitTests
1920
{
2021
public class IndexPageTests

0 commit comments

Comments
 (0)