mirror of
https://github.com/Radarr/Radarr
synced 2024-12-25 17:27:59 +00:00
Truncating TvDb Overviews actually works. Stupid Recaps.
This commit is contained in:
parent
3a992a8d0c
commit
399b50ec8e
5 changed files with 55 additions and 46 deletions
|
@ -191,5 +191,33 @@ public void MaxOrDefault_should_return_max_when_collection_is_not_empty()
|
|||
//Resolve
|
||||
result.Should().Be(10);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_should_truncate_strings_to_max_specified_number_of_bytes()
|
||||
{
|
||||
//Setup
|
||||
var str = File.ReadAllText(@"Files\LongOverview.txt");
|
||||
|
||||
//Act
|
||||
var resultString = str.Truncate(1000);
|
||||
|
||||
//Resolve
|
||||
var result = new System.Text.UTF8Encoding().GetBytes(resultString);
|
||||
result.Length.Should().BeLessOrEqualTo(1000);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_should_not_truncate_string_shorter_than_max_bytes()
|
||||
{
|
||||
//Setup
|
||||
var str = "Hello World";
|
||||
|
||||
//Act
|
||||
var resultString = str.Truncate(1000);
|
||||
|
||||
//Resolve
|
||||
var result = new System.Text.UTF8Encoding().GetBytes(resultString);
|
||||
result.Length.Should().Be(11);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -480,48 +480,6 @@ public void RefreshEpisodeInfo_ignore_season_zero()
|
|||
result.Where(e => e.Ignored).Should().HaveCount(episodeCount);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RefreshEpisodeInfo_should_trim_overview_to_4000_characters()
|
||||
{
|
||||
//Arrange
|
||||
const int seriesId = 71663;
|
||||
const int episodeCount = 10;
|
||||
|
||||
var longOverview = File.ReadAllText(@".\Files\LongOverview.txt");
|
||||
|
||||
var fakeEpisodes = Builder<TvdbSeries>.CreateNew().With(
|
||||
c => c.Episodes =
|
||||
new List<TvdbEpisode>(Builder<TvdbEpisode>.CreateListOfSize(episodeCount).
|
||||
All()
|
||||
.With(l => l.Language = new TvdbLanguage(0, "eng", "a"))
|
||||
.With(e => e.SeasonNumber = 0)
|
||||
.TheLast(1)
|
||||
.With(e => e.Overview = longOverview)
|
||||
.Build())
|
||||
).With(c => c.Id = seriesId).Build();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateNew().With(c => c.SeriesId = seriesId).Build();
|
||||
|
||||
WithRealDb();
|
||||
|
||||
Db.Insert(fakeSeries);
|
||||
|
||||
Mocker.GetMock<TvDbProvider>()
|
||||
.Setup(c => c.GetSeries(seriesId, true))
|
||||
.Returns(fakeEpisodes);
|
||||
|
||||
Mocker.GetMock<SeasonProvider>()
|
||||
.Setup(s => s.IsIgnored(seriesId, 0))
|
||||
.Returns(false);
|
||||
|
||||
//Act
|
||||
Mocker.Resolve<EpisodeProvider>().RefreshEpisodeInfo(fakeSeries);
|
||||
|
||||
//Assert
|
||||
var result = Mocker.Resolve<EpisodeProvider>().GetEpisodeBySeries(seriesId).ToList();
|
||||
result.Should().HaveCount(episodeCount);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void new_episodes_only_calls_Insert()
|
||||
{
|
||||
|
|
|
@ -62,5 +62,30 @@ public static int MaxOrDefault(this IEnumerable<int> ints)
|
|||
|
||||
return intList.Max();
|
||||
}
|
||||
|
||||
public static string Truncate(this string s, int maxLength)
|
||||
{
|
||||
if (Encoding.UTF8.GetByteCount(s) <= maxLength)
|
||||
return s;
|
||||
var cs = s.ToCharArray();
|
||||
int length = 0;
|
||||
int i = 0;
|
||||
while (i < cs.Length)
|
||||
{
|
||||
int charSize = 1;
|
||||
if (i < (cs.Length - 1) && char.IsSurrogate(cs[i]))
|
||||
charSize = 2;
|
||||
int byteSize = Encoding.UTF8.GetByteCount(cs, i, charSize);
|
||||
if ((byteSize + length) <= maxLength)
|
||||
{
|
||||
i = i + charSize;
|
||||
length += byteSize;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
return s.Substring(0, i);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -328,10 +328,7 @@ public virtual void RefreshEpisodeInfo(Series series)
|
|||
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
||||
episodeToUpdate.Title = episode.EpisodeName;
|
||||
|
||||
if (episode.Overview.Length > 3998)
|
||||
episodeToUpdate.Overview = episode.Overview.Substring(0, 3998);
|
||||
else
|
||||
episodeToUpdate.Overview = episode.Overview;
|
||||
episodeToUpdate.Overview = episode.Overview.Truncate(4000);
|
||||
|
||||
if (episode.FirstAired.Year > 1900)
|
||||
episodeToUpdate.AirDate = episode.FirstAired.Date;
|
||||
|
|
|
@ -247,6 +247,7 @@ Global
|
|||
{C0EA1A40-91AD-4EEB-BD16-2DDDEBD20AE5}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||
{C0EA1A40-91AD-4EEB-BD16-2DDDEBD20AE5}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{C0EA1A40-91AD-4EEB-BD16-2DDDEBD20AE5}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{C0EA1A40-91AD-4EEB-BD16-2DDDEBD20AE5}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{C0EA1A40-91AD-4EEB-BD16-2DDDEBD20AE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C0EA1A40-91AD-4EEB-BD16-2DDDEBD20AE5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C0EA1A40-91AD-4EEB-BD16-2DDDEBD20AE5}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
|
|
Loading…
Reference in a new issue