From 757d3e7c7d2f0d422d8ab658b311d8fa2f195367 Mon Sep 17 00:00:00 2001 From: Ben Magee Date: Wed, 12 Dec 2018 22:37:15 +0000 Subject: [PATCH] Load data files --- .../Jellyfin.XmlTv.Test.csproj | 6 + .../XmlTvReaderDateTimeTests.cs | 23 ++- .../XmlTvReaderLanguageTests.cs | 173 ++++++++++++++++++ 3 files changed, 192 insertions(+), 10 deletions(-) create mode 100644 tests/Jellyfin.XmlTv.Test/XmlTvReaderLanguageTests.cs diff --git a/tests/Jellyfin.XmlTv.Test/Jellyfin.XmlTv.Test.csproj b/tests/Jellyfin.XmlTv.Test/Jellyfin.XmlTv.Test.csproj index b63855e11..6740a65c9 100644 --- a/tests/Jellyfin.XmlTv.Test/Jellyfin.XmlTv.Test.csproj +++ b/tests/Jellyfin.XmlTv.Test/Jellyfin.XmlTv.Test.csproj @@ -20,4 +20,10 @@ + + + Always + + + diff --git a/tests/Jellyfin.XmlTv.Test/XmlTvReaderDateTimeTests.cs b/tests/Jellyfin.XmlTv.Test/XmlTvReaderDateTimeTests.cs index 4f139b171..12ddec713 100644 --- a/tests/Jellyfin.XmlTv.Test/XmlTvReaderDateTimeTests.cs +++ b/tests/Jellyfin.XmlTv.Test/XmlTvReaderDateTimeTests.cs @@ -8,11 +8,18 @@ namespace Jellyfin.XmlTv.Test [TestFixture] public class XmlTvReaderDateTimeTests { + private string _testFilePath; + + [SetUp] + public void BeforeEachTest() + { + _testFilePath = Path.GetFullPath(Path.Combine("XmlFiles", "MultilanguageData.xml")); + } + [Test] public void ShouldHandlePartDates() { - var testFile = Path.GetFullPath(@"MultilanguageData.xml"); - var reader = new XmlTvReader(testFile, "es"); + var reader = new XmlTvReader(_testFilePath, "es"); Assert.AreEqual(Parse("01 Jan 2016 00:00:00"), reader.ParseDate("2016")); Assert.AreEqual(Parse("01 Jan 2016 00:00:00"), reader.ParseDate("201601")); @@ -25,8 +32,7 @@ namespace Jellyfin.XmlTv.Test [Test] public void ShouldHandleDateWithOffset() { - var testFile = Path.GetFullPath(@"MultilanguageData.xml"); - var reader = new XmlTvReader(testFile, "es"); + var reader = new XmlTvReader(_testFilePath, "es"); // parse variations on 1:00AM Assert.AreEqual(Parse("01 Jan 2016 12:00:00"), reader.ParseDate("20160101120000 +0000")); @@ -44,8 +50,7 @@ namespace Jellyfin.XmlTv.Test [Test] public void ShouldHandlePartDatesWithOffset() { - var testFile = Path.GetFullPath(@"MultilanguageData.xml"); - var reader = new XmlTvReader(testFile, "es"); + var reader = new XmlTvReader(_testFilePath, "es"); Assert.AreEqual(Parse("01 Jan 2016 01:00:00"), reader.ParseDate("2016 -0100")); Assert.AreEqual(Parse("01 Jan 2016 01:00:00"), reader.ParseDate("201601 -0100")); @@ -58,8 +63,7 @@ namespace Jellyfin.XmlTv.Test [Test] public void ShouldHandleSpaces() { - var testFile = Path.GetFullPath(@"MultilanguageData.xml"); - var reader = new XmlTvReader(testFile, "es"); + var reader = new XmlTvReader(_testFilePath, "es"); // parse variations on 1:00AM Assert.AreEqual(Parse("01 Jan 2016 12:00:00"), reader.ParseDate("20160101120000 +000")); @@ -70,8 +74,7 @@ namespace Jellyfin.XmlTv.Test [Test] public void ShouldHandleSpaces2() { - var testFile = Path.GetFullPath(@"MultilanguageData.xml"); - var reader = new XmlTvReader(testFile, "es"); + var reader = new XmlTvReader(_testFilePath, "es"); // parse variations on 1:00AM Assert.AreEqual(Parse("01 Jan 2016 12:00:00"), reader.ParseDate("20160101120000 0")); diff --git a/tests/Jellyfin.XmlTv.Test/XmlTvReaderLanguageTests.cs b/tests/Jellyfin.XmlTv.Test/XmlTvReaderLanguageTests.cs new file mode 100644 index 000000000..145ab2598 --- /dev/null +++ b/tests/Jellyfin.XmlTv.Test/XmlTvReaderLanguageTests.cs @@ -0,0 +1,173 @@ +using System; +using System.IO; +using System.Linq; +using System.Threading; +using Emby.XmlTv.Classes; +using NUnit.Framework; + +namespace Jellyfin.XmlTv.Test +{ + [TestFixture] + public class XmlTvReaderLanguageTests + { + private string _testFilePath; + + [SetUp] + public void BeforeEachTest() + { + _testFilePath = Path.GetFullPath(Path.Combine("XmlFiles", "MultilanguageData.xml")); + } + + /* + Homes Under the Hammer - Spanish + Homes Under the Hammer - Spanish 2 + Homes Under the Hammer - English + Homes Under the Hammer - English 2 + Homes Under the Hammer - Empty Language + Homes Under the Hammer - Empty Language 2 + Homes Under the Hammer - No Language + Homes Under the Hammer - No Language 2 + */ + + /* Expected Behaviour: + - Language = Null Homes Under the Hammer - No Language + - Language = "" Homes Under the Hammer - No Language + - Language = es Homes Under the Hammer - Spanish + - Language = en Homes Under the Hammer - English + */ + + [Test] + public void Should_Return_The_First_Matching_Language_ES() + { + var reader = new XmlTvReader(_testFilePath, "es"); + var channel = reader.GetChannels().FirstOrDefault(); + Assert.IsNotNull(channel); + + var startDate = new DateTime(2015, 11, 26); + var cancellationToken = new CancellationToken(); + var programme = reader.GetProgrammes(channel.Id, startDate, startDate.AddDays(1), cancellationToken).FirstOrDefault(); + + Assert.IsNotNull(programme); + Assert.AreEqual("Homes Under the Hammer - Spanish", programme.Title); + Assert.AreEqual(1, programme.Categories.Count); + Assert.AreEqual("Property - Spanish", programme.Categories[0]); + } + + [Test] + public void Should_Return_The_First_Matching_Language_EN() + { + var reader = new XmlTvReader(_testFilePath, "en"); + + var channel = reader.GetChannels().FirstOrDefault(); + Assert.IsNotNull(channel); + + var startDate = new DateTime(2015, 11, 26); + var cancellationToken = new CancellationToken(); + var programme = reader.GetProgrammes(channel.Id, startDate, startDate.AddDays(1), cancellationToken).FirstOrDefault(); + + Assert.IsNotNull(programme); + Assert.AreEqual("Homes Under the Hammer - English", programme.Title); + Assert.AreEqual(1, programme.Categories.Count); + Assert.AreEqual("Property - English", programme.Categories[0]); + } + + [Test] + public void Should_Return_The_First_Matching_With_No_Language() + { + var reader = new XmlTvReader(_testFilePath, null); + + var channel = reader.GetChannels().FirstOrDefault(); + Assert.IsNotNull(channel); + + var startDate = new DateTime(2015, 11, 26); + var cancellationToken = new CancellationToken(); + var programme = reader.GetProgrammes(channel.Id, startDate, startDate.AddDays(1), cancellationToken).FirstOrDefault(); + + Assert.IsNotNull(programme); + Assert.AreEqual("Homes Under the Hammer - No Language", programme.Title); + Assert.AreEqual(1, programme.Categories.Count); + Assert.AreEqual("Property - No Language", programme.Categories[0]); + } + + [Test] + public void Should_Return_The_First_Matching_With_Empty_Language() + { + var reader = new XmlTvReader(_testFilePath, String.Empty); + + var channel = reader.GetChannels().FirstOrDefault(); + Assert.IsNotNull(channel); + + var startDate = new DateTime(2015, 11, 26); + var cancellationToken = new CancellationToken(); + var programme = reader.GetProgrammes(channel.Id, startDate, startDate.AddDays(1), cancellationToken).FirstOrDefault(); + + Assert.IsNotNull(programme); + Assert.AreEqual("Homes Under the Hammer - Empty Language", programme.Title); + Assert.AreEqual(1, programme.Categories.Count); + Assert.AreEqual("Property - Empty Language", programme.Categories[0]); + } + + [Test] + public void Should_Return_The_First_When_NoMatchFound() + { + var reader = new XmlTvReader(_testFilePath, "es"); // There are no titles or categories for spanish + + var channel = reader.GetChannels().FirstOrDefault(); + Assert.IsNotNull(channel); + + var startDate = new DateTime(2015, 11, 26); + var cancellationToken = new CancellationToken(); + var programme = reader.GetProgrammes(channel.Id, startDate, startDate.AddDays(1), cancellationToken).Skip(1).FirstOrDefault(); + + Assert.IsNotNull(programme); + Assert.AreEqual("Homes Under the Hammer - English", programme.Title); + + // Should return all categories + Assert.AreEqual(2, programme.Categories.Count); + Assert.IsTrue(programme.Categories.Contains("Property - English")); + Assert.IsTrue(programme.Categories.Contains("Property - Empty Language")); + } + + [Test] + public void Should_Return_The_First_When_NoLanguage() + { + var reader = new XmlTvReader(_testFilePath, null); + + var channel = reader.GetChannels().FirstOrDefault(); + Assert.IsNotNull(channel); + + var startDate = new DateTime(2015, 11, 26); + var cancellationToken = new CancellationToken(); + var programme = reader.GetProgrammes(channel.Id, startDate, startDate.AddDays(1), cancellationToken).Skip(1).FirstOrDefault(); + + Assert.IsNotNull(programme); + Assert.AreEqual("Homes Under the Hammer - English", programme.Title); // Should return the first in the list + + // Should return all categories + Assert.AreEqual(2, programme.Categories.Count); + Assert.IsTrue(programme.Categories.Contains("Property - English")); + Assert.IsTrue(programme.Categories.Contains("Property - Empty Language")); + } + + [Test] + public void Should_Return_All_Languages() + { + var reader = new XmlTvReader(_testFilePath); + var cancellationToken = new CancellationToken(); + + var results = reader.GetLanguages(cancellationToken); + Assert.IsNotNull(results); + + foreach (var result in results) + { + Console.WriteLine("{0} - {1}", result.Name, result.Relevance); + } + + Assert.AreEqual(2, results.Count); + Assert.AreEqual("en", results[0].Name); + Assert.AreEqual(11, results[0].Relevance); + Assert.AreEqual("es", results[1].Name); + Assert.AreEqual(3, results[1].Relevance); + } + } +} \ No newline at end of file