Skip to content

Commit

Permalink
Better target species find method tests + clearer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jongmassey committed Sep 4, 2018
1 parent 383eb26 commit 7e2fd58
Show file tree
Hide file tree
Showing 4 changed files with 1,680 additions and 10 deletions.
7 changes: 6 additions & 1 deletion VetMedData.NET.Tests/TestNameMatching.cs
Expand Up @@ -44,7 +44,12 @@ public void TestProductMatchRunner()
var pmr = new ProductMatchRunner(cfg);
//var pid = VMDPIDFactory.GetVmdpid(true, true, true).Result;
var pid = PersistentPID.Get(true,true);
var ap = new SoldProduct {TargetSpecies = new[] {"cattle"}, Product = new Product {Name = "metacam"},ActionDate = DateTime.Now};
var ap = new SoldProduct {
TargetSpecies = new[] {"cattle"},
//TargetSpeciesTyped = TargetSpecies.Find("cattle") ,
Product = new Product {Name = "metacam"} ,
ActionDate = DateTime.Now
};
var res = pmr.GetMatch(ap, pid.RealProducts);
Assert.IsNotNull(res);
Assert.IsTrue(res.ReferenceProduct.Name.StartsWith("metacam",StringComparison.InvariantCultureIgnoreCase));
Expand Down
19 changes: 14 additions & 5 deletions VetMedData.NET.Tests/TestTargetSpecies.cs
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using VetMedData.NET.Model;

Expand All @@ -10,13 +11,21 @@ public class TestTargetSpecies
[TestMethod]
public void TestFind()
{
var input = new[] { "Horses", "Dogs", "Cats" };
foreach (var s in input)
var results = new[]
{
Assert.IsNotNull(TargetSpecies.Find(s), $"species {s} not found");
Assert.IsTrue(TargetSpecies.Find(s).Any(), $"species {s} not found");
"Horses",
"Dogs",
"Cats"
}.Select(s => new {instr = s,res = TargetSpecies.Find(s)}).ToArray();

foreach (var result in results)
{
Assert.IsNotNull(result.res, $"species {result.instr} not found");
Assert.IsTrue(result.res.Any(), $"species {result.instr} not found");
}

var rescount = results.SelectMany(re => re.res).Distinct().Count();
Assert.IsTrue(rescount==3,$"Expected 3 results, got {rescount}");
}
}
}
Expand Down
22 changes: 18 additions & 4 deletions VetMedData.NET/Model/TargetSpecies.cs
Expand Up @@ -11,7 +11,13 @@ public class TargetSpecies
public string[] Synonyms { get; set; }
public string[] Names =>
Synonyms == null ? new[] { CanonicalName } : new[] { CanonicalName }.Union(Synonyms).ToArray();


public override bool Equals(object obj)
{
return obj.GetType() == typeof(TargetSpecies) &&
((TargetSpecies) obj).CanonicalName.Equals(this.CanonicalName);
}

public static IEnumerable<TargetSpecies> All { get; } = new[]
{
new TargetSpecies {CanonicalName = "all animals"},
Expand Down Expand Up @@ -117,9 +123,17 @@ public class TargetSpecies

public static IEnumerable<TargetSpecies> Find(string instr)
{
return All.Where(a =>
a.Names.Select(
n => n.ToLowerInvariant().Split(' ').Intersect(instr.ToLowerInvariant().Split(' ')).Any()).Any());
var refTokens = All.Select(a => new {a, split = a.Names.SelectMany(n => n.ToLowerInvariant().Split(' '))});
var inTokens = instr.ToLowerInvariant().Split(' ');

return refTokens.Where(f => f.split.Intersect(inTokens).Any()).Select(f => f.a);
//return All.Where(a =>
// a.Names.Select(
// n => n.ToLowerInvariant()
// .Split(' ')
// .Intersect(instr.ToLowerInvariant().Split(' '))
// .Any()
// ).Any());
}
}
}

0 comments on commit 7e2fd58

Please sign in to comment.