Skip to content

Commit

Permalink
Fixed channel matching
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlendea committed Feb 3, 2020
1 parent 35d9291 commit a51e7f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 46 deletions.
10 changes: 7 additions & 3 deletions Service/ChannelMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed class ChannelMatcher : IChannelMatcher
};
static readonly IDictionary<string, string> SubstringsToReplace = new Dictionary<string, string>
{
{ "^VIP RO", "RO" },
{ "^VIP|RO|", "" },
{ "^ROMANIA", "RO" },
{ "^RUMANIA", "RO" },
{ "^ROM", "RO" },
Expand Down Expand Up @@ -57,7 +57,8 @@ public string NormaliseName(string name)
}

public bool DoesMatch(ChannelName name1, string name2)
=> name1.Aliases.Any(x => DoChannelNamesMatch(x, name2));
=> DoChannelNamesMatch(name1.Value, name2) ||
name1.Aliases.Any(x => DoChannelNamesMatch(x, name2));

bool DoChannelNamesMatch(string name1, string name2)
=> NormaliseName(name1).Equals(NormaliseName(name2));
Expand All @@ -78,7 +79,10 @@ string StripChannelName(string name)

foreach (string substringToReplace in SubstringsToReplace.Keys)
{
strippedName = Regex.Replace(strippedName, substringToReplace, SubstringsToReplace[substringToReplace]);
strippedName = Regex.Replace(
strippedName,
substringToReplace,
SubstringsToReplace[substringToReplace]);
}

string finalString = string.Empty;
Expand Down
72 changes: 29 additions & 43 deletions UnitTests/Service/ChannelMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace IptvPlaylistAggregator.UnitTests.Service.Models
public sealed class ChannelMatcherTests
{
Mock<ICacheManager> cacheMock;

IChannelMatcher channelMatcher;

[SetUp]
Expand All @@ -21,64 +21,50 @@ public void SetUp()
channelMatcher = new ChannelMatcher(cacheMock.Object);
}

[TestCase("Ardeal TV", "RO: Ardeal TV", "|RO| Ardeal TV")]
[TestCase("Cartoon Network", "RO: Cartoon Network", "VIP|RO|: Cartoon Network")]
[TestCase("Digi Sport 2", "RO: Digi Sport 2", "RO: DIGI Sport 2")]
[TestCase("România TV", "România TV", "RO\" Romania TV")]
[TestCase("Somax TV", null, "Somax TV")]
[Test]
public void DoesMatch_CompareWithSameValue_ReturnsTrue()
public void DoesMatch_NamesMatch_ReturnsTrue(
string definedName,
string alias,
string providerName)
{
string definedName = "Digi Sport 2";
string providerName = "RO: Digi Sport 2";
string alias = "RO: Digi Sport 2";

ChannelName channelName = new ChannelName(definedName, alias);
ChannelName channelName;

if (alias is null)
{
channelName = new ChannelName(definedName);
}
else
{
channelName = new ChannelName(definedName, alias);
}

Assert.IsTrue(channelMatcher.DoesMatch(channelName, providerName));
}

[Test]
public void DoesMatch_CompareWithSameValueDifferentCasing_ReturnsTrue()
public void DoesMatch_CompareWithDifferentValue_ReturnsFalse()
{
string definedName = "Digi Sport 2";
string definedName = "Telekom Sport 2";
string providerName = "RO: Digi Sport 2";
string alias = "RO: DIGI Sport 2";

ChannelName channelName = new ChannelName(definedName, alias);

Assert.IsTrue(channelMatcher.DoesMatch(channelName, providerName));
}

[Test]
public void DoesMatch_CompareWithSameValueWithDiacritics_ReturnsTrue()
{
string definedName = "TVR Timișoara";
string providerName = "RO: TVR Timisoara";
string alias = "RO: TVR Timișoara";

ChannelName channelName = new ChannelName(definedName, alias);

Assert.IsTrue(channelMatcher.DoesMatch(channelName, providerName));
}

[Test]
public void DoesMatch_CompareWithSameValueWithBlacklistedSubstrings_ReturnsTrue()
{
string definedName = "Digi Sport 2";
string providerName = "RO: Digi Sport 2 [Multi-Audio]";
string alias = "RO: Digi Sport 2";
string alias = "RO: Telekom Sport 2";

ChannelName channelName = new ChannelName(definedName, alias);

Assert.IsTrue(channelMatcher.DoesMatch(channelName, providerName));
Assert.IsFalse(channelMatcher.DoesMatch(channelName, providerName));
}

[TestCase("VIP|RO|: Discovery Channel FHD", "DISCOVERYCHANNEL")]
[Test]
public void DoesMatch_CompareWithDifferentValue_ReturnsFalse()
public void NormaliseName_ReturnsExpectedValue(string inputValue, string expectedValue)
{
string definedName = "Digi Sport 2";
string providerName = "RO: Digi Sport 2";
string alias = "RO: Telekom Sport 2";

ChannelName channelName = new ChannelName(definedName, alias);

Assert.IsFalse(channelMatcher.DoesMatch(channelName, providerName));
string actualValue = channelMatcher.NormaliseName(inputValue);

Assert.AreEqual(expectedValue, actualValue);
}
}
}

0 comments on commit a51e7f5

Please sign in to comment.