PageRenderTime 54ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/Octokit.Tests/Reactive/ObservableIssuesClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 741 lines | 635 code | 106 blank | 0 comment | 114 complexity | f8aa6d44a842a4a9a360c20daaf8ab9d MD5 | raw file
  1. using NSubstitute;
  2. using Octokit;
  3. using Octokit.Internal;
  4. using Octokit.Reactive;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reactive.Linq;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. public class ObservableIssuesClientTests
  11. {
  12. public class TheGetMethod
  13. {
  14. [Fact]
  15. public void GetsFromClientIssueIssue()
  16. {
  17. var gitHubClient = Substitute.For<IGitHubClient>();
  18. var client = new ObservableIssuesClient(gitHubClient);
  19. client.Get("fake", "repo", 42);
  20. gitHubClient.Issue.Received().Get("fake", "repo", 42);
  21. }
  22. [Fact]
  23. public void GetsFromClientIssueIssueWithRepositoryId()
  24. {
  25. var gitHubClient = Substitute.For<IGitHubClient>();
  26. var client = new ObservableIssuesClient(gitHubClient);
  27. client.Get(1, 42);
  28. gitHubClient.Issue.Received().Get(1, 42);
  29. }
  30. [Fact]
  31. public void EnsuresNonNullArguments()
  32. {
  33. var client = new ObservableIssuesClient(Substitute.For<IGitHubClient>());
  34. Assert.Throws<ArgumentNullException>(() => client.Get(null, "name", 1));
  35. Assert.Throws<ArgumentNullException>(() => client.Get("owner", null, 1));
  36. Assert.Throws<ArgumentException>(() => client.Get("owner", "", 1));
  37. Assert.Throws<ArgumentException>(() => client.Get("", "name", 1));
  38. }
  39. }
  40. public class TheGetAllForRepositoryMethod
  41. {
  42. [Fact]
  43. public void EnsuresNonNullArguments()
  44. {
  45. var client = new ObservableIssuesClient(Substitute.For<IGitHubClient>());
  46. var options = new ApiOptions();
  47. var request = new RepositoryIssueRequest();
  48. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name"));
  49. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
  50. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", options));
  51. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, options));
  52. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (ApiOptions)null));
  53. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", request));
  54. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, request));
  55. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (RepositoryIssueRequest)null));
  56. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", request, options));
  57. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, request, options));
  58. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null, options));
  59. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", request, null));
  60. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, (ApiOptions)null));
  61. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, (RepositoryIssueRequest)null));
  62. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, null, options));
  63. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, request, null));
  64. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name"));
  65. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", ""));
  66. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", options));
  67. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", options));
  68. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", request));
  69. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", request));
  70. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", request, options));
  71. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", request, options));
  72. }
  73. [Fact]
  74. public void RequestsCorrectUrl()
  75. {
  76. var gitHubClient = Substitute.For<IGitHubClient>();
  77. var client = new ObservableIssuesClient(gitHubClient);
  78. client.GetAllForRepository("fake", "repo");
  79. gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/issues"),
  80. Arg.Any<IDictionary<string, string>>(),
  81. "application/vnd.github.squirrel-girl-preview");
  82. }
  83. [Fact]
  84. public void RequestsCorrectUrlWithRepositoryId()
  85. {
  86. var gitHubClient = Substitute.For<IGitHubClient>();
  87. var client = new ObservableIssuesClient(gitHubClient);
  88. client.GetAllForRepository(1);
  89. gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/issues"),
  90. Arg.Any<IDictionary<string, string>>(), null);
  91. }
  92. [Fact]
  93. public void RequestsCorrectUrlWithApiOptions()
  94. {
  95. var gitHubClient = Substitute.For<IGitHubClient>();
  96. var client = new ObservableIssuesClient(gitHubClient);
  97. var options = new ApiOptions
  98. {
  99. PageCount = 1,
  100. StartPage = 1,
  101. PageSize = 1
  102. };
  103. client.GetAllForRepository("fake", "repo", options);
  104. gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/issues"),
  105. Arg.Is<IDictionary<string, string>>(d => d.Count == 6
  106. && d["filter"] == "assigned"
  107. && d["state"] == "open"
  108. && d["sort"] == "created"
  109. && d["direction"] == "desc"
  110. && d["page"] == "1"
  111. && d["per_page"] == "1"),
  112. "application/vnd.github.squirrel-girl-preview");
  113. }
  114. [Fact]
  115. public void RequestsCorrectUrlWithRepositoryIdWithApiOptions()
  116. {
  117. var gitHubClient = Substitute.For<IGitHubClient>();
  118. var client = new ObservableIssuesClient(gitHubClient);
  119. var options = new ApiOptions
  120. {
  121. PageCount = 1,
  122. StartPage = 1,
  123. PageSize = 1
  124. };
  125. client.GetAllForRepository(1, options);
  126. gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/issues"),
  127. Arg.Is<IDictionary<string, string>>(d => d.Count == 6
  128. && d["filter"] == "assigned"
  129. && d["state"] == "open"
  130. && d["sort"] == "created"
  131. && d["direction"] == "desc"
  132. && d["page"] == "1"
  133. && d["per_page"] == "1"), null);
  134. }
  135. [Fact]
  136. public void SendsAppropriateParameters()
  137. {
  138. var gitHubClient = Substitute.For<IGitHubClient>();
  139. var client = new ObservableIssuesClient(gitHubClient);
  140. client.GetAllForRepository("fake", "repo", new RepositoryIssueRequest
  141. {
  142. SortDirection = SortDirection.Ascending
  143. });
  144. gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/issues"),
  145. Arg.Is<IDictionary<string, string>>(d => d.Count == 4
  146. && d["filter"] == "assigned"
  147. && d["state"] == "open"
  148. && d["sort"] == "created"
  149. && d["direction"] == "asc"),
  150. "application/vnd.github.squirrel-girl-preview");
  151. }
  152. [Fact]
  153. public void SendsAppropriateParametersWithRepositoryId()
  154. {
  155. var gitHubClient = Substitute.For<IGitHubClient>();
  156. var client = new ObservableIssuesClient(gitHubClient);
  157. client.GetAllForRepository(1, new RepositoryIssueRequest
  158. {
  159. SortDirection = SortDirection.Ascending
  160. });
  161. gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/issues"),
  162. Arg.Is<IDictionary<string, string>>(d => d.Count == 4
  163. && d["filter"] == "assigned"
  164. && d["state"] == "open"
  165. && d["sort"] == "created"
  166. && d["direction"] == "asc"), null);
  167. }
  168. [Fact]
  169. public void SendsAppropriateParametersWithApiOptions()
  170. {
  171. var gitHubClient = Substitute.For<IGitHubClient>();
  172. var client = new ObservableIssuesClient(gitHubClient);
  173. var options = new ApiOptions
  174. {
  175. PageCount = 1,
  176. StartPage = 1,
  177. PageSize = 1
  178. };
  179. client.GetAllForRepository("fake", "repo", new RepositoryIssueRequest
  180. {
  181. SortDirection = SortDirection.Ascending
  182. }, options);
  183. gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/issues"),
  184. Arg.Is<IDictionary<string, string>>(d => d.Count == 6
  185. && d["filter"] == "assigned"
  186. && d["state"] == "open"
  187. && d["sort"] == "created"
  188. && d["direction"] == "asc"
  189. && d["page"] == "1"
  190. && d["per_page"] == "1"),
  191. "application/vnd.github.squirrel-girl-preview");
  192. }
  193. [Fact]
  194. public void SendsAppropriateParametersWithRepositoryIdWithApiOptions()
  195. {
  196. var gitHubClient = Substitute.For<IGitHubClient>();
  197. var client = new ObservableIssuesClient(gitHubClient);
  198. var options = new ApiOptions
  199. {
  200. PageCount = 1,
  201. StartPage = 1,
  202. PageSize = 1
  203. };
  204. client.GetAllForRepository(1, new RepositoryIssueRequest
  205. {
  206. SortDirection = SortDirection.Ascending
  207. }, options);
  208. gitHubClient.Connection.Received().Get<List<Issue>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/issues"),
  209. Arg.Is<IDictionary<string, string>>(d => d.Count == 6
  210. && d["filter"] == "assigned"
  211. && d["state"] == "open"
  212. && d["sort"] == "created"
  213. && d["direction"] == "asc"
  214. && d["page"] == "1"
  215. && d["per_page"] == "1"),
  216. null);
  217. }
  218. [Fact]
  219. public async Task ReturnsEveryPageOfIssues()
  220. {
  221. var firstPageUrl = new Uri("repos/fake/repo/issues", UriKind.Relative);
  222. var secondPageUrl = new Uri("https://example.com/page/2");
  223. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  224. var firstPageResponse = new ApiResponse<List<Issue>>
  225. (
  226. CreateResponseWithApiInfo(firstPageLinks),
  227. new List<Issue>
  228. {
  229. CreateIssue(1),
  230. CreateIssue(2),
  231. CreateIssue(3)
  232. }
  233. );
  234. var thirdPageUrl = new Uri("https://example.com/page/3");
  235. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  236. var secondPageResponse = new ApiResponse<List<Issue>>
  237. (
  238. CreateResponseWithApiInfo(secondPageLinks),
  239. new List<Issue>
  240. {
  241. CreateIssue(4),
  242. CreateIssue(5),
  243. CreateIssue(6)
  244. }
  245. );
  246. var lastPageResponse = new ApiResponse<List<Issue>>
  247. (
  248. new Response(),
  249. new List<Issue>
  250. {
  251. CreateIssue(7)
  252. }
  253. );
  254. var gitHubClient = Substitute.For<IGitHubClient>();
  255. gitHubClient.Connection.Get<List<Issue>>(Arg.Is(firstPageUrl),
  256. Arg.Is<Dictionary<string, string>>(d => d.Count == 4
  257. && d["direction"] == "desc"
  258. && d["state"] == "open"
  259. && d["sort"] == "created"
  260. && d["filter"] == "assigned"), Arg.Any<string>())
  261. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => firstPageResponse));
  262. gitHubClient.Connection.Get<List<Issue>>(secondPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
  263. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => secondPageResponse));
  264. gitHubClient.Connection.Get<List<Issue>>(thirdPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
  265. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => lastPageResponse));
  266. var client = new ObservableIssuesClient(gitHubClient);
  267. var results = await client.GetAllForRepository("fake", "repo").ToArray();
  268. Assert.Equal(7, results.Length);
  269. Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
  270. Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
  271. Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
  272. }
  273. }
  274. public class TheGetAllForOwnedAndMemberRepositoriesMethod
  275. {
  276. [Fact]
  277. public void EnsuresNonNullArguments()
  278. {
  279. var client = new ObservableIssuesClient(Substitute.For<IGitHubClient>());
  280. Assert.Throws<ArgumentNullException>(
  281. () => client.GetAllForOwnedAndMemberRepositories((ApiOptions)null));
  282. Assert.Throws<ArgumentNullException>(
  283. () => client.GetAllForOwnedAndMemberRepositories((IssueRequest)null));
  284. Assert.Throws<ArgumentNullException>(
  285. () => client.GetAllForOwnedAndMemberRepositories(null, new ApiOptions()));
  286. Assert.Throws<ArgumentNullException>(
  287. () => client.GetAllForOwnedAndMemberRepositories(new IssueRequest(), null));
  288. }
  289. [Fact]
  290. public async Task ReturnsEveryPageOfIssues()
  291. {
  292. var firstPageUrl = new Uri("user/issues", UriKind.Relative);
  293. var secondPageUrl = new Uri("https://example.com/page/2");
  294. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  295. var firstPageResponse = new ApiResponse<List<Issue>>
  296. (
  297. CreateResponseWithApiInfo(firstPageLinks),
  298. new List<Issue>
  299. {
  300. CreateIssue(1),
  301. CreateIssue(2),
  302. CreateIssue(3)
  303. }
  304. );
  305. var thirdPageUrl = new Uri("https://example.com/page/3");
  306. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  307. var secondPageResponse = new ApiResponse<List<Issue>>
  308. (
  309. CreateResponseWithApiInfo(secondPageLinks),
  310. new List<Issue>
  311. {
  312. CreateIssue(4),
  313. CreateIssue(5),
  314. CreateIssue(6)
  315. }
  316. );
  317. var lastPageResponse = new ApiResponse<List<Issue>>
  318. (
  319. new Response(),
  320. new List<Issue>
  321. {
  322. CreateIssue(7)
  323. }
  324. );
  325. var gitHubClient = Substitute.For<IGitHubClient>();
  326. gitHubClient.Connection.Get<List<Issue>>(Arg.Is(firstPageUrl),
  327. Arg.Is<Dictionary<string, string>>(d => d.Count == 4
  328. && d["direction"] == "desc"
  329. && d["state"] == "open"
  330. && d["sort"] == "created"
  331. && d["filter"] == "assigned"),
  332. "application/vnd.github.squirrel-girl-preview")
  333. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => firstPageResponse));
  334. gitHubClient.Connection.Get<List<Issue>>(secondPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
  335. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => secondPageResponse));
  336. gitHubClient.Connection.Get<List<Issue>>(thirdPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
  337. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => lastPageResponse));
  338. var client = new ObservableIssuesClient(gitHubClient);
  339. var results = await client.GetAllForOwnedAndMemberRepositories().ToArray();
  340. Assert.Equal(7, results.Length);
  341. Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
  342. Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
  343. Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
  344. }
  345. }
  346. public class TheGetAllForOrganizationMethod
  347. {
  348. [Fact]
  349. public void EnsuresNonNullArguments()
  350. {
  351. var client = new ObservableIssuesClient(Substitute.For<IGitHubClient>());
  352. var options = new ApiOptions();
  353. var request = new RepositoryIssueRequest();
  354. Assert.Throws<ArgumentNullException>(() => client.GetAllForOrganization(null));
  355. Assert.Throws<ArgumentNullException>(() => client.GetAllForOrganization(null, options));
  356. Assert.Throws<ArgumentNullException>(() => client.GetAllForOrganization(null, request));
  357. Assert.Throws<ArgumentNullException>(() => client.GetAllForOrganization(null, request, options));
  358. Assert.Throws<ArgumentException>(() => client.GetAllForOrganization(""));
  359. Assert.Throws<ArgumentException>(() => client.GetAllForOrganization("", options));
  360. Assert.Throws<ArgumentException>(() => client.GetAllForOrganization("", request));
  361. Assert.Throws<ArgumentException>(() => client.GetAllForOrganization("", request, options));
  362. Assert.Throws<ArgumentNullException>(() => client.GetAllForOrganization("org", (ApiOptions)null));
  363. Assert.Throws<ArgumentNullException>(() => client.GetAllForOrganization("org", (IssueRequest)null));
  364. Assert.Throws<ArgumentNullException>(() => client.GetAllForOrganization("org", null, options));
  365. Assert.Throws<ArgumentNullException>(() => client.GetAllForOrganization("org", request, null));
  366. }
  367. [Fact]
  368. public async Task ReturnsEveryPageOfIssues()
  369. {
  370. var firstPageUrl = new Uri("orgs/test/issues", UriKind.Relative);
  371. var secondPageUrl = new Uri("https://example.com/page/2");
  372. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  373. var firstPageResponse = new ApiResponse<List<Issue>>
  374. (
  375. CreateResponseWithApiInfo(firstPageLinks),
  376. new List<Issue>
  377. {
  378. CreateIssue(1),
  379. CreateIssue(2),
  380. CreateIssue(3)
  381. }
  382. );
  383. var thirdPageUrl = new Uri("https://example.com/page/3");
  384. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  385. var secondPageResponse = new ApiResponse<List<Issue>>
  386. (
  387. CreateResponseWithApiInfo(secondPageLinks),
  388. new List<Issue>
  389. {
  390. CreateIssue(4),
  391. CreateIssue(5),
  392. CreateIssue(6)
  393. }
  394. );
  395. var lastPageResponse = new ApiResponse<List<Issue>>
  396. (
  397. new Response(),
  398. new List<Issue>
  399. {
  400. CreateIssue(7)
  401. }
  402. );
  403. var gitHubClient = Substitute.For<IGitHubClient>();
  404. gitHubClient.Connection.Get<List<Issue>>(Arg.Is(firstPageUrl),
  405. Arg.Is<Dictionary<string, string>>(d => d.Count == 4
  406. && d["direction"] == "desc"
  407. && d["state"] == "open"
  408. && d["sort"] == "created"
  409. && d["filter"] == "assigned"), "application/vnd.github.squirrel-girl-preview")
  410. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => firstPageResponse));
  411. gitHubClient.Connection.Get<List<Issue>>(secondPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
  412. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => secondPageResponse));
  413. gitHubClient.Connection.Get<List<Issue>>(thirdPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
  414. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => lastPageResponse));
  415. var client = new ObservableIssuesClient(gitHubClient);
  416. var results = await client.GetAllForOrganization("test").ToArray();
  417. Assert.Equal(7, results.Length);
  418. Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
  419. Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
  420. Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
  421. }
  422. }
  423. public class TheGetAllForCurrentMethod
  424. {
  425. [Fact]
  426. public void EnsuresNonNullArguments()
  427. {
  428. var client = new ObservableIssuesClient(Substitute.For<IGitHubClient>());
  429. Assert.Throws<ArgumentNullException>(() => client.GetAllForCurrent((ApiOptions)null));
  430. Assert.Throws<ArgumentNullException>(() => client.GetAllForCurrent((IssueRequest)null));
  431. Assert.Throws<ArgumentNullException>(() => client.GetAllForCurrent(null, new ApiOptions()));
  432. Assert.Throws<ArgumentNullException>(() => client.GetAllForCurrent(new IssueRequest(), null));
  433. }
  434. [Fact]
  435. public async Task ReturnsEveryPageOfIssues()
  436. {
  437. var firstPageUrl = new Uri("issues", UriKind.Relative);
  438. var secondPageUrl = new Uri("https://example.com/page/2");
  439. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  440. var firstPageResponse = new ApiResponse<List<Issue>>
  441. (
  442. CreateResponseWithApiInfo(firstPageLinks),
  443. new List<Issue>
  444. {
  445. CreateIssue(1),
  446. CreateIssue(2),
  447. CreateIssue(3)
  448. }
  449. );
  450. var thirdPageUrl = new Uri("https://example.com/page/3");
  451. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  452. var secondPageResponse = new ApiResponse<List<Issue>>
  453. (
  454. CreateResponseWithApiInfo(secondPageLinks),
  455. new List<Issue>
  456. {
  457. CreateIssue(4),
  458. CreateIssue(5),
  459. CreateIssue(6)
  460. }
  461. );
  462. var lastPageResponse = new ApiResponse<List<Issue>>
  463. (
  464. new Response(),
  465. new List<Issue>
  466. {
  467. CreateIssue(7)
  468. }
  469. );
  470. var gitHubClient = Substitute.For<IGitHubClient>();
  471. gitHubClient.Connection.Get<List<Issue>>(Arg.Is(firstPageUrl),
  472. Arg.Is<Dictionary<string, string>>(d => d.Count == 4
  473. && d["direction"] == "desc"
  474. && d["state"] == "open"
  475. && d["sort"] == "created"
  476. && d["filter"] == "assigned"), "application/vnd.github.squirrel-girl-preview")
  477. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => firstPageResponse));
  478. gitHubClient.Connection.Get<List<Issue>>(secondPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
  479. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => secondPageResponse));
  480. gitHubClient.Connection.Get<List<Issue>>(thirdPageUrl, Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview")
  481. .Returns(Task.Factory.StartNew<IApiResponse<List<Issue>>>(() => lastPageResponse));
  482. var client = new ObservableIssuesClient(gitHubClient);
  483. var results = await client.GetAllForCurrent().ToArray();
  484. Assert.Equal(7, results.Length);
  485. Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
  486. Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
  487. Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
  488. }
  489. }
  490. public class TheCreateMethod
  491. {
  492. [Fact]
  493. public void CreatesFromClientIssueIssue()
  494. {
  495. var newIssue = new NewIssue("some title");
  496. var gitHubClient = Substitute.For<IGitHubClient>();
  497. var client = new ObservableIssuesClient(gitHubClient);
  498. client.Create("fake", "repo", newIssue);
  499. gitHubClient.Issue.Received().Create("fake", "repo", newIssue);
  500. }
  501. [Fact]
  502. public void CreatesFromClientIssueIssueWithRepositoryId()
  503. {
  504. var newIssue = new NewIssue("some title");
  505. var gitHubClient = Substitute.For<IGitHubClient>();
  506. var client = new ObservableIssuesClient(gitHubClient);
  507. client.Create(1, newIssue);
  508. gitHubClient.Issue.Received().Create(1, newIssue);
  509. }
  510. [Fact]
  511. public void EnsuresNonNullArguments()
  512. {
  513. var gitHubClient = Substitute.For<IGitHubClient>();
  514. var client = new ObservableIssuesClient(gitHubClient);
  515. Assert.Throws<ArgumentNullException>(() => client.Create(null, "name", new NewIssue("x")));
  516. Assert.Throws<ArgumentNullException>(() => client.Create("owner", null, new NewIssue("x")));
  517. Assert.Throws<ArgumentNullException>(() => client.Create("owner", "name", null));
  518. Assert.Throws<ArgumentNullException>(() => client.Create(1, null));
  519. Assert.Throws<ArgumentException>(() => client.Create("", "name", new NewIssue("x")));
  520. Assert.Throws<ArgumentException>(() => client.Create("owner", "", new NewIssue("x")));
  521. }
  522. }
  523. public class TheUpdateMethod
  524. {
  525. [Fact]
  526. public void UpdatesClientIssueIssue()
  527. {
  528. var issueUpdate = new IssueUpdate();
  529. var gitHubClient = Substitute.For<IGitHubClient>();
  530. var client = new ObservableIssuesClient(gitHubClient);
  531. client.Update("fake", "repo", 42, issueUpdate);
  532. gitHubClient.Issue.Received().Update("fake", "repo", 42, issueUpdate);
  533. }
  534. [Fact]
  535. public void UpdatesClientIssueIssueWithRepositoryId()
  536. {
  537. var issueUpdate = new IssueUpdate();
  538. var gitHubClient = Substitute.For<IGitHubClient>();
  539. var client = new ObservableIssuesClient(gitHubClient);
  540. client.Update(1, 42, issueUpdate);
  541. gitHubClient.Issue.Received().Update(1, 42, issueUpdate);
  542. }
  543. [Fact]
  544. public void EnsuresNonNullArguments()
  545. {
  546. var gitHubClient = Substitute.For<IGitHubClient>();
  547. var client = new ObservableIssuesClient(gitHubClient);
  548. Assert.Throws<ArgumentNullException>(() => client.Update(null, "name", 42, new IssueUpdate()));
  549. Assert.Throws<ArgumentNullException>(() => client.Update("owner", null, 42, new IssueUpdate()));
  550. Assert.Throws<ArgumentNullException>(() => client.Update("owner", "name", 42, null));
  551. Assert.Throws<ArgumentNullException>(() => client.Update(1, 42, null));
  552. Assert.Throws<ArgumentException>(() => client.Update("", "name", 42, new IssueUpdate()));
  553. Assert.Throws<ArgumentException>(() => client.Update("owner", "", 42, new IssueUpdate()));
  554. }
  555. }
  556. public class TheLockMethod
  557. {
  558. [Fact]
  559. public void LocksIssue()
  560. {
  561. var gitHubClient = Substitute.For<IGitHubClient>();
  562. var client = new ObservableIssuesClient(gitHubClient);
  563. client.Lock("fake", "repo", 42);
  564. gitHubClient.Issue.Received().Lock("fake", "repo", 42);
  565. }
  566. [Fact]
  567. public void LocksIssueWithRepositoryId()
  568. {
  569. var gitHubClient = Substitute.For<IGitHubClient>();
  570. var client = new ObservableIssuesClient(gitHubClient);
  571. client.Lock(1, 42);
  572. gitHubClient.Issue.Received().Lock(1, 42);
  573. }
  574. [Fact]
  575. public void EnsuresNonNullArguments()
  576. {
  577. var gitHubClient = Substitute.For<IGitHubClient>();
  578. var client = new ObservableIssuesClient(gitHubClient);
  579. Assert.Throws<ArgumentNullException>(() => client.Lock(null, "name", 42));
  580. Assert.Throws<ArgumentNullException>(() => client.Lock("owner", null, 42));
  581. Assert.Throws<ArgumentException>(() => client.Lock("", "name", 42));
  582. Assert.Throws<ArgumentException>(() => client.Lock("owner", "", 42));
  583. }
  584. }
  585. public class TheUnlockMethod
  586. {
  587. [Fact]
  588. public void UnlocksIssue()
  589. {
  590. var gitHubClient = Substitute.For<IGitHubClient>();
  591. var client = new ObservableIssuesClient(gitHubClient);
  592. client.Unlock("fake", "repo", 42);
  593. gitHubClient.Issue.Received().Unlock("fake", "repo", 42);
  594. }
  595. [Fact]
  596. public void UnlocksIssueWithRepositoryId()
  597. {
  598. var gitHubClient = Substitute.For<IGitHubClient>();
  599. var client = new ObservableIssuesClient(gitHubClient);
  600. client.Unlock(1, 42);
  601. gitHubClient.Issue.Received().Unlock(1, 42);
  602. }
  603. [Fact]
  604. public void EnsuresNonNullArguments()
  605. {
  606. var gitHubClient = Substitute.For<IGitHubClient>();
  607. var client = new ObservableIssuesClient(gitHubClient);
  608. Assert.Throws<ArgumentNullException>(() => client.Unlock(null, "name", 42));
  609. Assert.Throws<ArgumentNullException>(() => client.Unlock("owner", null, 42));
  610. Assert.Throws<ArgumentException>(() => client.Unlock("", "name", 42));
  611. Assert.Throws<ArgumentException>(() => client.Unlock("owner", "", 42));
  612. }
  613. }
  614. public class TheCtor
  615. {
  616. [Fact]
  617. public void EnsuresNonNullArguments()
  618. {
  619. Assert.Throws<ArgumentNullException>(() => new IssuesClient(null));
  620. }
  621. }
  622. static Issue CreateIssue(int issueNumber)
  623. {
  624. var serializer = new SimpleJsonSerializer();
  625. return serializer.Deserialize<Issue>(@"{""number"": """ + issueNumber + @"""}");
  626. }
  627. static IResponse CreateResponseWithApiInfo(IDictionary<string, Uri> links)
  628. {
  629. var apiInfo = new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>()));
  630. var response = Substitute.For<IResponse>();
  631. response.ApiInfo.Returns(apiInfo);
  632. return response;
  633. }
  634. }