PageRenderTime 69ms CodeModel.GetById 3ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/ravendb/ravendb
C# | 2253 lines | 1947 code | 300 blank | 6 comment | 21 complexity | 26c2cddb85542902e44388389e482d2f MD5 | raw file

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

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

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