PageRenderTime 69ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/test/FastTests/Client/Indexing/IndexExtensionFromClient.cs

https://github.com/arekpalinski/ravendb
C# | 2253 lines | 1947 code | 300 blank | 6 comment | 21 complexity | 26c2cddb85542902e44388389e482d2f MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using FastTests.Server.Basic.Entities;
  9. using NodaTime;
  10. using Raven.Client.Documents.Indexes;
  11. using Raven.Client.Documents.Operations.Indexes;
  12. using Raven.Client.ServerWide.Operations;
  13. using Raven.Tests.Core.Utils.Entities;
  14. using Xunit;
  15. using static FastTests.Client.Indexing.PeopleUtil;
  16. using Xunit.Abstractions;
  17. namespace FastTests.Client.Indexing
  18. {
  19. public class IndexExtensionFromClient : RavenTestBase
  20. {
  21. public IndexExtensionFromClient(ITestOutputHelper output) : base(output)
  22. {
  23. }
  24. [Fact]
  25. public void CanCompileIndexWithExtensions()
  26. {
  27. CopyNodaTimeIfNeeded();
  28. using (var store = GetDocumentStore())
  29. {
  30. store.ExecuteIndex(new PeopleByEmail());
  31. using (var session = store.OpenSession())
  32. {
  33. var p = new Person() { Name = "Methuselah", Age = 969 };
  34. session.Store(p);
  35. session.SaveChanges();
  36. WaitForIndexing(store);
  37. var query = session.Query<PeopleByEmail.PeopleByEmailResult, PeopleByEmail>()
  38. .Where(x => x.Email == PeopleUtil.CalculatePersonEmail(p.Name, p.Age)).OfType<Person>().Single();
  39. }
  40. }
  41. }
  42. [Fact]
  43. public async Task CanUpdateIndexExtensions()
  44. {
  45. using (var store = GetDocumentStore())
  46. {
  47. var getRealCountry = @"
  48. using System.Globalization;
  49. namespace My.Crazy.Namespace
  50. {
  51. public static class Helper
  52. {
  53. public static string GetRealCountry(string name)
  54. {
  55. return new RegionInfo(name).EnglishName;
  56. }
  57. }
  58. }
  59. ";
  60. await store.ExecuteIndexAsync(new RealCountry(getRealCountry));
  61. var additionalSources = await GetAdditionalSources();
  62. Assert.Equal(1, additionalSources.Count);
  63. Assert.Equal(getRealCountry, additionalSources["Helper"]);
  64. getRealCountry = getRealCountry.Replace(".EnglishName", ".Name");
  65. store.ExecuteIndex(new RealCountry(getRealCountry));
  66. additionalSources = await GetAdditionalSources();
  67. Assert.Equal(1, additionalSources.Count);
  68. Assert.Equal(getRealCountry, additionalSources["Helper"]);
  69. async Task<Dictionary<string, string>> GetAdditionalSources()
  70. {
  71. var record = await store.Maintenance.Server.SendAsync(new GetDatabaseRecordOperation(store.Database));
  72. return record.Indexes.First().Value.AdditionalSources;
  73. }
  74. }
  75. }
  76. [Fact]
  77. public void CanUseMethodFromExtensionsInIndex_List()
  78. {
  79. using (var store = GetDocumentStore())
  80. {
  81. store.ExecuteIndex(new PeopleIndex1());
  82. using (var session = store.OpenSession())
  83. {
  84. session.Store(new Person
  85. {
  86. Name = "aviv",
  87. Friends = new List<string>
  88. {
  89. "jerry", "bob", "ayende"
  90. }
  91. });
  92. session.Store(new Person
  93. {
  94. Name = "reeb",
  95. Friends = new List<string>
  96. {
  97. "david", "ayende"
  98. }
  99. });
  100. session.SaveChanges();
  101. }
  102. WaitForIndexing(store);
  103. using (var session = store.OpenSession())
  104. {
  105. var person = session.Query<Person, PeopleIndex1>().Single();
  106. Assert.Equal("aviv", person.Name);
  107. }
  108. }
  109. }
  110. [Fact]
  111. public void CanUseMethodFromExtensionsInIndex_Dictionary()
  112. {
  113. using (var store = GetDocumentStore())
  114. {
  115. store.ExecuteIndex(new PeopleIndex2());
  116. using (var session = store.OpenSession())
  117. {
  118. session.Store(new Person
  119. {
  120. Name = "aviv",
  121. Contacts = new Dictionary<string, long>
  122. {
  123. {
  124. "jerry", 5554866812
  125. },
  126. {
  127. "bob", 5554866813
  128. },
  129. {
  130. "ayende", 5554866814
  131. }
  132. }
  133. });
  134. session.Store(new Person
  135. {
  136. Name = "reeb",
  137. Contacts = new Dictionary<string, long>
  138. {
  139. {
  140. "david", 5554866815
  141. },
  142. {
  143. "ayende", 5554866814
  144. }
  145. }
  146. });
  147. session.SaveChanges();
  148. }
  149. WaitForIndexing(store);
  150. using (var session = store.OpenSession())
  151. {
  152. var person = session.Query<Person, PeopleIndex2>().Single();
  153. Assert.Equal("aviv", person.Name);
  154. }
  155. }
  156. }
  157. [Fact]
  158. public void CanUseMethodFromExtensionsInIndex_ICollection()
  159. {
  160. using (var store = GetDocumentStore())
  161. {
  162. store.ExecuteIndex(new PeopleIndex3());
  163. using (var session = store.OpenSession())
  164. {
  165. session.Store(new Person
  166. {
  167. Name = "aviv",
  168. FriendsCollection = new List<string>
  169. {
  170. "jerry",
  171. "bob"
  172. }
  173. });
  174. session.Store(new Person
  175. {
  176. Name = "reeb",
  177. FriendsCollection = new List<string>
  178. {
  179. "jerry",
  180. "david",
  181. "ayende"
  182. }
  183. });
  184. session.SaveChanges();
  185. }
  186. WaitForIndexing(store);
  187. using (var session = store.OpenSession())
  188. {
  189. var person = session.Query<Person, PeopleIndex3>().Single();
  190. Assert.Equal("aviv", person.Name);
  191. }
  192. }
  193. }
  194. [Fact]
  195. public void CanUseMethodFromExtensionsInIndex_Hashset()
  196. {
  197. using (var store = GetDocumentStore())
  198. {
  199. store.ExecuteIndex(new PeopleIndex4());
  200. using (var session = store.OpenSession())
  201. {
  202. session.Store(new Person
  203. {
  204. Name = "aviv",
  205. FriendsHashset = new HashSet<string>
  206. {
  207. "jerry",
  208. "bob"
  209. }
  210. });
  211. session.Store(new Person
  212. {
  213. Name = "reeb",
  214. FriendsHashset = new HashSet<string>
  215. {
  216. "jerry",
  217. "david",
  218. "ayende"
  219. }
  220. });
  221. session.SaveChanges();
  222. }
  223. WaitForIndexing(store);
  224. using (var session = store.OpenSession())
  225. {
  226. var person = session.Query<Person, PeopleIndex4>().Single();
  227. Assert.Equal("aviv", person.Name);
  228. }
  229. }
  230. }
  231. [Fact]
  232. public void CanUseMethodFromExtensionsInIndex_ListOfUsers()
  233. {
  234. using (var store = GetDocumentStore())
  235. {
  236. store.ExecuteIndex(new PeopleIndex5());
  237. using (var session = store.OpenSession())
  238. {
  239. session.Store(new Person
  240. {
  241. Name = "aviv",
  242. UserFriends = new List<User>
  243. {
  244. new User
  245. {
  246. Name = "jerry"
  247. },
  248. new User
  249. {
  250. Name = "bob"
  251. }
  252. }
  253. });
  254. session.Store(new Person
  255. {
  256. Name = "reeb",
  257. UserFriends = new List<User>
  258. {
  259. new User
  260. {
  261. Name = "david"
  262. },
  263. new User
  264. {
  265. Name = "ayende"
  266. }
  267. }
  268. });
  269. session.SaveChanges();
  270. }
  271. WaitForIndexing(store);
  272. using (var session = store.OpenSession())
  273. {
  274. var person = session.Query<Person, PeopleIndex5>().Single();
  275. Assert.Equal("aviv", person.Name);
  276. }
  277. }
  278. }
  279. [Fact]
  280. public void CanUseMethodFromExtensionsInIndex_Array()
  281. {
  282. using (var store = GetDocumentStore())
  283. {
  284. store.ExecuteIndex(new PeopleIndex6());
  285. using (var session = store.OpenSession())
  286. {
  287. session.Store(new Person
  288. {
  289. Name = "aviv",
  290. FriendsArray = new[]
  291. {
  292. new User
  293. {
  294. Name = "jerry"
  295. },
  296. new User
  297. {
  298. Name = "bob"
  299. }
  300. }
  301. });
  302. session.Store(new Person
  303. {
  304. Name = "reeb",
  305. FriendsArray = new[]
  306. {
  307. new User
  308. {
  309. Name = "david"
  310. },
  311. new User
  312. {
  313. Name = "ayende"
  314. }
  315. }
  316. });
  317. session.SaveChanges();
  318. }
  319. WaitForIndexing(store);
  320. using (var session = store.OpenSession())
  321. {
  322. var person = session.Query<Person, PeopleIndex6>().Single();
  323. Assert.Equal("aviv", person.Name);
  324. }
  325. }
  326. }
  327. [Fact]
  328. public void CanUseMethodFromExtensionsInIndex_MyList()
  329. {
  330. using (var store = GetDocumentStore())
  331. {
  332. store.ExecuteIndex(new PeopleIndex7());
  333. using (var session = store.OpenSession())
  334. {
  335. session.Store(new Person
  336. {
  337. Name = "aviv",
  338. MyList = new MyList<string>
  339. {
  340. "julian", "ricky", "ayende"
  341. }
  342. });
  343. session.Store(new Person
  344. {
  345. Name = "reeb",
  346. MyList = new MyList<string>
  347. {
  348. "david", "ayende"
  349. }
  350. });
  351. session.SaveChanges();
  352. }
  353. WaitForIndexing(store);
  354. using (var session = store.OpenSession())
  355. {
  356. var person = session.Query<Person, PeopleIndex7>().Single();
  357. Assert.Equal("aviv", person.Name);
  358. }
  359. }
  360. }
  361. [Fact]
  362. public void CanUseMethodFromExtensionsInIndex_MyEnumerable()
  363. {
  364. using (var store = GetDocumentStore())
  365. {
  366. store.ExecuteIndex(new PeopleIndex8());
  367. using (var session = store.OpenSession())
  368. {
  369. session.Store(new Person
  370. {
  371. Name = "aviv",
  372. MyEnumerable = new MyEnumerable<string>(new List<string>
  373. {
  374. "julian", "ricky", "ayende"
  375. })
  376. });
  377. session.Store(new Person
  378. {
  379. Name = "reeb",
  380. MyEnumerable = new MyEnumerable<string>(new List<string>
  381. {
  382. "david", "ayende"
  383. })
  384. });
  385. session.SaveChanges();
  386. }
  387. WaitForIndexing(store);
  388. using (var session = store.OpenSession())
  389. {
  390. var person = session.Query<Person, PeopleIndex8>().Single();
  391. Assert.Equal("aviv", person.Name);
  392. }
  393. }
  394. }
  395. [Fact]
  396. public void CanUseMethodFromExtensionsInIndex_DateTime()
  397. {
  398. using (var store = GetDocumentStore())
  399. {
  400. store.ExecuteIndex(new PeopleIndex9());
  401. using (var session = store.OpenSession())
  402. {
  403. session.Store(new Person
  404. {
  405. Name = "aviv",
  406. Event = new Event
  407. {
  408. StartTime = new DateTime(2018, 1, 1),
  409. EndTime = new DateTime(2020, 1, 1)
  410. }
  411. });
  412. session.Store(new Person
  413. {
  414. Name = "reeb",
  415. Event = new Event
  416. {
  417. StartTime = new DateTime(2018, 1, 1),
  418. EndTime = new DateTime(2018, 2, 1)
  419. }
  420. });
  421. session.SaveChanges();
  422. }
  423. WaitForIndexing(store);
  424. using (var session = store.OpenSession())
  425. {
  426. var person = session.Query<Person, PeopleIndex9>().Single();
  427. Assert.Equal("reeb", person.Name);
  428. }
  429. }
  430. }
  431. [Fact(Skip = "need to use DynamicDictionary. waiting for PR from Egor")]
  432. public void CanUseMethodFromExtensionsInIndex_DictionaryFunctions()
  433. {
  434. using (var store = GetDocumentStore())
  435. {
  436. store.ExecuteIndex(new PeopleIndex10());
  437. using (var session = store.OpenSession())
  438. {
  439. session.Store(new Person
  440. {
  441. Name = "aviv",
  442. Contacts = new Dictionary<string, long>
  443. {
  444. {
  445. "jerry", 5554866812
  446. },
  447. {
  448. "bob", 5554866813
  449. },
  450. {
  451. "ayende", 5554866814
  452. }
  453. }
  454. });
  455. session.Store(new Person
  456. {
  457. Name = "reeb",
  458. Contacts = new Dictionary<string, long>
  459. {
  460. {
  461. "david", 5554866815
  462. },
  463. {
  464. "ayende", 5554866814
  465. },
  466. {
  467. "home", 1024
  468. }
  469. }
  470. });
  471. session.SaveChanges();
  472. }
  473. WaitForIndexing(store);
  474. using (var session = store.OpenSession())
  475. {
  476. var person = session.Query<Person, PeopleIndex10>().Single();
  477. Assert.Equal("reeb", person.Name);
  478. }
  479. }
  480. }
  481. [Fact]
  482. public void CanUseMethodFromExtensionsInIndex_WithIEnumerableReturnType()
  483. {
  484. using (var store = GetDocumentStore())
  485. {
  486. var index = new PeopleIndex11();
  487. store.ExecuteIndex(index);
  488. using (var session = store.OpenSession())
  489. {
  490. session.Store(new Person
  491. {
  492. Name = "aviv",
  493. Friends = new List<string>
  494. {
  495. "jerry", "bob"
  496. }
  497. });
  498. session.Store(new Person
  499. {
  500. Name = "reeb",
  501. Friends = new List<string>
  502. {
  503. "david", "ayende"
  504. }
  505. });
  506. session.SaveChanges();
  507. }
  508. WaitForIndexing(store);
  509. using (var session = store.OpenSession())
  510. {
  511. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  512. Assert.Equal(0, indexErrors[0].Errors.Length);
  513. var person = session.Query<Person, PeopleIndex11>()
  514. .Single(p => p.Friends.Contains("ayende"));
  515. Assert.Equal("reeb", person.Name);
  516. }
  517. }
  518. }
  519. [Fact]
  520. public void CanUseMethodFromExtensionsInIndex_WithIEnumerableParameterAndIEnumerableReturnType()
  521. {
  522. using (var store = GetDocumentStore())
  523. {
  524. var index = new PeopleIndex12();
  525. store.ExecuteIndex(index);
  526. using (var session = store.OpenSession())
  527. {
  528. session.Store(new Person
  529. {
  530. Name = "aviv",
  531. Friends = new List<string>
  532. {
  533. "jerry", "bob"
  534. }
  535. });
  536. session.SaveChanges();
  537. }
  538. WaitForIndexing(store);
  539. using (var session = store.OpenSession())
  540. {
  541. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  542. Assert.Equal(0, indexErrors[0].Errors.Length);
  543. var combined = session.Query<PeopleIndex12.Result, PeopleIndex12>()
  544. .Select(p => p.Combined)
  545. .Single();
  546. Assert.Equal(2, combined.Count);
  547. Assert.Equal("jerry|aviv", combined[0]);
  548. Assert.Equal("bob|aviv", combined[1]);
  549. }
  550. }
  551. }
  552. [Fact]
  553. public void CanUseMethodFromExtensionsInIndex_WithUintReturnType()
  554. {
  555. using (var store = GetDocumentStore())
  556. {
  557. var index = new PeopleIndex13();
  558. store.ExecuteIndex(index);
  559. using (var session = store.OpenSession())
  560. {
  561. session.Store(new Person
  562. {
  563. Name = "aviv",
  564. Age = 33
  565. });
  566. session.Store(new Person
  567. {
  568. Name = "egor",
  569. Age = 29
  570. });
  571. session.SaveChanges();
  572. }
  573. WaitForIndexing(store);
  574. using (var session = store.OpenSession())
  575. {
  576. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  577. Assert.Equal(0, indexErrors[0].Errors.Length);
  578. var person = session.Query<Person, PeopleIndex13>().Single();
  579. Assert.Equal("aviv", person.Name);
  580. }
  581. }
  582. }
  583. [Fact]
  584. public void CanUseMethodFromExtensionsInIndex_WithListReturnType()
  585. {
  586. using (var store = GetDocumentStore())
  587. {
  588. var index = new PeopleIndex14();
  589. store.ExecuteIndex(index);
  590. using (var session = store.OpenSession())
  591. {
  592. session.Store(new Person
  593. {
  594. Name = "aviv",
  595. Friends = new List<string>
  596. {
  597. "jerry", "bob"
  598. }
  599. });
  600. session.Store(new Person
  601. {
  602. Name = "reeb",
  603. Friends = new List<string>
  604. {
  605. "ayende"
  606. }
  607. });
  608. session.SaveChanges();
  609. }
  610. WaitForIndexing(store);
  611. using (var session = store.OpenSession())
  612. {
  613. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  614. Assert.Equal(0, indexErrors[0].Errors.Length);
  615. var person = session.Query<Person, PeopleIndex14>().Single();
  616. Assert.Equal("aviv", person.Name);
  617. }
  618. }
  619. }
  620. [Fact]
  621. public void CanUseMethodFromExtensionsInIndex_WithHashsetReturnType()
  622. {
  623. using (var store = GetDocumentStore())
  624. {
  625. var index = new PeopleIndex15();
  626. store.ExecuteIndex(index);
  627. using (var session = store.OpenSession())
  628. {
  629. session.Store(new Person
  630. {
  631. Name = "aviv",
  632. FriendsHashset = new HashSet<string>
  633. {
  634. "jerry", "bob"
  635. }
  636. });
  637. session.Store(new Person
  638. {
  639. Name = "reeb",
  640. FriendsHashset = new HashSet<string>
  641. {
  642. "ayende"
  643. }
  644. });
  645. session.SaveChanges();
  646. }
  647. WaitForIndexing(store);
  648. using (var session = store.OpenSession())
  649. {
  650. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  651. Assert.Equal(0, indexErrors[0].Errors.Length);
  652. var person = session.Query<Person, PeopleIndex15>().Single();
  653. Assert.Equal("aviv", person.Name);
  654. }
  655. }
  656. }
  657. [Fact]
  658. public void CanUseMethodFromExtensionsInIndex_WithArrayReturnType()
  659. {
  660. using (var store = GetDocumentStore())
  661. {
  662. var index = new PeopleIndex16();
  663. store.ExecuteIndex(index);
  664. using (var session = store.OpenSession())
  665. {
  666. session.Store(new Person
  667. {
  668. Name = "aviv",
  669. Numbers = new []
  670. {
  671. 6, 6, 6
  672. }
  673. });
  674. session.Store(new Person
  675. {
  676. Name = "reeb",
  677. Numbers = new[]
  678. {
  679. 10
  680. }
  681. });
  682. session.SaveChanges();
  683. }
  684. WaitForIndexing(store);
  685. using (var session = store.OpenSession())
  686. {
  687. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  688. Assert.Equal(0, indexErrors[0].Errors.Length);
  689. var person = session.Query<Person, PeopleIndex16>().Single();
  690. Assert.Equal("aviv", person.Name);
  691. }
  692. }
  693. }
  694. [Fact]
  695. public void CanUseMethodFromExtensionsInIndex_WithMyListReturnType()
  696. {
  697. using (var store = GetDocumentStore())
  698. {
  699. var index = new PeopleIndex17();
  700. store.ExecuteIndex(index);
  701. using (var session = store.OpenSession())
  702. {
  703. session.Store(new Person
  704. {
  705. Name = "aviv",
  706. MyList = new MyList<string>
  707. {
  708. "julian", "ricky", "ayende"
  709. }
  710. });
  711. session.Store(new Person
  712. {
  713. Name = "reeb",
  714. MyList = new MyList<string>
  715. {
  716. "david", "ayende"
  717. }
  718. });
  719. session.SaveChanges();
  720. }
  721. WaitForIndexing(store);
  722. using (var session = store.OpenSession())
  723. {
  724. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  725. Assert.Equal(0, indexErrors[0].Errors.Length);
  726. var person = session.Query<Person, PeopleIndex17>().Single();
  727. Assert.Equal("aviv", person.Name);
  728. }
  729. }
  730. }
  731. [Fact]
  732. public void CanUseMethodFromExtensionsInIndex_WithMyEnumerableReturnType()
  733. {
  734. using (var store = GetDocumentStore())
  735. {
  736. var index = new PeopleIndex18();
  737. store.ExecuteIndex(index);
  738. using (var session = store.OpenSession())
  739. {
  740. session.Store(new Person
  741. {
  742. Name = "aviv",
  743. MyEnumerable = new MyEnumerable<string>(new []
  744. {
  745. "julian", "ricky", "ayende"
  746. })
  747. });
  748. session.Store(new Person
  749. {
  750. Name = "reeb",
  751. MyEnumerable = new MyEnumerable<string>(new[]
  752. {
  753. "david", "ayende"
  754. })
  755. });
  756. session.SaveChanges();
  757. }
  758. WaitForIndexing(store);
  759. using (var session = store.OpenSession())
  760. {
  761. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  762. Assert.Equal(0, indexErrors[0].Errors.Length);
  763. var person = session.Query<Person, PeopleIndex18>().Single();
  764. Assert.Equal("aviv", person.Name);
  765. }
  766. }
  767. }
  768. [Fact]
  769. public void CanUseMethodFromExtensionsInIndex_WithListParameterAndListReturnType()
  770. {
  771. using (var store = GetDocumentStore())
  772. {
  773. var index = new PeopleIndex19();
  774. store.ExecuteIndex(index);
  775. using (var session = store.OpenSession())
  776. {
  777. session.Store(new Person
  778. {
  779. Friends = new List<string>
  780. {
  781. "jerry", "bob"
  782. }
  783. });
  784. session.Store(new Person
  785. {
  786. Friends = new List<string>
  787. {
  788. "david"
  789. }
  790. });
  791. session.SaveChanges();
  792. }
  793. WaitForIndexing(store);
  794. using (var session = store.OpenSession())
  795. {
  796. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  797. Assert.Equal(0, indexErrors[0].Errors.Length);
  798. var newFriends = session.Query<PeopleIndex19.Result, PeopleIndex19>()
  799. .Where(p => p.FriendsCount > 3)
  800. .Select(p => p.NewFriends)
  801. .Single();
  802. Assert.Equal(4, newFriends.Count);
  803. Assert.Equal("jerry", newFriends[0]);
  804. Assert.Equal("bob", newFriends[1]);
  805. Assert.Equal("ayende", newFriends[2]);
  806. Assert.Equal("ppekrol", newFriends[3]);
  807. }
  808. }
  809. }
  810. [Fact]
  811. public void CanUseMethodFromExtensionsInIndex_WithValueTypeListReturnType()
  812. {
  813. using (var store = GetDocumentStore())
  814. {
  815. var index = new PeopleIndex20();
  816. store.ExecuteIndex(index);
  817. using (var session = store.OpenSession())
  818. {
  819. session.Store(new Person
  820. {
  821. Name = "aviv"
  822. });
  823. session.SaveChanges();
  824. }
  825. WaitForIndexing(store);
  826. using (var session = store.OpenSession())
  827. {
  828. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  829. Assert.Equal(0, indexErrors[0].Errors.Length);
  830. var numbers = session.Query<PeopleIndex20.Result, PeopleIndex20>()
  831. .Select(p => p.Numbers)
  832. .Single();
  833. Assert.Equal(3, numbers.Count);
  834. Assert.Equal(1, numbers[0]);
  835. Assert.Equal(2, numbers[1]);
  836. Assert.Equal(3, numbers[2]);
  837. }
  838. }
  839. }
  840. [Fact]
  841. public void CanUseMethodFromExtensionsInIndex_WithVoidReturnType()
  842. {
  843. using (var store = GetDocumentStore())
  844. {
  845. var index = new PeopleIndex21();
  846. store.ExecuteIndex(index);
  847. using (var session = store.OpenSession())
  848. {
  849. session.Store(new Person
  850. {
  851. Name = "aviv"
  852. });
  853. session.SaveChanges();
  854. }
  855. WaitForIndexing(store);
  856. using (var session = store.OpenSession())
  857. {
  858. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  859. Assert.Equal(0, indexErrors[0].Errors.Length);
  860. var person = session.Query<Person, PeopleIndex21>()
  861. .Single();
  862. Assert.Equal("aviv", person.Name);
  863. }
  864. }
  865. }
  866. [Fact]
  867. public void CanUseMethodFromExtensionsInIndex_WithXmlComments()
  868. {
  869. using (var store = GetDocumentStore())
  870. {
  871. var index = new PeopleIndex22();
  872. store.ExecuteIndex(index);
  873. using (var session = store.OpenSession())
  874. {
  875. session.Store(new Person
  876. {
  877. Name = "aviv"
  878. });
  879. session.SaveChanges();
  880. }
  881. WaitForIndexing(store);
  882. using (var session = store.OpenSession())
  883. {
  884. var indexErrors = store.Maintenance.Send(new GetIndexErrorsOperation(new[] { index.IndexName }));
  885. Assert.Equal(0, indexErrors[0].Errors.Length);
  886. var person = session.Query<Person, PeopleIndex22>()
  887. .Single();
  888. Assert.Equal("aviv", person.Name);
  889. }
  890. }
  891. }
  892. private class RealCountry : AbstractIndexCreationTask<Order>
  893. {
  894. public RealCountry(string getRealCountry)
  895. {
  896. Map = orders => from order in orders
  897. select new
  898. {
  899. Country = Helper.GetRealCountry(order.ShipTo.Country)
  900. };
  901. AdditionalSources = new Dictionary<string, string>
  902. {
  903. {
  904. "Helper",
  905. getRealCountry
  906. }
  907. };
  908. }
  909. private static class Helper
  910. {
  911. public static string GetRealCountry(string name)
  912. {
  913. return new RegionInfo(name).EnglishName;
  914. }
  915. }
  916. }
  917. private static void CopyNodaTimeIfNeeded()
  918. {
  919. var nodaLocation = new FileInfo(typeof(Instant).Assembly.Location);
  920. var currentLocation = new FileInfo(typeof(IndexExtensionFromClient).Assembly.Location);
  921. var newLocation = new FileInfo(Path.Combine(currentLocation.DirectoryName, nodaLocation.Name));
  922. if (newLocation.Exists)
  923. return;
  924. File.Copy(nodaLocation.FullName, newLocation.FullName, overwrite: true);
  925. }
  926. public class Person
  927. {
  928. public string Name { get; set; }
  929. public uint Age { get; set; }
  930. public List<string> Friends { get; set; }
  931. public Dictionary<string, long> Contacts { get; set; }
  932. public ICollection<string> FriendsCollection { get; set; }
  933. public HashSet<string> FriendsHashset { get; set; }
  934. public IEnumerable<User> UserFriends { get; set; }
  935. public User[] FriendsArray { get; set; }
  936. public MyList<string> MyList { get; set; }
  937. public MyEnumerable<string> MyEnumerable { get; set; }
  938. public Event Event { get; set; }
  939. public int[] Numbers { get; set; }
  940. }
  941. public class Event
  942. {
  943. public DateTime StartTime { get; set; }
  944. public DateTime? EndTime { get; set; }
  945. }
  946. private class PeopleByEmail : AbstractIndexCreationTask<Person>
  947. {
  948. public class PeopleByEmailResult
  949. {
  950. public string Email { get; set; }
  951. }
  952. public PeopleByEmail()
  953. {
  954. Map = people => from person in people
  955. select new
  956. {
  957. _ = CreateField("Email", CalculatePersonEmail(person.Name, person.Age), true, true),
  958. };
  959. AdditionalSources = new Dictionary<string, string>
  960. {
  961. {
  962. "PeopleUtil",
  963. @"
  964. using System;
  965. using NodaTime;
  966. using static My.Crazy.Namespace.PeopleUtil;
  967. namespace My.Crazy.Namespace
  968. {
  969. public static class PeopleUtil
  970. {
  971. public static string CalculatePersonEmail(string name, uint age)
  972. {
  973. //The code below intention is just to make sure NodaTime is compiling with our index
  974. return $""{name}.{Instant.FromDateTimeUtc(DateTime.Now.ToUniversalTime()).ToDateTimeUtc().Year - age}@ayende.com"";
  975. }
  976. }
  977. }
  978. "
  979. }
  980. };
  981. }
  982. }
  983. private class PeopleIndex1 : AbstractIndexCreationTask<Person>
  984. {
  985. public PeopleIndex1()
  986. {
  987. Map = people => from person in people
  988. where Foo1(person.Friends)
  989. select new
  990. {
  991. person.Name
  992. };
  993. AdditionalSources = new Dictionary<string, string>
  994. {
  995. {
  996. "PeopleUtil",
  997. @"
  998. using System.Collections.Generic;
  999. using System.Linq;
  1000. namespace ETIS
  1001. {
  1002. public static class PeopleUtil
  1003. {
  1004. public static bool Foo1(List<string> friends)
  1005. {
  1006. return friends.Count(n => n != ""ayende"") > 1;
  1007. }
  1008. }
  1009. }
  1010. "
  1011. }
  1012. };
  1013. }
  1014. }
  1015. private class PeopleIndex2 : AbstractIndexCreationTask<Person>
  1016. {
  1017. public PeopleIndex2()
  1018. {
  1019. Map = people => from person in people
  1020. where Foo2(person.Contacts)
  1021. select new
  1022. {
  1023. person.Name
  1024. };
  1025. AdditionalSources = new Dictionary<string, string>
  1026. {
  1027. {
  1028. "PeopleUtil",
  1029. @"
  1030. using System.Collections.Generic;
  1031. using System.Linq;
  1032. namespace ETIS
  1033. {
  1034. public static class PeopleUtil
  1035. {
  1036. public static bool Foo2(Dictionary<string, long> friends)
  1037. {
  1038. return friends.Count(n => n.Key != ""ayende"") > 1;
  1039. }
  1040. }
  1041. }
  1042. "
  1043. }
  1044. };
  1045. }
  1046. }
  1047. private class PeopleIndex3 : AbstractIndexCreationTask<Person>
  1048. {
  1049. public PeopleIndex3()
  1050. {
  1051. Map = people => from person in people
  1052. where Foo3(person.FriendsCollection)
  1053. select new
  1054. {
  1055. person.Name
  1056. };
  1057. AdditionalSources = new Dictionary<string, string>
  1058. {
  1059. {
  1060. "PeopleUtil",
  1061. @"
  1062. using System.Collections.Generic;
  1063. using System.Linq;
  1064. namespace ETIS
  1065. {
  1066. public static class PeopleUtil
  1067. {
  1068. public static bool Foo3(ICollection<string> friends)
  1069. {
  1070. return friends.All(n => n != ""ayende"");
  1071. }
  1072. }
  1073. }
  1074. "
  1075. }
  1076. };
  1077. }
  1078. }
  1079. private class PeopleIndex4 : AbstractIndexCreationTask<Person>
  1080. {
  1081. public PeopleIndex4()
  1082. {
  1083. Map = people => from person in people
  1084. where Foo4(person.FriendsHashset)
  1085. select new
  1086. {
  1087. person.Name
  1088. };
  1089. AdditionalSources = new Dictionary<string, string>
  1090. {
  1091. {
  1092. "PeopleUtil",
  1093. @"
  1094. using System.Collections.Generic;
  1095. using System.Linq;
  1096. namespace ETIS
  1097. {
  1098. public static class PeopleUtil
  1099. {
  1100. public static bool Foo4(HashSet<string> friends)
  1101. {
  1102. return friends.All(n => n != ""ayende"");
  1103. }
  1104. }
  1105. }
  1106. "
  1107. }
  1108. };
  1109. }
  1110. }
  1111. private class PeopleIndex5 : AbstractIndexCreationTask<Person>
  1112. {
  1113. public PeopleIndex5()
  1114. {
  1115. Map = people => from person in people
  1116. where Foo5(person.UserFriends)
  1117. select new
  1118. {
  1119. person.Name
  1120. };
  1121. AdditionalSources = new Dictionary<string, string>
  1122. {
  1123. {
  1124. "PeopleUtil",
  1125. @"
  1126. using System.Collections.Generic;
  1127. using System.Linq;
  1128. namespace ETIS
  1129. {
  1130. public static class PeopleUtil
  1131. {
  1132. public class User
  1133. {
  1134. public string Name { get; set; }
  1135. }
  1136. public static bool Foo5(IEnumerable<User> friends)
  1137. {
  1138. return friends.All(n => n.Name != ""ayende"");
  1139. }
  1140. }
  1141. }
  1142. "
  1143. }
  1144. };
  1145. }
  1146. }
  1147. private class PeopleIndex6 : AbstractIndexCreationTask<Person>
  1148. {
  1149. public PeopleIndex6()
  1150. {
  1151. Map = people => from person in people
  1152. where Foo6(person.FriendsArray)
  1153. select new
  1154. {
  1155. person.Name
  1156. };
  1157. AdditionalSources = new Dictionary<string, string>
  1158. {
  1159. {
  1160. "PeopleUtil",
  1161. @"
  1162. using System.Collections.Generic;
  1163. using System.Linq;
  1164. namespace ETIS
  1165. {
  1166. public static class PeopleUtil
  1167. {
  1168. public class User
  1169. {
  1170. public string Name { get; set; }
  1171. }
  1172. public static bool Foo6(User[] friends)
  1173. {
  1174. return friends.All(n => n.Name != ""ayende"");
  1175. }
  1176. }
  1177. }
  1178. "
  1179. }
  1180. };
  1181. }
  1182. }
  1183. private class PeopleIndex7 : AbstractIndexCreationTask<Person>
  1184. {
  1185. public PeopleIndex7()
  1186. {
  1187. Map = people => from person in people
  1188. where Foo7(person.MyList)
  1189. select new
  1190. {
  1191. person.Name
  1192. };
  1193. AdditionalSources = new Dictionary<string, string>
  1194. {
  1195. {
  1196. "PeopleUtil",
  1197. @"
  1198. using System.Collections.Generic;
  1199. using System.Linq;
  1200. namespace ETIS
  1201. {
  1202. public static class PeopleUtil
  1203. {
  1204. public class MyList<T> : List<T>
  1205. {
  1206. }
  1207. public static bool Foo7(MyList<string> friends)
  1208. {
  1209. return friends.Count(n => n != ""ayende"") > 1;
  1210. }
  1211. }
  1212. }
  1213. "
  1214. }
  1215. };
  1216. }
  1217. }
  1218. private class PeopleIndex8 : AbstractIndexCreationTask<Person>
  1219. {
  1220. public PeopleIndex8()
  1221. {
  1222. Map = people => from person in people
  1223. where Foo8(person.MyEnumerable)
  1224. select new
  1225. {
  1226. person.Name
  1227. };
  1228. AdditionalSources = new Dictionary<string, string>
  1229. {
  1230. {
  1231. "PeopleUtil",
  1232. @"
  1233. using System.Collections;
  1234. using System.Collections.Generic;
  1235. using System.Linq;
  1236. namespace ETIS
  1237. {
  1238. public static class PeopleUtil
  1239. {
  1240. public class MyEnumerable<T> : IEnumerable<T>
  1241. {
  1242. private IEnumerable<T> _list;
  1243. public MyEnumerable()
  1244. {
  1245. }
  1246. public MyEnumerable(IEnumerable<T> list)
  1247. {
  1248. _list = list;
  1249. }
  1250. public IEnumerator<T> GetEnumerator()
  1251. {
  1252. return _list.GetEnumerator();
  1253. }
  1254. IEnumerator IEnumerable.GetEnumerator()
  1255. {
  1256. return GetEnumerator();
  1257. }
  1258. }
  1259. public static bool Foo8(MyEnumerable<string> friends)
  1260. {
  1261. return friends.Count(n => n != ""ayende"") > 1;
  1262. }
  1263. }
  1264. }
  1265. "
  1266. }
  1267. };
  1268. }
  1269. }
  1270. private class PeopleIndex9 : AbstractIndexCreationTask<Person>
  1271. {
  1272. public PeopleIndex9()
  1273. {
  1274. Map = people => from person in people
  1275. where Foo9(person.Event.StartTime, person.Event.EndTime).TotalDays < 100
  1276. select new
  1277. {
  1278. person.Name
  1279. };
  1280. AdditionalSources = new Dictionary<string, string>
  1281. {
  1282. {
  1283. "PeopleUtil",
  1284. @"
  1285. using System;
  1286. namespace ETIS
  1287. {
  1288. public static class PeopleUtil
  1289. {
  1290. public static TimeSpan Foo9(DateTime start, DateTime? end)
  1291. {
  1292. if (end.HasValue == false)
  1293. return TimeSpan.MaxValue;
  1294. return end.Value - start;
  1295. }
  1296. }
  1297. }
  1298. "
  1299. }
  1300. };
  1301. }
  1302. }
  1303. private class PeopleIndex10 : AbstractIndexCreationTask<Person>
  1304. {
  1305. public PeopleIndex10()
  1306. {
  1307. Map = people => from person in people
  1308. where Foo10(person.Contacts) > 100
  1309. select new
  1310. {
  1311. person.Name
  1312. };
  1313. AdditionalSources = new Dictionary<string, string>
  1314. {
  1315. {
  1316. "PeopleUtil",
  1317. @"
  1318. using System.Collections.Generic;
  1319. using System.Linq;
  1320. namespace ETIS
  1321. {
  1322. public static class PeopleUtil
  1323. {
  1324. public static long Foo10(Dictionary<string, long> friends)
  1325. {
  1326. if (friends == null)
  1327. return -1;
  1328. if (friends.ContainsKey(""home""))
  1329. return 0;
  1330. return friends.Values.Sum(x => x);
  1331. }
  1332. }
  1333. }
  1334. "
  1335. }
  1336. };
  1337. }
  1338. }
  1339. private class PeopleIndex11 : AbstractIndexCreationTask<Person>
  1340. {
  1341. public PeopleIndex11()
  1342. {
  1343. Map = people => from person in people
  1344. select new
  1345. {
  1346. person.Name,
  1347. Friends = Foo11(person)
  1348. };
  1349. AdditionalSources = new Dictionary<string, string>
  1350. {
  1351. {
  1352. "PeopleUtil",
  1353. @"
  1354. using System.Collections.Generic;
  1355. using System.Linq;
  1356. namespace ETIS
  1357. {
  1358. public static class PeopleUtil
  1359. {
  1360. public class Person
  1361. {
  1362. public IEnumerable<string> Friends;
  1363. }
  1364. public static IEnumerable<string> Foo11(Person p)
  1365. {
  1366. return p.Friends;
  1367. }
  1368. }
  1369. }
  1370. "
  1371. }
  1372. };
  1373. }
  1374. }
  1375. private class PeopleIndex12 : AbstractIndexCreationTask<Person>
  1376. {
  1377. public class Result
  1378. {
  1379. public string Name { get; set; }
  1380. public List<string> Combined { get; set; }
  1381. }
  1382. public PeopleIndex12()
  1383. {
  1384. Map = people => from person in people
  1385. select new Result
  1386. {
  1387. Name = person.Name,
  1388. Combined = Foo12(person.Friends, person.Name).ToList()
  1389. };
  1390. AdditionalSources = new Dictionary<string, string>
  1391. {
  1392. {
  1393. "PeopleUtil",
  1394. @"
  1395. using System.Collections.Generic;
  1396. using System.Linq;
  1397. namespace ETIS
  1398. {
  1399. public static class PeopleUtil
  1400. {
  1401. public static IEnumerable<string> Foo12(IEnumerable<string> foreachString, string add)
  1402. {
  1403. if (foreachString == null || !foreachString.Any())
  1404. return new List<string>();
  1405. var combined = foreachString.Select(x => x + ""|"" + add);
  1406. return combined;
  1407. }
  1408. }
  1409. }
  1410. "
  1411. }
  1412. };
  1413. StoreAllFields(FieldStorage.Yes);
  1414. }
  1415. }
  1416. private class PeopleIndex13 : AbstractIndexCreationTask<Person>
  1417. {
  1418. public PeopleIndex13()
  1419. {
  1420. // before RavenDB-13539 we were getting index errors when trying to execute this map function:
  1421. // Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'long' to 'uint'
  1422. Map = people => from person in people
  1423. where Foo13(person) > 30
  1424. select new
  1425. {
  1426. person.Name
  1427. };
  1428. AdditionalSources = new Dictionary<string, string>
  1429. {
  1430. {
  1431. "PeopleUtil",
  1432. @"
  1433. using System.Collections.Generic;
  1434. using System.Linq;
  1435. namespace ETIS
  1436. {
  1437. public static class PeopleUtil
  1438. {
  1439. public class Person
  1440. {
  1441. public uint Age;
  1442. }
  1443. public static uint Foo13(Person p)
  1444. {
  1445. return p.Age;
  1446. }
  1447. }
  1448. }
  1449. "
  1450. }
  1451. };
  1452. }
  1453. }
  1454. private class PeopleIndex14 : AbstractIndexCreationTask<Person>
  1455. {
  1456. public PeopleIndex14()
  1457. {
  1458. Map = people => from person in people
  1459. where Foo14(person).Count > 1
  1460. select new
  1461. {
  1462. person.Name
  1463. };
  1464. AdditionalSources = new Dictionary<string, string>
  1465. {
  1466. {
  1467. "PeopleUtil",
  1468. @"
  1469. using System.Collections.Generic;
  1470. using System.Linq;
  1471. namespace ETIS
  1472. {
  1473. public static class PeopleUtil
  1474. {
  1475. public class Person
  1476. {
  1477. public List<string> Friends;
  1478. }
  1479. public static List<string> Foo14(Person p)
  1480. {
  1481. return p.Friends;
  1482. }
  1483. }
  1484. }
  1485. "
  1486. }
  1487. };
  1488. }
  1489. }
  1490. private class PeopleIndex15 : AbstractIndexCreationTask<Person>
  1491. {
  1492. public PeopleIndex15()
  1493. {
  1494. Map = people => from person in people
  1495. where Foo15(person).Count > 1
  1496. select new
  1497. {
  1498. person.Name
  1499. };
  1500. AdditionalSources = new Dictionary<string, string>
  1501. {
  1502. {
  1503. "PeopleUtil",
  1504. @"
  1505. using System.Collections.Generic;
  1506. using System.Linq;
  1507. namespace ETIS
  1508. {
  1509. public static class PeopleUtil
  1510. {
  1511. public class Person
  1512. {
  1513. public HashSet<string> FriendsHashset;
  1514. }
  1515. public static HashSet<string> Foo15(Person p)
  1516. {
  1517. return p.FriendsHashset;
  1518. }
  1519. }
  1520. }
  1521. "
  1522. }
  1523. };
  1524. }
  1525. }
  1526. private class PeopleIndex16 : AbstractIndexCreationTask<Person>
  1527. {
  1528. public PeopleIndex16()
  1529. {
  1530. Map = people => from person in people
  1531. where Foo16(person).Length > 1
  1532. select new
  1533. {
  1534. person.Name
  1535. };
  1536. AdditionalSources = new Dictionary<string, string>
  1537. {
  1538. {
  1539. "PeopleUtil",
  1540. @"
  1541. using System.Collections.Generic;
  1542. using System.Linq;
  1543. namespace ETIS
  1544. {
  1545. public static class PeopleUtil
  1546. {
  1547. public class Person
  1548. {
  1549. public int[] Numbers;
  1550. }
  1551. public static int[] Foo16(Person p)
  1552. {
  1553. return p.Numbers;
  1554. }
  1555. }
  1556. }
  1557. "
  1558. }
  1559. };
  1560. }
  1561. }
  1562. private class PeopleIndex17 : AbstractIndexCreationTask<Person>
  1563. {
  1564. public PeopleIndex17()
  1565. {
  1566. Map = people => from person in people
  1567. where Foo17(person).Count > 2
  1568. select new
  1569. {
  1570. person.Name
  1571. };
  1572. AdditionalSources = new Dictionary<string, string>
  1573. {
  1574. {
  1575. "PeopleUtil",
  1576. @"
  1577. using System.Collections.Generic;
  1578. using System.Linq;
  1579. namespace ETIS
  1580. {
  1581. public static class PeopleUtil
  1582. {
  1583. public class MyList<T> : List<T>
  1584. {
  1585. }
  1586. public class Person
  1587. {
  1588. public MyList<string> MyList;
  1589. }
  1590. public static MyList<string> Foo17(Person p)
  1591. {
  1592. return p.MyList;
  1593. }
  1594. }
  1595. }
  1596. "
  1597. }
  1598. };
  1599. }
  1600. }
  1601. private class PeopleIndex18 : AbstractIndexCreationTask<Person>
  1602. {
  1603. public PeopleIndex18()
  1604. {
  1605. Map = people => from person in people
  1606. where Foo18(person).Count() > 2
  1607. select new
  1608. {
  1609. person.Name
  1610. };
  1611. AdditionalSources = new Dictionary<string, string>
  1612. {
  1613. {
  1614. "PeopleUtil",
  1615. @"
  1616. using System.Collections;
  1617. using System.Collections.Generic;
  1618. using System.Linq;
  1619. namespace ETIS
  1620. {
  1621. public static class PeopleUtil
  1622. {
  1623. public class MyEnumerable<T> : IEnumerable<T>
  1624. {
  1625. private IEnumerable<T> _list;
  1626. public MyEnumerable()
  1627. {
  1628. }
  1629. public MyEnumerable(IEnumerable<T> list)
  1630. {
  1631. _list = list;
  1632. }
  1633. public IEnumerator<T> GetEnumerator()
  1634. {
  1635. return _list.GetEnumerator();
  1636. }
  1637. IEnumerator IEnumerable.GetEnumerator()
  1638. {
  1639. return GetEnumerator();
  1640. }
  1641. }
  1642. public class Person
  1643. {
  1644. public MyEnumerable<string> MyEnumerable;
  1645. }
  1646. public static MyEnumerable<string> Foo18(Person p)
  1647. {
  1648. return p.MyEnumerable;
  1649. }
  1650. }
  1651. }
  1652. "
  1653. }
  1654. };
  1655. }
  1656. }
  1657. private class PeopleIndex19 : AbstractIndexCreationTask<Person>
  1658. {
  1659. public class Result
  1660. {
  1661. public string Name { get; set; }
  1662. public List<string> NewFriends { get; set; }
  1663. public int FriendsCount { get; set; }
  1664. }
  1665. public PeopleIndex19()
  1666. {
  1667. Map = people => from person in people
  1668. let newFriends = Foo19(person.Friends)
  1669. select new Result
  1670. {
  1671. Name = person.Name,
  1672. NewFriends = newFriends,
  1673. FriendsCount = newFriends.Count
  1674. };
  1675. AdditionalSources = new Dictionary<string, string>
  1676. {
  1677. {
  1678. "PeopleUtil",
  1679. @"
  1680. using System.Collections.Generic;
  1681. using System.Linq;
  1682. namespace ETIS
  1683. {
  1684. public static class PeopleUtil
  1685. {
  1686. public static List<string> Foo19(List<string> friends)
  1687. {
  1688. if (friends == null || !friends.Any())
  1689. return new List<string>();
  1690. return friends.Concat(new []{ ""ayende"", ""ppekrol"" }).ToList();
  1691. }
  1692. }
  1693. }
  1694. "
  1695. }
  1696. };
  1697. StoreAllFields(FieldStorage.Yes);
  1698. }
  1699. }
  1700. private class PeopleIndex20 : AbstractIndexCreationTask<Person>
  1701. {
  1702. public class Result
  1703. {
  1704. public string Name { get; set; }
  1705. public List<int> Numbers { get; set; }
  1706. }
  1707. public PeopleIndex20()
  1708. {
  1709. Map = people => from person in people
  1710. select new Result
  1711. {
  1712. Name = person.Name,
  1713. Numbers = Foo20()
  1714. };
  1715. AdditionalSources = new Dictionary<string, string>
  1716. {
  1717. {
  1718. "PeopleUtil",
  1719. @"
  1720. using System.Collections.Generic;
  1721. using System.Linq;
  1722. namespace ETIS
  1723. {
  1724. public static class PeopleUtil
  1725. {
  1726. public static List<int> Foo20()
  1727. {
  1728. var x = new List<int>
  1729. {
  1730. 1, 2, 3
  1731. };
  1732. return x;
  1733. }
  1734. }
  1735. }
  1736. "
  1737. }
  1738. };
  1739. StoreAllFields(FieldStorage.Yes);
  1740. }
  1741. }
  1742. private class PeopleIndex21 : AbstractIndexCreationTask<Person>
  1743. {
  1744. public PeopleIndex21()
  1745. {
  1746. Map = people => from person in people
  1747. select new
  1748. {
  1749. person.Name
  1750. };
  1751. AdditionalSources = new Dictionary<string, string>
  1752. {
  1753. {
  1754. "PeopleUtil",
  1755. @"
  1756. namespace MyNamespace
  1757. {
  1758. public static class Program
  1759. {
  1760. public static void Foo()
  1761. {
  1762. }
  1763. }
  1764. }
  1765. "
  1766. }
  1767. };
  1768. }
  1769. }
  1770. private class PeopleIndex22 : AbstractIndexCreationTask<Person>
  1771. {
  1772. public PeopleIndex22()
  1773. {
  1774. Map = people => from person in people
  1775. select new
  1776. {
  1777. person.Name,
  1778. };
  1779. AdditionalSources = new Dictionary<string, string>
  1780. {
  1781. {
  1782. "PeopleUtil",
  1783. @"
  1784. namespace ETIS
  1785. {
  1786. public static class PeopleUtil
  1787. {
  1788. /// <summary>
  1789. /// It does nothing
  1790. /// </summary>
  1791. public static void Foo21()
  1792. {
  1793. }
  1794. }
  1795. }
  1796. "
  1797. }
  1798. };
  1799. }
  1800. }
  1801. }
  1802. public class MyList<T> : List<T>
  1803. {
  1804. }
  1805. public class MyEnumerable<T> : IEnumerable<T>
  1806. {
  1807. private IEnumerable<T> _list;
  1808. public MyEnumerable()
  1809. {
  1810. }
  1811. public MyEnumerable(IEnumerable<T> list)
  1812. {
  1813. _list = list;
  1814. }
  1815. public IEnumerator<T> GetEnumerator()
  1816. {
  1817. return _list.GetEnumerator();
  1818. }
  1819. IEnumerator IEnumerable.GetEnumerator()
  1820. {
  1821. return GetEnumerator();
  1822. }
  1823. }
  1824. public static class PeopleUtil
  1825. {
  1826. public static string CalculatePersonEmail(string name, uint age)
  1827. {
  1828. //The code below intention is just to make sure NodaTime is compiling with our index
  1829. return $"{name}.{Instant.FromDateTimeUtc(DateTime.Now.ToUniversalTime()).ToDateTimeUtc().Year - age}@ayende.com";
  1830. }
  1831. public static bool Foo1(List<string> friends)
  1832. {
  1833. return friends.Count(n => n != "ayende") > 1;
  1834. }
  1835. public static bool Foo2(Dictionary<string, long> friends)
  1836. {
  1837. return friends.Count(n => n.Key != "ayende") > 1;
  1838. }
  1839. public static bool Foo3(ICollection<string> friends)
  1840. {
  1841. return friends.All(n => n != "ayende");
  1842. }
  1843. public static bool Foo4(HashSet<string> friends)
  1844. {
  1845. return friends.All(n => n != "ayende");
  1846. }
  1847. public static bool Foo5(IEnumerable<User> friends)
  1848. {
  1849. return friends.All(n => n.Name != "ayende");
  1850. }
  1851. public static bool Foo6(User[] friends)
  1852. {
  1853. return friends.All(n => n.Name != "ayende");
  1854. }
  1855. public static bool Foo7(MyList<string> friends)
  1856. {
  1857. return friends.Count(n => n != "ayende") > 1;
  1858. }
  1859. public static bool Foo8(MyEnumerable<string> friends)
  1860. {
  1861. return friends.Count(n => n != "ayende") > 1;
  1862. }
  1863. public static TimeSpan Foo9(DateTime start, DateTime? end)
  1864. {
  1865. if (end.HasValue == false)
  1866. return TimeSpan.MaxValue;
  1867. return end.Value - start;
  1868. }
  1869. public static long Foo10(Dictionary<string, long> friends)
  1870. {
  1871. if (friends == null)
  1872. return -1;
  1873. if (friends.ContainsKey("home"))
  1874. return 0;
  1875. return friends.Values.Sum(x => x);
  1876. }
  1877. public static IEnumerable<string> Foo11(IndexExtensionFromClient.Person p)
  1878. {
  1879. return p.Friends;
  1880. }
  1881. public static IEnumerable<string> Foo12(IEnumerable<string> foreachString, string add)
  1882. {
  1883. if (foreachString == null || !foreachString.Any())
  1884. return new List<string>();
  1885. var combined = foreachString.Select(x => x + "|" + add);
  1886. return combined;
  1887. }
  1888. public static uint Foo13(IndexExtensionFromClient.Person p)
  1889. {
  1890. return p.Age;
  1891. }
  1892. public static List<string> Foo14(IndexExtensionFromClient.Person p)
  1893. {
  1894. return p.Friends;
  1895. }
  1896. public static HashSet<string> Foo15(IndexExtensionFromClient.Person p)
  1897. {
  1898. return p.FriendsHashset;
  1899. }
  1900. public static int[] Foo16(IndexExtensionFromClient.Person p)
  1901. {
  1902. return p.Numbers;
  1903. }
  1904. public static MyList<string> Foo17(IndexExtensionFromClient.Person p)
  1905. {
  1906. return p.MyList;
  1907. }
  1908. public static MyEnumerable<string> Foo18(IndexExtensionFromClient.Person p)
  1909. {
  1910. return p.MyEnumerable;
  1911. }
  1912. public static List<string> Foo19(List<string> friends)
  1913. {
  1914. if (friends == null || !friends.Any())
  1915. return new List<string>();
  1916. return friends.Concat(new []{ "ayende", "ppekrol" }).ToList();
  1917. }
  1918. public static List<int> Foo20()
  1919. {
  1920. var x = new List<int>
  1921. {
  1922. 1, 2, 3
  1923. };
  1924. return x;
  1925. }
  1926. /// <summary>
  1927. /// It does nothing
  1928. /// </summary>
  1929. public static void Foo21()
  1930. {
  1931. }
  1932. }
  1933. }