-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathListingSourceCodeServiceTests.cs
More file actions
147 lines (116 loc) · 4.52 KB
/
ListingSourceCodeServiceTests.cs
File metadata and controls
147 lines (116 loc) · 4.52 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using EssentialCSharp.Web.Models;
using EssentialCSharp.Web.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
using Moq;
using Moq.AutoMock;
namespace EssentialCSharp.Web.Tests;
public class ListingSourceCodeServiceTests
{
[Fact]
public async Task GetListingAsync_WithValidChapterAndListing_ReturnsCorrectListing()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
ListingSourceCodeResponse? result = await service.GetListingAsync(1, 1);
// Assert
Assert.NotNull(result);
Assert.Equal(1, result.ChapterNumber);
Assert.Equal(1, result.ListingNumber);
Assert.Equal("cs", result.FileExtension);
Assert.NotEmpty(result.Content);
}
[Fact]
public async Task GetListingAsync_WithInvalidChapter_ReturnsNull()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
ListingSourceCodeResponse? result = await service.GetListingAsync(999, 1);
// Assert
Assert.Null(result);
}
[Fact]
public async Task GetListingAsync_WithInvalidListing_ReturnsNull()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
ListingSourceCodeResponse? result = await service.GetListingAsync(1, 999);
// Assert
Assert.Null(result);
}
[Fact]
public async Task GetListingAsync_DifferentFileExtension_AutoDiscoversFileExtension()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act - Get an XML file (01.02.xml exists in Chapter 1)
ListingSourceCodeResponse? result = await service.GetListingAsync(1, 2);
// Assert
Assert.NotNull(result);
Assert.Equal("xml", result.FileExtension);
}
[Fact]
public async Task GetListingsByChapterAsync_WithValidChapter_ReturnsAllListings()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(1);
// Assert
Assert.NotEmpty(results);
Assert.All(results, r => Assert.Equal(1, r.ChapterNumber));
Assert.All(results, r => Assert.NotEmpty(r.Content));
Assert.All(results, r => Assert.NotEmpty(r.FileExtension));
// Verify results are ordered
Assert.Equal(results.OrderBy(r => r.ListingNumber).ToList(), results);
}
[Fact]
public async Task GetListingsByChapterAsync_DirectoryContainsNonListingFiles_ExcludesNonListingFiles()
{
// Arrange - Chapter 10 has Employee.cs which doesn't match the pattern
ListingSourceCodeService service = CreateService();
// Act
IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(10);
// Assert
Assert.NotEmpty(results);
// Ensure all results match the {CC}.{LL}.{ext} pattern
Assert.All(results, r =>
{
Assert.Equal(10, r.ChapterNumber);
Assert.InRange(r.ListingNumber, 1, 99);
});
}
[Fact]
public async Task GetListingsByChapterAsync_WithInvalidChapter_ReturnsEmptyList()
{
// Arrange
ListingSourceCodeService service = CreateService();
// Act
IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(999);
// Assert
Assert.Empty(results);
}
private static ListingSourceCodeService CreateService()
{
DirectoryInfo testDataRoot = GetTestDataPath();
AutoMocker mocker = new();
Mock<IWebHostEnvironment> mockWebHostEnvironment = mocker.GetMock<IWebHostEnvironment>();
mockWebHostEnvironment.Setup(m => m.ContentRootPath).Returns(testDataRoot.FullName);
mockWebHostEnvironment.Setup(m => m.ContentRootFileProvider).Returns(new PhysicalFileProvider(testDataRoot.FullName));
return mocker.CreateInstance<ListingSourceCodeService>();
}
private static DirectoryInfo GetTestDataPath()
{
string baseDirectory = AppContext.BaseDirectory;
string testDataPath = Path.Join(baseDirectory, "TestData");
DirectoryInfo testDataDirectory = new(testDataPath);
if (!testDataDirectory.Exists)
{
throw new InvalidOperationException($"TestData directory not found at: {testDataDirectory.FullName}");
}
return testDataDirectory;
}
}