|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Linq.Expressions; |
| 6 | +using FluentAssertions; |
| 7 | +using Nest.Resolvers; |
| 8 | +using NUnit.Framework; |
| 9 | +using Nest.Tests.MockData.Domain; |
| 10 | + |
| 11 | +namespace Nest.Tests.Unit.Internals.Inferno |
| 12 | +{ |
| 13 | + [TestFixture] |
| 14 | + public class PropertyPathResolverTests |
| 15 | + { |
| 16 | + private class CustomDict : Dictionary<string, ElasticsearchProject> { } |
| 17 | + |
| 18 | + private class DomainObject |
| 19 | + { |
| 20 | + public string Name { get; set; } |
| 21 | + public IDictionary<string, ElasticsearchProject> Dictionary { get; set; } |
| 22 | + public CustomDict CustomDict { get; set; } |
| 23 | + public IList<ElasticsearchProject> Collection { get; set; } |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + private readonly ElasticInferrer _infer; |
| 28 | + private readonly string _variable = "vari"; |
| 29 | + |
| 30 | + public PropertyPathResolverTests() |
| 31 | + { |
| 32 | + _infer = TestElasticClient.Client.Infer; |
| 33 | + } |
| 34 | + |
| 35 | + private string P(Expression<Func<DomainObject, object>> path) |
| 36 | + { |
| 37 | + return this._infer.PropertyPath(Property.Path(path)); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void SimpleProperty() |
| 42 | + { |
| 43 | + P(p => p.Name).Should().Be("name"); |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public void SuffixOnPropery() |
| 48 | + { |
| 49 | + P(p => p.Name.Suffix("sort")).Should().Be("name.sort"); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void IndexOnCollection() |
| 54 | + { |
| 55 | + P(p => p.Collection[0]).Should().Be("collection"); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public void IndexOnCollectionProperty() |
| 60 | + { |
| 61 | + P(p => p.Collection[0].Name).Should().Be("collection.name"); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void FirstOnCollection() |
| 66 | + { |
| 67 | + P(p => p.Collection.First()).Should().Be("collection"); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void FirstOnCollectionProperty() |
| 72 | + { |
| 73 | + P(p => p.Collection.First().Name).Should().Be("collection.name"); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void Dictionary() |
| 78 | + { |
| 79 | + P(p => p.Dictionary["hardcoded"]).Should().Be("dictionary.hardcoded"); |
| 80 | + } |
| 81 | + |
| 82 | + [Test] |
| 83 | + public void DictionaryPropery() |
| 84 | + { |
| 85 | + P(p => p.Dictionary["hardcoded"].Name).Should().Be("dictionary.hardcoded.name"); |
| 86 | + } |
| 87 | + |
| 88 | + //Test variables |
| 89 | + [Test] |
| 90 | + public void DictionaryVariableKey() |
| 91 | + { |
| 92 | + P(p => p.Dictionary[_variable]).Should().Be("dictionary.vari"); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public void DictionaryVariableKeyProperty() |
| 97 | + { |
| 98 | + P(p => p.Dictionary[_variable].Name).Should().Be("dictionary.vari.name"); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public void CustomDictionary() |
| 103 | + { |
| 104 | + P(p => p.CustomDict["hardcoded"]).Should().Be("customDict.hardcoded"); |
| 105 | + } |
| 106 | + |
| 107 | + [Test] |
| 108 | + public void CustomDictionaryPropery() |
| 109 | + { |
| 110 | + P(p => p.CustomDict["hardcoded"].Name).Should().Be("customDict.hardcoded.name"); |
| 111 | + } |
| 112 | + |
| 113 | + //Test variables |
| 114 | + |
| 115 | + [Test] |
| 116 | + public void CustomDictionaryVariableKey() |
| 117 | + { |
| 118 | + P(p => p.CustomDict[_variable]).Should().Be("customDict.vari"); |
| 119 | + } |
| 120 | + |
| 121 | + [Test] |
| 122 | + public void CustomDictionaryVariableKeyProperty() |
| 123 | + { |
| 124 | + P(p => p.CustomDict[_variable].Name).Should().Be("customDict.vari.name"); |
| 125 | + } |
| 126 | + |
| 127 | + //Test suffixes |
| 128 | + [Test] |
| 129 | + public void PropertySuffix() |
| 130 | + { |
| 131 | + P(p => p.Name.Suffix("suffix")).Should().Be("name.suffix"); |
| 132 | + } |
| 133 | + |
| 134 | + [Test] |
| 135 | + public void DictionarySuffix() |
| 136 | + { |
| 137 | + P(p => p.Dictionary["hardcoded"].Suffix("suffix")).Should().Be("dictionary.hardcoded.suffix"); |
| 138 | + } |
| 139 | + |
| 140 | + [Test] |
| 141 | + public void FirstOnCollectionSuffix() |
| 142 | + { |
| 143 | + P(p => p.Collection.First().Suffix("suffix")).Should().Be("collection.suffix"); |
| 144 | + } |
| 145 | + |
| 146 | + [Test] |
| 147 | + public void IndexOnCollectionSuffix() |
| 148 | + { |
| 149 | + P(p => p.Collection[0].Suffix("suffix")).Should().Be("collection.suffix"); |
| 150 | + } |
| 151 | + |
| 152 | + [Test] |
| 153 | + public void CollectionSuffix() |
| 154 | + { |
| 155 | + P(p => p.Collection.Suffix("suffix")).Should().Be("collection.suffix"); |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + public void PropertySuffixVariable() |
| 160 | + { |
| 161 | + P(p => p.Name.Suffix(_variable)).Should().Be("name.vari"); |
| 162 | + } |
| 163 | + |
| 164 | + [Test] |
| 165 | + public void PropertySuffixLocalVariable() |
| 166 | + { |
| 167 | + var prop = "propXY12"; |
| 168 | + P(p => p.Name.Suffix(prop)).Should().Be("name." + prop); |
| 169 | + } |
| 170 | + |
| 171 | + //Fully qualified tests |
| 172 | + //TODO remove in 2.0 as type.properties are gonna be removed in elasticsearch 2.0 |
| 173 | + |
| 174 | + [Test] |
| 175 | + public void PropertySuffixVariableFullyQualified() |
| 176 | + { |
| 177 | + P(p => p.FullyQualified().Name.Suffix(_variable)).Should().Be("domainobject.name.vari"); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments