-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache for PDF files, bump version to v6.1.0 (#65)
* Add cache for PDF files * PDFs are created when outdated, or taken from cache * ReportSheetCache added as scoped service * _Layout extended to render a section with meta tags if needed * Match.Results has "noFollow" meta tag for robots * Match.Fixtures has "noFollow" meta tag for robots * Add unit tests for PDF caching * Bump version to v6.1.0
- Loading branch information
Showing
21 changed files
with
462 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// | ||
// Copyright Volleyball League Project maintainers and contributors. | ||
// Licensed under the MIT license. | ||
// | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using League.Caching; | ||
using League.Tests.TestComponents; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using TournamentManager.DAL.TypedViewClasses; | ||
using TournamentManager.MultiTenancy; | ||
|
||
namespace League.Tests.Caching; | ||
|
||
[TestFixture] | ||
public class ReportSheetCacheTests | ||
{ | ||
private readonly ReportSheetCache _cache; | ||
private readonly ITenantContext _tenantContext; | ||
private readonly IWebHostEnvironment _webHostEnvironment; | ||
|
||
public ReportSheetCacheTests() | ||
{ | ||
_webHostEnvironment = new HostingEnvironment { | ||
WebRootPath = Path.GetTempPath(), ContentRootPath | ||
// Because we use the Chromium installation in the demo web app | ||
= DirectoryLocator.GetTargetProjectPath(typeof(League.WebApp.WebAppStartup)) | ||
}; | ||
|
||
_tenantContext = new TenantContext | ||
{ | ||
Identifier = "testorg" | ||
}; | ||
|
||
var chromiumPath = new List<KeyValuePair<string, string?>> | ||
{ new("Chromium:ExecutablePath", "Chromium-Win\\chrome.exe") }; | ||
|
||
IServiceProvider services = UnitTestHelpers.GetReportSheetCacheServiceProvider(_tenantContext, _webHostEnvironment, chromiumPath); | ||
_cache = services.GetRequiredService<ReportSheetCache>(); | ||
} | ||
|
||
[TestCase("en")] | ||
[TestCase("de")] | ||
public async Task CreateNewPdfInOutputPath(string c) | ||
{ | ||
DeleteOutputFolder(); | ||
var culture = CultureInfo.GetCultureInfo(c); | ||
using var switcher = new CultureSwitcher(culture, culture); | ||
|
||
var data = new MatchReportSheetRow | ||
{ Id = 1234, ModifiedOn = new DateTime(2023, 03, 22, 12, 0, 0).ToUniversalTime() }; | ||
|
||
var stream = await _cache.GetOrCreatePdf(data, "<html><body>Some text</body></html>", CancellationToken.None); | ||
var fileName = Path.GetFileName(((FileStream) stream).Name); | ||
await stream.DisposeAsync(); | ||
|
||
Assert.That(fileName, | ||
Is.EqualTo(string.Format(ReportSheetCache.ReportSheetFilenameTemplate, _tenantContext.Identifier, data.Id, | ||
culture.TwoLetterISOLanguageName))); | ||
} | ||
|
||
[TestCase("en")] | ||
[TestCase("de")] | ||
public async Task ShouldReturnExistingPdfFromCache(string c) | ||
{ | ||
DeleteOutputFolder(); | ||
var culture = CultureInfo.GetCultureInfo(c); | ||
using var switcher = new CultureSwitcher(culture, culture); | ||
|
||
var data = new MatchReportSheetRow | ||
{ Id = 1234, ModifiedOn = new DateTime(2023, 03, 22, 12, 0, 0).ToUniversalTime() }; | ||
|
||
// (1) This should create the file in the cache | ||
var stream1 = await _cache.GetOrCreatePdf(data, "<html><body>Some text</body></html>", CancellationToken.None); | ||
var fileInfo1 = new FileInfo(Path.GetFileName(((FileStream) stream1).Name)); | ||
await stream1.DisposeAsync(); | ||
|
||
// (1) This should return the file from the cache | ||
var stream2 = await _cache.GetOrCreatePdf(data, "<html><body>Some text</body></html>", CancellationToken.None); | ||
var fileInfo2 = new FileInfo(Path.GetFileName(((FileStream) stream1).Name)); | ||
await stream2.DisposeAsync(); | ||
|
||
// Assert the file was not created again | ||
Assert.That(fileInfo1.CreationTimeUtc.Ticks, Is.EqualTo(fileInfo2.CreationTimeUtc.Ticks)); | ||
} | ||
|
||
private void DeleteOutputFolder() | ||
{ | ||
var outputFolder = Path.Combine(_webHostEnvironment.WebRootPath, ReportSheetCache.ReportSheetCacheFolder); | ||
|
||
// Delete folder in TempPath | ||
if (!Directory.Exists(outputFolder)) return; | ||
Directory.Delete(outputFolder, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace League.Test.TestComponents; | ||
namespace League.Tests.TestComponents; | ||
|
||
public class CultureSwitcher : IDisposable | ||
{ | ||
private readonly CultureInfo _originalCulture; | ||
private readonly CultureInfo _originalUiCulture; | ||
private readonly CultureInfo? _originalDefaultThreadCulture; | ||
private readonly CultureInfo? _originalDefaultThreadUiCulture; | ||
|
||
public CultureSwitcher(CultureInfo culture, CultureInfo uiCulture) | ||
{ | ||
_originalCulture = CultureInfo.CurrentCulture; | ||
_originalUiCulture = CultureInfo.CurrentUICulture; | ||
SetCulture(culture, uiCulture); | ||
_originalDefaultThreadCulture = CultureInfo.DefaultThreadCurrentCulture; | ||
_originalDefaultThreadUiCulture = CultureInfo.DefaultThreadCurrentUICulture; | ||
SetCurrentCulture(culture, uiCulture); | ||
SetThreadDefaultCulture(culture, uiCulture); | ||
} | ||
|
||
private static void SetCulture(CultureInfo culture, CultureInfo uiCulture) | ||
private static void SetCurrentCulture(CultureInfo culture, CultureInfo uiCulture) | ||
{ | ||
CultureInfo.CurrentCulture = culture; | ||
CultureInfo.CurrentUICulture = uiCulture; | ||
} | ||
|
||
private static void SetThreadDefaultCulture(CultureInfo? culture, CultureInfo? uiCulture) | ||
{ | ||
CultureInfo.DefaultThreadCurrentCulture = culture; | ||
CultureInfo.DefaultThreadCurrentUICulture = uiCulture; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
GC.SuppressFinalize(this); | ||
SetCulture(_originalCulture, _originalUiCulture); | ||
SetCurrentCulture(_originalCulture, _originalUiCulture); | ||
SetThreadDefaultCulture(_originalDefaultThreadCulture, _originalDefaultThreadUiCulture); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Copyright Volleyball League Project maintainers and contributors. | ||
// Licensed under the MIT license. | ||
// | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.FileProviders; | ||
|
||
namespace League.Tests.TestComponents; | ||
public class HostingEnvironment : IWebHostEnvironment | ||
{ | ||
public string EnvironmentName { get; set; } = Microsoft.Extensions.Hosting.Environments.Development; | ||
|
||
public string ApplicationName { get; set; } = null!; | ||
|
||
public string WebRootPath { get; set; } = null!; | ||
|
||
public IFileProvider WebRootFileProvider { get; set; } = null!; | ||
|
||
public string ContentRootPath { get; set; } = null!; | ||
|
||
public IFileProvider ContentRootFileProvider { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.