|
| 1 | +using EssentialCSharp.Web.Models; |
| 2 | +using EssentialCSharp.Web.Services; |
| 3 | +using Microsoft.AspNetCore.Hosting; |
| 4 | +using Microsoft.Extensions.FileProviders; |
| 5 | +using Moq; |
| 6 | +using Moq.AutoMock; |
| 7 | + |
| 8 | +namespace EssentialCSharp.Web.Tests; |
| 9 | + |
| 10 | +public class ListingSourceCodeServiceTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public async Task GetListingAsync_WithValidChapterAndListing_ReturnsCorrectListing() |
| 14 | + { |
| 15 | + // Arrange |
| 16 | + ListingSourceCodeService service = CreateService(); |
| 17 | + |
| 18 | + // Act |
| 19 | + ListingSourceCodeResponse? result = await service.GetListingAsync(1, 1); |
| 20 | + |
| 21 | + // Assert |
| 22 | + Assert.NotNull(result); |
| 23 | + Assert.Equal(1, result.ChapterNumber); |
| 24 | + Assert.Equal(1, result.ListingNumber); |
| 25 | + Assert.Equal("cs", result.FileExtension); |
| 26 | + Assert.NotEmpty(result.Content); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public async Task GetListingAsync_WithInvalidChapter_ReturnsNull() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + ListingSourceCodeService service = CreateService(); |
| 34 | + |
| 35 | + // Act |
| 36 | + ListingSourceCodeResponse? result = await service.GetListingAsync(999, 1); |
| 37 | + |
| 38 | + // Assert |
| 39 | + Assert.Null(result); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public async Task GetListingAsync_WithInvalidListing_ReturnsNull() |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + ListingSourceCodeService service = CreateService(); |
| 47 | + |
| 48 | + // Act |
| 49 | + ListingSourceCodeResponse? result = await service.GetListingAsync(1, 999); |
| 50 | + |
| 51 | + // Assert |
| 52 | + Assert.Null(result); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task GetListingAsync_DifferentFileExtension_AutoDiscoversFileExtension() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + ListingSourceCodeService service = CreateService(); |
| 60 | + |
| 61 | + // Act - Get an XML file (01.02.xml exists in Chapter 1) |
| 62 | + ListingSourceCodeResponse? result = await service.GetListingAsync(1, 2); |
| 63 | + |
| 64 | + // Assert |
| 65 | + Assert.NotNull(result); |
| 66 | + Assert.Equal("xml", result.FileExtension); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task GetListingsByChapterAsync_WithValidChapter_ReturnsAllListings() |
| 71 | + { |
| 72 | + // Arrange |
| 73 | + ListingSourceCodeService service = CreateService(); |
| 74 | + |
| 75 | + // Act |
| 76 | + IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(1); |
| 77 | + |
| 78 | + // Assert |
| 79 | + Assert.NotEmpty(results); |
| 80 | + Assert.All(results, r => Assert.Equal(1, r.ChapterNumber)); |
| 81 | + Assert.All(results, r => Assert.NotEmpty(r.Content)); |
| 82 | + Assert.All(results, r => Assert.NotEmpty(r.FileExtension)); |
| 83 | + |
| 84 | + // Verify results are ordered |
| 85 | + Assert.Equal(results.OrderBy(r => r.ListingNumber).ToList(), results); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public async Task GetListingsByChapterAsync_DirectoryContainsNonListingFiles_ExcludesNonListingFiles() |
| 90 | + { |
| 91 | + // Arrange - Chapter 10 has Employee.cs which doesn't match the pattern |
| 92 | + ListingSourceCodeService service = CreateService(); |
| 93 | + |
| 94 | + // Act |
| 95 | + IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(10); |
| 96 | + |
| 97 | + // Assert |
| 98 | + Assert.NotEmpty(results); |
| 99 | + |
| 100 | + // Ensure all results match the {CC}.{LL}.{ext} pattern |
| 101 | + Assert.All(results, r => |
| 102 | + { |
| 103 | + Assert.Equal(10, r.ChapterNumber); |
| 104 | + Assert.InRange(r.ListingNumber, 1, 99); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public async Task GetListingsByChapterAsync_WithInvalidChapter_ReturnsEmptyList() |
| 110 | + { |
| 111 | + // Arrange |
| 112 | + ListingSourceCodeService service = CreateService(); |
| 113 | + |
| 114 | + // Act |
| 115 | + IReadOnlyList<ListingSourceCodeResponse> results = await service.GetListingsByChapterAsync(999); |
| 116 | + |
| 117 | + // Assert |
| 118 | + Assert.Empty(results); |
| 119 | + } |
| 120 | + |
| 121 | + private static ListingSourceCodeService CreateService() |
| 122 | + { |
| 123 | + DirectoryInfo testDataRoot = GetTestDataPath(); |
| 124 | + |
| 125 | + AutoMocker mocker = new(); |
| 126 | + Mock<IWebHostEnvironment> mockWebHostEnvironment = mocker.GetMock<IWebHostEnvironment>(); |
| 127 | + mockWebHostEnvironment.Setup(m => m.ContentRootPath).Returns(testDataRoot.FullName); |
| 128 | + mockWebHostEnvironment.Setup(m => m.ContentRootFileProvider).Returns(new PhysicalFileProvider(testDataRoot.FullName)); |
| 129 | + |
| 130 | + return mocker.CreateInstance<ListingSourceCodeService>(); |
| 131 | + } |
| 132 | + |
| 133 | + private static DirectoryInfo GetTestDataPath() |
| 134 | + { |
| 135 | + string baseDirectory = AppContext.BaseDirectory; |
| 136 | + string testDataPath = Path.Combine(baseDirectory, "TestData"); |
| 137 | + |
| 138 | + DirectoryInfo testDataDirectory = new(testDataPath); |
| 139 | + |
| 140 | + if (!testDataDirectory.Exists) |
| 141 | + { |
| 142 | + throw new InvalidOperationException($"TestData directory not found at: {testDataDirectory.FullName}"); |
| 143 | + } |
| 144 | + |
| 145 | + return testDataDirectory; |
| 146 | + } |
| 147 | +} |
0 commit comments