/test/SlowTests/Issues/RavenDb-1934.cs

https://github.com/fitzchak/ravendb · C# · 461 lines · 377 code · 84 blank · 0 comment · 5 complexity · ffa6802c3040fe878e50ffb98a5b4bb4 MD5 · raw file

  1. using System;
  2. using System.Linq;
  3. using FastTests;
  4. using Raven.Client.Documents;
  5. using Raven.Client.Documents.Indexes;
  6. using Xunit;
  7. namespace SlowTests.Issues
  8. {
  9. public class RavenDb_1934 : RavenTestBase
  10. {
  11. private class Foo
  12. {
  13. public string Id { get; set; }
  14. public int? Age { get; set; }
  15. public short? TestShort { get; set; }
  16. public float? Grade { get; set; }
  17. public double? Price { get; set; }
  18. public string Name { get; set; }
  19. public TimeSpan? Start { get; set; }
  20. public TimeSpan Until { get; set; }
  21. }
  22. private class Bar
  23. {
  24. public string Id { get; set; }
  25. public string SomeData { get; set; }
  26. }
  27. [Fact]
  28. public void TimeSpan_Can_Get_Range_Under_A_Day()
  29. {
  30. using (var documentStore = GetDocumentStore())
  31. {
  32. using (var session = documentStore.OpenSession())
  33. {
  34. session.Store(new Foo { Start = TimeSpan.FromHours(10), Until = TimeSpan.FromHours(20) });
  35. session.SaveChanges();
  36. }
  37. using (var session = documentStore.OpenSession())
  38. {
  39. var time = TimeSpan.FromHours(15);
  40. var result = session.Query<Foo>()
  41. .Customize(x => x.WaitForNonStaleResults())
  42. .SingleOrDefault(x => x.Start <= time && x.Until > time);
  43. Assert.NotNull(result);
  44. }
  45. }
  46. }
  47. [Fact]
  48. public void TimeSpan_Can_Get_Range_Over_A_Day()
  49. {
  50. using (var documentStore = GetDocumentStore())
  51. {
  52. using (var session = documentStore.OpenSession())
  53. {
  54. session.Store(new Foo { Start = TimeSpan.FromHours(30), Until = TimeSpan.FromHours(40) });
  55. session.SaveChanges();
  56. }
  57. using (var session = documentStore.OpenSession())
  58. {
  59. var time = TimeSpan.FromHours(35);
  60. var result = session.Query<Foo>()
  61. .Customize(x => x.WaitForNonStaleResults())
  62. .SingleOrDefault(x => x.Start <= time && x.Until > time);
  63. Assert.NotNull(result);
  64. }
  65. }
  66. }
  67. [Fact]
  68. public void TimeSpan_Can_Get_Range_Mixed_Days()
  69. {
  70. using (var documentStore = GetDocumentStore())
  71. {
  72. using (var session = documentStore.OpenSession())
  73. {
  74. session.Store(new Foo { Start = TimeSpan.FromHours(20), Until = TimeSpan.FromHours(30) });
  75. session.SaveChanges();
  76. }
  77. using (var session = documentStore.OpenSession())
  78. {
  79. var time = TimeSpan.FromHours(25);
  80. var result = session.Query<Foo>()
  81. .Customize(x => x.WaitForNonStaleResults())
  82. .SingleOrDefault(x => x.Start <= time && x.Until > time);
  83. Assert.NotNull(result);
  84. }
  85. }
  86. }
  87. [Fact]
  88. public void TimeSpan_Can_Get_Range_VeryLarge()
  89. {
  90. using (var documentStore = GetDocumentStore())
  91. {
  92. using (var session = documentStore.OpenSession())
  93. {
  94. session.Store(new Foo { Start = TimeSpan.FromHours(10), Until = TimeSpan.FromDays(100) });
  95. session.SaveChanges();
  96. }
  97. using (var session = documentStore.OpenSession())
  98. {
  99. var time = TimeSpan.FromDays(2);
  100. var result = session.Query<Foo>()
  101. .Customize(x => x.WaitForNonStaleResults())
  102. .SingleOrDefault(x => x.Start <= time && x.Until > time);
  103. Assert.NotNull(result);
  104. }
  105. }
  106. }
  107. [Fact]
  108. public void TimeSpan_Can_Get_Range_Negatives()
  109. {
  110. using (var documentStore = GetDocumentStore())
  111. {
  112. using (var session = documentStore.OpenSession())
  113. {
  114. session.Store(new Foo { Start = TimeSpan.FromHours(-10), Until = TimeSpan.FromHours(10) });
  115. session.SaveChanges();
  116. }
  117. using (var session = documentStore.OpenSession())
  118. {
  119. var time = TimeSpan.FromHours(1);
  120. var result = session.Query<Foo>()
  121. .Customize(x => x.WaitForNonStaleResults())
  122. .SingleOrDefault(x => x.Start <= time && x.Until > time);
  123. Assert.NotNull(result);
  124. }
  125. }
  126. }
  127. [Fact]
  128. public void Can_Sort_On_TimeSpans()
  129. {
  130. using (var documentStore = GetDocumentStore())
  131. {
  132. using (var session = documentStore.OpenSession())
  133. {
  134. session.Store(new Foo { Id = "1", Start = TimeSpan.FromSeconds(10) });
  135. session.Store(new Foo { Id = "2", Start = TimeSpan.FromSeconds(20) });
  136. session.Store(new Foo { Id = "3", Start = TimeSpan.FromSeconds(15) });
  137. session.SaveChanges();
  138. }
  139. WaitForIndexing(documentStore);
  140. using (var session = documentStore.OpenSession())
  141. {
  142. var results = session.Query<Foo>()
  143. .OrderBy(x => x.Start)
  144. .ToArray();
  145. Assert.Equal("1", results[0].Id);
  146. Assert.Equal("3", results[1].Id);
  147. Assert.Equal("2", results[2].Id);
  148. }
  149. using (var session = documentStore.OpenSession())
  150. {
  151. var results = session.Query<Foo>()
  152. .OrderByDescending(x => x.Start)
  153. .ToArray();
  154. Assert.Equal("1", results[2].Id);
  155. Assert.Equal("3", results[1].Id);
  156. Assert.Equal("2", results[0].Id);
  157. }
  158. }
  159. }
  160. [Fact]
  161. public void Can_Sort_On_TimeSpans_With_Nulls()
  162. {
  163. using (var documentStore = GetDocumentStore())
  164. {
  165. using (var session = documentStore.OpenSession())
  166. {
  167. session.Store(new Foo { Id = "1", Start = TimeSpan.FromSeconds(10) });
  168. session.Store(new Foo { Id = "2", Start = TimeSpan.FromSeconds(20) });
  169. session.Store(new Foo { Id = "3", Start = TimeSpan.FromSeconds(15) });
  170. session.Store(new Foo { Id = "4", Start = null });
  171. session.SaveChanges();
  172. }
  173. WaitForIndexing(documentStore);
  174. using (var session = documentStore.OpenSession())
  175. {
  176. var results = session.Query<Foo>()
  177. .OrderBy(x => x.Start)
  178. .ToArray();
  179. Assert.Equal("4", results[0].Id);
  180. Assert.Equal("1", results[1].Id);
  181. Assert.Equal("3", results[2].Id);
  182. Assert.Equal("2", results[3].Id);
  183. }
  184. }
  185. }
  186. [Fact]
  187. public void Can_Sort_On_Ints_With_Nulls()
  188. {
  189. using (var documentStore = GetDocumentStore())
  190. {
  191. using (var session = documentStore.OpenSession())
  192. {
  193. session.Store(new Foo { Id = "1", Age = 10 });
  194. session.Store(new Foo { Id = "2", Age = 20 });
  195. session.Store(new Foo { Id = "3", Age = 15 });
  196. session.Store(new Foo { Id = "4", Age = null });
  197. session.SaveChanges();
  198. }
  199. WaitForIndexing(documentStore);
  200. using (var session = documentStore.OpenSession())
  201. {
  202. var results = session.Query<Foo>()
  203. .Customize(x => x.WaitForNonStaleResults())
  204. .OrderBy(x => x.Age)
  205. .ToArray();
  206. Assert.Equal("4", results[0].Id);
  207. Assert.Equal("1", results[1].Id);
  208. Assert.Equal("3", results[2].Id);
  209. Assert.Equal("2", results[3].Id);
  210. }
  211. }
  212. }
  213. [Fact]
  214. public void Can_Sort_On_Short_With_Nulls()
  215. {
  216. using (var documentStore = GetDocumentStore())
  217. {
  218. using (var session = documentStore.OpenSession())
  219. {
  220. session.Store(new Foo { Id = "1", TestShort = 10 });
  221. session.Store(new Foo { Id = "2", TestShort = 20 });
  222. session.Store(new Foo { Id = "3", TestShort = 15 });
  223. session.Store(new Foo { Id = "4", TestShort = null });
  224. session.SaveChanges();
  225. }
  226. WaitForIndexing(documentStore);
  227. using (var session = documentStore.OpenSession())
  228. {
  229. var results = session.Query<Foo>()
  230. .Customize(x => x.WaitForNonStaleResults())
  231. .OrderBy(x => x.TestShort)
  232. .ToArray();
  233. Assert.Equal("4", results[0].Id);
  234. Assert.Equal("1", results[1].Id);
  235. Assert.Equal("3", results[2].Id);
  236. Assert.Equal("2", results[3].Id);
  237. }
  238. }
  239. }
  240. [Fact]
  241. public void Can_Sort_On_Float_With_Nulls()
  242. {
  243. using (var documentStore = GetDocumentStore())
  244. {
  245. using (var session = documentStore.OpenSession())
  246. {
  247. session.Store(new Foo { Id = "1", Grade = (float?)10.1 });
  248. session.Store(new Foo { Id = "2", Grade = (float?)20.4 });
  249. session.Store(new Foo { Id = "3", Grade = (float?)15.5 });
  250. session.Store(new Foo { Id = "4", Grade = null });
  251. session.SaveChanges();
  252. }
  253. WaitForIndexing(documentStore);
  254. using (var session = documentStore.OpenSession())
  255. {
  256. var results = session.Query<Foo>()
  257. .Customize(x => x.WaitForNonStaleResults())
  258. .OrderBy(x => x.Grade)
  259. .ToArray();
  260. Assert.Equal("4", results[0].Id);
  261. Assert.Equal("1", results[1].Id);
  262. Assert.Equal("3", results[2].Id);
  263. Assert.Equal("2", results[3].Id);
  264. }
  265. }
  266. }
  267. [Fact]
  268. public void Can_Sort_On_Double_With_Nulls()
  269. {
  270. using (var documentStore = GetDocumentStore())
  271. {
  272. using (var session = documentStore.OpenSession())
  273. {
  274. session.Store(new Foo { Id = "1", Price = 10.1 });
  275. session.Store(new Foo { Id = "2", Price = 20.4 });
  276. session.Store(new Foo { Id = "3", Price = 15.5 });
  277. session.Store(new Foo { Id = "4", Price = null });
  278. session.SaveChanges();
  279. }
  280. WaitForIndexing(documentStore);
  281. using (var session = documentStore.OpenSession())
  282. {
  283. var results = session.Query<Foo>()
  284. .Customize(x => x.WaitForNonStaleResults())
  285. .OrderBy(x => x.Price)
  286. .ToArray();
  287. Assert.Equal("4", results[0].Id);
  288. Assert.Equal("1", results[1].Id);
  289. Assert.Equal("3", results[2].Id);
  290. Assert.Equal("2", results[3].Id);
  291. }
  292. }
  293. }
  294. [Fact]
  295. public void Can_Sort_On_String_With_Nulls()
  296. {
  297. using (var documentStore = GetDocumentStore())
  298. {
  299. using (var session = documentStore.OpenSession())
  300. {
  301. session.Store(new Foo { Id = "1", Name = "aaa" });
  302. session.Store(new Foo { Id = "2", Name = "cc" });
  303. session.Store(new Foo { Id = "3", Name = "b1" });
  304. session.Store(new Foo { Id = "4", Name = null });
  305. session.SaveChanges();
  306. }
  307. WaitForIndexing(documentStore);
  308. using (var session = documentStore.OpenSession())
  309. {
  310. var results = session.Query<Foo>()
  311. .Customize(x => x.WaitForNonStaleResults())
  312. .OrderBy(x => x.Name)
  313. .ToArray();
  314. Assert.Equal("4", results[0].Id);
  315. Assert.Equal("1", results[1].Id);
  316. Assert.Equal("3", results[2].Id);
  317. Assert.Equal("2", results[3].Id);
  318. }
  319. }
  320. }
  321. [Fact]
  322. public void Can_Sort_On_TimeSpans_With_Nulls_Using_MultiMap_Idx()
  323. {
  324. using (var documentStore = GetDocumentStore())
  325. {
  326. new TimeSpanTestMultiMapIndex().Execute(documentStore);
  327. using (var session = documentStore.OpenSession())
  328. {
  329. session.Store(new Foo { Id = "1", Start = TimeSpan.FromSeconds(10) });
  330. session.Store(new Foo { Id = "2", Start = TimeSpan.FromSeconds(20) });
  331. session.Store(new Foo { Id = "3", Start = TimeSpan.FromSeconds(15) });
  332. session.Store(new Bar { Id = "4" });
  333. session.SaveChanges();
  334. }
  335. WaitForIndexing(documentStore);
  336. using (var session = documentStore.OpenSession())
  337. {
  338. var results = session.Query<Foo, TimeSpanTestMultiMapIndex>()
  339. .OrderBy(x => x.Start)
  340. .ProjectInto<Foo>()
  341. .ToArray();
  342. Assert.Equal("4", results[0].Id);
  343. Assert.Equal("1", results[1].Id);
  344. Assert.Equal("3", results[2].Id);
  345. Assert.Equal("2", results[3].Id);
  346. }
  347. using (var session = documentStore.OpenSession())
  348. {
  349. var results = session.Query<Foo, TimeSpanTestMultiMapIndex>()
  350. .OrderByDescending(x => x.Start)
  351. .ProjectInto<Foo>()
  352. .ToArray();
  353. Assert.Equal("4", results[3].Id);
  354. Assert.Equal("1", results[2].Id);
  355. Assert.Equal("3", results[1].Id);
  356. Assert.Equal("2", results[0].Id);
  357. }
  358. }
  359. }
  360. private class TimeSpanTestMultiMapIndex : AbstractMultiMapIndexCreationTask<Foo>
  361. {
  362. public TimeSpanTestMultiMapIndex()
  363. {
  364. AddMap<Foo>(docs => from d in docs
  365. select new Foo
  366. {
  367. Start = d.Start,
  368. });
  369. AddMap<Bar>(docs => from d in docs
  370. select new Foo
  371. {
  372. Start = null,
  373. });
  374. }
  375. }
  376. }
  377. }