Adding more tests and fixing subtitle identification

This commit is contained in:
Zoe Roux 2021-07-23 19:36:43 +02:00
parent e55f60166e
commit 03bf2c4f10
2 changed files with 66 additions and 8 deletions

View File

@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Kyoo.Controllers;
using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Kyoo.Models.Options;
using Microsoft.Extensions.Options;
using Moq;
@ -23,6 +24,10 @@ namespace Kyoo.Tests.Identifier
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))? S(?<Season>\\d+)E(?<Episode>\\d+)\\..*$",
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))? (?<Absolute>\\d+)\\..*$",
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))?\\..*$"
},
SubtitleRegex = new[]
{
"^(?<Episode>.+)\\.(?<Language>\\w{1,3})\\.(?<Default>default\\.)?(?<Forced>forced\\.)?.*$"
}
});
@ -131,5 +136,57 @@ namespace Kyoo.Tests.Identifier
Assert.Null(episode.EpisodeNumber);
Assert.Null(episode.AbsoluteNumber);
}
[Fact]
public async Task InvalidEpisodeIdentification()
{
_manager.Setup(x => x.GetAll(null, new Sort<Library>(), default)).ReturnsAsync(new[]
{
new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}}
});
await Assert.ThrowsAsync<IdentificationFailedException>(() => _identifier.Identify("/invalid/path"));
}
[Fact]
public async Task SubtitleIdentification()
{
_manager.Setup(x => x.GetAll(null, new Sort<Library>(), default)).ReturnsAsync(new[]
{
new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}}
});
Track track = await _identifier.IdentifyTrack("/kyoo/Library/Collection/Show (2000)/Show.eng.default.str");
Assert.True(track.IsExternal);
Assert.Equal("eng", track.Language);
Assert.Equal("subrip", track.Codec);
Assert.True(track.IsDefault);
Assert.False(track.IsForced);
Assert.StartsWith("/kyoo/Library/Collection/Show (2000)/Show", track.Episode.Path);
}
[Fact]
public async Task SubtitleIdentificationUnknownCodec()
{
_manager.Setup(x => x.GetAll(null, new Sort<Library>(), default)).ReturnsAsync(new[]
{
new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}}
});
Track track = await _identifier.IdentifyTrack("/kyoo/Library/Collection/Show (2000)/Show.eng.default.extension");
Assert.True(track.IsExternal);
Assert.Equal("eng", track.Language);
Assert.Equal("extension", track.Codec);
Assert.True(track.IsDefault);
Assert.False(track.IsForced);
Assert.StartsWith("/kyoo/Library/Collection/Show (2000)/Show", track.Episode.Path);
}
[Fact]
public async Task InvalidSubtitleIdentification()
{
_manager.Setup(x => x.GetAll(null, new Sort<Library>(), default)).ReturnsAsync(new[]
{
new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}}
});
await Assert.ThrowsAsync<IdentificationFailedException>(() => _identifier.IdentifyTrack("/invalid/path"));
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
@ -109,32 +110,32 @@ namespace Kyoo.Controllers
}
/// <inheritdoc />
public async Task<Track> IdentifyTrack(string path)
public Task<Track> IdentifyTrack(string path)
{
string relativePath = await _GetRelativePath(path);
Match match = _configuration.CurrentValue.SubtitleRegex
.Select(x => new Regex(x, RegexOptions.IgnoreCase | RegexOptions.Compiled))
.Select(x => x.Match(relativePath))
.Select(x => x.Match(path))
.FirstOrDefault(x => x.Success);
if (match == null)
throw new IdentificationFailedException($"The subtitle at {path} does not match the subtitle's regex.");
string episodePath = match.Groups["Episode"].Value;
return new Track
string extension = Path.GetExtension(path);
return Task.FromResult(new Track
{
Type = StreamType.Subtitle,
Language = match.Groups["Language"].Value,
IsDefault = match.Groups["Default"].Value.Length > 0,
IsForced = match.Groups["Forced"].Value.Length > 0,
Codec = FileExtensions.SubtitleExtensions[Path.GetExtension(path)],
IsDefault = match.Groups["Default"].Success,
IsForced = match.Groups["Forced"].Success,
Codec = FileExtensions.SubtitleExtensions.GetValueOrDefault(extension, extension[1..]),
IsExternal = true,
Path = path,
Episode = new Episode
{
Path = episodePath
}
};
});
}
}
}