Skip to content

Commit

Permalink
refactor: Simplify more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Mar 28, 2024
1 parent 5ed34a2 commit 7539d73
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace LinkDotNet.Blog.IntegrationTests.Web.Features;
public class TransformBlogPostRecordsJobTests : SqlDatabaseTestBase<BlogPost>
{
private readonly TransformBlogPostRecordsJob sut;
private readonly IRepository<BlogPostRecord> blogPostRecordRepository;
private readonly IRepository<UserRecord> userRecordRepository;
private readonly Repository<BlogPostRecord> blogPostRecordRepository;
private readonly Repository<UserRecord> userRecordRepository;

public TransformBlogPostRecordsJobTests()
{
Expand All @@ -37,23 +37,23 @@ public async Task ShouldTransformRecords()
{
// Arrange
var someDate = new DateOnly(2023, 08, 13);
var blogPosts = new List<BlogPost>
{
List<BlogPost> blogPosts =
[
new BlogPostBuilder().Build(),
new BlogPostBuilder().Build(),
new BlogPostBuilder().Build(),
new BlogPostBuilder().Build(),
};
];

await Repository.StoreBulkAsync(blogPosts);
var userRecords = new List<UserRecord>
{
List<UserRecord> userRecords =
[
new() { Id = "A", DateClicked = someDate, UrlClicked = $"blogPost/{blogPosts[0].Id}" },
new() { Id = "B", DateClicked = someDate, UrlClicked = $"blogPost/{blogPosts[0].Id}/suffix" },
new() { Id = "C", DateClicked = someDate.AddDays(-3), UrlClicked = $"blogPost/{blogPosts[1].Id}" },
new() { Id = "D", DateClicked = someDate.AddDays(-3), UrlClicked = $"blogPost/{blogPosts[1].Id}" },
new() { Id = "E", DateClicked = someDate.AddDays(-2), UrlClicked = $"blogPost/{blogPosts[2].Id}" }
};
];
await userRecordRepository.StoreBulkAsync(userRecords);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,53 @@
using LinkDotNet.Blog.Infrastructure.Persistence;
using LinkDotNet.Blog.Web.Features.Admin.Sitemap.Services;

namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Services
namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Services;

public sealed class SitemapServiceTests : IDisposable
{
public sealed class SitemapServiceTests : IDisposable
{
private const string OutputDirectory = "wwwroot";
private const string OutputFilename = $"{OutputDirectory}/sitemap.xml";
private readonly SitemapService sut;
private const string OutputDirectory = "wwwroot";
private const string OutputFilename = $"{OutputDirectory}/sitemap.xml";
private readonly SitemapService sut;

public SitemapServiceTests()
{
var repositoryMock = Substitute.For<IRepository<BlogPost>>();
sut = new SitemapService(repositoryMock, null, new XmlFileWriter());
Directory.CreateDirectory("wwwroot");
}
public SitemapServiceTests()
{
var repositoryMock = Substitute.For<IRepository<BlogPost>>();
sut = new SitemapService(repositoryMock, null, new XmlFileWriter());
Directory.CreateDirectory("wwwroot");
}

[Fact]
public async Task ShouldSaveSitemapUrlInCorrectFormat()
[Fact]
public async Task ShouldSaveSitemapUrlInCorrectFormat()
{
var urlSet = new SitemapUrlSet
{
var urlSet = new SitemapUrlSet()
{
Urls = new List<SitemapUrl>
{
new SitemapUrl
{
Location = "here",
},
},
};
await sut.SaveSitemapToFileAsync(urlSet);
Urls =
[
new SitemapUrl { Location = "here", }
],
};
await sut.SaveSitemapToFileAsync(urlSet);

var lines = await File.ReadAllTextAsync(OutputFilename);
lines.Should().Be(
@"<?xml version=""1.0"" encoding=""utf-8""?>
var lines = await File.ReadAllTextAsync(OutputFilename);
lines.Should().Be(
@"<?xml version=""1.0"" encoding=""utf-8""?>
<urlset xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">
<url>
<loc>here</loc>
</url>
</urlset>");
}
}

public void Dispose()
public void Dispose()
{
if (File.Exists(OutputFilename))
{
if (File.Exists(OutputFilename))
{
File.Delete(OutputFilename);
}
File.Delete(OutputFilename);
}

if (Directory.Exists(OutputDirectory))
{
Directory.Delete(OutputDirectory, true);
}
if (Directory.Exists(OutputDirectory))
{
Directory.Delete(OutputDirectory, true);
}
}
}
}

0 comments on commit 7539d73

Please sign in to comment.