|
| 1 | +using Elasticsearch.Net; |
| 2 | +using FluentAssertions; |
| 3 | +using Nest; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Tests.Framework; |
| 10 | +using Tests.Framework.MockData; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Tests.ClientConcepts.HighLevel.Inferrence |
| 14 | +{ |
| 15 | + public class IndexNameInference |
| 16 | + { |
| 17 | + [U] |
| 18 | + public void DefaultIndexIsInferred() |
| 19 | + { |
| 20 | + var settings = new ConnectionSettings() |
| 21 | + .DefaultIndex("defaultindex"); |
| 22 | + var resolver = new IndexNameResolver(settings); |
| 23 | + var index = resolver.Resolve<Project>(); |
| 24 | + index.Should().Be("defaultindex"); |
| 25 | + } |
| 26 | + |
| 27 | + [U] |
| 28 | + public void ExplicitMappingIsInferred() |
| 29 | + { |
| 30 | + var settings = new ConnectionSettings() |
| 31 | + .MapDefaultTypeIndices(m => m |
| 32 | + .Add(typeof(Project), "projects") |
| 33 | + ); |
| 34 | + var resolver = new IndexNameResolver(settings); |
| 35 | + var index = resolver.Resolve<Project>(); |
| 36 | + index.Should().Be("projects"); |
| 37 | + } |
| 38 | + |
| 39 | + [U] |
| 40 | + public void ExplicitMappingTakesPrecedence() |
| 41 | + { |
| 42 | + var settings = new ConnectionSettings() |
| 43 | + .DefaultIndex("defaultindex") |
| 44 | + .MapDefaultTypeIndices(m => m |
| 45 | + .Add(typeof(Project), "projects") |
| 46 | + ); |
| 47 | + var resolver = new IndexNameResolver(settings); |
| 48 | + var index = resolver.Resolve<Project>(); |
| 49 | + index.Should().Be("projects"); |
| 50 | + } |
| 51 | + |
| 52 | + [U] |
| 53 | + public void UppercaseCharacterThrowsResolveException() |
| 54 | + { |
| 55 | + var settings = new ConnectionSettings() |
| 56 | + .DefaultIndex("Default") |
| 57 | + .MapDefaultTypeIndices(m => m |
| 58 | + .Add(typeof(Project), "myProjects") |
| 59 | + ); |
| 60 | + |
| 61 | + var resolver = new IndexNameResolver(settings); |
| 62 | + |
| 63 | + var e = Assert.Throws<ResolveException>(() => resolver.Resolve<Project>()); |
| 64 | + e.Message.Should().Be($"Index names cannot contain uppercase characters: myProjects."); |
| 65 | + e = Assert.Throws<ResolveException>(() => resolver.Resolve<Tag>()); |
| 66 | + e.Message.Should().Be($"Index names cannot contain uppercase characters: Default."); |
| 67 | + e = Assert.Throws<ResolveException>(() => resolver.Resolve("Foo")); |
| 68 | + e.Message.Should().Be($"Index names cannot contain uppercase characters: Foo."); |
| 69 | + } |
| 70 | + |
| 71 | + [U] |
| 72 | + public void NoIndexThrowsResolveException() |
| 73 | + { |
| 74 | + var settings = new ConnectionSettings(); |
| 75 | + var resolver = new IndexNameResolver(settings); |
| 76 | + var e = Assert.Throws<ResolveException>(() => resolver.Resolve<Project>()); |
| 77 | + e.Message.Should().Contain("Index name is null"); |
| 78 | + } |
| 79 | + |
| 80 | + [U] |
| 81 | + public void ResolveExceptionBubblesOut() |
| 82 | + { |
| 83 | + var client = TestClient.GetInMemoryClient(s => new ConnectionSettings()); |
| 84 | + var e = Assert.Throws<ResolveException>(() => client.Search<Project>()); |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments