PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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