PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 62ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Tests/Soapi.Tests/LazyLoadingFixture.cs

https://bitbucket.org/bitpusher/soapi.cs
C# | 1071 lines | 803 code | 219 blank | 49 comment | 18 complexity | 3a9a1d9744962896c2fe5f4c7ad5be4c MD5 | raw file
   1#region imports
   2
   3using System;
   4using System.Collections.Generic;
   5using System.Diagnostics;
   6using System.IO;
   7using System.Linq;
   8using NUnit.Framework;
   9using Soapi;
  10using Soapi.Domain;
  11using Soapi.Net;
  12using Soapi.Queries;
  13using Soapi.Responses;
  14using Soapi.Parameters;
  15#if SILVERLIGHT
  16using Microsoft.Silverlight.Testing;
  17using Microsoft.VisualStudio.TestTools.UnitTesting;
  18using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
  19using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
  20using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute;
  21#else
  22
  23#endif
  24
  25#endregion
  26
  27namespace Soapi.Tests
  28{
  29
  30    [TestFixture]
  31    public class LazyLoadingFixture : CommonFixtureBase
  32    {
  33    
  34        [SetUp]
  35        public void SetUp()
  36        {
  37            base.Setup();
  38            Context.Options.LazyLoadingEnabled(true).EagerLoadingEnabled(true);
  39        }
  40
  41        [Test]
  42        public void QuickAnswer01()
  43        {
  44            var epicQuestions = SO
  45                .Questions
  46                .Sort(QuestionSort.Votes)
  47                .Min(300)
  48                .PageCount(-1)
  49                .ToList();
  50
  51            var badassUsers = SO
  52                .Users
  53                .Sort(UserSort.Reputation)
  54                .Min(10000)
  55                .PageCount(-1)
  56                .ToList();
  57        }
  58
  59        [Test]
  60        public void SitesEnum()
  61        {
  62            foreach (Site item in Context.Sites)
  63            {
  64                Console.WriteLine("/sites                             {0}", item.Name);
  65            }
  66
  67        }
  68
  69        [Test]
  70        public void AnswersById()
  71        {
  72
  73            foreach (Answer item in SO.Answers.ById(2917525))
  74            {
  75
  76                foreach (var i in item.Comments)
  77                {
  78                    Assert.IsFalse(i.IsStub);
  79                    Assert.IsFalse(i.Owner.IsStub);
  80                    Assert.IsFalse(i.ReplyToUser.IsStub);
  81                }
  82                Assert.IsFalse(item.Owner.IsStub);
  83                Question question = item.Question;
  84                Assert.IsFalse(question.IsStub);
  85                User questionOwner = question.Owner;
  86                Assert.IsFalse(questionOwner.IsStub);
  87                var accepted = question.AcceptedAnswer;
  88                Assert.IsFalse(accepted.IsStub);
  89
  90
  91
  92                Console.WriteLine("/answers/{{id}}                    {0}", item.Title);
  93            }
  94
  95        }
  96
  97        [Test]
  98        public void AnswerComments()
  99        {
 100            foreach (Comment item in SO.Answers.ById(2907216).Comments)
 101            {
 102                Assert.IsFalse(item.IsStub);
 103                Assert.IsFalse(item.Owner.IsStub); // asserts that there is always an owner, which is probably not the case
 104                if (item.ReplyToUser != null)
 105                {
 106                    Assert.IsFalse(item.ReplyToUser.IsStub);
 107                }
 108                Console.WriteLine("/answers/{{id}}/comments           {0}", item.Body);
 109            }
 110        }
 111
 112        [Test]
 113        public void Badges()
 114        {
 115
 116            foreach (Badge item in SO.Badges)
 117            {
 118
 119                Console.WriteLine("/badges                            {0}", item.Name);
 120            }
 121
 122        }
 123
 124        [Test]
 125        public void BadgesId()
 126        {
 127
 128            foreach (User item in SO.Badges.ById(9))
 129            {
 130                Console.WriteLine("/badges/{{id}}                     {0}", item.DisplayName);
 131            }
 132
 133        }
 134
 135        [Test]
 136        public void BadgesName()
 137        {
 138            foreach (Badge item in SO.Badges.ByName)
 139            {
 140                Console.WriteLine("/badges/name                       {0}", item.Name);
 141            }
 142
 143        }
 144        [Test]
 145        public void BadgesTags()
 146        {
 147
 148            foreach (Badge item in SO.Badges.ByTags)
 149            {
 150                Console.WriteLine("/badges/tags                       {0}", item.Name);
 151            }
 152
 153        }
 154        [Test]
 155        public void Comments()
 156        {
 157            foreach (Comment item in SO.Comments.ById(2917319))
 158            {
 159                Assert.IsFalse(item.IsStub);
 160                Assert.IsFalse(item.Owner.IsStub);
 161                if (item.ReplyToUser != null)
 162                {
 163                    Assert.IsFalse(item.ReplyToUser.IsStub);
 164                }
 165                
 166                Console.WriteLine("/comments/{{id}}                   {0}", item.Body);
 167            }
 168
 169        }
 170        [Test]
 171        public void PostComments()
 172        {
 173            foreach (Comment item in SO.Comments.ByPostId(2912300))
 174            {
 175                Assert.IsFalse(item.IsStub);
 176                Assert.IsFalse(item.Owner.IsStub);
 177                if (item.ReplyToUser != null)
 178                {
 179                    Assert.IsFalse(item.ReplyToUser.IsStub);
 180                }
 181                
 182
 183                Console.WriteLine("/posts/{{id}}/comments             {0}", item.Body);
 184            }
 185
 186        }
 187        [Test]
 188        public void Questions2()
 189        {
 190            foreach (Question item in SO.Questions)
 191            {
 192                Assert.IsFalse(item.IsStub);
 193                Assert.IsFalse(item.Owner.IsStub);
 194                if (item.AcceptedAnswer != null)
 195                {
 196                    Assert.IsFalse(item.AcceptedAnswer.IsStub);
 197                }
 198                
 199                item.Answers.ForEach(a =>
 200                    {
 201                        a.Comments.ForEach(c =>
 202                            {
 203                                Assert.IsFalse(c.IsStub);
 204                                Assert.IsFalse(c.Owner.IsStub);
 205                                if (c.ReplyToUser != null)
 206                                {
 207                                    Assert.IsFalse(c.ReplyToUser.IsStub);
 208                                }
 209                                
 210                            });
 211
 212                        Assert.IsFalse(a.Owner.IsStub);
 213                        Assert.IsFalse(a.Question.IsStub);
 214                    });
 215                Console.WriteLine("/questions                         {0}", item.Title);
 216            }
 217
 218        }
 219        [Test]
 220        public void QuestionsById()
 221        {
 222            foreach (Question item in SO.Questions.ById(2912300))
 223            {
 224                Console.WriteLine("/questions/{{id}}                  {0}", item.Title);
 225            }
 226
 227        }
 228        [Test]
 229        public void QuestionsAnswers()
 230        {
 231            foreach (Answer item in SO.Questions.ById(2912300).Answers)
 232            {
 233                Console.WriteLine("/questions/{{id}}/answers          {0}", item.Title);
 234            }
 235
 236
 237        }
 238        [Test]
 239        public void QuestionsComments()
 240        {
 241
 242            foreach (Comment item in SO.Questions.ById(2912300).Comments)
 243            {
 244                Console.WriteLine("/questions/{{id}}/comments         {0}", item.Body);
 245            }
 246
 247        }
 248        [Test]
 249        public void QuestionsByIdTimeline()
 250        {
 251
 252            foreach (PostTimeline item in SO.Questions.ById(2912300).Timeline)
 253            {
 254                if (item.User != null)
 255                {
 256                    Assert.IsFalse(item.User.IsStub); //TODO: what exactly is the 'user' on a post_timeline. I am assuming that it is the user responsible for the action, if not the user    
 257                }
 258                
 259                Assert.IsFalse(item.Owner.IsStub);
 260
 261                Console.WriteLine("/questions/{{id}}/timeline         {0}", item.TimelineType);
 262            }
 263
 264        }
 265
 266        [Test]
 267        public void QuestionsUnanswered()
 268        {
 269            foreach (Question item in SO.Questions.Unanswered)
 270            {
 271                Console.WriteLine("/questions/unanswered              {0}", item.Title);
 272            }
 273
 274        }
 275
 276        [Test]
 277        public void Revisions()
 278        {
 279            foreach (Revision item in SO.Revisions.ById(2350874))
 280            {
 281                Assert.IsFalse(item.User.IsStub);
 282                Console.WriteLine("/revisions/{{id}}                  {0}", item.PostId);
 283            }
 284
 285        }
 286        [Test]
 287        public void RevisionsById()
 288        {
 289
 290            foreach (Revision item in SO.Revisions.ById(2350874).ByRevisionGuid(new Guid("d6b99e8e-0f6c-4f68-92d0-6a050feea1fc")))
 291            {
 292                Assert.IsFalse(item.User.IsStub);
 293
 294
 295                Console.WriteLine("/revisions/{{id}}/{{revisionguid}} {0}", item.Title);
 296            }
 297        }
 298
 299        [Test]
 300        public void Search()
 301        {
 302            foreach (Question item in SO.Search.InTitle("sqlite"))
 303            {
 304                Console.WriteLine("/search                            {0}", item.Title);
 305            }
 306        }
 307
 308        [Test]
 309        public void Tags2()
 310        {
 311            foreach (Tag item in SO.Tags)
 312            {
 313                Assert.IsFalse(item.User.IsStub);
 314                Console.WriteLine("/tags                              {0}", item.Name);
 315            }
 316
 317        }
 318
 319
 320
 321        [Test]
 322        public void Users()
 323        {
 324            foreach (User item in SO.Users)
 325            {
 326                Console.WriteLine("/users                             {0}", item.DisplayName);
 327            }
 328
 329        }
 330        [Test]
 331        public void UsersById()
 332        {
 333
 334            foreach (User item in SO.Users.ById(242897))
 335            {
 336                Console.WriteLine("/users/{{id}}                      {0}", item.DisplayName);
 337            }
 338
 339
 340        }
 341
 342
 343        [Test]
 344        public void UsersByIdAnswers()
 345        {
 346
 347
 348
 349            foreach (Answer item in SO.Users.ById(242897).Answers)
 350            {
 351                Console.WriteLine("/users/{{id}}/answers              {0}", item.Title);
 352            }
 353
 354        }
 355
 356        [Test]
 357        public void UsersByIdAssociated()
 358        {
 359
 360            foreach (User item in SO.Users.ById(new Guid("d6b99e8e-0f6c-4f68-92d0-6a050feea1fc")).Associated)
 361            {
 362                Console.WriteLine("/users/{{id}}/associated           {0}", item.DisplayName);
 363            }
 364
 365        }
 366        [Test]
 367        public void UsersByIdBadges()
 368        {
 369            foreach (Badge item in SO.Users.ById(242897).Badges)
 370            {
 371
 372                Console.WriteLine("/users/{{id}}/badges               {0}", item.Name);
 373            }
 374
 375        }
 376        [Test]
 377        public void UsersByIdComments()
 378        {
 379
 380            foreach (Comment item in SO.Users.ById(242897).Comments)
 381            {
 382                Console.WriteLine("/users/{{id}}/comments             {0}", item.Body);
 383            }
 384
 385        }
 386        [Test]
 387        public void UsersByIdCommentsTo()
 388        {
 389
 390
 391            foreach (Comment item in SO.Users.ById(242897).Comments.ToId(160173))
 392            {
 393                Console.WriteLine("/users/{{id}}/comments/{{toid}}    {0}", item.Body);
 394            }
 395
 396        }
 397        [Test]
 398        public void UsersByIdFavorites()
 399        {
 400            foreach (Question item in SO.Users.ById(242897).Favorites)
 401            {
 402                Console.WriteLine("/users/{{id}}/favorites            {0}", item.Title);
 403            }
 404
 405        }
 406        [Test]
 407        public void UsersByIdMentioned()
 408        {
 409
 410            foreach (Comment item in SO.Users.ById(242897).Mentioned)
 411            {
 412                Console.WriteLine("/users/{{id}}/mentioned            {0}", item.Body);
 413            }
 414
 415        }
 416
 417        [Test]
 418        public void UsersByIdQuestions()
 419        {
 420            foreach (Question item in SO.Users.ById(242897).Questions.WithAnswers(true))
 421            {
 422                Console.WriteLine("/users/{{id}}/questions            {0}", item.Title);
 423            }
 424        }
 425
 426        [Test]
 427        public void UsersByIdReputation()
 428        {
 429            foreach (RepChange item in SO.Users.ById(242897).Reputation)
 430            {
 431                Assert.IsFalse(item.User.IsStub);
 432                Console.WriteLine("/users/{{id}}/reputation           {0}", item.PostId);
 433            }
 434
 435        }
 436
 437        [Test]
 438        public void UsersByIdTags()
 439        {
 440
 441            foreach (Tag item in SO.Users.ById(242897).Tags)
 442            {
 443                Assert.IsFalse(item.User.IsStub);
 444
 445                Console.WriteLine("/users/{{id}}/tags                 {0}", item.Name);
 446            }
 447
 448        }
 449
 450        [Test]
 451        public void UsersByIdTimeline()
 452        {
 453            foreach (UserTimeline item in SO.Users.ById(242897).Timeline)
 454            {
 455                if (item.User != null)
 456                {
 457                    Assert.IsFalse(item.User.IsStub);     //TODO: when is the user in a usertimeline null?
 458                }
 459                else
 460                {
 461                    Assert.Fail("user timeline has no user?");
 462                }
 463
 464                Console.WriteLine("/users/{{id}}/timeline             {0}", item.PostId);
 465            }
 466
 467        }
 468
 469        [Test]
 470        public void UsersModerators()
 471        {
 472            foreach (User item in SO.Users.Moderators)
 473            {
 474                Console.WriteLine("/users/moderators                  {0}", item.DisplayName);
 475            }
 476
 477        }
 478
 479
 480        [Test]
 481        public void UserAnswers()
 482        {
 483
 484            foreach (Answer item in SO.User(242897).Answers)
 485            {
 486                Console.WriteLine("/users/{{id}}/answers              {0}", item.Title);
 487            }
 488        }
 489        [Test]
 490        public void UserByIdBadges()
 491        {
 492            foreach (Badge item in SO.User(242897).Badges)
 493            {
 494                Console.WriteLine("/users/{{id}}/badges               {0}", item.Name);
 495            }
 496
 497        }
 498        [Test]
 499        public void UserComments()
 500        {
 501            foreach (Comment item in SO.User(242897).Comments)
 502            {
 503                Console.WriteLine("/users/{{id}}/comments             {0}", item.Body);
 504            }
 505
 506        }
 507        [Test]
 508        public void UserCommentsTo()
 509        {
 510            foreach (Comment item in SO.User(242897).CommentsTo(160173))
 511            {
 512                Console.WriteLine("/users/{{id}}/comments/{{toid}}    {0}", item.Body);
 513            }
 514        }
 515
 516        [Test]
 517        public void UserFavorites()
 518        {
 519            foreach (Question item in SO.User(242897).Favorites)
 520            {
 521                Console.WriteLine("/users/{{id}}/favorites            {0}", item.Title);
 522            }
 523
 524        }
 525
 526        [Test]
 527        public void UserMentioned()
 528        {
 529            foreach (Comment item in SO.User(242897).Mentioned)
 530            {
 531                Console.WriteLine("/users/{{id}}/mentioned            {0}", item.Body);
 532            }
 533
 534        }
 535        [Test]
 536        public void UserQuestions()
 537        {
 538
 539            foreach (Question item in SO.User(242897).Questions)
 540            {
 541                Console.WriteLine("/users/{{id}}/questions            {0}", item.Title);
 542            }
 543
 544        }
 545        [Test]
 546        public void UserRep()
 547        {
 548            foreach (RepChange item in SO.User(242897).RepChanges)
 549            {
 550                Console.WriteLine("/users/{{id}}/reputation           {0}", item.PostId);
 551            }
 552
 553        }
 554
 555        [Test]
 556        public void UserTags()
 557        {
 558            foreach (Tag item in SO.User(242897).Tags)
 559            {
 560                Console.WriteLine("/users/{{id}}/tags                 {0}", item.Name);
 561            }
 562
 563        }
 564
 565        [Test]
 566        public void UserTimeline()
 567        {
 568            foreach (UserTimeline item in SO.User(242897).Timeline)
 569            {
 570                Console.WriteLine("/users/{{id}}/timeline             {0}", item.PostId);
 571            }
 572
 573        }
 574
 575        [Test]
 576        public void LazyLoadedProperties()
 577        {
 578
 579            var interestingQuestions =
 580                (from question in SO.Questions.Unanswered.WithAnswers(true)
 581                 where question.AnswerCount > 0
 582                 orderby question.CreationDate descending
 583                 select new
 584                 {
 585                     Asker = question.Owner.DisplayName,
 586                     Title = question.Title,
 587                     Answers = question.Answers,
 588                     AskerFavCount = question.Owner.Favorites.Total,
 589                     AskerFaves = question.Owner.Favorites
 590                 }).ToList();
 591
 592
 593            foreach (var item in interestingQuestions)
 594            {
 595
 596                Console.WriteLine("----------------------------------------\r\n");
 597                Console.WriteLine("{0} asked '{1}'", item.Asker, item.Title);
 598
 599                if (item.AskerFavCount > 0)
 600                {
 601                    Console.WriteLine("\t asker likes:");
 602
 603                    foreach (Question fave in item.AskerFaves)
 604                    {
 605                        Console.WriteLine("\t\t{0}", fave.Title);
 606                    }
 607                }
 608
 609                Console.WriteLine();
 610
 611                foreach (var answer in item.Answers)
 612                {
 613                    // here we are lazy loading the answer's owner and 
 614                    // then eager loading his other answers
 615
 616                    User answerOwner = answer.Owner;
 617                    UsersByIdAnswersQuery answerOwnerAnswers = answerOwner.Answers;
 618                    var answerOwnerTopAnswer = answerOwnerAnswers.Sort(PostSort.Votes).PageSize(1).FirstOrDefault();
 619
 620                    int answerOwnerTopScore = answerOwnerTopAnswer != null
 621                        ? answerOwnerTopAnswer.Score
 622                        : 0;
 623
 624                    Console.WriteLine("Answerer {0}, credibility {1}",
 625                        answerOwner.DisplayName, answerOwnerTopScore);
 626                }
 627
 628                Console.WriteLine();
 629            }
 630        }
 631        [Test]
 632        public void Parameters()
 633        {
 634         
 635            // this is hanging - likely due to the changes I made in the 
 636            // parameter validation and handling of paged requests
 637
 638            var questionWithStuff =
 639                Context // soapi context
 640                .Official // convenience properties for known sites
 641                .StackOverflow
 642                .Questions
 643                .Tagged("asp.net", "c#", "asp.net-membership")
 644                .FromDate("january 1, 2001")
 645                .ToDate(new DateTime(2020, 10, 10))
 646                .WithBody(true)
 647                .WithAnswers(true)
 648                .WithComments(true)
 649                .Page(1)
 650                .PageSize(2)
 651                .PageCount(3)
 652                .Sort(QuestionSort.Votes)
 653                .Min(1)
 654                .Max(100)
 655                .Order(SortOrder.Asc);
 656
 657            var query = questionWithStuff.ToList();
 658
 659            var results = questionWithStuff.ToList();
 660
 661            Assert.AreEqual(6, questionWithStuff.Count());
 662
 663            // issues the following requests
 664
 665            // http://api.stackoverflow.com/1.0/questions?answers=true&body=true&comments=true&fromdate=978307200&max=100&min=1&order=Asc&page=1&pagesize=2&sort=Votes&tagged=asp.net%3bc%23&todate=1602288000
 666            // http://api.stackoverflow.com/1.0/questions?answers=true&body=true&comments=true&fromdate=978307200&max=100&min=1&order=Asc&page=2&pagesize=2&sort=Votes&tagged=asp.net%3bc%23&todate=1602288000
 667            // http://api.stackoverflow.com/1.0/questions?answers=true&body=true&comments=true&fromdate=978307200&max=100&min=1&order=Asc&page=3&pagesize=2&sort=Votes&tagged=asp.net%3bc%23&todate=1602288000
 668
 669            questionWithStuff =
 670               Context // soapi context
 671               .Official // convenience properties for known sites
 672               .StackOverflow
 673               .Questions
 674               .Tagged("asp.net", "c#", "asp.net-membership")
 675               .FromDate("january 1, 2001")
 676               .ToDate(new DateTime(2020, 10, 10))
 677               .WithBody(true)
 678               .WithAnswers(true)
 679               .WithComments(true)
 680               .Page(1)
 681               .PageSize(2)
 682               .PageCount(3)
 683               .Sort(QuestionSort.Votes)
 684               .Min(1)
 685               .Max(100)
 686               .Order(SortOrder.Asc);
 687
 688            int total = questionWithStuff.PageSize(5).PageCount(-1).Total;
 689            var questionWithStuffFetch = questionWithStuff.ToList();
 690            Assert.AreEqual(total, questionWithStuffFetch.Count);
 691
 692
 693        }
 694        [Test]
 695        public void BadgesLazyRouteResponseTest()
 696        {
 697            ;
 698
 699            var badges = new BadgesQuery(SO);
 700
 701            var x = from badge in badges
 702                    where badge.Name.StartsWith("a", StringComparison.InvariantCultureIgnoreCase)
 703                    orderby badge.Rank descending
 704                    select new { badge.Name, badge.Rank };
 705
 706            foreach (var item in x)
 707            {
 708                Console.WriteLine("{0} {1}", item.Name, item.Rank);
 709            }
 710
 711
 712            Context.Options.EagerLoadingEnabled(false);
 713
 714            int epicBadge = 145;
 715
 716            var epicMicrosoftUsersAndWhatTheyLike =
 717                SO.Badges.ById(epicBadge).PageCount(-1)
 718                .Where(u => (u.AboutMe ?? "").IndexOf("Microsoft", StringComparison.OrdinalIgnoreCase) > -1)
 719                .Select(u => new
 720                    {
 721                        u.DisplayName,
 722                        u.Reputation,
 723                        FavoriteQuestion = u.Favorites.FirstOrDefault()
 724                    });
 725
 726            foreach (var user in epicMicrosoftUsersAndWhatTheyLike)
 727            {
 728                Console.WriteLine("{0} [{1}]\r\n\tlikes\r\n\t{2}", user.DisplayName, user.Reputation,
 729                    user.FavoriteQuestion != null ? user.FavoriteQuestion.Title + " - by "
 730                    + user.FavoriteQuestion.Owner.DisplayName
 731                    + " [" + user.FavoriteQuestion.Owner.Reputation + "]" : "");
 732            }
 733
 734
 735        }
 736
 737        [Test]
 738        public void Site()
 739        {
 740            List<User> associatedUsers = SO.Users.ById(new Guid("e58345f5-0f7b-4261-b449-3959c596f91f")).ToList();
 741
 742            foreach (User item in associatedUsers)
 743            {
 744                Console.WriteLine("associatedUsers " + item.Site.Name + " " + item.DisplayName);
 745            }
 746
 747            List<User> moderators = SO.Users.Moderators.ToList();
 748            foreach (User item in moderators)
 749            {
 750                Console.WriteLine("{0} ", item.DisplayName + " " + item.Reputation);
 751            }
 752            List<Tag> t = SO.Tags.ToList();
 753            List<Question> s = SO.Search.InTitle("SQLITE").ToList();
 754            List<Badge> x = SO.Badges.ToList();
 755            List<Badge> y = SO.Badges.ByTags.ToList();
 756            List<Badge> z = SO.Badges.ByName.ToList();
 757            List<User> u = SO.Badges.ById(9).ToList();
 758
 759            // postComments is only getting the first comment
 760            List<Comment> q = SO.Comments.ByPostId(3146826).ToList();
 761
 762
 763            List<Question> questions = SO.Questions.ToList().ToList();
 764            List<Question> questionById = SO.Questions.ById(3146817).ToList();
 765            List<Answer> questionByIdAnsers = SO.Questions.ById(3146817).Answers.ToList();
 766            List<PostTimeline> questionByIdTimelines = SO.Questions.ById(3146817).Timeline.ToList();
 767            List<Comment> questionByIdComments = SO.Questions.ById(3146817).Comments.ToList();
 768            List<Question> questionUnanswered = SO.Questions.Unanswered.ToList();
 769
 770            List<Revision> revision = SO.Revisions.ById(3299997).ToList();
 771            List<Revision> revisionByGuid =
 772                SO.Revisions.ById(3299997).ByRevisionGuid(new Guid("68cb0ec1-72f9-44d2-aac9-fa09bb9ce988")).ToList();
 773        }
 774
 775        [Test]
 776        public void UsersQueryTest()
 777        {
 778            var users = new UsersQuery(SO);
 779
 780            int total = users.Total;
 781
 782            var x = from user in users orderby user.LastAccessDate descending select new { user.DisplayName };
 783            foreach (var anon in x)
 784            {
 785                Console.WriteLine(anon.DisplayName);
 786            }
 787            foreach (var user in users)
 788            {
 789                user.Badges.ToList().ForEach(b => Console.WriteLine(b.Rank + " " + b.Name));
 790            }
 791
 792            User skeet = users.First();
 793            var answerCount = skeet.AnswerCount;
 794            var answerTotal = skeet.Answers.Total;
 795
 796            // the default query is 1 standard page. this can be 
 797            // tweaked by modifying the default parameter on the query
 798            // or specifying query parameters
 799            var queryCount = skeet.Answers.Count(); // will return 0 because no fetch has been made. the collection is empty
 800
 801            foreach (Answer answer in skeet.Answers) // default enumeration will get 30
 802            {
 803                Console.WriteLine(answer.Title);
 804            }
 805            skeet.Answers.Parameters.PageSize = 100;
 806
 807            foreach (Answer answer in skeet.Answers) // now we will get 100 questions, this will make a new request as the query string is different
 808            {
 809                Console.WriteLine(answer.Title);
 810            }
 811
 812            // a default enumeration will fetch 10 pages. In this case only 9 requests will be made because page 1 is already cached.
 813            // there may be some issues with some of the arbitrarily ordered results like search - they will need special attention
 814
 815            skeet.Answers.Parameters.PageCount = 10;
 816
 817            foreach (Answer answer in skeet.Answers)
 818            {
 819                Console.WriteLine(answer.Title);
 820            }
 821
 822            // lets repeat the enumeration. there are actually 10 new request being made but they are cached
 823            // so this is an almost free query.
 824            foreach (Answer answer in skeet.Answers)
 825            {
 826                Console.WriteLine(answer.Title);
 827            }
 828
 829
 830
 831            // note: the default behavior is for the Query to accumulate all data requested, so as queries
 832            // are executed, the results are either added or updated in the list.
 833
 834            // Query is a full list<T> impelemntation, so you may simply clear the list if you like.
 835
 836            foreach (Comment item in skeet.Comments)
 837            {
 838                Console.WriteLine("Comments " + item.Body);
 839            }
 840            foreach (Comment item in skeet.Comments.ToId(44269))
 841            {
 842                Console.WriteLine("Comments commentsTo" + item.Body);
 843            }
 844            foreach (Comment item in skeet.Comments.Mentioned)
 845            {
 846                Console.WriteLine("Comments mentioned" + item.Body);
 847            }
 848            foreach (Answer item in skeet.Answers)
 849            {
 850                Console.WriteLine("Answers" + item.Title);
 851            }
 852            foreach (Question item in skeet.Favorites)
 853            {
 854                Console.WriteLine("Favorites" + item.Title);
 855            }
 856            foreach (Question item in skeet.Questions)
 857            {
 858                Console.WriteLine("Questions" + item.Title);
 859            }
 860            foreach (RepChange item in skeet.RepChanges)
 861            {
 862                Console.WriteLine("RepChanges" + item.PositiveRep);
 863            }
 864            foreach (Tag item in skeet.Tags)
 865            {
 866                Console.WriteLine("Tags" + item.Name);
 867            }
 868            foreach (UserTimeline item in skeet.Timeline)
 869            {
 870                Console.WriteLine("Timelines" + item.TimelineType);
 871            }
 872
 873
 874
 875
 876
 877        }
 878
 879        [Test]
 880        public void SiteUsers()
 881        {
 882
 883            var users = SO.Users;
 884
 885            var first = users.FirstOrDefault();
 886
 887            Assert.IsNotNull(first);
 888
 889            foreach (User user in users) // enumerating a new collection fetches default page
 890            {
 891                Console.WriteLine(user.DisplayName);
 892            }
 893
 894
 895            var x = from p in SO.Users orderby p.Reputation select new { p.DisplayName, p.Reputation, Favorites = p.Favorites };
 896            foreach (var item in x)
 897            {
 898                Console.WriteLine("{0} - {1}", item.DisplayName, item.Reputation);
 899                item.Favorites.ToList().ForEach(f => Console.WriteLine("\t{0}", f.Title));
 900            }
 901        }
 902
 903        [Test]
 904        public void SiteStatistics()
 905        {
 906
 907            var target = SO.Statistics;
 908            Assert.AreEqual("1.0", target.ApiVersion.Version);
 909
 910        }
 911
 912        [Test]
 913        public void Test()
 914        {
 915            var target = SO.Badges;
 916            Assert.IsTrue(target.Count() > 0, "empty badges");
 917            // check for badge.User
 918        }
 919
 920        [Test]
 921        public void User()
 922        {
 923            var user = SO.User(242897);
 924            Assert.IsNotNull(user);
 925        }
 926        [Test]
 927        public void UserAssociated2()
 928        {
 929
 930            foreach (User item in SO.User(242897).Associated)
 931            {
 932                Console.WriteLine("/users/{{id}}/answers              {0}", item.DisplayName);
 933                item.Questions.ToList().ForEach(q => Console.WriteLine(q.Title));
 934            }
 935        }
 936        [Test]
 937        public void UserAssociated()
 938        {
 939            var user = SO.User(242897);
 940            var target = user.Associated.ToList();
 941            Assert.IsTrue(target.Count > 0);
 942
 943            // load
 944            foreach (var other in target)
 945            {
 946                Assert.IsFalse(other.IsStub);
 947            }
 948
 949
 950
 951            // do a quick gut check - watch duration and http traffic.
 952            // there should be no delay and no traffic, not even from cache
 953            var now = DateTime.Now;
 954            for (int i = 0; i < 10; i++)
 955            {
 956                foreach (var other in target)
 957                {
 958                    Assert.IsFalse(other.IsStub);
 959                }
 960            }
 961
 962            Assert.IsTrue(DateTime.Now < now.AddMilliseconds(200));
 963        }
 964
 965        [Test]
 966        public void UserBadges()
 967        {
 968            var user = SO.User(242897);
 969
 970            // lazy load the badges
 971            var target = user.Badges;
 972
 973            // i gots me some badges
 974            Assert.IsTrue(target.Count() > 0);
 975
 976
 977        }
 978
 979        [Test]
 980        public void Tags()
 981        {
 982            var user = SO.User(242897);
 983
 984            // lazy load the Tags
 985            var target = user.Tags;
 986
 987            // i gots me some Tags
 988            Assert.IsTrue(target.Count() > 0);
 989
 990
 991
 992        }
 993
 994        [Test]
 995        public void Questions()
 996        {
 997            var user = SO.User(242897);
 998
 999            // lazy load the Favorites
1000            var target = user.Favorites.WithBody(true);
1001
1002            // i gots me some Favorites
1003            Assert.IsTrue(target.Count() > 0);
1004
1005            // make sure my Favorites gots lift and body
1006            Assert.IsNotNull(target.First().Body);
1007        }
1008
1009        [Test]
1010        public void info_param()
1011        {
1012            // /questions
1013            SO.Questions
1014                // answers (optional)   When "true", the answers to a question will be returned
1015                .WithAnswers(true)
1016                // body (optional)      When "true", a post's body will be included in the response.
1017                .WithBody(true)
1018                // comments (optional)  When "true", any comments on a post will be included in the response.
1019                .WithComments(true)
1020                // fromdate (optional)  Unix timestamp of the minimum creation date on a returned item.
1021                .FromDate("january 1, 2001") // can parse dates
1022                // max (optional)       Maximum of the range to include in the response according to the current sort.
1023                .Max(2)
1024                // min (optional)       Minimum of the range to include in the response according to the current sort.
1025                .Min(1)
1026                // order (optional)     How the current sort should be ordered.
1027                .Order(SortOrder.Asc)
1028                // page (optional)      The pagination offset for the current collection. Affected by the specified pagesize.
1029                .Page(1)
1030                // pagesize (optional)  The number of collection results to display during pagination. Should be between 1 and 100 inclusive.
1031                .PageSize(100)
1032                // sort (optional)      How a collection should be sorted.
1033                .Sort(QuestionSort.Votes)
1034                // tagged (optional)    Semicolon delimited list of tags questions must have
1035                .Tagged("a", "b") // accepts param aary
1036                // todate (optional)    Unix timestamp of the maximum creation date on a returned item.
1037                .ToDate(new DateTime(2020, 1, 1))
1038
1039                .PageCount(10) // fetch multiple pages .PageCount(-1) to get all pages
1040                .ToList();
1041
1042
1043        }
1044
1045        [Test]
1046        public void PagingIntro()
1047        {
1048            // turn off lazy/eager loading for this demo
1049            Context
1050                .Options
1051                .EagerLoadingEnabled(false)
1052                .LazyLoadingEnabled(false);
1053
1054            // Soapi elegantly handles multi-page requests.
1055            // Multi-paged requests are issued in parallel making data retrieval quick
1056
1057            // get the default, 1 page of 30 using default sort
1058
1059            var questions = SO.Questions.ToList();
1060
1061            // get pages 3-4
1062
1063            var pages3to5 = SO.Questions.Page(3).PageCount(2).ToList();
1064
1065            // get all pages of a set
1066
1067            var stackAppsUsers = Context.Official.StackApps.Users.PageCount(-1).ToList();
1068
1069        }
1070    }
1071}