/trunk/Tests/Soapi.Tests/LazyLoadingFixture.cs
C# | 1071 lines | 803 code | 219 blank | 49 comment | 18 complexity | 3a9a1d9744962896c2fe5f4c7ad5be4c MD5 | raw file
Possible License(s): AGPL-1.0
- #region imports
-
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using NUnit.Framework;
- using Soapi;
- using Soapi.Domain;
- using Soapi.Net;
- using Soapi.Queries;
- using Soapi.Responses;
- using Soapi.Parameters;
- #if SILVERLIGHT
- using Microsoft.Silverlight.Testing;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
- using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
- using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute;
- #else
-
- #endif
-
- #endregion
-
- namespace Soapi.Tests
- {
-
- [TestFixture]
- public class LazyLoadingFixture : CommonFixtureBase
- {
-
- [SetUp]
- public void SetUp()
- {
- base.Setup();
- Context.Options.LazyLoadingEnabled(true).EagerLoadingEnabled(true);
- }
-
- [Test]
- public void QuickAnswer01()
- {
- var epicQuestions = SO
- .Questions
- .Sort(QuestionSort.Votes)
- .Min(300)
- .PageCount(-1)
- .ToList();
-
- var badassUsers = SO
- .Users
- .Sort(UserSort.Reputation)
- .Min(10000)
- .PageCount(-1)
- .ToList();
- }
-
- [Test]
- public void SitesEnum()
- {
- foreach (Site item in Context.Sites)
- {
- Console.WriteLine("/sites {0}", item.Name);
- }
-
- }
-
- [Test]
- public void AnswersById()
- {
-
- foreach (Answer item in SO.Answers.ById(2917525))
- {
-
- foreach (var i in item.Comments)
- {
- Assert.IsFalse(i.IsStub);
- Assert.IsFalse(i.Owner.IsStub);
- Assert.IsFalse(i.ReplyToUser.IsStub);
- }
- Assert.IsFalse(item.Owner.IsStub);
- Question question = item.Question;
- Assert.IsFalse(question.IsStub);
- User questionOwner = question.Owner;
- Assert.IsFalse(questionOwner.IsStub);
- var accepted = question.AcceptedAnswer;
- Assert.IsFalse(accepted.IsStub);
-
-
-
- Console.WriteLine("/answers/{{id}} {0}", item.Title);
- }
-
- }
-
- [Test]
- public void AnswerComments()
- {
- foreach (Comment item in SO.Answers.ById(2907216).Comments)
- {
- Assert.IsFalse(item.IsStub);
- Assert.IsFalse(item.Owner.IsStub); // asserts that there is always an owner, which is probably not the case
- if (item.ReplyToUser != null)
- {
- Assert.IsFalse(item.ReplyToUser.IsStub);
- }
- Console.WriteLine("/answers/{{id}}/comments {0}", item.Body);
- }
- }
-
- [Test]
- public void Badges()
- {
-
- foreach (Badge item in SO.Badges)
- {
-
- Console.WriteLine("/badges {0}", item.Name);
- }
-
- }
-
- [Test]
- public void BadgesId()
- {
-
- foreach (User item in SO.Badges.ById(9))
- {
- Console.WriteLine("/badges/{{id}} {0}", item.DisplayName);
- }
-
- }
-
- [Test]
- public void BadgesName()
- {
- foreach (Badge item in SO.Badges.ByName)
- {
- Console.WriteLine("/badges/name {0}", item.Name);
- }
-
- }
- [Test]
- public void BadgesTags()
- {
-
- foreach (Badge item in SO.Badges.ByTags)
- {
- Console.WriteLine("/badges/tags {0}", item.Name);
- }
-
- }
- [Test]
- public void Comments()
- {
- foreach (Comment item in SO.Comments.ById(2917319))
- {
- Assert.IsFalse(item.IsStub);
- Assert.IsFalse(item.Owner.IsStub);
- if (item.ReplyToUser != null)
- {
- Assert.IsFalse(item.ReplyToUser.IsStub);
- }
-
- Console.WriteLine("/comments/{{id}} {0}", item.Body);
- }
-
- }
- [Test]
- public void PostComments()
- {
- foreach (Comment item in SO.Comments.ByPostId(2912300))
- {
- Assert.IsFalse(item.IsStub);
- Assert.IsFalse(item.Owner.IsStub);
- if (item.ReplyToUser != null)
- {
- Assert.IsFalse(item.ReplyToUser.IsStub);
- }
-
-
- Console.WriteLine("/posts/{{id}}/comments {0}", item.Body);
- }
-
- }
- [Test]
- public void Questions2()
- {
- foreach (Question item in SO.Questions)
- {
- Assert.IsFalse(item.IsStub);
- Assert.IsFalse(item.Owner.IsStub);
- if (item.AcceptedAnswer != null)
- {
- Assert.IsFalse(item.AcceptedAnswer.IsStub);
- }
-
- item.Answers.ForEach(a =>
- {
- a.Comments.ForEach(c =>
- {
- Assert.IsFalse(c.IsStub);
- Assert.IsFalse(c.Owner.IsStub);
- if (c.ReplyToUser != null)
- {
- Assert.IsFalse(c.ReplyToUser.IsStub);
- }
-
- });
-
- Assert.IsFalse(a.Owner.IsStub);
- Assert.IsFalse(a.Question.IsStub);
- });
- Console.WriteLine("/questions {0}", item.Title);
- }
-
- }
- [Test]
- public void QuestionsById()
- {
- foreach (Question item in SO.Questions.ById(2912300))
- {
- Console.WriteLine("/questions/{{id}} {0}", item.Title);
- }
-
- }
- [Test]
- public void QuestionsAnswers()
- {
- foreach (Answer item in SO.Questions.ById(2912300).Answers)
- {
- Console.WriteLine("/questions/{{id}}/answers {0}", item.Title);
- }
-
-
- }
- [Test]
- public void QuestionsComments()
- {
-
- foreach (Comment item in SO.Questions.ById(2912300).Comments)
- {
- Console.WriteLine("/questions/{{id}}/comments {0}", item.Body);
- }
-
- }
- [Test]
- public void QuestionsByIdTimeline()
- {
-
- foreach (PostTimeline item in SO.Questions.ById(2912300).Timeline)
- {
- if (item.User != null)
- {
- 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
- }
-
- Assert.IsFalse(item.Owner.IsStub);
-
- Console.WriteLine("/questions/{{id}}/timeline {0}", item.TimelineType);
- }
-
- }
-
- [Test]
- public void QuestionsUnanswered()
- {
- foreach (Question item in SO.Questions.Unanswered)
- {
- Console.WriteLine("/questions/unanswered {0}", item.Title);
- }
-
- }
-
- [Test]
- public void Revisions()
- {
- foreach (Revision item in SO.Revisions.ById(2350874))
- {
- Assert.IsFalse(item.User.IsStub);
- Console.WriteLine("/revisions/{{id}} {0}", item.PostId);
- }
-
- }
- [Test]
- public void RevisionsById()
- {
-
- foreach (Revision item in SO.Revisions.ById(2350874).ByRevisionGuid(new Guid("d6b99e8e-0f6c-4f68-92d0-6a050feea1fc")))
- {
- Assert.IsFalse(item.User.IsStub);
-
-
- Console.WriteLine("/revisions/{{id}}/{{revisionguid}} {0}", item.Title);
- }
- }
-
- [Test]
- public void Search()
- {
- foreach (Question item in SO.Search.InTitle("sqlite"))
- {
- Console.WriteLine("/search {0}", item.Title);
- }
- }
-
- [Test]
- public void Tags2()
- {
- foreach (Tag item in SO.Tags)
- {
- Assert.IsFalse(item.User.IsStub);
- Console.WriteLine("/tags {0}", item.Name);
- }
-
- }
-
-
-
- [Test]
- public void Users()
- {
- foreach (User item in SO.Users)
- {
- Console.WriteLine("/users {0}", item.DisplayName);
- }
-
- }
- [Test]
- public void UsersById()
- {
-
- foreach (User item in SO.Users.ById(242897))
- {
- Console.WriteLine("/users/{{id}} {0}", item.DisplayName);
- }
-
-
- }
-
-
- [Test]
- public void UsersByIdAnswers()
- {
-
-
-
- foreach (Answer item in SO.Users.ById(242897).Answers)
- {
- Console.WriteLine("/users/{{id}}/answers {0}", item.Title);
- }
-
- }
-
- [Test]
- public void UsersByIdAssociated()
- {
-
- foreach (User item in SO.Users.ById(new Guid("d6b99e8e-0f6c-4f68-92d0-6a050feea1fc")).Associated)
- {
- Console.WriteLine("/users/{{id}}/associated {0}", item.DisplayName);
- }
-
- }
- [Test]
- public void UsersByIdBadges()
- {
- foreach (Badge item in SO.Users.ById(242897).Badges)
- {
-
- Console.WriteLine("/users/{{id}}/badges {0}", item.Name);
- }
-
- }
- [Test]
- public void UsersByIdComments()
- {
-
- foreach (Comment item in SO.Users.ById(242897).Comments)
- {
- Console.WriteLine("/users/{{id}}/comments {0}", item.Body);
- }
-
- }
- [Test]
- public void UsersByIdCommentsTo()
- {
-
-
- foreach (Comment item in SO.Users.ById(242897).Comments.ToId(160173))
- {
- Console.WriteLine("/users/{{id}}/comments/{{toid}} {0}", item.Body);
- }
-
- }
- [Test]
- public void UsersByIdFavorites()
- {
- foreach (Question item in SO.Users.ById(242897).Favorites)
- {
- Console.WriteLine("/users/{{id}}/favorites {0}", item.Title);
- }
-
- }
- [Test]
- public void UsersByIdMentioned()
- {
-
- foreach (Comment item in SO.Users.ById(242897).Mentioned)
- {
- Console.WriteLine("/users/{{id}}/mentioned {0}", item.Body);
- }
-
- }
-
- [Test]
- public void UsersByIdQuestions()
- {
- foreach (Question item in SO.Users.ById(242897).Questions.WithAnswers(true))
- {
- Console.WriteLine("/users/{{id}}/questions {0}", item.Title);
- }
- }
-
- [Test]
- public void UsersByIdReputation()
- {
- foreach (RepChange item in SO.Users.ById(242897).Reputation)
- {
- Assert.IsFalse(item.User.IsStub);
- Console.WriteLine("/users/{{id}}/reputation {0}", item.PostId);
- }
-
- }
-
- [Test]
- public void UsersByIdTags()
- {
-
- foreach (Tag item in SO.Users.ById(242897).Tags)
- {
- Assert.IsFalse(item.User.IsStub);
-
- Console.WriteLine("/users/{{id}}/tags {0}", item.Name);
- }
-
- }
-
- [Test]
- public void UsersByIdTimeline()
- {
- foreach (UserTimeline item in SO.Users.ById(242897).Timeline)
- {
- if (item.User != null)
- {
- Assert.IsFalse(item.User.IsStub); //TODO: when is the user in a usertimeline null?
- }
- else
- {
- Assert.Fail("user timeline has no user?");
- }
-
- Console.WriteLine("/users/{{id}}/timeline {0}", item.PostId);
- }
-
- }
-
- [Test]
- public void UsersModerators()
- {
- foreach (User item in SO.Users.Moderators)
- {
- Console.WriteLine("/users/moderators {0}", item.DisplayName);
- }
-
- }
-
-
- [Test]
- public void UserAnswers()
- {
-
- foreach (Answer item in SO.User(242897).Answers)
- {
- Console.WriteLine("/users/{{id}}/answers {0}", item.Title);
- }
- }
- [Test]
- public void UserByIdBadges()
- {
- foreach (Badge item in SO.User(242897).Badges)
- {
- Console.WriteLine("/users/{{id}}/badges {0}", item.Name);
- }
-
- }
- [Test]
- public void UserComments()
- {
- foreach (Comment item in SO.User(242897).Comments)
- {
- Console.WriteLine("/users/{{id}}/comments {0}", item.Body);
- }
-
- }
- [Test]
- public void UserCommentsTo()
- {
- foreach (Comment item in SO.User(242897).CommentsTo(160173))
- {
- Console.WriteLine("/users/{{id}}/comments/{{toid}} {0}", item.Body);
- }
- }
-
- [Test]
- public void UserFavorites()
- {
- foreach (Question item in SO.User(242897).Favorites)
- {
- Console.WriteLine("/users/{{id}}/favorites {0}", item.Title);
- }
-
- }
-
- [Test]
- public void UserMentioned()
- {
- foreach (Comment item in SO.User(242897).Mentioned)
- {
- Console.WriteLine("/users/{{id}}/mentioned {0}", item.Body);
- }
-
- }
- [Test]
- public void UserQuestions()
- {
-
- foreach (Question item in SO.User(242897).Questions)
- {
- Console.WriteLine("/users/{{id}}/questions {0}", item.Title);
- }
-
- }
- [Test]
- public void UserRep()
- {
- foreach (RepChange item in SO.User(242897).RepChanges)
- {
- Console.WriteLine("/users/{{id}}/reputation {0}", item.PostId);
- }
-
- }
-
- [Test]
- public void UserTags()
- {
- foreach (Tag item in SO.User(242897).Tags)
- {
- Console.WriteLine("/users/{{id}}/tags {0}", item.Name);
- }
-
- }
-
- [Test]
- public void UserTimeline()
- {
- foreach (UserTimeline item in SO.User(242897).Timeline)
- {
- Console.WriteLine("/users/{{id}}/timeline {0}", item.PostId);
- }
-
- }
-
- [Test]
- public void LazyLoadedProperties()
- {
-
- var interestingQuestions =
- (from question in SO.Questions.Unanswered.WithAnswers(true)
- where question.AnswerCount > 0
- orderby question.CreationDate descending
- select new
- {
- Asker = question.Owner.DisplayName,
- Title = question.Title,
- Answers = question.Answers,
- AskerFavCount = question.Owner.Favorites.Total,
- AskerFaves = question.Owner.Favorites
- }).ToList();
-
-
- foreach (var item in interestingQuestions)
- {
-
- Console.WriteLine("----------------------------------------\r\n");
- Console.WriteLine("{0} asked '{1}'", item.Asker, item.Title);
-
- if (item.AskerFavCount > 0)
- {
- Console.WriteLine("\t asker likes:");
-
- foreach (Question fave in item.AskerFaves)
- {
- Console.WriteLine("\t\t{0}", fave.Title);
- }
- }
-
- Console.WriteLine();
-
- foreach (var answer in item.Answers)
- {
- // here we are lazy loading the answer's owner and
- // then eager loading his other answers
-
- User answerOwner = answer.Owner;
- UsersByIdAnswersQuery answerOwnerAnswers = answerOwner.Answers;
- var answerOwnerTopAnswer = answerOwnerAnswers.Sort(PostSort.Votes).PageSize(1).FirstOrDefault();
-
- int answerOwnerTopScore = answerOwnerTopAnswer != null
- ? answerOwnerTopAnswer.Score
- : 0;
-
- Console.WriteLine("Answerer {0}, credibility {1}",
- answerOwner.DisplayName, answerOwnerTopScore);
- }
-
- Console.WriteLine();
- }
- }
- [Test]
- public void Parameters()
- {
-
- // this is hanging - likely due to the changes I made in the
- // parameter validation and handling of paged requests
-
- var questionWithStuff =
- Context // soapi context
- .Official // convenience properties for known sites
- .StackOverflow
- .Questions
- .Tagged("asp.net", "c#", "asp.net-membership")
- .FromDate("january 1, 2001")
- .ToDate(new DateTime(2020, 10, 10))
- .WithBody(true)
- .WithAnswers(true)
- .WithComments(true)
- .Page(1)
- .PageSize(2)
- .PageCount(3)
- .Sort(QuestionSort.Votes)
- .Min(1)
- .Max(100)
- .Order(SortOrder.Asc);
-
- var query = questionWithStuff.ToList();
-
- var results = questionWithStuff.ToList();
-
- Assert.AreEqual(6, questionWithStuff.Count());
-
- // issues the following requests
-
- // 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
- // 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
- // 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
-
- questionWithStuff =
- Context // soapi context
- .Official // convenience properties for known sites
- .StackOverflow
- .Questions
- .Tagged("asp.net", "c#", "asp.net-membership")
- .FromDate("january 1, 2001")
- .ToDate(new DateTime(2020, 10, 10))
- .WithBody(true)
- .WithAnswers(true)
- .WithComments(true)
- .Page(1)
- .PageSize(2)
- .PageCount(3)
- .Sort(QuestionSort.Votes)
- .Min(1)
- .Max(100)
- .Order(SortOrder.Asc);
-
- int total = questionWithStuff.PageSize(5).PageCount(-1).Total;
- var questionWithStuffFetch = questionWithStuff.ToList();
- Assert.AreEqual(total, questionWithStuffFetch.Count);
-
-
- }
- [Test]
- public void BadgesLazyRouteResponseTest()
- {
- ;
-
- var badges = new BadgesQuery(SO);
-
- var x = from badge in badges
- where badge.Name.StartsWith("a", StringComparison.InvariantCultureIgnoreCase)
- orderby badge.Rank descending
- select new { badge.Name, badge.Rank };
-
- foreach (var item in x)
- {
- Console.WriteLine("{0} {1}", item.Name, item.Rank);
- }
-
-
- Context.Options.EagerLoadingEnabled(false);
-
- int epicBadge = 145;
-
- var epicMicrosoftUsersAndWhatTheyLike =
- SO.Badges.ById(epicBadge).PageCount(-1)
- .Where(u => (u.AboutMe ?? "").IndexOf("Microsoft", StringComparison.OrdinalIgnoreCase) > -1)
- .Select(u => new
- {
- u.DisplayName,
- u.Reputation,
- FavoriteQuestion = u.Favorites.FirstOrDefault()
- });
-
- foreach (var user in epicMicrosoftUsersAndWhatTheyLike)
- {
- Console.WriteLine("{0} [{1}]\r\n\tlikes\r\n\t{2}", user.DisplayName, user.Reputation,
- user.FavoriteQuestion != null ? user.FavoriteQuestion.Title + " - by "
- + user.FavoriteQuestion.Owner.DisplayName
- + " [" + user.FavoriteQuestion.Owner.Reputation + "]" : "");
- }
-
-
- }
-
- [Test]
- public void Site()
- {
- List<User> associatedUsers = SO.Users.ById(new Guid("e58345f5-0f7b-4261-b449-3959c596f91f")).ToList();
-
- foreach (User item in associatedUsers)
- {
- Console.WriteLine("associatedUsers " + item.Site.Name + " " + item.DisplayName);
- }
-
- List<User> moderators = SO.Users.Moderators.ToList();
- foreach (User item in moderators)
- {
- Console.WriteLine("{0} ", item.DisplayName + " " + item.Reputation);
- }
- List<Tag> t = SO.Tags.ToList();
- List<Question> s = SO.Search.InTitle("SQLITE").ToList();
- List<Badge> x = SO.Badges.ToList();
- List<Badge> y = SO.Badges.ByTags.ToList();
- List<Badge> z = SO.Badges.ByName.ToList();
- List<User> u = SO.Badges.ById(9).ToList();
-
- // postComments is only getting the first comment
- List<Comment> q = SO.Comments.ByPostId(3146826).ToList();
-
-
- List<Question> questions = SO.Questions.ToList().ToList();
- List<Question> questionById = SO.Questions.ById(3146817).ToList();
- List<Answer> questionByIdAnsers = SO.Questions.ById(3146817).Answers.ToList();
- List<PostTimeline> questionByIdTimelines = SO.Questions.ById(3146817).Timeline.ToList();
- List<Comment> questionByIdComments = SO.Questions.ById(3146817).Comments.ToList();
- List<Question> questionUnanswered = SO.Questions.Unanswered.ToList();
-
- List<Revision> revision = SO.Revisions.ById(3299997).ToList();
- List<Revision> revisionByGuid =
- SO.Revisions.ById(3299997).ByRevisionGuid(new Guid("68cb0ec1-72f9-44d2-aac9-fa09bb9ce988")).ToList();
- }
-
- [Test]
- public void UsersQueryTest()
- {
- var users = new UsersQuery(SO);
-
- int total = users.Total;
-
- var x = from user in users orderby user.LastAccessDate descending select new { user.DisplayName };
- foreach (var anon in x)
- {
- Console.WriteLine(anon.DisplayName);
- }
- foreach (var user in users)
- {
- user.Badges.ToList().ForEach(b => Console.WriteLine(b.Rank + " " + b.Name));
- }
-
- User skeet = users.First();
- var answerCount = skeet.AnswerCount;
- var answerTotal = skeet.Answers.Total;
-
- // the default query is 1 standard page. this can be
- // tweaked by modifying the default parameter on the query
- // or specifying query parameters
- var queryCount = skeet.Answers.Count(); // will return 0 because no fetch has been made. the collection is empty
-
- foreach (Answer answer in skeet.Answers) // default enumeration will get 30
- {
- Console.WriteLine(answer.Title);
- }
- skeet.Answers.Parameters.PageSize = 100;
-
- foreach (Answer answer in skeet.Answers) // now we will get 100 questions, this will make a new request as the query string is different
- {
- Console.WriteLine(answer.Title);
- }
-
- // a default enumeration will fetch 10 pages. In this case only 9 requests will be made because page 1 is already cached.
- // there may be some issues with some of the arbitrarily ordered results like search - they will need special attention
-
- skeet.Answers.Parameters.PageCount = 10;
-
- foreach (Answer answer in skeet.Answers)
- {
- Console.WriteLine(answer.Title);
- }
-
- // lets repeat the enumeration. there are actually 10 new request being made but they are cached
- // so this is an almost free query.
- foreach (Answer answer in skeet.Answers)
- {
- Console.WriteLine(answer.Title);
- }
-
-
-
- // note: the default behavior is for the Query to accumulate all data requested, so as queries
- // are executed, the results are either added or updated in the list.
-
- // Query is a full list<T> impelemntation, so you may simply clear the list if you like.
-
- foreach (Comment item in skeet.Comments)
- {
- Console.WriteLine("Comments " + item.Body);
- }
- foreach (Comment item in skeet.Comments.ToId(44269))
- {
- Console.WriteLine("Comments commentsTo" + item.Body);
- }
- foreach (Comment item in skeet.Comments.Mentioned)
- {
- Console.WriteLine("Comments mentioned" + item.Body);
- }
- foreach (Answer item in skeet.Answers)
- {
- Console.WriteLine("Answers" + item.Title);
- }
- foreach (Question item in skeet.Favorites)
- {
- Console.WriteLine("Favorites" + item.Title);
- }
- foreach (Question item in skeet.Questions)
- {
- Console.WriteLine("Questions" + item.Title);
- }
- foreach (RepChange item in skeet.RepChanges)
- {
- Console.WriteLine("RepChanges" + item.PositiveRep);
- }
- foreach (Tag item in skeet.Tags)
- {
- Console.WriteLine("Tags" + item.Name);
- }
- foreach (UserTimeline item in skeet.Timeline)
- {
- Console.WriteLine("Timelines" + item.TimelineType);
- }
-
-
-
-
-
- }
-
- [Test]
- public void SiteUsers()
- {
-
- var users = SO.Users;
-
- var first = users.FirstOrDefault();
-
- Assert.IsNotNull(first);
-
- foreach (User user in users) // enumerating a new collection fetches default page
- {
- Console.WriteLine(user.DisplayName);
- }
-
-
- var x = from p in SO.Users orderby p.Reputation select new { p.DisplayName, p.Reputation, Favorites = p.Favorites };
- foreach (var item in x)
- {
- Console.WriteLine("{0} - {1}", item.DisplayName, item.Reputation);
- item.Favorites.ToList().ForEach(f => Console.WriteLine("\t{0}", f.Title));
- }
- }
-
- [Test]
- public void SiteStatistics()
- {
-
- var target = SO.Statistics;
- Assert.AreEqual("1.0", target.ApiVersion.Version);
-
- }
-
- [Test]
- public void Test()
- {
- var target = SO.Badges;
- Assert.IsTrue(target.Count() > 0, "empty badges");
- // check for badge.User
- }
-
- [Test]
- public void User()
- {
- var user = SO.User(242897);
- Assert.IsNotNull(user);
- }
- [Test]
- public void UserAssociated2()
- {
-
- foreach (User item in SO.User(242897).Associated)
- {
- Console.WriteLine("/users/{{id}}/answers {0}", item.DisplayName);
- item.Questions.ToList().ForEach(q => Console.WriteLine(q.Title));
- }
- }
- [Test]
- public void UserAssociated()
- {
- var user = SO.User(242897);
- var target = user.Associated.ToList();
- Assert.IsTrue(target.Count > 0);
-
- // load
- foreach (var other in target)
- {
- Assert.IsFalse(other.IsStub);
- }
-
-
-
- // do a quick gut check - watch duration and http traffic.
- // there should be no delay and no traffic, not even from cache
- var now = DateTime.Now;
- for (int i = 0; i < 10; i++)
- {
- foreach (var other in target)
- {
- Assert.IsFalse(other.IsStub);
- }
- }
-
- Assert.IsTrue(DateTime.Now < now.AddMilliseconds(200));
- }
-
- [Test]
- public void UserBadges()
- {
- var user = SO.User(242897);
-
- // lazy load the badges
- var target = user.Badges;
-
- // i gots me some badges
- Assert.IsTrue(target.Count() > 0);
-
-
- }
-
- [Test]
- public void Tags()
- {
- var user = SO.User(242897);
-
- // lazy load the Tags
- var target = user.Tags;
-
- // i gots me some Tags
- Assert.IsTrue(target.Count() > 0);
-
-
-
- }
-
- [Test]
- public void Questions()
- {
- var user = SO.User(242897);
-
- // lazy load the Favorites
- var target = user.Favorites.WithBody(true);
-
- // i gots me some Favorites
- Assert.IsTrue(target.Count() > 0);
-
- // make sure my Favorites gots lift and body
- Assert.IsNotNull(target.First().Body);
- }
-
- [Test]
- public void info_param()
- {
- // /questions
- SO.Questions
- // answers (optional) When "true", the answers to a question will be returned
- .WithAnswers(true)
- // body (optional) When "true", a post's body will be included in the response.
- .WithBody(true)
- // comments (optional) When "true", any comments on a post will be included in the response.
- .WithComments(true)
- // fromdate (optional) Unix timestamp of the minimum creation date on a returned item.
- .FromDate("january 1, 2001") // can parse dates
- // max (optional) Maximum of the range to include in the response according to the current sort.
- .Max(2)
- // min (optional) Minimum of the range to include in the response according to the current sort.
- .Min(1)
- // order (optional) How the current sort should be ordered.
- .Order(SortOrder.Asc)
- // page (optional) The pagination offset for the current collection. Affected by the specified pagesize.
- .Page(1)
- // pagesize (optional) The number of collection results to display during pagination. Should be between 1 and 100 inclusive.
- .PageSize(100)
- // sort (optional) How a collection should be sorted.
- .Sort(QuestionSort.Votes)
- // tagged (optional) Semicolon delimited list of tags questions must have
- .Tagged("a", "b") // accepts param aary
- // todate (optional) Unix timestamp of the maximum creation date on a returned item.
- .ToDate(new DateTime(2020, 1, 1))
-
- .PageCount(10) // fetch multiple pages .PageCount(-1) to get all pages
- .ToList();
-
-
- }
-
- [Test]
- public void PagingIntro()
- {
- // turn off lazy/eager loading for this demo
- Context
- .Options
- .EagerLoadingEnabled(false)
- .LazyLoadingEnabled(false);
-
- // Soapi elegantly handles multi-page requests.
- // Multi-paged requests are issued in parallel making data retrieval quick
-
- // get the default, 1 page of 30 using default sort
-
- var questions = SO.Questions.ToList();
-
- // get pages 3-4
-
- var pages3to5 = SO.Questions.Page(3).PageCount(2).ToList();
-
- // get all pages of a set
-
- var stackAppsUsers = Context.Official.StackApps.Users.PageCount(-1).ToList();
-
- }
- }
- }