Truncating TvDb Overviews actually works. Stupid Recaps.

This commit is contained in:
Mark McDowall 2012-04-12 16:03:01 -07:00
parent 3a992a8d0c
commit 399b50ec8e
5 changed files with 55 additions and 46 deletions

View File

@ -191,5 +191,33 @@ namespace NzbDrone.Core.Test
//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);
}
}
}

View File

@ -480,48 +480,6 @@ namespace NzbDrone.Core.Test.ProviderTests
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()
{

View File

@ -62,5 +62,30 @@ namespace NzbDrone.Core
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);
}
}
}

View File

@ -328,10 +328,7 @@ namespace NzbDrone.Core.Providers
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;

View File

@ -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