From 8271db09567813f3dd7f3157fab8eaacdf83c5af Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 6 May 2015 14:35:23 +0200 Subject: [PATCH 1/4] found a bug on propertypaths on IDictionary<,> where Dictionary<,> worked fine --- src/Nest/Resolvers/PropertyNameResolver.cs | 8 +- .../Internals/Inferno/PropertyVisitorTests.cs | 175 ++++++++++++++++-- 2 files changed, 166 insertions(+), 17 deletions(-) diff --git a/src/Nest/Resolvers/PropertyNameResolver.cs b/src/Nest/Resolvers/PropertyNameResolver.cs index a650668ae64..19cac6a5fba 100644 --- a/src/Nest/Resolvers/PropertyNameResolver.cs +++ b/src/Nest/Resolvers/PropertyNameResolver.cs @@ -142,7 +142,13 @@ protected override Expression VisitMethodCall(MethodCallExpression m, Stack).IsAssignableFrom(t) + || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof (IDictionary<,>)); + + if (!isDict) { return base.VisitMethodCall(m, stack, properties); } diff --git a/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs b/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs index 3143659d849..443604cce73 100644 --- a/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs +++ b/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs @@ -1,4 +1,11 @@ -using NUnit.Framework; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using FluentAssertions; +using Nest.Resolvers; +using NUnit.Framework; using Nest.Tests.MockData.Domain; namespace Nest.Tests.Unit.Internals.Inferno @@ -6,24 +13,160 @@ namespace Nest.Tests.Unit.Internals.Inferno [TestFixture] public class PropertyVisitorTests { + private class CustomDict : Dictionary { } + + private class DomainObject + { + public string Name { get; set; } + public IDictionary Dictionary { get; set; } + public CustomDict CustomDict { get; set; } + public IList Collection { get; set; } + + } + + private readonly ElasticInferrer _infer; + private readonly string _variable = "vari"; + + public PropertyVisitorTests() + { + _infer = TestElasticClient.Client.Infer; + } + + private string P(Expression> path) + { + return this._infer.PropertyPath(Property.Path(path)); + } + + [Test] + public void SimpleProperty() + { + P(p => p.Name).Should().Be("name"); + } + + [Test] + public void SuffixOnPropery() + { + P(p => p.Name.Suffix("sort")).Should().Be("name.sort"); + } + + [Test] + public void IndexOnCollection() + { + P(p => p.Collection[0]).Should().Be("collection"); + } + + [Test] + public void IndexOnCollectionProperty() + { + P(p => p.Collection[0].Name).Should().Be("collection.name"); + } + + [Test] + public void FirstOnCollection() + { + P(p => p.Collection.First()).Should().Be("collection"); + } + + [Test] + public void FirstOnCollectionProperty() + { + P(p => p.Collection.First().Name).Should().Be("collection.name"); + } + + [Test] + public void Dictionary() + { + P(p => p.Dictionary["hardcoded"]).Should().Be("dictionary.hardcoded"); + } + + [Test] + public void DictionaryPropery() + { + P(p => p.Dictionary["hardcoded"].Name).Should().Be("dictionary.hardcoded.name"); + } + + //Test variables + [Test] + public void DictionaryVariableKey() + { + P(p => p.Dictionary[_variable]).Should().Be("dictionary.vari"); + } + + [Test] + public void DictionaryVariableKeyProperty() + { + P(p => p.Dictionary[_variable].Name).Should().Be("dictionary.vari.name"); + } + + [Test] + public void CustomDictionary() + { + P(p => p.CustomDict["hardcoded"]).Should().Be("customDict.hardcoded"); + } + + [Test] + public void CustomDictionaryPropery() + { + P(p => p.CustomDict["hardcoded"].Name).Should().Be("customDict.hardcoded.name"); + } + + //Test variables + + [Test] + public void CustomDictionaryVariableKey() + { + P(p => p.CustomDict[_variable]).Should().Be("customDict.vari"); + } + + [Test] + public void CustomDictionaryVariableKeyProperty() + { + P(p => p.CustomDict[_variable].Name).Should().Be("customDict.vari.name"); + } + + //Test suffixes + [Test] + public void PropertySuffix() + { + P(p => p.Name.Suffix("suffix")).Should().Be("name.suffix"); + } + + [Test] + public void DictionarySuffix() + { + P(p => p.Dictionary["hardcoded"].Suffix("suffix")).Should().Be("dictionary.hardcoded.suffix"); + } + + [Test] + public void FirstOnCollectionSuffix() + { + P(p => p.Collection.First().Suffix("suffix")).Should().Be("collection.suffix"); + } + [Test] - public void SuffixMakesItIntoPropertyName() + public void IndexOnCollectionSuffix() { - var s = new SearchDescriptor() - .From(0) - .Size(10) - .Query(q => q.Term(f => f.Name.Suffix("sort"), "value")); - var json = TestElasticClient.Serialize(s); - var expected = @"{ from: 0, size: 10, - query: { - term: { - ""name.sort"": { - value: ""value"" - } - } + P(p => p.Collection[0].Suffix("suffix")).Should().Be("collection.suffix"); } - }"; - Assert.True(json.JsonEquals(expected), json); + + [Test] + public void CollectionSuffix() + { + P(p => p.Collection.Suffix("suffix")).Should().Be("collection.suffix"); } + + + + + + + + + + + + + + } } From 39fa610e98e387c7d80e73cd9b0b254e6605d7ac Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 6 May 2015 15:15:04 +0200 Subject: [PATCH 2/4] fix #998 add support for variables in --- src/Nest/Resolvers/PropertyNameResolver.cs | 30 ++++++++++++------- .../Internals/Inferno/PropertyVisitorTests.cs | 28 ++++++++++------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/Nest/Resolvers/PropertyNameResolver.cs b/src/Nest/Resolvers/PropertyNameResolver.cs index 19cac6a5fba..b87f37c4dd3 100644 --- a/src/Nest/Resolvers/PropertyNameResolver.cs +++ b/src/Nest/Resolvers/PropertyNameResolver.cs @@ -8,6 +8,8 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Collections.Concurrent; +using System.Collections.ObjectModel; +using System.ComponentModel; namespace Nest.Resolvers { @@ -130,9 +132,12 @@ protected override Expression VisitMethodCall(MethodCallExpression m, Stack( + new List {{m.Arguments.First()}} + ); + base.VisitExpressionList(callingMember, stack, properties); + return m; } else if (m.Method.Name == "FullyQualified" && m.Arguments.Any()) { @@ -152,16 +157,11 @@ protected override Expression VisitMethodCall(MethodCallExpression m, Stack stack) + { + var lastArg = m.Arguments.Last(); + var constantExpression = lastArg as ConstantExpression; + var value = constantExpression != null + ? constantExpression.Value.ToString() + : Expression.Lambda(lastArg).Compile().DynamicInvoke().ToString(); + stack.Push(value); + } private static bool IsLinqOperator(MethodInfo method) { diff --git a/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs b/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs index 443604cce73..539296bb65e 100644 --- a/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs +++ b/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs @@ -155,18 +155,26 @@ public void CollectionSuffix() P(p => p.Collection.Suffix("suffix")).Should().Be("collection.suffix"); } + [Test] + public void PropertySuffixVariable() + { + P(p => p.Name.Suffix(_variable)).Should().Be("name.vari"); + } + [Test] + public void PropertySuffixLocalVariable() + { + var prop = "propXY12"; + P(p => p.Name.Suffix(prop)).Should().Be("name." + prop); + } + //Fully qualified tests + //TODO remove in 2.0 as type.properties are gonna be removed in elasticsearch 2.0 - - - - - - - - - - + [Test] + public void PropertySuffixVariableFullyQualified() + { + P(p => p.FullyQualified().Name.Suffix(_variable)).Should().Be("domainobject.name.vari"); + } } } From 8eaf5ff5e19a5532bb248bd0151cd82c76081965 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 6 May 2015 15:15:48 +0200 Subject: [PATCH 3/4] Renamed test class --- .../{PropertyVisitorTests.cs => PropertyPathResolverTests.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/Tests/Nest.Tests.Unit/Internals/Inferno/{PropertyVisitorTests.cs => PropertyPathResolverTests.cs} (98%) diff --git a/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs b/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyPathResolverTests.cs similarity index 98% rename from src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs rename to src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyPathResolverTests.cs index 539296bb65e..7a316ec15a5 100644 --- a/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyVisitorTests.cs +++ b/src/Tests/Nest.Tests.Unit/Internals/Inferno/PropertyPathResolverTests.cs @@ -11,7 +11,7 @@ namespace Nest.Tests.Unit.Internals.Inferno { [TestFixture] - public class PropertyVisitorTests + public class PropertyPathResolverTests { private class CustomDict : Dictionary { } @@ -27,7 +27,7 @@ private class DomainObject private readonly ElasticInferrer _infer; private readonly string _variable = "vari"; - public PropertyVisitorTests() + public PropertyPathResolverTests() { _infer = TestElasticClient.Client.Infer; } From 5d03818373bb9cd876092a82fa4718ec66f2a6df Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 6 May 2015 15:25:28 +0200 Subject: [PATCH 4/4] csproj file changes --- src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj index 6a55438fe52..f131b75c189 100644 --- a/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj +++ b/src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj @@ -501,7 +501,7 @@ - +