|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Elasticsearch.Net; |
| 5 | +using Elasticsearch.Net.Connection; |
| 6 | +using FluentAssertions; |
| 7 | +using NUnit.Framework; |
| 8 | +using System.Linq.Expressions; |
| 9 | +using Newtonsoft.Json; |
| 10 | + |
| 11 | +namespace Nest.Tests.Unit.Internals.Inferno |
| 12 | +{ |
| 13 | + [TestFixture] |
| 14 | + public class MapPropertyIgnoreForTests : BaseJsonTests |
| 15 | + { |
| 16 | + [ElasticType(IdProperty = "Guid")] |
| 17 | + internal class SomeClass |
| 18 | + { |
| 19 | + public MyCustomClass MyCustomClass { get; set; } |
| 20 | + [JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))] |
| 21 | + public Dictionary<string, SomeOtherClass> StringDict { get; set; } |
| 22 | + public Dictionary<int, MyCustomClass> IntDict { get; set; } |
| 23 | + public IList<MyCustomClass> ListOfCustomClasses { get; set; } |
| 24 | + public B BInstance { get; set; } |
| 25 | + public C CInstance { get; set; } |
| 26 | + } |
| 27 | + |
| 28 | + internal class B |
| 29 | + { |
| 30 | + internal int X { get; set; } |
| 31 | + } |
| 32 | + |
| 33 | + internal class C : B |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + [ElasticType(IdProperty = "Guid")] |
| 38 | + internal class SomeOtherClass |
| 39 | + { |
| 40 | + [ElasticProperty(Name = "CreateDate")] |
| 41 | + public DateTime CreateDate { get; set; } |
| 42 | + |
| 43 | + [ElasticProperty(Name = "custom")] |
| 44 | + public MyCustomOtherClass MyCustomOtherClass { get; set; } |
| 45 | + } |
| 46 | + internal class MyCustomClass |
| 47 | + { |
| 48 | + [ElasticProperty(Name = "MID")] |
| 49 | + public string MyProperty { get; set; } |
| 50 | + |
| 51 | + public override string ToString() |
| 52 | + { |
| 53 | + return "static id ftw"; |
| 54 | + } |
| 55 | + } |
| 56 | + [ElasticType(IdProperty = "Guid", Name = "mycustomother")] |
| 57 | + internal class MyCustomOtherClass |
| 58 | + { |
| 59 | + [ElasticProperty(Name = "MID")] |
| 60 | + public string MyProperty { get; set; } |
| 61 | + |
| 62 | + public override string ToString() |
| 63 | + { |
| 64 | + return "static id ftw"; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + internal class UserItemData |
| 69 | + { |
| 70 | + public string Id { get; set; } |
| 71 | + public string UserId { get; set; } |
| 72 | + public string Title { get; set; } |
| 73 | + public string Hidden { get; set; } |
| 74 | + public string UserLabels { get; set; } |
| 75 | + } |
| 76 | + |
| 77 | + public MapPropertyIgnoreForTests() |
| 78 | + { |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + public ElasticClient ConfigureClient(Action<ConnectionSettings> settingsSelector) |
| 83 | + { |
| 84 | + var settings = new ConnectionSettings(UnitTestDefaults.Uri, UnitTestDefaults.DefaultIndex); |
| 85 | + settingsSelector(settings); |
| 86 | + var client = new ElasticClient(settings, connection: new InMemoryConnection()); |
| 87 | + return client; |
| 88 | + } |
| 89 | + |
| 90 | + public IElasticClient ClientWithPropertiesFor<T>(Action<PropertyMappingDescriptor<T>> propertiesSelector) |
| 91 | + { |
| 92 | + return this.ConfigureClient(c=>c.MapPropertiesFor<T>(propertiesSelector)); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public void Global_Ignore_Should_Be_Adhered() |
| 97 | + { |
| 98 | + var client = this.ClientWithPropertiesFor<MyCustomClass>(props => props |
| 99 | + .Ignore(p=>p.MyProperty) |
| 100 | + ); |
| 101 | + var json = client.Serializer.Serialize(new MyCustomClass |
| 102 | + { |
| 103 | + MyProperty = "should not be serialized" |
| 104 | + }); |
| 105 | + json.Utf8String().Should().Be("{}"); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public void CanIgnoreTwice_ExceptionMessageMakesSense() |
| 110 | + { |
| 111 | + var e = Assert.Throws<ArgumentException>(() => |
| 112 | + { |
| 113 | + this.ClientWithPropertiesFor<MyCustomClass>(props => props |
| 114 | + .Ignore(p => p.MyProperty) |
| 115 | + .Ignore(p => p.MyProperty) |
| 116 | + ); |
| 117 | + }); |
| 118 | + e.Message.Should() |
| 119 | + .Contain("is already ignored") |
| 120 | + .And.Contain("p => p.MyProperty"); |
| 121 | + } |
| 122 | + |
| 123 | + [Test] |
| 124 | + public void CanNotMapTwiceDifferently_ExceptionMessageMakesSense() |
| 125 | + { |
| 126 | + var e = Assert.Throws<ArgumentException>(() => |
| 127 | + { |
| 128 | + this.ClientWithPropertiesFor<MyCustomClass>(props => props |
| 129 | + .Ignore(p => p.MyProperty) |
| 130 | + .Rename(p => p.MyProperty, "mahProperty4") |
| 131 | + ); |
| 132 | + }); |
| 133 | + e.Message.Should() |
| 134 | + .Contain("can not be mapped to 'mahProperty4'") |
| 135 | + .And.Contain("already has an ignore mapping"); |
| 136 | + } |
| 137 | + |
| 138 | + [Test] |
| 139 | + public void CanNotMapTwiceDifferently_ExceptionMessageMakesSense_AlternativeOrder() |
| 140 | + { |
| 141 | + var e = Assert.Throws<ArgumentException>(() => |
| 142 | + { |
| 143 | + this.ClientWithPropertiesFor<MyCustomClass>(props => props |
| 144 | + .Rename(p => p.MyProperty, "mahProperty4") |
| 145 | + .Ignore(p => p.MyProperty) |
| 146 | + ); |
| 147 | + }); |
| 148 | + e.Message.Should() |
| 149 | + .Contain("already has a mapping to 'mahProperty4'") |
| 150 | + .And.Contain("can not be ignored"); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments