|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using Nest.Tests.MockData; |
| 6 | +using Nest.Tests.MockData.Domain; |
| 7 | +using NUnit.Framework; |
| 8 | +using System.Diagnostics; |
| 9 | +using FluentAssertions; |
| 10 | + |
| 11 | +namespace Nest.Tests.Integration.Reproduce |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// tests to reproduce reported errors |
| 15 | + /// </summary> |
| 16 | + [TestFixture] |
| 17 | + public class Reproduce346Tests : IntegrationTests |
| 18 | + { |
| 19 | + public class MediaStreamEntry |
| 20 | + { |
| 21 | + public int Id { get; set; } |
| 22 | + public Guid StreamId { get; set; } |
| 23 | + public ApprovalSettings ApprovalSettings { get; set; } |
| 24 | + } |
| 25 | + public class ApprovalSettings |
| 26 | + { |
| 27 | + public bool Approved { get; set; } |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// https://github.com/Mpdreamz/NEST/issues/346 |
| 33 | + /// </summary> |
| 34 | + [Test] |
| 35 | + public void NoSearchResults() |
| 36 | + { |
| 37 | + //test teardown will delete defaultindex_* indices |
| 38 | + //process id makes it so we can run these tests concurrently using NCrunch |
| 39 | + var index = ElasticsearchConfiguration.DefaultIndex + "_posts_" + Process.GetCurrentProcess().Id.ToString(); |
| 40 | + |
| 41 | + var client = new ElasticClient(this._settings, new InMemoryConnection(this._settings)); |
| 42 | + |
| 43 | + var streamId = new Guid("8d00cf65-bf84-4035-9adb-695b1366304c"); |
| 44 | + var approved = true; |
| 45 | + |
| 46 | + var response = client.Count<MediaStreamEntry>( |
| 47 | + new[] { "StreamEntry" }, |
| 48 | + x => x.Bool( |
| 49 | + b => b.Must |
| 50 | + ( |
| 51 | + x.Term(f => f.StreamId, streamId.ToString()) |
| 52 | + , x.Term(f => f.ApprovalSettings.Approved, approved) |
| 53 | + ) |
| 54 | + ) |
| 55 | + ); |
| 56 | + |
| 57 | + //Approval settings appears twice because we are spawing the nested queries of x |
| 58 | + Assert.AreEqual(2, Regex.Matches(response.ConnectionStatus.Request, @"approvalSettings\.approved").Count); |
| 59 | + |
| 60 | + //either use the lambda overload |
| 61 | + response = client.Count<MediaStreamEntry>( |
| 62 | + new[] { "StreamEntry" }, |
| 63 | + x => x.Bool( |
| 64 | + b => b.Must |
| 65 | + ( |
| 66 | + m=> m.Term(f => f.StreamId, streamId.ToString()) |
| 67 | + , m => m.Term(f => f.ApprovalSettings.Approved, approved) |
| 68 | + ) |
| 69 | + ) |
| 70 | + ); |
| 71 | + |
| 72 | + //now we only see the query once |
| 73 | + Assert.AreEqual(1, Regex.Matches(response.ConnectionStatus.Request, @"approvalSettings\.approved").Count); |
| 74 | + |
| 75 | + |
| 76 | + //or use the static Query<MediaStreamEntry> |
| 77 | + response = client.Count<MediaStreamEntry>( |
| 78 | + new[] { "StreamEntry" }, |
| 79 | + x => x.Bool( |
| 80 | + b => b.Must |
| 81 | + ( |
| 82 | + Query<MediaStreamEntry>.Term(f => f.StreamId, streamId.ToString()) |
| 83 | + , Query<MediaStreamEntry>.Term(f => f.ApprovalSettings.Approved, approved.ToString()) |
| 84 | + ) |
| 85 | + ) |
| 86 | + ); |
| 87 | + |
| 88 | + //now we still only see the query once |
| 89 | + Assert.AreEqual(1, Regex.Matches(response.ConnectionStatus.Request, @"approvalSettings\.approved").Count); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments