Skip to content

Commit 2e963da

Browse files
authored
Merge pull request #89 from fboucher/83-fix-the-failing-tests
Fixes failing note endpoint tests
2 parents 7252771 + 2b53edc commit 2e963da

File tree

7 files changed

+817
-2
lines changed

7 files changed

+817
-2
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using FluentAssertions;
3+
using NoteBookmark.Domain;
4+
using Xunit;
5+
6+
namespace NoteBookmark.Api.Tests.Domain;
7+
8+
public class ContainsPlaceholderAttributeTests
9+
{
10+
[Fact]
11+
public void IsValid_ShouldReturnSuccess_WhenValueContainsPlaceholder()
12+
{
13+
// Arrange
14+
var attribute = new ContainsPlaceholderAttribute("topic");
15+
var validationContext = new ValidationContext(new object());
16+
17+
// Act
18+
var result = attribute.GetValidationResult("Find articles about {topic}", validationContext);
19+
20+
// Assert
21+
result.Should().Be(ValidationResult.Success);
22+
}
23+
24+
[Fact]
25+
public void IsValid_ShouldReturnError_WhenValueDoesNotContainPlaceholder()
26+
{
27+
// Arrange
28+
var attribute = new ContainsPlaceholderAttribute("topic");
29+
var validationContext = new ValidationContext(new object());
30+
31+
// Act
32+
var result = attribute.GetValidationResult("Find articles about something", validationContext);
33+
34+
// Assert
35+
result.Should().NotBe(ValidationResult.Success);
36+
result?.ErrorMessage.Should().Contain("topic");
37+
}
38+
39+
[Fact]
40+
public void IsValid_ShouldReturnSuccess_WhenValueIsNull()
41+
{
42+
// Arrange
43+
var attribute = new ContainsPlaceholderAttribute("content");
44+
var validationContext = new ValidationContext(new object());
45+
46+
// Act
47+
var result = attribute.GetValidationResult(null, validationContext);
48+
49+
// Assert
50+
result.Should().Be(ValidationResult.Success);
51+
}
52+
53+
[Fact]
54+
public void IsValid_ShouldReturnSuccess_WhenValueIsEmpty()
55+
{
56+
// Arrange
57+
var attribute = new ContainsPlaceholderAttribute("content");
58+
var validationContext = new ValidationContext(new object());
59+
60+
// Act
61+
var result = attribute.GetValidationResult("", validationContext);
62+
63+
// Assert
64+
result.Should().Be(ValidationResult.Success);
65+
}
66+
67+
[Fact]
68+
public void IsValid_ShouldReturnSuccess_WhenValueIsWhitespace()
69+
{
70+
// Arrange
71+
var attribute = new ContainsPlaceholderAttribute("content");
72+
var validationContext = new ValidationContext(new object());
73+
74+
// Act
75+
var result = attribute.GetValidationResult(" ", validationContext);
76+
77+
// Assert
78+
result.Should().Be(ValidationResult.Success);
79+
}
80+
81+
[Fact]
82+
public void Constructor_ShouldSetPlaceholder()
83+
{
84+
// Arrange & Act
85+
var attribute = new ContainsPlaceholderAttribute("custom");
86+
var validationContext = new ValidationContext(new object());
87+
88+
// Assert
89+
var result = attribute.GetValidationResult("text with {custom} placeholder", validationContext);
90+
result.Should().Be(ValidationResult.Success);
91+
}
92+
93+
[Fact]
94+
public void ErrorMessage_ShouldContainPlaceholderName()
95+
{
96+
// Arrange
97+
var attribute = new ContainsPlaceholderAttribute("myplaceholder");
98+
var validationContext = new ValidationContext(new object());
99+
100+
// Act
101+
var result = attribute.GetValidationResult("text without placeholder", validationContext);
102+
103+
// Assert
104+
result?.ErrorMessage.Should().Contain("myplaceholder");
105+
result?.ErrorMessage.Should().Contain("must contain");
106+
}
107+
108+
[Theory]
109+
[InlineData("Summary of {content}", "content", true)]
110+
[InlineData("Summary of content", "content", false)]
111+
[InlineData("Use {topic} for search", "topic", true)]
112+
[InlineData("Use topic for search", "topic", false)]
113+
[InlineData("Multiple {var1} and {var2}", "var1", true)]
114+
[InlineData("Multiple {var1} and {var2}", "var2", true)]
115+
[InlineData("Multiple var1 and var2", "var1", false)]
116+
public void IsValid_ShouldValidateCorrectly_ForVariousInputs(string value, string placeholder, bool shouldBeValid)
117+
{
118+
// Arrange
119+
var attribute = new ContainsPlaceholderAttribute(placeholder);
120+
var validationContext = new ValidationContext(new object());
121+
122+
// Act
123+
var result = attribute.GetValidationResult(value, validationContext);
124+
125+
// Assert
126+
if (shouldBeValid)
127+
{
128+
result.Should().Be(ValidationResult.Success);
129+
}
130+
else
131+
{
132+
result.Should().NotBe(ValidationResult.Success);
133+
}
134+
}
135+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using FluentAssertions;
2+
using NoteBookmark.Domain;
3+
using Xunit;
4+
5+
namespace NoteBookmark.Api.Tests.Domain;
6+
7+
public class NoteCategoriesTests
8+
{
9+
[Theory]
10+
[InlineData("ai", "AI")]
11+
[InlineData("AI", "AI")]
12+
[InlineData("cloud", "Cloud")]
13+
[InlineData("CLOUD", "Cloud")]
14+
[InlineData("data", "Data")]
15+
[InlineData("database", "Databases")]
16+
[InlineData("dev", "Programming")]
17+
[InlineData("devops", "DevOps")]
18+
[InlineData("lowcode", "LowCode")]
19+
[InlineData("misc", "Miscellaneous")]
20+
[InlineData("top", "Suggestion of the week")]
21+
[InlineData("oss", "Open Source")]
22+
[InlineData("del", "del")]
23+
public void GetCategory_ShouldReturnCorrectCategory_ForValidInput(string input, string expected)
24+
{
25+
// Act
26+
var result = NoteCategories.GetCategory(input);
27+
28+
// Assert
29+
result.Should().Be(expected);
30+
}
31+
32+
[Theory]
33+
[InlineData("unknown")]
34+
[InlineData("invalid")]
35+
[InlineData("")]
36+
public void GetCategory_ShouldReturnMiscellaneous_ForInvalidCategory(string input)
37+
{
38+
// Act
39+
var result = NoteCategories.GetCategory(input);
40+
41+
// Assert
42+
result.Should().Be("Miscellaneous");
43+
}
44+
45+
[Fact]
46+
public void GetCategory_ShouldReturnMiscellaneous_ForNullInput()
47+
{
48+
// Act
49+
var result = NoteCategories.GetCategory(null);
50+
51+
// Assert
52+
result.Should().Be("Miscellaneous");
53+
}
54+
55+
[Fact]
56+
public void GetCategory_ShouldBeCaseInsensitive()
57+
{
58+
// Arrange
59+
var inputs = new[] { "AI", "ai", "Ai", "aI" };
60+
61+
// Act & Assert
62+
foreach (var input in inputs)
63+
{
64+
var result = NoteCategories.GetCategory(input);
65+
result.Should().Be("AI");
66+
}
67+
}
68+
69+
[Fact]
70+
public void GetCategories_ShouldReturnAllCategories()
71+
{
72+
// Act
73+
var result = NoteCategories.GetCategories();
74+
75+
// Assert
76+
result.Should().NotBeNull();
77+
result.Should().HaveCount(11);
78+
result.Should().Contain("AI");
79+
result.Should().Contain("Cloud");
80+
result.Should().Contain("Data");
81+
result.Should().Contain("Databases");
82+
result.Should().Contain("DevOps");
83+
result.Should().Contain("LowCode");
84+
result.Should().Contain("Miscellaneous");
85+
result.Should().Contain("Programming");
86+
result.Should().Contain("Open Source");
87+
result.Should().Contain("Suggestion of the week");
88+
result.Should().Contain("del");
89+
}
90+
91+
[Fact]
92+
public void GetCategories_ShouldReturnListType()
93+
{
94+
// Act
95+
var result = NoteCategories.GetCategories();
96+
97+
// Assert
98+
result.Should().BeOfType<List<string>>();
99+
}
100+
}

src/NoteBookmark.Api.Tests/Domain/NoteTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,41 @@ public void Note_WhenCreated_HasCorrectDefaultValues()
1919
note.Category.Should().BeNull();
2020
}
2121

22+
[Fact]
23+
public void Note_Constructor_ShouldInitializePartitionKey_WithCurrentYearMonth()
24+
{
25+
// Act
26+
var note = new Note();
27+
28+
// Assert
29+
note.PartitionKey.Should().Be(DateTime.UtcNow.ToString("yyyy-MM"));
30+
}
31+
32+
[Fact]
33+
public void Note_Constructor_ShouldInitializeRowKey_WithValidGuid()
34+
{
35+
// Act
36+
var note = new Note();
37+
38+
// Assert
39+
note.RowKey.Should().NotBeNullOrEmpty();
40+
Guid.TryParse(note.RowKey, out _).Should().BeTrue();
41+
}
42+
43+
[Fact]
44+
public void Note_Constructor_ShouldInitializeDateAdded_WithCurrentUtcTime()
45+
{
46+
// Arrange
47+
var before = DateTime.UtcNow;
48+
49+
// Act
50+
var note = new Note();
51+
var after = DateTime.UtcNow;
52+
53+
// Assert
54+
note.DateAdded.Should().BeOnOrAfter(before).And.BeOnOrBefore(after);
55+
}
56+
2257
[Fact]
2358
public void Note_WhenPropertiesSet_ReturnsCorrectValues()
2459
{
@@ -41,4 +76,56 @@ public void Note_WhenPropertiesSet_ReturnsCorrectValues()
4176
note.Tags.Should().Be("azure, functions, serverless");
4277
note.Category.Should().Be("Technology");
4378
}
79+
80+
[Fact]
81+
public void Validate_ShouldReturnTrue_WhenCommentIsNotEmpty()
82+
{
83+
// Arrange
84+
var note = new Note { Comment = "This is a valid comment" };
85+
86+
// Act
87+
var result = note.Validate();
88+
89+
// Assert
90+
result.Should().BeTrue();
91+
}
92+
93+
[Fact]
94+
public void Validate_ShouldReturnFalse_WhenCommentIsNull()
95+
{
96+
// Arrange
97+
var note = new Note { Comment = null };
98+
99+
// Act
100+
var result = note.Validate();
101+
102+
// Assert
103+
result.Should().BeFalse();
104+
}
105+
106+
[Fact]
107+
public void Validate_ShouldReturnFalse_WhenCommentIsEmpty()
108+
{
109+
// Arrange
110+
var note = new Note { Comment = "" };
111+
112+
// Act
113+
var result = note.Validate();
114+
115+
// Assert
116+
result.Should().BeFalse();
117+
}
118+
119+
[Fact]
120+
public void Validate_ShouldReturnFalse_WhenCommentIsWhitespace()
121+
{
122+
// Arrange
123+
var note = new Note { Comment = " " };
124+
125+
// Act
126+
var result = note.Validate();
127+
128+
// Assert
129+
result.Should().BeFalse();
130+
}
44131
}

0 commit comments

Comments
 (0)