PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/Linq/MongoQueryableTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 1746 lines | 1419 code | 311 blank | 16 comment | 44 complexity | 17e1e9dbe3623a66f17da36e8bb2282c MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Threading.Tasks;
  19. using FluentAssertions;
  20. using MongoDB.Bson;
  21. using MongoDB.Driver;
  22. using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
  23. using MongoDB.Driver.Linq;
  24. using MongoDB.Driver.Tests.Linq;
  25. using Xunit;
  26. namespace Tests.MongoDB.Driver.Linq
  27. {
  28. public class MongoQueryableTests : IntegrationTestBase
  29. {
  30. [Fact]
  31. public void Any()
  32. {
  33. var result = CreateQuery().Any();
  34. result.Should().BeTrue();
  35. }
  36. [Fact]
  37. public void Any_with_predicate()
  38. {
  39. var result = CreateQuery().Any(x => x.C.E.F == 234124);
  40. result.Should().BeFalse();
  41. result = CreateQuery().Any(x => x.C.E.F == 11);
  42. result.Should().BeTrue();
  43. }
  44. [Fact]
  45. public async Task AnyAsync()
  46. {
  47. var result = await CreateQuery().AnyAsync();
  48. result.Should().BeTrue();
  49. }
  50. [Fact]
  51. public async Task AnyAsync_with_predicate()
  52. {
  53. var result = await CreateQuery().AnyAsync(x => x.C.E.F == 234124);
  54. result.Should().BeFalse();
  55. result = await CreateQuery().AnyAsync(x => x.C.E.F == 11);
  56. result.Should().BeTrue();
  57. }
  58. [Fact]
  59. public void Average()
  60. {
  61. var result = CreateQuery().Select(x => x.C.E.F + 1).Average();
  62. result.Should().Be(62);
  63. }
  64. [Fact]
  65. public void Average_with_select_and_where()
  66. {
  67. var result = CreateQuery()
  68. .Select(x => x.C.E.F)
  69. .Where(x => x > 20)
  70. .Average();
  71. result.Should().Be(111);
  72. }
  73. [Fact]
  74. public void Average_with_selector()
  75. {
  76. var result = CreateQuery().Average(x => x.C.E.F + 1);
  77. result.Should().Be(62);
  78. }
  79. [Fact]
  80. public async Task AverageAsync()
  81. {
  82. var result = await CreateQuery().Select(x => x.C.E.F).AverageAsync();
  83. result.Should().Be(61);
  84. }
  85. [Fact]
  86. public async Task AverageAsync_with_selector()
  87. {
  88. var result = await CreateQuery().AverageAsync(x => x.C.E.F);
  89. result.Should().Be(61);
  90. }
  91. [Fact]
  92. public void GroupBy_combined_with_a_previous_embedded_pipeline()
  93. {
  94. var bs = new List<string>
  95. {
  96. "Baloon",
  97. "Balloon"
  98. };
  99. var query = CreateQuery()
  100. .Where(x => bs.Contains(x.B))
  101. .GroupBy(x => x.A)
  102. .Select(x => x.Max(y => y.C));
  103. Assert(query,
  104. 1,
  105. "{ $match: { 'B': { '$in': ['Baloon', 'Balloon'] } } }",
  106. "{ $group: { '_id': '$A', '__agg0': { '$max': '$C' } } }",
  107. "{ $project: { '__fld0': '$__agg0', '_id': 0 } }");
  108. }
  109. [Fact]
  110. public void Count()
  111. {
  112. var result = CreateQuery().Count();
  113. result.Should().Be(2);
  114. }
  115. [Fact]
  116. public void Count_with_predicate()
  117. {
  118. var result = CreateQuery().Count(x => x.C.E.F == 11);
  119. result.Should().Be(1);
  120. }
  121. [Fact]
  122. public void Count_with_no_matches()
  123. {
  124. var result = CreateQuery().Count(x => x.C.E.F == 13151235);
  125. result.Should().Be(0);
  126. }
  127. [Fact]
  128. public async Task CountAsync()
  129. {
  130. var result = await CreateQuery().CountAsync();
  131. result.Should().Be(2);
  132. }
  133. [Fact]
  134. public async Task CountAsync_with_predicate()
  135. {
  136. var result = await CreateQuery().CountAsync(x => x.C.E.F == 11);
  137. result.Should().Be(1);
  138. }
  139. [Fact]
  140. public async Task CountAsync_with_no_matches()
  141. {
  142. var result = await CreateQuery().CountAsync(x => x.C.E.F == 123412523);
  143. result.Should().Be(0);
  144. }
  145. [SkippableFact]
  146. public void Distinct_document_followed_by_where()
  147. {
  148. RequireServer.Check().VersionGreaterThanOrEqualTo("2.6.0");
  149. RequireServer.Check().VersionLessThan("4.1.0"); // TODO: remove this line when SERVER-37459 is fixed
  150. var query = CreateQuery()
  151. .Distinct()
  152. .Where(x => x.A == "Awesome");
  153. Assert(query,
  154. 1,
  155. "{ $group: { _id: '$$ROOT' } }",
  156. "{ $match: { '_id.A': 'Awesome' } }");
  157. }
  158. [SkippableFact]
  159. public void Distinct_document_preceded_by_select_where()
  160. {
  161. RequireServer.Check().VersionGreaterThanOrEqualTo("2.6.0");
  162. var query = CreateQuery()
  163. .Select(x => new { x.A, x.B })
  164. .Where(x => x.A == "Awesome")
  165. .Distinct();
  166. Assert(query,
  167. 1,
  168. "{ $project: { 'A': '$A', 'B': '$B', '_id': 0 } }",
  169. "{ $match: { 'A': 'Awesome' } }",
  170. "{ $group: { '_id': '$$ROOT' } }");
  171. }
  172. [SkippableFact]
  173. public void Distinct_document_preceded_by_where_select()
  174. {
  175. RequireServer.Check().VersionGreaterThanOrEqualTo("2.6.0");
  176. var query = CreateQuery()
  177. .Where(x => x.A == "Awesome")
  178. .Select(x => new { x.A, x.B })
  179. .Distinct();
  180. Assert(query,
  181. 1,
  182. "{ $match: { 'A': 'Awesome' } }",
  183. "{ $group: { '_id': { 'A': '$A', 'B': '$B' } } }");
  184. }
  185. [SkippableFact]
  186. public void Distinct_field_preceded_by_where_select()
  187. {
  188. RequireServer.Check().VersionGreaterThanOrEqualTo("2.6.0");
  189. var query = CreateQuery()
  190. .Where(x => x.A == "Awesome")
  191. .Select(x => x.A)
  192. .Distinct();
  193. Assert(query,
  194. 1,
  195. "{ $match: { 'A': 'Awesome' } }",
  196. "{ $group: { '_id': '$A' } }");
  197. }
  198. [SkippableFact]
  199. public void Distinct_field_preceded_by_select_where()
  200. {
  201. RequireServer.Check().VersionGreaterThanOrEqualTo("2.6.0");
  202. var query = CreateQuery()
  203. .Select(x => x.A)
  204. .Where(x => x == "Awesome")
  205. .Distinct();
  206. Assert(query,
  207. 1,
  208. "{ $project: { 'A': '$A', '_id': 0 } }",
  209. "{ $match: { 'A': 'Awesome' } }",
  210. "{ $group: { '_id': '$A' } }");
  211. }
  212. [Fact]
  213. public void First()
  214. {
  215. var result = CreateQuery().Select(x => x.C.E.F).First();
  216. result.Should().Be(11);
  217. }
  218. [Fact]
  219. public void First_with_predicate()
  220. {
  221. var result = CreateQuery().Select(x => x.C.E.F).First(x => x == 11);
  222. result.Should().Be(11);
  223. }
  224. [Fact]
  225. public async Task FirstAsync()
  226. {
  227. var result = await CreateQuery().Select(x => x.C.E.F).FirstAsync();
  228. result.Should().Be(11);
  229. }
  230. [Fact]
  231. public async Task FirstAsync_with_predicate()
  232. {
  233. var result = await CreateQuery().Select(x => x.C.E.F).FirstAsync(x => x == 11);
  234. result.Should().Be(11);
  235. }
  236. [Fact]
  237. public void FirstOrDefault()
  238. {
  239. var result = CreateQuery().Select(x => x.C.E.F).FirstOrDefault();
  240. result.Should().Be(11);
  241. }
  242. [Fact]
  243. public void FirstOrDefault_with_predicate()
  244. {
  245. var result = CreateQuery().Select(x => x.C.E.F).FirstOrDefault(x => x == 11);
  246. result.Should().Be(11);
  247. }
  248. [Fact]
  249. public async Task FirstOrDefaultAsync()
  250. {
  251. var result = await CreateQuery().Select(x => x.C.E.F).FirstOrDefaultAsync();
  252. result.Should().Be(11);
  253. }
  254. [Fact]
  255. public async Task FirstOrDefaultAsync_with_predicate()
  256. {
  257. var result = await CreateQuery().Select(x => x.C.E.F).FirstOrDefaultAsync(x => x == 11);
  258. result.Should().Be(11);
  259. }
  260. [Fact]
  261. public void Enumerable_foreach()
  262. {
  263. var query = from x in CreateQuery()
  264. select x.M;
  265. int sum = 0;
  266. foreach (var item in query)
  267. {
  268. sum += item.Sum();
  269. }
  270. foreach (var item in query)
  271. {
  272. sum += item.Sum();
  273. }
  274. sum.Should().Be(50);
  275. }
  276. [Fact]
  277. public void GroupBy_method()
  278. {
  279. var query = CreateQuery()
  280. .GroupBy(x => x.A);
  281. Assert(query,
  282. 2,
  283. "{ $group: { _id: '$A' } }");
  284. }
  285. [Fact]
  286. public void Group_method_using_select()
  287. {
  288. var query = CreateQuery()
  289. .GroupBy(x => x.A)
  290. .Select(x => new { A = x.Key, Count = x.Count(), Min = x.Min(y => y.U) });
  291. Assert(query,
  292. 2,
  293. "{ $group: { _id: '$A', __agg0: { $sum: 1 }, __agg1: { $min: '$U' } } }",
  294. "{ $project: { A: '$_id', Count: '$__agg0', Min: '$__agg1', _id: 0 } }");
  295. }
  296. [Fact]
  297. public void GroupBy_groupby_method()
  298. {
  299. var query = CreateQuery()
  300. .GroupBy(x => x.A)
  301. .GroupBy(g => g.First().B);
  302. Assert(query,
  303. 2,
  304. "{ $group: { _id: '$A', __agg0: { $first: '$B'} } }",
  305. "{ $group: { _id: '$__agg0' } }");
  306. }
  307. [Fact]
  308. public void GroupBy_select_anonymous_type_method()
  309. {
  310. var query = CreateQuery()
  311. .GroupBy(x => x.A)
  312. .Select(g => new { Key = g.Key, FirstB = g.First().B });
  313. Assert(query,
  314. 2,
  315. "{ $group: { _id: '$A', __agg0: { $first: '$B'} } }",
  316. "{ $project: { Key: '$_id', FirstB: '$__agg0', _id: 0 } }");
  317. }
  318. #if !MONO
  319. [Fact]
  320. public void GroupBy_select_anonymous_type_syntax()
  321. {
  322. var query = from p in CreateQuery()
  323. group p by p.A into g
  324. select new { g.Key, FirstB = g.First().B };
  325. Assert(query,
  326. 2,
  327. "{ $group: { _id: '$A', __agg0: { $first: '$B'} } }",
  328. "{ $project: { Key: '$_id', FirstB: '$__agg0', _id: 0 } }");
  329. }
  330. #endif
  331. [Fact]
  332. public void GroupBy_where_method()
  333. {
  334. var query = CreateQuery()
  335. .GroupBy(x => x.A)
  336. .Where(g => g.Key == "Awesome");
  337. Assert(query,
  338. 1,
  339. "{ $group: { _id: '$A' } }",
  340. "{ $match: { _id: 'Awesome' } }");
  341. }
  342. [Fact]
  343. public void GroupBy_where_with_accumulator_method()
  344. {
  345. var query = CreateQuery()
  346. .GroupBy(x => x.A)
  347. .Where(g => g.First().B == "Balloon");
  348. Assert(query,
  349. 1,
  350. "{ $group: { _id: '$A', __agg0: { $first: '$B' } } }",
  351. "{ $match: { __agg0: 'Balloon' } }");
  352. }
  353. [Fact]
  354. public void GroupBy_where_select_anonymous_type_with_duplicate_accumulators_method()
  355. {
  356. var query = CreateQuery()
  357. .GroupBy(x => x.A)
  358. .Where(g => g.First().B == "Balloon")
  359. .Select(x => new { x.Key, FirstB = x.First().B });
  360. Assert(query,
  361. 1,
  362. "{ $group: { _id: '$A', __agg0: { $first: '$B'} } }",
  363. "{ $match: { __agg0: 'Balloon' } }",
  364. "{ $project: { Key: '$_id', FirstB: '$__agg0', _id: 0 } }");
  365. }
  366. #if !MONO
  367. [Fact]
  368. public void GroupBy_where_select_anonymous_type_with_duplicate_accumulators_syntax()
  369. {
  370. var query = from p in CreateQuery()
  371. group p by p.A into g
  372. where g.First().B == "Balloon"
  373. select new { g.Key, FirstB = g.First().B };
  374. Assert(query,
  375. 1,
  376. "{ $group: { _id: '$A', __agg0: { $first: '$B'} } }",
  377. "{ $match: { __agg0: 'Balloon' } }",
  378. "{ $project: { Key: '$_id', FirstB: '$__agg0', _id: 0 } }");
  379. }
  380. #endif
  381. [Fact]
  382. public void GroupBy_with_resultSelector_anonymous_type_method()
  383. {
  384. var query = CreateQuery()
  385. .GroupBy(x => x.A, (k, s) => new { Key = k, FirstB = s.First().B });
  386. Assert(query,
  387. 2,
  388. "{ $group: { _id: '$A', FirstB: { $first: '$B'} } }");
  389. }
  390. [SkippableFact]
  391. public void GroupJoin_method()
  392. {
  393. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  394. var query = CreateQuery()
  395. .GroupJoin(
  396. CreateOtherQuery(),
  397. p => p.Id,
  398. o => o.Id,
  399. (p, o) => new { p, o });
  400. Assert(query,
  401. 2,
  402. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: '_id', as: 'o' } }");
  403. }
  404. [SkippableFact]
  405. public void GroupJoinForeignField_method()
  406. {
  407. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  408. var query = CreateQuery()
  409. .GroupJoin(
  410. CreateOtherQuery(),
  411. p => p.Id,
  412. o => o.CEF,
  413. (p, o) => new { p, o });
  414. Assert(query,
  415. 2,
  416. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: 'CEF', as: 'o' } }");
  417. }
  418. [SkippableFact]
  419. public void GroupJoin_syntax()
  420. {
  421. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  422. var query = from p in CreateQuery()
  423. join o in CreateOtherQuery() on p.Id equals o.Id into joined
  424. select new { A = p.A, SumCEF = joined.Sum(x => x.CEF) };
  425. Assert(query,
  426. 2,
  427. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: '_id', as: 'joined' } }",
  428. "{ $project: { A: '$A', SumCEF: { $sum: '$joined.CEF' }, _id: 0 } }");
  429. }
  430. [SkippableFact]
  431. public void GroupJoin_syntax_with_a_transparent_identifier()
  432. {
  433. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  434. var query = from p in CreateQuery()
  435. join o in CreateOtherQuery() on p.Id equals o.Id into joined
  436. orderby p.B
  437. select new { A = p.A, Joined = joined };
  438. Assert(query,
  439. 2,
  440. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: '_id', as: 'joined' } }",
  441. "{ $sort: { B: 1 } }",
  442. "{ $project: { A: '$A', Joined: '$joined', _id: 0 } }");
  443. }
  444. [SkippableFact]
  445. public void GroupJoin_syntax_with_select_many()
  446. {
  447. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  448. var query = from p in CreateQuery()
  449. join o in __otherCollection on p.Id equals o.Id into joined
  450. from subo in joined
  451. select new { A = p.A, CEF = subo.CEF };
  452. Assert(query,
  453. 1,
  454. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: '_id', as: 'joined' } }",
  455. "{ $unwind: '$joined' }",
  456. "{ $project: { A: '$A', CEF: '$joined.CEF', _id: 0 } }");
  457. }
  458. [SkippableFact]
  459. public void GroupJoin_syntax_with_select_many_and_DefaultIfEmpty()
  460. {
  461. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  462. var query = from p in CreateQuery()
  463. join o in __otherCollection on p.Id equals o.Id into joined
  464. from subo in joined.DefaultIfEmpty()
  465. select new { A = p.A, CEF = (int?)subo.CEF ?? null };
  466. Assert(query,
  467. 2,
  468. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: '_id', as: 'joined' } }",
  469. "{ $unwind: { path: '$joined', preserveNullAndEmptyArrays: true } }",
  470. "{ $project: { A: '$A', CEF: { $ifNull: ['$joined.CEF', null] }, _id: 0 } }");
  471. }
  472. [SkippableFact]
  473. public void Join_method()
  474. {
  475. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  476. var query = CreateQuery()
  477. .Join(
  478. CreateOtherQuery(),
  479. p => p.Id,
  480. o => o.Id,
  481. (p, o) => new { p, o });
  482. Assert(query,
  483. 1,
  484. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: '_id', as: 'o' } }",
  485. "{ $unwind: '$o' }");
  486. }
  487. [SkippableFact]
  488. public void JoinForeignField_method()
  489. {
  490. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  491. var query = CreateQuery()
  492. .Join(
  493. CreateOtherQuery(),
  494. p => p.Id,
  495. o => o.CEF,
  496. (p, o) => new { p, o });
  497. Assert(query,
  498. 0,
  499. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: 'CEF', as: 'o' } }",
  500. "{ $unwind: '$o' }");
  501. }
  502. [SkippableFact]
  503. public void Join_syntax()
  504. {
  505. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  506. var query = from p in CreateQuery()
  507. join o in CreateOtherQuery() on p.Id equals o.Id
  508. select new { A = p.A, CEF = o.CEF };
  509. Assert(query,
  510. 1,
  511. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: '_id', as: 'o' } }",
  512. "{ $unwind: '$o' }",
  513. "{ $project: { A: '$A', CEF: '$o.CEF', _id: 0 } }");
  514. }
  515. [SkippableFact]
  516. public void Join_syntax_with_a_transparent_identifier()
  517. {
  518. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  519. var query = from p in CreateQuery()
  520. join o in CreateOtherQuery() on p.Id equals o.Id
  521. orderby p.B, o.Id
  522. select new { A = p.A, CEF = o.CEF };
  523. Assert(query,
  524. 1,
  525. "{ $lookup: { from: 'testcollection_other', localField: '_id', foreignField: '_id', as: 'o' } }",
  526. "{ $unwind: '$o' }",
  527. "{ $sort: { B: 1, 'o._id': 1 } }",
  528. "{ $project: { A: '$A', CEF: '$o.CEF', _id: 0 } }");
  529. }
  530. [Fact]
  531. public void LongCount()
  532. {
  533. var result = CreateQuery().LongCount();
  534. result.Should().Be(2);
  535. }
  536. [Fact]
  537. public void LongCount_with_predicate()
  538. {
  539. var result = CreateQuery().LongCount(x => x.C.E.F == 11);
  540. result.Should().Be(1);
  541. }
  542. [Fact]
  543. public void LongCount_with_no_results()
  544. {
  545. var result = CreateQuery().LongCount(x => x.C.E.F == 123452135);
  546. result.Should().Be(0);
  547. }
  548. [Fact]
  549. public async Task LongCountAsync()
  550. {
  551. var result = await CreateQuery().LongCountAsync();
  552. result.Should().Be(2);
  553. }
  554. [Fact]
  555. public async Task LongCountAsync_with_predicate()
  556. {
  557. var result = await CreateQuery().LongCountAsync(x => x.C.E.F == 11);
  558. result.Should().Be(1);
  559. }
  560. [Fact]
  561. public async Task LongCountAsync_with_no_results()
  562. {
  563. var result = await CreateQuery().LongCountAsync(x => x.C.E.F == 12351235);
  564. result.Should().Be(0);
  565. }
  566. [Fact]
  567. public void Max()
  568. {
  569. var result = CreateQuery().Select(x => x.C.E.F).Max();
  570. result.Should().Be(111);
  571. }
  572. [Fact]
  573. public void Max_with_selector()
  574. {
  575. var result = CreateQuery().Max(x => x.C.E.F);
  576. result.Should().Be(111);
  577. }
  578. [Fact]
  579. public async Task MaxAsync()
  580. {
  581. var result = await CreateQuery().Select(x => x.C.E.F).MaxAsync();
  582. result.Should().Be(111);
  583. }
  584. [Fact]
  585. public async Task MaxAsync_with_selector()
  586. {
  587. var result = await CreateQuery().MaxAsync(x => x.C.E.F);
  588. result.Should().Be(111);
  589. }
  590. [Fact]
  591. public void Min()
  592. {
  593. var result = CreateQuery().Select(x => x.C.E.F).Min();
  594. result.Should().Be(11);
  595. }
  596. [Fact]
  597. public void Min_with_selector()
  598. {
  599. var result = CreateQuery().Min(x => x.C.E.F);
  600. result.Should().Be(11);
  601. }
  602. [Fact]
  603. public async Task MinAsync()
  604. {
  605. var result = await CreateQuery().Select(x => x.C.E.F).MinAsync();
  606. result.Should().Be(11);
  607. }
  608. [Fact]
  609. public async Task MinAsync_with_selector()
  610. {
  611. var result = await CreateQuery().MinAsync(x => x.C.E.F);
  612. result.Should().Be(11);
  613. }
  614. [Fact]
  615. public void OfType()
  616. {
  617. var query = CreateQuery().OfType<RootDescended>();
  618. Assert(query,
  619. 1,
  620. "{ $match: { _t: 'RootDescended' } }");
  621. }
  622. [Fact]
  623. public void OfType_with_a_field()
  624. {
  625. var query = CreateQuery()
  626. .Select(x => x.C.E)
  627. .OfType<V>()
  628. .Where(v => v.W == 1111);
  629. Assert(query,
  630. 1,
  631. "{ $project: { E: '$C.E', _id: 0 } }",
  632. "{ $match: { 'E._t': 'V' } }",
  633. "{ $match: { 'E.W': 1111 } }");
  634. }
  635. [Fact]
  636. public void OrderBy_method()
  637. {
  638. var query = CreateQuery()
  639. .OrderBy(x => x.A);
  640. Assert(query,
  641. 2,
  642. "{ $sort: { A: 1 } }");
  643. }
  644. [Fact]
  645. public void OrderBy_syntax()
  646. {
  647. var query = from x in CreateQuery()
  648. orderby x.A
  649. select x;
  650. Assert(query,
  651. 2,
  652. "{ $sort: { A: 1 } }");
  653. }
  654. [Fact]
  655. public void OrderByDescending_method()
  656. {
  657. var query = CreateQuery()
  658. .OrderByDescending(x => x.A);
  659. Assert(query,
  660. 2,
  661. "{ $sort: { A: -1 } }");
  662. }
  663. [Fact]
  664. public void OrderByDescending_syntax()
  665. {
  666. var query = from x in CreateQuery()
  667. orderby x.A descending
  668. select x;
  669. Assert(query,
  670. 2,
  671. "{ $sort: { A: -1 } }");
  672. }
  673. [Fact]
  674. public void OrderBy_ThenBy_ThenByDescending_method()
  675. {
  676. var query = CreateQuery()
  677. .OrderBy(x => x.A)
  678. .ThenBy(x => x.B)
  679. .ThenByDescending(x => x.C);
  680. Assert(query,
  681. 2,
  682. "{ $sort: { A: 1, B: 1, C: -1 } }");
  683. }
  684. [Fact]
  685. public void OrderBy_ThenBy_ThenByDescending_syntax()
  686. {
  687. var query = from x in CreateQuery()
  688. orderby x.A, x.B, x.C descending
  689. select x;
  690. Assert(query,
  691. 2,
  692. "{ $sort: { A: 1, B: 1, C: -1 } }");
  693. }
  694. [Fact]
  695. public void OrderBy_ThenBy_ThenByDescending_with_redundant_fields_method()
  696. {
  697. var query = CreateQuery()
  698. .OrderBy(x => x.A)
  699. .ThenBy(x => x.B)
  700. .ThenBy(x => x.A);
  701. Action act = () => query.ToList();
  702. act.ShouldThrow<NotSupportedException>();
  703. }
  704. [Fact]
  705. public void OrderBy_ThenBy_ThenByDescending_with_redundant_fields_in_different_directions_method()
  706. {
  707. var query = CreateQuery()
  708. .OrderBy(x => x.A)
  709. .ThenBy(x => x.B)
  710. .ThenByDescending(x => x.A);
  711. Action act = () => query.ToList();
  712. act.ShouldThrow<NotSupportedException>();
  713. }
  714. [SkippableFact]
  715. public void Sample()
  716. {
  717. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  718. var query = CreateQuery().Sample(100);
  719. Assert(query,
  720. 2,
  721. "{ $sample: { size: 100 } }");
  722. }
  723. [SkippableFact]
  724. public void Sample_after_another_function()
  725. {
  726. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  727. var query = CreateQuery().Select(x => x.A).Sample(100);
  728. Assert(query,
  729. 2,
  730. "{ $project: { A: '$A', _id: 0 } }",
  731. "{ $sample: { size: 100 } }");
  732. }
  733. [Fact]
  734. public void Select_identity()
  735. {
  736. var query = CreateQuery().Select(x => x);
  737. Assert(query, 2);
  738. }
  739. [Fact]
  740. public void Select_new_of_same()
  741. {
  742. var query = CreateQuery().Select(x => new Root { Id = x.Id, A = x.A });
  743. Assert(query,
  744. 2,
  745. "{ $project: { Id: '$_id', A: '$A', _id: 0} }");
  746. }
  747. [Fact]
  748. public void Select_method_computed_scalar_followed_by_distinct_followed_by_where()
  749. {
  750. var query = CreateQuery()
  751. .Select(x => x.A + " " + x.B)
  752. .Distinct()
  753. .Where(x => x == "Awesome Balloon");
  754. Assert(query,
  755. 1,
  756. "{ $group: { _id: { $concat: ['$A', ' ', '$B'] } } }",
  757. "{ $match: { _id: 'Awesome Balloon' } }");
  758. }
  759. [Fact]
  760. public void Select_method_computed_scalar_followed_by_where()
  761. {
  762. var query = CreateQuery()
  763. .Select(x => x.A + " " + x.B)
  764. .Where(x => x == "Awesome Balloon");
  765. Assert(query,
  766. 1,
  767. "{ $project: { __fld0: { $concat: ['$A', ' ', '$B'] }, _id: 0 } }",
  768. "{ $match: { __fld0: 'Awesome Balloon' } }");
  769. }
  770. [SkippableFact]
  771. public void Select_method_with_predicated_any()
  772. {
  773. RequireServer.Check().VersionGreaterThanOrEqualTo("2.6.0");
  774. var query = CreateQuery()
  775. .Select(x => x.G.Any(g => g.D == "Don't"));
  776. Assert(query,
  777. 2,
  778. "{ $project: { __fld0: { $anyElementTrue: { $map: { input: '$G', as: 'g', 'in': { $eq: ['$$g.D', \"Don't\"] } } } }, _id: 0 } }");
  779. }
  780. [Fact]
  781. public void Select_anonymous_type_where_method()
  782. {
  783. var query = CreateQuery()
  784. .Select(x => new { Yeah = x.A })
  785. .Where(x => x.Yeah == "Awesome");
  786. Assert(query,
  787. 1,
  788. "{ $project: { Yeah: '$A', _id: 0 } }",
  789. "{ $match: { Yeah: 'Awesome' } }");
  790. }
  791. [Fact]
  792. public void Select_scalar_where_method()
  793. {
  794. var query = CreateQuery()
  795. .Select(x => x.A)
  796. .Where(x => x == "Awesome");
  797. Assert(query,
  798. 1,
  799. "{ $project: { A: '$A', _id: 0 } }",
  800. "{ $match: { A: 'Awesome' } }");
  801. }
  802. [Fact]
  803. public void Select_anonymous_type_method()
  804. {
  805. var query = CreateQuery().Select(x => new { Yeah = x.A });
  806. Assert(query,
  807. 2,
  808. "{ $project: { Yeah: '$A', _id: 0 } }");
  809. }
  810. [Fact]
  811. public void Select_anonymous_type_syntax()
  812. {
  813. var query = from x in CreateQuery()
  814. select new { Yeah = x.A };
  815. Assert(query,
  816. 2,
  817. "{ $project: { Yeah: '$A', _id: 0 } }");
  818. }
  819. [Fact]
  820. public void Select_method_scalar()
  821. {
  822. var query = CreateQuery().Select(x => x.A);
  823. Assert(query,
  824. 2,
  825. "{ $project: { A: '$A', _id: 0 } }");
  826. }
  827. [Fact]
  828. public void Select_syntax_scalar()
  829. {
  830. var query = from x in CreateQuery()
  831. select x.A;
  832. Assert(query,
  833. 2,
  834. "{ $project: { A: '$A', _id: 0 } }");
  835. }
  836. [Fact]
  837. public void Select_method_computed_scalar()
  838. {
  839. var query = CreateQuery().Select(x => x.A + " " + x.B);
  840. Assert(query,
  841. 2,
  842. "{ $project: { __fld0: { $concat: ['$A', ' ', '$B'] }, _id: 0 } }");
  843. }
  844. [Fact]
  845. public void Select_syntax_computed_scalar()
  846. {
  847. var query = from x in CreateQuery()
  848. select x.A + " " + x.B;
  849. Assert(query,
  850. 2,
  851. "{ $project: { __fld0: { $concat: ['$A', ' ', '$B'] }, _id: 0 } }");
  852. }
  853. [Fact]
  854. public void Select_method_array()
  855. {
  856. var query = CreateQuery().Select(x => x.M);
  857. Assert(query,
  858. 2,
  859. "{ $project: { M: '$M', _id: 0 } }");
  860. }
  861. [Fact]
  862. public void Select_syntax_array()
  863. {
  864. var query = from x in CreateQuery()
  865. select x.M;
  866. Assert(query,
  867. 2,
  868. "{ $project: { M: '$M', _id: 0 } }");
  869. }
  870. [SkippableFact]
  871. public void Select_method_array_index()
  872. {
  873. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  874. var query = CreateQuery().Select(x => x.M[0]);
  875. Assert(query,
  876. 2,
  877. "{ $project: { __fld0: { $arrayElemAt: ['$M', 0] }, _id: 0 } }");
  878. }
  879. [SkippableFact]
  880. public void Select_syntax_array_index()
  881. {
  882. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  883. var query = from x in CreateQuery()
  884. select x.M[0];
  885. Assert(query,
  886. 2,
  887. "{ $project: { __fld0: { $arrayElemAt: ['$M', 0] }, _id: 0 } }");
  888. }
  889. [SkippableFact]
  890. public void Select_method_embedded_pipeline()
  891. {
  892. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  893. var query = CreateQuery().Select(x => x.M.First());
  894. Assert(query,
  895. 2,
  896. "{ $project: { __fld0: { $arrayElemAt: ['$M', 0] }, _id: 0 } }");
  897. }
  898. [SkippableFact]
  899. public void Select_method_computed_array()
  900. {
  901. RequireServer.Check().VersionGreaterThanOrEqualTo("2.6.0");
  902. var query = CreateQuery()
  903. .Select(x => x.M.Select(i => i + 1));
  904. Assert(query,
  905. 2,
  906. "{ $project: { __fld0: { $map: { input: '$M', as: 'i', in: { $add: ['$$i', 1] } } }, _id: 0 } }");
  907. }
  908. [SkippableFact]
  909. public void Select_syntax_computed_array()
  910. {
  911. RequireServer.Check().VersionGreaterThanOrEqualTo("2.6.0");
  912. var query = from x in CreateQuery()
  913. select x.M.Select(i => i + 1);
  914. Assert(query,
  915. 2,
  916. "{ $project: { __fld0: { $map: { input: '$M', as: 'i', in: { $add: ['$$i', 1] } } }, _id: 0 } }");
  917. }
  918. [Fact]
  919. public void Select_followed_by_group()
  920. {
  921. var query = CreateQuery()
  922. .Select(x => new
  923. {
  924. Id = x.Id,
  925. First = x.A,
  926. Second = x.B
  927. })
  928. .GroupBy(x => x.First, (k, s) => new
  929. {
  930. First = k,
  931. Stuff = s.Select(y => new { y.Id, y.Second })
  932. });
  933. Assert(query,
  934. 2,
  935. "{ $project: { Id: '$_id', First: '$A', Second: '$B', _id: 0 } }",
  936. "{ $group: { _id: '$First', Stuff: { $push: { Id: '$Id', Second: '$Second' } } } }");
  937. }
  938. [Fact]
  939. public void SelectMany_with_only_resultSelector()
  940. {
  941. var query = CreateQuery()
  942. .SelectMany(x => x.G);
  943. Assert(query,
  944. 4,
  945. "{ $unwind: '$G' }",
  946. "{ $project: { G: '$G', _id: 0 } }");
  947. }
  948. [Fact]
  949. public void SelectMany_with_result_selector_which_has_subobjects()
  950. {
  951. var query = CreateQuery()
  952. .SelectMany(x => x.C.X);
  953. Assert(query,
  954. 2,
  955. "{ $unwind : '$C.X' }",
  956. "{ $project : { X : '$C.X', _id : 0 } }");
  957. }
  958. [Fact]
  959. public void SelectMany_with_result_selector_which_is_called_from_SelectMany()
  960. {
  961. var cQuery = CreateQuery()
  962. .SelectMany(g => g.G)
  963. .SelectMany(s => s.S);
  964. Assert(cQuery,
  965. 1,
  966. "{ $unwind : '$G' }",
  967. "{ $project : { G : '$G', _id : 0 } }",
  968. "{ $unwind : '$G.S' }",
  969. "{ $project : { S : '$G.S', _id : 0 } }");
  970. var xQuery = CreateQuery()
  971. .SelectMany(g => g.G)
  972. .SelectMany(s => s.S)
  973. .SelectMany(x => x.X);
  974. Assert(xQuery,
  975. 0,
  976. "{ $unwind : '$G' }",
  977. "{ $project : { G : '$G', _id : 0 } }",
  978. "{ $unwind : '$G.S' }",
  979. "{ $project : { S : '$G.S', _id : 0 } }",
  980. "{ $unwind : '$S.X' }",
  981. "{ $project : { X : '$S.X', _id : 0 } }");
  982. }
  983. [Fact]
  984. public void SelectMany_with_result_selector_which_called_from_where()
  985. {
  986. var query = CreateQuery()
  987. .Where(c => c.K)
  988. .SelectMany(x => x.G);
  989. Assert(query,
  990. 2,
  991. "{ $match : { 'K' : true } }",
  992. "{ $unwind : '$G' }",
  993. "{ $project : { G : '$G', _id : 0 } }");
  994. }
  995. [Fact]
  996. public void SelectMany_with_collection_selector_method_simple_scalar()
  997. {
  998. var query = CreateQuery()
  999. .SelectMany(x => x.G, (x, c) => c);
  1000. Assert(query,
  1001. 4,
  1002. "{ $unwind: '$G' }",
  1003. "{ $project: { G: '$G', _id: 0 } }");
  1004. }
  1005. [Fact]
  1006. public void SelectMany_with_collection_selector_method_simple_scalar_which_is_called_from_SelectMany()
  1007. {
  1008. var cQuery = CreateQuery()
  1009. .SelectMany(g => g.G, (x, c) => c)
  1010. .SelectMany(s => s.S);
  1011. Assert(cQuery,
  1012. 1,
  1013. "{ $unwind : '$G' }",
  1014. "{ $project : { G : '$G', _id : 0 } }",
  1015. "{ $unwind : '$G.S' }",
  1016. "{ $project : { S : '$G.S', _id : 0 } }");
  1017. cQuery = CreateQuery()
  1018. .SelectMany(g => g.G)
  1019. .SelectMany(s => s.S, (x, c) => c);
  1020. Assert(cQuery,
  1021. 1,
  1022. "{ $unwind : '$G' }",
  1023. "{ $project : { G : '$G', _id : 0 } }",
  1024. "{ $unwind : '$G.S' }",
  1025. "{ $project : { S : '$G.S', _id : 0 } }");
  1026. cQuery = CreateQuery()
  1027. .SelectMany(g => g.G, (x, c) => c)
  1028. .SelectMany(s => s.S, (x, c) => c);
  1029. Assert(cQuery,
  1030. 1,
  1031. "{ $unwind : '$G' }",
  1032. "{ $project : { G : '$G', _id : 0 } }",
  1033. "{ $unwind : '$G.S' }",
  1034. "{ $project : { S : '$G.S', _id : 0 } }");
  1035. var xQuery = CreateQuery()
  1036. .SelectMany(g => g.G, (x, c) => c)
  1037. .SelectMany(s => s.S, (x, c) => c)
  1038. .SelectMany(x => x.X, (x, c) => c);
  1039. Assert(xQuery,
  1040. 0,
  1041. "{ $unwind : '$G' }",
  1042. "{ $project : { G : '$G', _id : 0 } }",
  1043. "{ $unwind : '$G.S' }",
  1044. "{ $project : { S : '$G.S', _id : 0 } }",
  1045. "{ $unwind : '$S.X' }",
  1046. "{ $project : { X : '$S.X', _id : 0 } }");
  1047. }
  1048. [Fact]
  1049. public void SelectMany_with_collection_selector_syntax_simple_scalar()
  1050. {
  1051. var query = from x in CreateQuery()
  1052. from y in x.G
  1053. select y;
  1054. Assert(query,
  1055. 4,
  1056. "{ $unwind: '$G' }",
  1057. "{ $project: { G: '$G', _id: 0 } }");
  1058. }
  1059. [Fact]
  1060. public void SelectMany_with_collection_selector_syntax_simple_scalar_which_is_called_from_SelectMany()
  1061. {
  1062. var selectMany1 = from x in CreateQuery()
  1063. from g in x.G
  1064. select g;
  1065. var selectMany2 = from g in selectMany1
  1066. from s in g.S
  1067. select s;
  1068. Assert(selectMany2,
  1069. 1,
  1070. "{ $unwind : '$G' }",
  1071. "{ $project : { G : '$G', _id : 0 } }",
  1072. "{ $unwind : '$G.S' }",
  1073. "{ $project : { S : '$G.S', _id : 0 } }");
  1074. }
  1075. [Fact]
  1076. public void SelectMany_with_collection_selector_method_computed_scalar()
  1077. {
  1078. var query = CreateQuery()
  1079. .SelectMany(x => x.G, (x, c) => x.C.E.F + c.E.F + c.E.H);
  1080. Assert(query,
  1081. 4,
  1082. "{ $unwind: '$G' }",
  1083. "{ $project: { __fld0: { $add: ['$C.E.F', '$G.E.F', '$G.E.H'] }, _id: 0 } }");
  1084. }
  1085. [Fact]
  1086. public void SelectMany_with_collection_selector_method_computed_scalar_which_is_called_from_SelectMany()
  1087. {
  1088. var query = CreateQuery()
  1089. .SelectMany(g => g.G)
  1090. .SelectMany(s => s.S, (x, c) => (int?)(x.E.F + c.E.F + c.E.H));
  1091. Assert(query,
  1092. 1,
  1093. "{ $unwind : '$G' }",
  1094. "{ $project : { G : '$G', _id : 0 } }",
  1095. "{ $unwind : '$G.S' }",
  1096. "{ $project : { __fld0 : { $add : ['$G.E.F', '$G.S.E.F', '$G.S.E.H'] }, _id : 0 } }");
  1097. }
  1098. [Fact]
  1099. public void SelectMany_with_collection_selector_syntax_computed_scalar()
  1100. {
  1101. var query = from x in CreateQuery()
  1102. from y in x.G
  1103. select x.C.E.F + y.E.F + y.E.H;
  1104. Assert(query,
  1105. 4,
  1106. "{ $unwind: '$G' }",
  1107. "{ $project: { __fld0: { $add: ['$C.E.F', '$G.E.F', '$G.E.H'] }, _id: 0 } }");
  1108. }
  1109. [Fact]
  1110. public void SelectMany_with_collection_selector_syntax_computed_scalar_which_is_called_from_SelectMany()
  1111. {
  1112. var selectMany1 =
  1113. from x in CreateQuery()
  1114. from g in x.G
  1115. select g;
  1116. var selectMany2 =
  1117. from g in selectMany1
  1118. from s in g.S
  1119. select (int?)(g.E.F + s.E.F + s.E.H);
  1120. Assert(selectMany2,
  1121. 1,
  1122. "{ $unwind : '$G' }",
  1123. "{ $project : { G : '$G', _id : 0 } }",
  1124. "{ $unwind : '$G.S' }",
  1125. "{ $project: { __fld0 : { $add : ['$G.E.F', '$G.S.E.F', '$G.S.E.H'] }, _id : 0 } }");
  1126. }
  1127. [Fact]
  1128. public void SelectMany_with_collection_selector_method_anonymous_type()
  1129. {
  1130. var query = CreateQuery()
  1131. .SelectMany(x => x.G, (x, c) => new { x.C.E.F, Other = c.D });
  1132. Assert(query,
  1133. 4,
  1134. "{ $unwind: '$G' }",
  1135. "{ $project: { F: '$C.E.F', Other: '$G.D', _id: 0 } }");
  1136. }
  1137. [Fact]
  1138. public void SelectMany_with_collection_selector_method_anonymous_type_which_is_called_from_SelectMany()
  1139. {
  1140. var query = CreateQuery()
  1141. .SelectMany(g => g.G)
  1142. .SelectMany(s => s.S, (x, c) => new { x.E.F, Other = c.D });
  1143. Assert(query,
  1144. 1,
  1145. "{ $unwind : '$G' }",
  1146. "{ $project : { G : '$G', _id : 0 } }",
  1147. "{ $unwind : '$G.S' }",
  1148. "{ $project : { F : '$G.E.F', Other : '$G.S.D', _id : 0 } }");
  1149. }
  1150. [Fact]
  1151. public void SelectMany_with_collection_selector_syntax_anonymous_type()
  1152. {
  1153. var query = from x in CreateQuery()
  1154. from y in x.G
  1155. select new { x.C.E.F, Other = y.D };
  1156. Assert(query,
  1157. 4,
  1158. "{ $unwind: '$G' }",
  1159. "{ $project: { F: '$C.E.F', Other: '$G.D', _id: 0 } }");
  1160. }
  1161. [Fact]
  1162. public void SelectMany_with_collection_selector_syntax_anonymous_type_which_is_called_from_SelectMany()
  1163. {
  1164. var selectMany1 =
  1165. from x in CreateQuery()
  1166. from g in x.G
  1167. select g;
  1168. var selectMany2 =
  1169. from g in selectMany1
  1170. from s in g.S
  1171. select new { g.E.F, Other = s.D };
  1172. Assert(selectMany2,
  1173. 1,
  1174. "{ $unwind : '$G' }",
  1175. "{ $project : { G : '$G', _id : 0 } }",
  1176. "{ $unwind : '$G.S' }",
  1177. "{ $project : { F : '$G.E.F', Other : '$G.S.D', _id : 0 } }");
  1178. }
  1179. [Fact]
  1180. public void SelectMany_followed_by_a_group()
  1181. {
  1182. var first =
  1183. from x in CreateQuery()
  1184. from y in x.G
  1185. select y;
  1186. var query =
  1187. from f in first
  1188. group f by f.D into g
  1189. select new
  1190. {
  1191. g.Key,
  1192. SumF = g.Sum(x => x.E.F)
  1193. };
  1194. Assert(query,
  1195. 4,
  1196. "{ $unwind: '$G' }",
  1197. "{ $project: { G: '$G', _id: 0 } }",
  1198. "{ $group: { _id: '$G.D', __agg0: { $sum : '$G.E.F' } } }",
  1199. "{ $project: { Key: '$_id', SumF: '$__agg0', _id: 0 } }");
  1200. }
  1201. [Fact]
  1202. public void SelectMany_followed_by_a_group_which_is_called_from_SelectMany()
  1203. {
  1204. var selectMany1 =
  1205. from x in CreateQuery()
  1206. from g in x.G
  1207. select g;
  1208. var selectMany2 =
  1209. from g in selectMany1
  1210. from s in g.S
  1211. select s;
  1212. var query =
  1213. from s in selectMany2
  1214. group s by s.D into g
  1215. select new
  1216. {
  1217. g.Key,
  1218. SumF = g.Sum(x => x.E.F)
  1219. };
  1220. Assert(query,
  1221. 1,
  1222. "{ $unwind : '$G' }",
  1223. "{ $project : { G: '$G', _id : 0 } }",
  1224. "{ $unwind : '$G.S' }",
  1225. "{ $project : { 'S' : '$G.S', '_id' : 0 } }",
  1226. "{ $group : { _id : '$S.D', __agg0 : { $sum : '$S.E.F' } } }",
  1227. "{ $project : { Key : '$_id', SumF : '$__agg0', _id : 0 } }");
  1228. }
  1229. [Fact]
  1230. public void Single()
  1231. {
  1232. var result = CreateQuery().Where(x => x.Id == 10).Select(x => x.C.E.F).Single();
  1233. result.Should().Be(11);
  1234. }
  1235. [Fact]
  1236. public void Single_with_predicate()
  1237. {
  1238. var result = CreateQuery().Select(x => x.C.E.F).Single(x => x == 11);
  1239. result.Should().Be(11);
  1240. }
  1241. [Fact]
  1242. public async Task SingleAsync()
  1243. {
  1244. var result = await CreateQuery().Where(x => x.Id == 10).Select(x => x.C.E.F).SingleAsync();
  1245. result.Should().Be(11);
  1246. }
  1247. [Fact]
  1248. public async Task SingleAsync_with_predicate()
  1249. {
  1250. var result = await CreateQuery().Select(x => x.C.E.F).SingleAsync(x => x == 11);
  1251. result.Should().Be(11);
  1252. }
  1253. [Fact]
  1254. public void SingleOrDefault()
  1255. {
  1256. var result = CreateQuery().Where(x => x.Id == 10).Select(x => x.C.E.F).SingleOrDefault();
  1257. result.Should().Be(11);
  1258. }
  1259. [Fact]
  1260. public void SingleOrDefault_with_predicate()
  1261. {
  1262. var result = CreateQuery().Select(x => x.C.E.F).SingleOrDefault(x => x == 11);
  1263. result.Should().Be(11);
  1264. }
  1265. [Fact]
  1266. public async Task SingleOrDefaultAsync()
  1267. {
  1268. var result = await CreateQuery().Where(x => x.Id == 10).Select(x => x.C.E.F).SingleOrDefaultAsync();
  1269. result.Should().Be(11);
  1270. }
  1271. [Fact]
  1272. public async Task SingleOrDefaultAsync_with_predicate()
  1273. {
  1274. var result = await CreateQuery().Select(x => x.C.E.F).SingleOrDefaultAsync(x => x == 11);
  1275. result.Should().Be(11);
  1276. }
  1277. [Fact]
  1278. public void Skip()
  1279. {
  1280. var query = CreateQuery().Skip(10);
  1281. Assert(query,
  1282. 0,
  1283. "{ $skip: 10 }");
  1284. }
  1285. [SkippableFact]
  1286. public void StandardDeviationPopulation()
  1287. {
  1288. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  1289. var result = CreateQuery().Select(x => x.C.E.F).StandardDeviationPopulation();
  1290. result.Should().Be(50);
  1291. }
  1292. [SkippableFact]
  1293. public void StandardDeviationPopulation_with_selector()
  1294. {
  1295. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  1296. var result = CreateQuery().StandardDeviationPopulation(x => x.C.E.F);
  1297. result.Should().Be(50);
  1298. }
  1299. [SkippableFact]
  1300. public async Task StandardDeviationPopulationAsync()
  1301. {
  1302. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  1303. var result = await CreateQuery().Select(x => x.C.E.F).StandardDeviationPopulationAsync();
  1304. result.Should().Be(50);
  1305. }
  1306. [SkippableFact]
  1307. public async Task StandardDeviationPopulationAsync_with_selector()
  1308. {
  1309. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  1310. var result = await CreateQuery().StandardDeviationPopulationAsync(x => x.C.E.F);
  1311. result.Should().Be(50);
  1312. }
  1313. [SkippableFact]
  1314. public void StandardDeviationSample()
  1315. {
  1316. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  1317. var result = CreateQuery().Select(x => x.C.E.F).StandardDeviationSample();
  1318. result.Should().BeApproximately(70.7106781186548, .0001);
  1319. }
  1320. [SkippableFact]
  1321. public void StandardDeviationSample_with_selector()
  1322. {
  1323. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  1324. var result = CreateQuery().StandardDeviationSample(x => x.C.E.F);
  1325. result.Should().BeApproximately(70.7106781186548, .0001);
  1326. }
  1327. [SkippableFact]
  1328. public async Task StandardDeviationSampleAsync()
  1329. {
  1330. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  1331. var result = await CreateQuery().Select(x => x.C.E.F).StandardDeviationSampleAsync();
  1332. result.Should().BeApproximately(70.7106781186548, .0001);
  1333. }
  1334. [SkippableFact]
  1335. public async Task StandardDeviationSampleAsync_with_selector()
  1336. {
  1337. RequireServer.Check().VersionGreaterThanOrEqualTo("3.2.0");
  1338. var result = await CreateQuery().StandardDeviationSampleAsync(x => x.C.E.F);
  1339. result.Should().BeApproximately(70.7106781186548, .0001);
  1340. }
  1341. [Fact]
  1342. public void Sum()
  1343. {
  1344. var result = CreateQuery().Select(x => x.C.E.F).Sum();
  1345. result.Should().Be(122);
  1346. }
  1347. [Fact]
  1348. public void Sum_with_selector()
  1349. {
  1350. var result = CreateQuery().Sum(x => x.C.E.F);
  1351. result.Should().Be(122);
  1352. }
  1353. [Fact]
  1354. public void Sum_with_no_results()
  1355. {
  1356. var result = CreateQuery().Where(x => x.C.E.F == 12341235).Sum(x => x.C.E.F);
  1357. result.Should().Be(0);
  1358. }
  1359. [Fact]
  1360. public async Task SumAsync()
  1361. {
  1362. var result = await CreateQuery().Select(x => x.C.E.F).SumAs

Large files files are truncated, but you can click here to view the full file