/test/SlowTests/Tests/Faceted/FacetsWithParameters.cs

https://github.com/fitzchak/ravendb · C# · 522 lines · 430 code · 91 blank · 1 comment · 29 complexity · 4e5a5cfdd6204ba6eb446890242fd690 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using FastTests;
  6. using Raven.Client.Documents;
  7. using Raven.Client.Documents.Indexes;
  8. using Raven.Client.Documents.Queries.Facets;
  9. using Xunit;
  10. namespace SlowTests.Tests.Faceted
  11. {
  12. public class FacetsWithParameters : RavenTestBase
  13. {
  14. private class Foo
  15. {
  16. public DateTime? DateIn { get; set; }
  17. public DateTime? DateIn2 { get; set; }
  18. public int Age { get; set; }
  19. public long Long { get; set; }
  20. public float Float { get; set; }
  21. public double Double { get; set; }
  22. public decimal Decimal { get; set; }
  23. }
  24. private class FooIndex : AbstractIndexCreationTask<Foo>
  25. {
  26. public FooIndex()
  27. {
  28. Map = docs => from foo in docs select new { foo.DateIn, foo.DateIn2, foo.Age, foo.Decimal, foo.Double, foo.Float, foo.Long };
  29. }
  30. }
  31. private static void StoreSampleData(DocumentStore store, DateTime now)
  32. {
  33. using (var session = store.OpenSession())
  34. {
  35. session.Store(new Foo());
  36. session.Store(new Foo { DateIn = now.AddMonths(-4) });
  37. session.Store(new Foo { DateIn = now.AddMonths(-10) });
  38. session.Store(new Foo { DateIn = now.AddMonths(-13) });
  39. session.Store(new Foo { DateIn = now.AddYears(-2) });
  40. session.SaveChanges();
  41. }
  42. }
  43. private static void AssertResults(IReadOnlyDictionary<string, FacetResult> facetResult, string key, DateTime now)
  44. {
  45. Assert.True(facetResult.TryGetValue(key, out var value));
  46. var oneYearAgo = DateTime.SpecifyKind(now - TimeSpan.FromDays(365), DateTimeKind.Unspecified);
  47. var sixMonthsAgo = DateTime.SpecifyKind(now - TimeSpan.FromDays(180), DateTimeKind.Unspecified);
  48. Assert.Equal($"{key} < {oneYearAgo:o}", value.Values[0].Range);
  49. Assert.Equal(2, value.Values[0].Count);
  50. Assert.Equal($"{key} >= {oneYearAgo:o} and {key} < {sixMonthsAgo:o}", value.Values[1].Range);
  51. Assert.Equal(1, value.Values[1].Count);
  52. Assert.Equal($"{key} >= {sixMonthsAgo:o}", value.Values[2].Range);
  53. Assert.Equal(1, value.Values[2].Count);
  54. }
  55. [Fact]
  56. public void FacetShouldUseParameters_WithFacetBaseList()
  57. {
  58. using (var store = GetDocumentStore())
  59. {
  60. new FooIndex().Execute(store);
  61. var now = DateTime.Now;
  62. StoreSampleData(store, now);
  63. WaitForIndexing(store);
  64. using (var session = store.OpenSession())
  65. {
  66. var facets = new List<FacetBase>
  67. {
  68. new RangeFacet<Foo>
  69. {
  70. Ranges =
  71. {
  72. c => c.DateIn < now - TimeSpan.FromDays(365), //2 hits
  73. c => c.DateIn >= now - TimeSpan.FromDays(365) && c.DateIn < now - TimeSpan.FromDays(180), //1 hit
  74. c => c.DateIn >= now - TimeSpan.FromDays(180) //1 hit
  75. }
  76. }
  77. };
  78. var query = session.Query<Foo, FooIndex>()
  79. .Where(x => x.DateIn != null && x.Age < 90)
  80. .AggregateBy(facets);
  81. Assert.Equal("from index 'FooIndex' where (DateIn != $p0 and Age < $p1) select " +
  82. "facet(DateIn < $p2, DateIn >= $p3 and DateIn < $p4, DateIn >= $p5)"
  83. , query.ToString());
  84. var facetResult = query.Execute();
  85. AssertResults(facetResult, "DateIn", now);
  86. }
  87. }
  88. }
  89. [Fact]
  90. public void FacetShouldUseParameters_WithTypedRangeFacetList()
  91. {
  92. using (var store = GetDocumentStore())
  93. {
  94. new FooIndex().Execute(store);
  95. var now = DateTime.Now;
  96. StoreSampleData(store, now);
  97. WaitForIndexing(store);
  98. using (var session = store.OpenSession())
  99. {
  100. var facets = new List<RangeFacet<Foo>>
  101. {
  102. new RangeFacet<Foo>
  103. {
  104. Ranges =
  105. {
  106. c => c.DateIn < now - TimeSpan.FromDays(365),
  107. c => c.DateIn >= now - TimeSpan.FromDays(365) && c.DateIn < now - TimeSpan.FromDays(180),
  108. c => c.DateIn >= now - TimeSpan.FromDays(180)
  109. }
  110. }
  111. };
  112. var query = session.Query<Foo, FooIndex>()
  113. .Where(x => x.DateIn != null && x.Age < 90)
  114. .AggregateBy(facets);
  115. Assert.Equal("from index 'FooIndex' where (DateIn != $p0 and Age < $p1) select " +
  116. "facet(DateIn < $p2, DateIn >= $p3 and DateIn < $p4, DateIn >= $p5)"
  117. , query.ToString());
  118. var facetResult = query.Execute();
  119. AssertResults(facetResult, "DateIn", now);
  120. }
  121. }
  122. }
  123. [Fact]
  124. public void FacetShouldUseParameters_WithIFacetBuilder()
  125. {
  126. using (var store = GetDocumentStore())
  127. {
  128. new FooIndex().Execute(store);
  129. var now = DateTime.Now;
  130. StoreSampleData(store, now);
  131. WaitForIndexing(store);
  132. using (var session = store.OpenSession())
  133. {
  134. var query = session
  135. .Query<Foo, FooIndex>()
  136. .Where(x => x.DateIn != null && x.Age < 90)
  137. .AggregateBy(builder => builder
  138. .ByRanges(
  139. c => c.DateIn < now - TimeSpan.FromDays(365),
  140. c => c.DateIn >= now - TimeSpan.FromDays(365) && c.DateIn < now - TimeSpan.FromDays(180),
  141. c => c.DateIn >= now - TimeSpan.FromDays(180)));
  142. Assert.Equal("from index 'FooIndex' where (DateIn != $p0 and Age < $p1) select " +
  143. "facet(DateIn < $p2, DateIn >= $p3 and DateIn < $p4, DateIn >= $p5)"
  144. , query.ToString());
  145. var facetResult = query.Execute();
  146. AssertResults(facetResult, "DateIn", now);
  147. }
  148. }
  149. }
  150. [Fact]
  151. public void FacetShouldUseParameters_WithUntypedRangeFacetList()
  152. {
  153. using (var store = GetDocumentStore())
  154. {
  155. new FooIndex().Execute(store);
  156. var now = DateTime.Now;
  157. StoreSampleData(store, now);
  158. WaitForIndexing(store);
  159. using (var session = store.OpenSession())
  160. {
  161. var facets = new List<RangeFacet>
  162. {
  163. new RangeFacet<Foo>
  164. {
  165. Ranges =
  166. {
  167. c => c.DateIn < now - TimeSpan.FromDays(365),
  168. c => c.DateIn >= now - TimeSpan.FromDays(365) && c.DateIn < now - TimeSpan.FromDays(180),
  169. c => c.DateIn >= now - TimeSpan.FromDays(180)
  170. }
  171. }
  172. };
  173. var query = session.Query<Foo, FooIndex>()
  174. .Where(x => x.DateIn != null && x.Age < 90)
  175. .AggregateBy(facets);
  176. Assert.Equal("from index 'FooIndex' where (DateIn != $p0 and Age < $p1) select " +
  177. "facet(DateIn < $p2, DateIn >= $p3 and DateIn < $p4, DateIn >= $p5)"
  178. , query.ToString());
  179. var facetResult = query.Execute();
  180. AssertResults(facetResult, "DateIn", now);
  181. }
  182. }
  183. }
  184. [Fact]
  185. public void FacetSetupDocument_ShouldNotUseParameters()
  186. {
  187. using (var store = GetDocumentStore())
  188. {
  189. new FooIndex().Execute(store);
  190. var now = DateTime.Now;
  191. var oneYearAgo = DateTime.SpecifyKind(now - TimeSpan.FromDays(365), DateTimeKind.Unspecified);
  192. var sixMonthsAgo = DateTime.SpecifyKind(now - TimeSpan.FromDays(180), DateTimeKind.Unspecified);
  193. StoreSampleData(store, now);
  194. WaitForIndexing(store);
  195. using (var s = store.OpenSession())
  196. {
  197. var facets = new List<RangeFacet>
  198. {
  199. new RangeFacet<Foo>
  200. {
  201. Ranges =
  202. {
  203. c => c.DateIn < now - TimeSpan.FromDays(365),
  204. c => c.DateIn >= now - TimeSpan.FromDays(365) && c.DateIn < now - TimeSpan.FromDays(180),
  205. c => c.DateIn >= now - TimeSpan.FromDays(180)
  206. }
  207. }
  208. };
  209. var facetSetupDoc = new FacetSetup { Id = "facets/FooFacets", RangeFacets = facets };
  210. s.Store(facetSetupDoc);
  211. s.SaveChanges();
  212. var rangeFacet = facetSetupDoc.RangeFacets[0];
  213. Assert.Equal($"DateIn < '{oneYearAgo:o}'", rangeFacet.Ranges[0]);
  214. Assert.Equal($"DateIn >= '{oneYearAgo:o}' and DateIn < '{sixMonthsAgo:o}'", rangeFacet.Ranges[1]);
  215. Assert.Equal($"DateIn >= '{sixMonthsAgo:o}'", rangeFacet.Ranges[2]);
  216. }
  217. using (var session = store.OpenSession())
  218. {
  219. var query = session.Query<Foo, FooIndex>()
  220. .Where(x => x.DateIn != null && x.Age < 90)
  221. .AggregateUsing("facets/FooFacets");
  222. Assert.Equal("from index 'FooIndex' where (DateIn != $p0 and Age < $p1) " +
  223. "select facet(id('facets/FooFacets'))"
  224. , query.ToString());
  225. var facetResult = query.Execute();
  226. AssertResults(facetResult, "DateIn", now);
  227. }
  228. using (var session = store.OpenSession())
  229. {
  230. var facets = session.Load<FacetSetup>("facets/FooFacets").RangeFacets;
  231. var query = session.Query<Foo, FooIndex>()
  232. .Where(x => x.DateIn != null && x.Age < 90)
  233. .AggregateBy(facets);
  234. Assert.Equal("from index 'FooIndex' where (DateIn != $p0 and Age < $p1) " +
  235. $"select facet(DateIn < '{oneYearAgo:o}', DateIn >= '{oneYearAgo:o}' and DateIn < '{sixMonthsAgo:o}', DateIn >= '{sixMonthsAgo:o}')"
  236. , query.ToString());
  237. var facetResult = query.Execute();
  238. AssertResults(facetResult, "DateIn", now);
  239. }
  240. }
  241. }
  242. [Fact]
  243. public async Task TwoDifferentAsyncQueriesThatAreUsingTheSameFacetWithParametersShouldWork()
  244. {
  245. using (var store = GetDocumentStore())
  246. {
  247. new FooIndex().Execute(store);
  248. var now = DateTime.Now;
  249. StoreSampleData(store, now);
  250. WaitForIndexing(store);
  251. using (var session = store.OpenAsyncSession())
  252. {
  253. var facets = new List<RangeFacet<Foo>>
  254. {
  255. new RangeFacet<Foo>
  256. {
  257. Ranges =
  258. {
  259. c => c.DateIn < now - TimeSpan.FromDays(365),
  260. c => c.DateIn >= now - TimeSpan.FromDays(365) && c.DateIn < now - TimeSpan.FromDays(180),
  261. c => c.DateIn >= now - TimeSpan.FromDays(180)
  262. }
  263. }
  264. };
  265. var query1 = session.Query<Foo, FooIndex>()
  266. .Where(x => x.DateIn != null && x.Age < 90)
  267. .AggregateBy(facets);
  268. var query2 = session.Query<Foo, FooIndex>()
  269. .Where(x => x.DateIn != null)
  270. .AggregateBy(facets);
  271. Assert.Equal("from index 'FooIndex' where (DateIn != $p0 and Age < $p1) select " +
  272. "facet(DateIn < $p2, DateIn >= $p3 and DateIn < $p4, DateIn >= $p5)"
  273. , query1.ToString());
  274. Assert.Equal("from index 'FooIndex' where DateIn != $p0 select " +
  275. "facet(DateIn < $p1, DateIn >= $p2 and DateIn < $p3, DateIn >= $p4)"
  276. , query2.ToString());
  277. var facetResult1 = await query1.ExecuteAsync();
  278. var facetResult2 = await query2.ExecuteAsync();
  279. AssertResults(facetResult1, "DateIn", now);
  280. AssertResults(facetResult2, "DateIn", now);
  281. }
  282. }
  283. }
  284. [Fact]
  285. public void QueryUsingMultipuleFacetsWithParametersShouldWork()
  286. {
  287. using (var store = GetDocumentStore())
  288. {
  289. new FooIndex().Execute(store);
  290. var now = DateTime.Now;
  291. using (var session = store.OpenSession())
  292. {
  293. session.Store(new Foo());
  294. session.Store(new Foo { DateIn = now.AddMonths(-4), DateIn2 = now.AddMonths(-4) });
  295. session.Store(new Foo { DateIn = now.AddMonths(-10), DateIn2 = now.AddMonths(-10) });
  296. session.Store(new Foo { DateIn = now.AddMonths(-13), DateIn2 = now.AddMonths(-13) });
  297. session.Store(new Foo { DateIn = now.AddYears(-2), DateIn2 = now.AddYears(-2) });
  298. session.SaveChanges();
  299. }
  300. WaitForIndexing(store);
  301. using (var session = store.OpenSession())
  302. {
  303. //new List<RangeFacet>
  304. var facets = new List<RangeFacet<Foo>>
  305. {
  306. new RangeFacet<Foo>
  307. {
  308. Ranges =
  309. {
  310. c => c.DateIn < now - TimeSpan.FromDays(365),
  311. c => c.DateIn >= now - TimeSpan.FromDays(365) && c.DateIn < now - TimeSpan.FromDays(180),
  312. c => c.DateIn >= now - TimeSpan.FromDays(180)
  313. }
  314. },
  315. new RangeFacet<Foo>
  316. {
  317. Ranges =
  318. {
  319. c => c.DateIn2 < now - TimeSpan.FromDays(365),
  320. c => c.DateIn2 >= now - TimeSpan.FromDays(365) && c.DateIn2 < now - TimeSpan.FromDays(180),
  321. c => c.DateIn2 >= now - TimeSpan.FromDays(180)
  322. }
  323. }
  324. };
  325. var query = session.Query<Foo, FooIndex>()
  326. .Where(x => x.DateIn != null && x.Age < 90)
  327. .AggregateBy(facets);
  328. Assert.Equal("from index 'FooIndex' where (DateIn != $p0 and Age < $p1) select " +
  329. "facet(DateIn < $p2, DateIn >= $p3 and DateIn < $p4, DateIn >= $p5), " +
  330. "facet(DateIn2 < $p6, DateIn2 >= $p7 and DateIn2 < $p8, DateIn2 >= $p9)"
  331. , query.ToString());
  332. var facetResult = query.Execute();
  333. Assert.Equal(2, facetResult.Count);
  334. AssertResults(facetResult, "DateIn", now);
  335. AssertResults(facetResult, "DateIn2", now);
  336. }
  337. }
  338. }
  339. [Fact]
  340. public void FacetShouldUseParameters_WithNumbers()
  341. {
  342. using (var store = GetDocumentStore())
  343. {
  344. new FooIndex().Execute(store);
  345. using (var session = store.OpenSession())
  346. {
  347. session.Store(new Foo
  348. {
  349. Age = 19,
  350. Decimal = 99.8m,
  351. Long = 2000,
  352. Double = 0.25,
  353. Float = (float)0.75
  354. });
  355. session.Store(new Foo
  356. {
  357. Age = 17,
  358. Decimal = 150,
  359. Long = 3000,
  360. Double = 0.75,
  361. Float = (float)1.8
  362. });
  363. session.SaveChanges();
  364. }
  365. WaitForIndexing(store);
  366. using (var session = store.OpenSession())
  367. {
  368. var facets = new List<RangeFacet>
  369. {
  370. new RangeFacet<Foo>
  371. {
  372. Ranges =
  373. {
  374. c => c.Age < 18,
  375. c => c.Age >= 18
  376. }
  377. },
  378. new RangeFacet<Foo>
  379. {
  380. Ranges =
  381. {
  382. c => c.Long > 1000 && c.Long < 2500,
  383. c => c.Long >= 2500
  384. }
  385. },
  386. new RangeFacet<Foo>
  387. {
  388. Ranges =
  389. {
  390. c => c.Float > (float)0.65 && c.Float < (float)1.15,
  391. c => c.Float >= (float)1.15,
  392. }
  393. },
  394. new RangeFacet<Foo>
  395. {
  396. Ranges =
  397. {
  398. c => c.Double > 0.15 && c.Double < 0.5,
  399. c => c.Double >= 0.5
  400. }
  401. },
  402. new RangeFacet<Foo>
  403. {
  404. Ranges =
  405. {
  406. c => c.Decimal < 100m ,
  407. c => c.Decimal >= 100m && c.Decimal < 200m
  408. }
  409. }
  410. };
  411. var query = session.Query<Foo, FooIndex>()
  412. .AggregateBy(facets);
  413. Assert.Equal("from index 'FooIndex' select facet(Age < $p0, Age >= $p1), " +
  414. "facet(Long > $p2 and Long < $p3, Long >= $p4), " +
  415. "facet(Float > $p5 and Float < $p6, Float >= $p7), " +
  416. "facet(Double > $p8 and Double < $p9, Double >= $p10), " +
  417. "facet(Decimal < $p11, Decimal >= $p12 and Decimal < $p13)"
  418. , query.ToString());
  419. var facetResult = query.Execute();
  420. Assert.Equal(5, facetResult.Count);
  421. foreach (var key in facetResult.Keys)
  422. {
  423. Assert.Equal(2, facetResult[key].Values.Count);
  424. Assert.Equal(1, facetResult[key].Values[0].Count);
  425. Assert.Equal(1, facetResult[key].Values[1].Count);
  426. }
  427. }
  428. }
  429. }
  430. }
  431. }