Skip to content

Commit 0d84665

Browse files
author
Brian Cummings
committed
Added more tests
1 parent 8a9c4a9 commit 0d84665

File tree

2 files changed

+241
-218
lines changed

2 files changed

+241
-218
lines changed

PathfinderHonorManager.Tests/Validator/HonorValidatorTests.cs

Lines changed: 41 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -14,182 +14,83 @@ namespace PathfinderHonorManager.Tests.Validator
1414
[TestFixture]
1515
public class HonorValidatorTests
1616
{
17-
private DbContextOptions<PathfinderContext> _dbContextOptions;
18-
private HonorValidator _validator;
17+
private HonorValidator _honorValidator;
18+
protected DbContextOptions<PathfinderContext> ContextOptions { get; }
1919

20-
[SetUp]
21-
public void Setup()
20+
public HonorValidatorTests()
2221
{
23-
_dbContextOptions = new DbContextOptionsBuilder<PathfinderContext>()
22+
ContextOptions = new DbContextOptionsBuilder<PathfinderContext>()
2423
.UseInMemoryDatabase(databaseName: "TestDb")
2524
.Options;
26-
27-
using (var context = new PathfinderContext(_dbContextOptions))
28-
{
29-
// Seed the database with some data
30-
context.Honors.Add(new Honor
31-
{
32-
HonorID = Guid.NewGuid(),
33-
Name = "Honor 1",
34-
Level = 1,
35-
PatchFilename = "patch1.png",
36-
WikiPath = new Uri("https://example.com/honor1")
37-
});
38-
39-
context.Honors.Add(new Honor
40-
{
41-
HonorID = Guid.NewGuid(),
42-
Name = "Honor 2",
43-
Level = 2,
44-
PatchFilename = "patch2.png",
45-
WikiPath = new Uri("https://example.com/honor2")
46-
});
47-
48-
context.SaveChanges();
49-
}
50-
51-
_validator = new HonorValidator(new PathfinderContext(_dbContextOptions));
52-
}
53-
54-
55-
[TestCase]
56-
public async Task Validate_HonorDto_WithValidData_ShouldPass()
57-
{
58-
// Arrange
59-
var honorDto = new Incoming.HonorDto
60-
{
61-
Name = "Test Honor",
62-
Level = 1,
63-
PatchFilename = "test.png",
64-
WikiPath = new Uri("https://example.com")
65-
};
66-
67-
// Act
68-
var result = await _validator
69-
.TestValidateAsync(honorDto);
70-
71-
// Assert
72-
result
73-
.ShouldNotHaveAnyValidationErrors();
74-
}
75-
76-
[TestCase("")]
77-
[TestCase(null)]
78-
public async Task Validate_HonorDto_WithMissingName_ShouldFail(string name)
79-
{
80-
// Arrange
81-
var honorDto = new Incoming.HonorDto
82-
{
83-
Name = name,
84-
Level = 1,
85-
PatchFilename = "test.png",
86-
WikiPath = new Uri("https://example.com")
87-
};
88-
89-
// Act
90-
var result = await _validator
91-
.TestValidateAsync(honorDto);
92-
93-
// Assert
94-
result
95-
.ShouldHaveValidationErrorFor(x => x.Name)
96-
.WithSeverity(Severity.Error);
9725
}
9826

99-
[TestCase(null)]
100-
public async Task Validate_HonorDto_WithMissingLevel_ShouldFail(int level)
27+
[SetUp]
28+
public void SetUp()
10129
{
102-
// Arrange
103-
var honorDto = new Incoming.HonorDto
104-
{
105-
Name = "Test Honor",
106-
Level = level,
107-
PatchFilename = "test.png",
108-
WikiPath = new Uri("https://example.com")
109-
};
110-
111-
// Act
112-
var result = await _validator.TestValidateAsync(honorDto);
113-
114-
// Assert
115-
result.ShouldHaveValidationErrorFor(x => x.Level);
30+
var context = new PathfinderContext(ContextOptions);
31+
_honorValidator = new HonorValidator(context);
11632
}
11733

118-
[TestCase("invalid")]
119-
public async Task Validate_HonorDto_WithInvalidPatchFilename_ShouldFail(string patchFilename)
34+
[TestCase("test.txt")]
35+
[TestCase("test")]
36+
public async Task Validate_InvalidFileExtension_ShouldFail(string filename)
12037
{
121-
// Arrange
12238
var honorDto = new Incoming.HonorDto
12339
{
12440
Name = "Test Honor",
125-
Level = 1,
126-
PatchFilename = patchFilename,
41+
Level = 2,
42+
PatchFilename = filename,
12743
WikiPath = new Uri("https://example.com")
12844
};
12945

130-
// Act
131-
var result = await _validator.TestValidateAsync(honorDto);
132-
133-
// Assert
46+
var result = await _honorValidator.TestValidateAsync(honorDto);
13447
result.ShouldHaveValidationErrorFor(x => x.PatchFilename);
13548
}
13649

137-
[TestCase("invalid")]
138-
public async Task Validate_HonorDto_WithInvalidWikiPath_ShouldFail(string wikiPath)
50+
[TestCase("ftp://example.com")]
51+
[TestCase("file://example.com")]
52+
public async Task Validate_NonHttpUri_ShouldFail(string url)
13953
{
140-
// Arrange
14154
var honorDto = new Incoming.HonorDto
14255
{
14356
Name = "Test Honor",
144-
Level = 1,
57+
Level = 2,
14558
PatchFilename = "test.png",
146-
WikiPath = new Uri(wikiPath, UriKind.RelativeOrAbsolute)
59+
WikiPath = new Uri(url)
14760
};
14861

149-
// Act
150-
var result = await _validator.TestValidateAsync(honorDto);
151-
152-
// Assert
62+
var result = await _honorValidator.TestValidateAsync(honorDto);
15363
result.ShouldHaveValidationErrorFor(x => x.WikiPath);
15464
}
15565

156-
[TestCase]
157-
public async Task Validate_HonorDto_WithDuplicateName_ShouldFail()
66+
[Test]
67+
public async Task Validate_DuplicateName_InPostRuleset_ShouldFail()
15868
{
159-
// Arrange
160-
var honorDto = new Incoming.HonorDto
69+
using (var context = new PathfinderContext(ContextOptions))
16170
{
162-
Name = "Test Honor",
163-
Level = 1,
164-
PatchFilename = "test.png",
165-
WikiPath = new Uri("https://example.com")
166-
};
71+
await context.Honors.AddAsync(new Honor
72+
{
73+
Name = "Existing Honor",
74+
Level = 1,
75+
PatchFilename = "test.png",
76+
WikiPath = new Uri("https://example.com")
77+
});
78+
await context.SaveChangesAsync();
16779

168-
using (var context = new PathfinderContext(_dbContextOptions))
169-
{
170-
// Add a duplicate honor to the database
171-
var existingHonor = new Honor
80+
var validator = new HonorValidator(context);
81+
var honorDto = new Incoming.HonorDto
17282
{
173-
Name = honorDto.Name,
174-
Level = honorDto.Level,
175-
PatchFilename = honorDto.PatchFilename,
176-
WikiPath = honorDto.WikiPath
83+
Name = "Existing Honor",
84+
Level = 2,
85+
PatchFilename = "test.png",
86+
WikiPath = new Uri("https://example.com")
17787
};
178-
await context.Honors.AddAsync(existingHonor);
179-
await context.SaveChangesAsync();
180-
}
18188

182-
// Act
183-
var result = await _validator
184-
.TestValidateAsync(honorDto, options =>
185-
{
186-
options.IncludeAllRuleSets();
187-
});
89+
var result = await validator.TestValidateAsync(honorDto, opt =>
90+
opt.IncludeRuleSets("post"));
18891

189-
// Assert
190-
result
191-
.ShouldHaveValidationErrorFor(x => x.Name)
192-
.WithSeverity(Severity.Error);
92+
result.ShouldHaveValidationErrorFor(x => x.Name);
93+
}
19394
}
19495
}
19596
}

0 commit comments

Comments
 (0)