/ToMigrate/Raven.Tests.Issues/RavenDB_1302.cs

https://github.com/fitzchak/ravendb · C# · 554 lines · 458 code · 91 blank · 5 comment · 1 complexity · fcfe2f59ad5786b65b11f290f818c26b MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="RavenDB_1302.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System;
  7. using System.Globalization;
  8. using System.Threading;
  9. using Raven.Client;
  10. using Raven.Client.Document;
  11. using Raven.Tests.Common;
  12. using Raven.Tests.Common.Util;
  13. using Xunit;
  14. namespace Raven.Tests.Issues
  15. {
  16. public class RavenDB_1302 : RavenTest
  17. {
  18. private class Product
  19. {
  20. }
  21. [Fact]
  22. public void FirstOrDefaultShouldSetPageSizeToOne()
  23. {
  24. using (var store = NewRemoteDocumentStore())
  25. {
  26. store.InitializeProfiling();
  27. Guid id;
  28. using (var session = store.OpenSession())
  29. {
  30. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  31. var product = session
  32. .Advanced
  33. .DocumentQuery<Product>()
  34. .FirstOrDefault();
  35. Assert.Null(product);
  36. }
  37. var profilingInformation = store.GetProfilingInformationFor(id);
  38. Assert.Equal(1, profilingInformation.Requests.Count);
  39. Assert.Contains("pageSize=1", profilingInformation.Requests[0].Url);
  40. }
  41. }
  42. [Fact]
  43. public void FirstShouldSetPageSizeToOne()
  44. {
  45. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  46. using (var store = NewRemoteDocumentStore())
  47. {
  48. store.InitializeProfiling();
  49. Guid id;
  50. using (var session = store.OpenSession())
  51. {
  52. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  53. var e = Assert.Throws<InvalidOperationException>(
  54. () => session
  55. .Advanced
  56. .DocumentQuery<Product>()
  57. .First());
  58. Assert.Equal("Sequence contains no elements", e.Message);
  59. }
  60. var profilingInformation = store.GetProfilingInformationFor(id);
  61. Assert.Equal(1, profilingInformation.Requests.Count);
  62. Assert.Contains("pageSize=1", profilingInformation.Requests[0].Url);
  63. }
  64. }
  65. [Fact]
  66. public void SingleOrDefaultShouldSetPageSizeToTwoIfItHasNotBeenSet()
  67. {
  68. using (var store = NewRemoteDocumentStore())
  69. {
  70. store.InitializeProfiling();
  71. Guid id;
  72. using (var session = store.OpenSession())
  73. {
  74. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  75. var product = session
  76. .Advanced
  77. .DocumentQuery<Product>()
  78. .SingleOrDefault();
  79. Assert.Null(product);
  80. }
  81. var profilingInformation = store.GetProfilingInformationFor(id);
  82. Assert.Equal(1, profilingInformation.Requests.Count);
  83. Assert.Contains("pageSize=2", profilingInformation.Requests[0].Url);
  84. }
  85. }
  86. [Fact]
  87. public void SingleShouldSetPageSizeToTwoIfItHasNotBeenSet()
  88. {
  89. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  90. using (var store = NewRemoteDocumentStore())
  91. {
  92. store.InitializeProfiling();
  93. Guid id;
  94. using (var session = store.OpenSession())
  95. {
  96. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  97. var e = Assert.Throws<InvalidOperationException>(
  98. () => session
  99. .Advanced
  100. .DocumentQuery<Product>()
  101. .Single());
  102. Assert.Equal("Sequence contains no elements", e.Message);
  103. }
  104. var profilingInformation = store.GetProfilingInformationFor(id);
  105. Assert.Equal(1, profilingInformation.Requests.Count);
  106. Assert.Contains("pageSize=2", profilingInformation.Requests[0].Url);
  107. }
  108. }
  109. [Fact]
  110. public void FirstOrDefaultShouldSetPageSizeToOneIfItIsBigger()
  111. {
  112. using (var store = NewRemoteDocumentStore())
  113. {
  114. store.InitializeProfiling();
  115. Guid id;
  116. using (var session = store.OpenSession())
  117. {
  118. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  119. var product = session
  120. .Advanced
  121. .DocumentQuery<Product>()
  122. .Take(100)
  123. .FirstOrDefault();
  124. Assert.Null(product);
  125. }
  126. var profilingInformation = store.GetProfilingInformationFor(id);
  127. Assert.Equal(1, profilingInformation.Requests.Count);
  128. Assert.Contains("pageSize=1", profilingInformation.Requests[0].Url);
  129. }
  130. }
  131. [Fact]
  132. public void FirstShouldSetPageSizeToOneIfItIsBigger()
  133. {
  134. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  135. using (var store = NewRemoteDocumentStore())
  136. {
  137. store.InitializeProfiling();
  138. Guid id;
  139. using (var session = store.OpenSession())
  140. {
  141. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  142. var e = Assert.Throws<InvalidOperationException>(
  143. () => session
  144. .Advanced
  145. .DocumentQuery<Product>()
  146. .Take(100)
  147. .First());
  148. Assert.Equal("Sequence contains no elements", e.Message);
  149. }
  150. var profilingInformation = store.GetProfilingInformationFor(id);
  151. Assert.Equal(1, profilingInformation.Requests.Count);
  152. Assert.Contains("pageSize=1", profilingInformation.Requests[0].Url);
  153. }
  154. }
  155. [Fact]
  156. public void SingleOrDefaultShouldSetPageSizeToTwoIfItIsBigger()
  157. {
  158. using (var store = NewRemoteDocumentStore())
  159. {
  160. store.InitializeProfiling();
  161. Guid id;
  162. using (var session = store.OpenSession())
  163. {
  164. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  165. var product = session
  166. .Advanced
  167. .DocumentQuery<Product>()
  168. .Take(100)
  169. .SingleOrDefault();
  170. Assert.Null(product);
  171. }
  172. var profilingInformation = store.GetProfilingInformationFor(id);
  173. Assert.Equal(1, profilingInformation.Requests.Count);
  174. Assert.Contains("pageSize=2", profilingInformation.Requests[0].Url);
  175. }
  176. }
  177. [Fact]
  178. public void SingleShouldSetPageSizeToTwoIfItIsBigger()
  179. {
  180. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  181. using (var store = NewRemoteDocumentStore())
  182. {
  183. store.InitializeProfiling();
  184. Guid id;
  185. using (var session = store.OpenSession())
  186. {
  187. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  188. var e = Assert.Throws<InvalidOperationException>(
  189. () => session
  190. .Advanced
  191. .DocumentQuery<Product>()
  192. .Take(100)
  193. .Single());
  194. Assert.Equal("Sequence contains no elements", e.Message);
  195. }
  196. var profilingInformation = store.GetProfilingInformationFor(id);
  197. Assert.Equal(1, profilingInformation.Requests.Count);
  198. Assert.Contains("pageSize=2", profilingInformation.Requests[0].Url);
  199. }
  200. }
  201. [Fact]
  202. public void SingleOrDefaultShouldNotSetPageToTwoIfPageIsSmaller()
  203. {
  204. using (var store = NewRemoteDocumentStore())
  205. {
  206. store.InitializeProfiling();
  207. Guid id;
  208. using (var session = store.OpenSession())
  209. {
  210. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  211. var product = session
  212. .Advanced
  213. .DocumentQuery<Product>()
  214. .Take(1)
  215. .SingleOrDefault();
  216. Assert.Null(product);
  217. }
  218. var profilingInformation = store.GetProfilingInformationFor(id);
  219. Assert.Equal(1, profilingInformation.Requests.Count);
  220. Assert.Contains("pageSize=1", profilingInformation.Requests[0].Url);
  221. }
  222. }
  223. [Fact]
  224. public void SingleShouldNotSetPageToTwoIfPageIsSmaller()
  225. {
  226. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  227. using (var store = NewRemoteDocumentStore())
  228. {
  229. store.InitializeProfiling();
  230. Guid id;
  231. using (var session = store.OpenSession())
  232. {
  233. id = ((DocumentSession)session).DatabaseCommands.ProfilingInformation.Id;
  234. var e = Assert.Throws<InvalidOperationException>(
  235. () => session
  236. .Advanced
  237. .DocumentQuery<Product>()
  238. .Take(1)
  239. .Single());
  240. Assert.Equal("Sequence contains no elements", e.Message);
  241. }
  242. var profilingInformation = store.GetProfilingInformationFor(id);
  243. Assert.Equal(1, profilingInformation.Requests.Count);
  244. Assert.Contains("pageSize=1", profilingInformation.Requests[0].Url);
  245. }
  246. }
  247. [Fact]
  248. public void FirstOrDefaultShouldNotThrowIfSequenceIsEmpty()
  249. {
  250. using (var store = NewDocumentStore())
  251. {
  252. using (var session = store.OpenSession())
  253. {
  254. var product = session
  255. .Advanced
  256. .DocumentQuery<Product>()
  257. .FirstOrDefault();
  258. Assert.Null(product);
  259. }
  260. }
  261. }
  262. [Fact]
  263. public void FirstShouldThrowIfSequenceIsEmpty()
  264. {
  265. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  266. using (var store = NewDocumentStore())
  267. {
  268. using (var session = store.OpenSession())
  269. {
  270. var e = Assert.Throws<InvalidOperationException>(
  271. () => session
  272. .Advanced
  273. .DocumentQuery<Product>()
  274. .First());
  275. Assert.Equal("Sequence contains no elements", e.Message);
  276. }
  277. }
  278. }
  279. [Fact]
  280. public void SingleOrDefaultShouldNotThrowIfSequenceIsEmpty()
  281. {
  282. using (var store = NewDocumentStore())
  283. {
  284. using (var session = store.OpenSession())
  285. {
  286. var product = session
  287. .Advanced
  288. .DocumentQuery<Product>()
  289. .SingleOrDefault();
  290. Assert.Null(product);
  291. }
  292. }
  293. }
  294. [Fact]
  295. public void SingleShouldThrowIfSequenceIsEmpty()
  296. {
  297. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  298. using (var store = NewDocumentStore())
  299. {
  300. using (var session = store.OpenSession())
  301. {
  302. var e = Assert.Throws<InvalidOperationException>(
  303. () => session
  304. .Advanced
  305. .DocumentQuery<Product>()
  306. .Single());
  307. Assert.Equal("Sequence contains no elements", e.Message);
  308. }
  309. }
  310. }
  311. [Fact]
  312. public void FirstOrDefaultShouldNotThrowIfSequenceContainsOneElement()
  313. {
  314. using (var store = NewDocumentStore())
  315. {
  316. Fill(store, 1);
  317. using (var session = store.OpenSession())
  318. {
  319. var product = session
  320. .Advanced
  321. .DocumentQuery<Product>()
  322. .WaitForNonStaleResultsAsOfNow()
  323. .FirstOrDefault();
  324. Assert.NotNull(product);
  325. }
  326. }
  327. }
  328. [Fact]
  329. public void FirstShouldNonThrowIfSequenceContainsOneElement()
  330. {
  331. using (var store = NewDocumentStore())
  332. {
  333. Fill(store, 1);
  334. using (var session = store.OpenSession())
  335. {
  336. var product = session
  337. .Advanced
  338. .DocumentQuery<Product>()
  339. .WaitForNonStaleResultsAsOfNow()
  340. .First();
  341. Assert.NotNull(product);
  342. }
  343. }
  344. }
  345. [Fact]
  346. public void SingleOrDefaultShouldNotThrowIfSequenceContainsOneElement()
  347. {
  348. using (var store = NewDocumentStore())
  349. {
  350. Fill(store, 1);
  351. using (var session = store.OpenSession())
  352. {
  353. var product = session
  354. .Advanced
  355. .DocumentQuery<Product>()
  356. .WaitForNonStaleResultsAsOfNow()
  357. .SingleOrDefault();
  358. Assert.NotNull(product);
  359. }
  360. }
  361. }
  362. [Fact]
  363. public void SingleShouldNotThrowIfSequenceContainsOneElement()
  364. {
  365. using (var store = NewDocumentStore())
  366. {
  367. Fill(store, 1);
  368. using (var session = store.OpenSession())
  369. {
  370. var product = session
  371. .Advanced
  372. .DocumentQuery<Product>()
  373. .WaitForNonStaleResultsAsOfNow()
  374. .Single();
  375. Assert.NotNull(product);
  376. }
  377. }
  378. }
  379. [Fact]
  380. public void FirstOrDefaultShouldNotThrowIfSequenceContainsTwoElements()
  381. {
  382. using (var store = NewDocumentStore())
  383. {
  384. Fill(store, 2);
  385. using (var session = store.OpenSession())
  386. {
  387. var product = session
  388. .Advanced
  389. .DocumentQuery<Product>()
  390. .WaitForNonStaleResultsAsOfNow()
  391. .FirstOrDefault();
  392. Assert.NotNull(product);
  393. }
  394. }
  395. }
  396. [Fact]
  397. public void FirstShouldNonThrowIfSequenceContainsTwoElements()
  398. {
  399. using (var store = NewDocumentStore())
  400. {
  401. Fill(store, 2);
  402. using (var session = store.OpenSession())
  403. {
  404. var product = session
  405. .Advanced
  406. .DocumentQuery<Product>()
  407. .WaitForNonStaleResultsAsOfNow()
  408. .First();
  409. Assert.NotNull(product);
  410. }
  411. }
  412. }
  413. [Fact]
  414. public void SingleOrDefaultShouldThrowIfSequenceContainsTwoElements()
  415. {
  416. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  417. using (var store = NewDocumentStore())
  418. {
  419. Fill(store, 2);
  420. using (var session = store.OpenSession())
  421. {
  422. var e = Assert.Throws<InvalidOperationException>(
  423. () => session
  424. .Advanced
  425. .DocumentQuery<Product>()
  426. .WaitForNonStaleResultsAsOfNow()
  427. .SingleOrDefault());
  428. Assert.Equal("Sequence contains more than one element", e.Message);
  429. }
  430. }
  431. }
  432. [Fact]
  433. public void SingleShouldThrowIfSequenceContainsTwoElements()
  434. {
  435. using (new TemporaryCulture(CultureInfo.InvariantCulture))
  436. using (var store = NewDocumentStore())
  437. {
  438. Fill(store, 2);
  439. using (var session = store.OpenSession())
  440. {
  441. var e = Assert.Throws<InvalidOperationException>(
  442. () => session
  443. .Advanced
  444. .DocumentQuery<Product>()
  445. .WaitForNonStaleResultsAsOfNow()
  446. .Single());
  447. Assert.Equal("Sequence contains more than one element", e.Message);
  448. }
  449. }
  450. }
  451. private void Fill(IDocumentStore store, int count)
  452. {
  453. using (var session = store.OpenSession())
  454. {
  455. for (int i = 0; i < count; i++)
  456. {
  457. session.Store(new Product());
  458. }
  459. session.SaveChanges();
  460. }
  461. }
  462. }
  463. }