PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Octokit.Tests/Reactive/ObservableMilestonesClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 471 lines | 386 code | 85 blank | 0 comment | 21 complexity | d4ab9ac6d0d16a5a68fcbf6edaef8826 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Linq;
  4. using System.Reactive.Threading.Tasks;
  5. using System.Threading.Tasks;
  6. using NSubstitute;
  7. using Octokit.Internal;
  8. using Octokit.Reactive;
  9. using Xunit;
  10. namespace Octokit.Tests.Reactive
  11. {
  12. public class ObservableMilestonesClientTests
  13. {
  14. public class TheGetMethod
  15. {
  16. [Fact]
  17. public void GetsFromClientIssueMilestone()
  18. {
  19. var gitHubClient = Substitute.For<IGitHubClient>();
  20. var client = new ObservableMilestonesClient(gitHubClient);
  21. client.Get("fake", "repo", 42);
  22. gitHubClient.Issue.Milestone.Received().Get("fake", "repo", 42);
  23. }
  24. [Fact]
  25. public void GetsFromClientIssueMilestoneWithRepositoryId()
  26. {
  27. var gitHubClient = Substitute.For<IGitHubClient>();
  28. var client = new ObservableMilestonesClient(gitHubClient);
  29. client.Get(1, 42);
  30. gitHubClient.Issue.Milestone.Received().Get(1, 42);
  31. }
  32. [Fact]
  33. public async Task EnsuresNonNullArguments()
  34. {
  35. var client = new ObservableMilestonesClient(Substitute.For<IGitHubClient>());
  36. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get(null, "name", 1).ToTask());
  37. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get("owner", null, 1).ToTask());
  38. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get(null, "", 1).ToTask());
  39. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("", null, 1).ToTask());
  40. }
  41. }
  42. public class TheGetForRepositoryMethod
  43. {
  44. [Fact]
  45. public void RequestsCorrectUrl()
  46. {
  47. var gitHubClient = Substitute.For<IGitHubClient>();
  48. var client = new ObservableMilestonesClient(gitHubClient);
  49. client.GetAllForRepository("fake", "repo");
  50. gitHubClient.Received().Issue.Milestone.GetAllForRepository("fake", "repo");
  51. }
  52. [Fact]
  53. public void RequestsCorrectUrlWithRepositoryId()
  54. {
  55. var gitHubClient = Substitute.For<IGitHubClient>();
  56. var client = new ObservableMilestonesClient(gitHubClient);
  57. client.GetAllForRepository(1);
  58. gitHubClient.Received().Issue.Milestone.GetAllForRepository(1);
  59. }
  60. [Fact]
  61. public void RequestsCorrectUrlWithApiOptions()
  62. {
  63. var gitHubClient = Substitute.For<IGitHubClient>();
  64. var client = new ObservableMilestonesClient(gitHubClient);
  65. var options = new ApiOptions
  66. {
  67. PageCount = 1,
  68. PageSize = 1,
  69. StartPage = 1
  70. };
  71. client.GetAllForRepository("fake", "repo", options);
  72. gitHubClient.Received().Issue.Milestone.GetAllForRepository("fake", "repo", options);
  73. }
  74. [Fact]
  75. public void RequestsCorrectUrlWithApiOptionsWithRepositoryId()
  76. {
  77. var gitHubClient = Substitute.For<IGitHubClient>();
  78. var client = new ObservableMilestonesClient(gitHubClient);
  79. var options = new ApiOptions
  80. {
  81. PageCount = 1,
  82. PageSize = 1,
  83. StartPage = 1
  84. };
  85. client.GetAllForRepository(1, options);
  86. gitHubClient.Received().Issue.Milestone.GetAllForRepository(1, options);
  87. }
  88. [Fact]
  89. public void SendsAppropriateParameters()
  90. {
  91. var gitHubClient = Substitute.For<IGitHubClient>();
  92. var client = new ObservableMilestonesClient(gitHubClient);
  93. var milestoneRequest = new MilestoneRequest { SortDirection = SortDirection.Descending };
  94. client.GetAllForRepository("fake", "repo", milestoneRequest);
  95. gitHubClient.Received().Issue.Milestone.GetAllForRepository("fake", "repo", milestoneRequest, Args.ApiOptions);
  96. }
  97. [Fact]
  98. public void SendsAppropriateParametersWithRepositoryId()
  99. {
  100. var gitHubClient = Substitute.For<IGitHubClient>();
  101. var client = new ObservableMilestonesClient(gitHubClient);
  102. var milestoneRequest = new MilestoneRequest { SortDirection = SortDirection.Descending };
  103. client.GetAllForRepository(1, milestoneRequest);
  104. gitHubClient.Received().Issue.Milestone.GetAllForRepository(1, milestoneRequest, Args.ApiOptions);
  105. }
  106. [Fact]
  107. public void SendsAppropriateParametersWithApiOptions()
  108. {
  109. var gitHubClient = Substitute.For<IGitHubClient>();
  110. var client = new ObservableMilestonesClient(gitHubClient);
  111. var options = new ApiOptions
  112. {
  113. PageCount = 1,
  114. PageSize = 1,
  115. StartPage = 1
  116. };
  117. var milestoneRequest = new MilestoneRequest { SortDirection = SortDirection.Descending };
  118. client.GetAllForRepository("fake", "repo", milestoneRequest, options);
  119. gitHubClient.Received().Issue.Milestone.GetAllForRepository("fake", "repo", milestoneRequest, options);
  120. }
  121. [Fact]
  122. public void SendsAppropriateParametersWithApiOptionsWithRepositoryId()
  123. {
  124. var gitHubClient = Substitute.For<IGitHubClient>();
  125. var client = new ObservableMilestonesClient(gitHubClient);
  126. var options = new ApiOptions
  127. {
  128. PageCount = 1,
  129. PageSize = 1,
  130. StartPage = 1
  131. };
  132. var milestoneRequest = new MilestoneRequest { SortDirection = SortDirection.Descending };
  133. client.GetAllForRepository(1, milestoneRequest, options);
  134. gitHubClient.Received().Issue.Milestone.GetAllForRepository(1, milestoneRequest, options);
  135. }
  136. [Fact]
  137. public void EnsuresNonNullArguments()
  138. {
  139. var client = new ObservableMilestonesClient(Substitute.For<IGitHubClient>());
  140. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name"));
  141. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
  142. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", ApiOptions.None));
  143. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, ApiOptions.None));
  144. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (ApiOptions)null));
  145. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", new MilestoneRequest()));
  146. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, new MilestoneRequest()));
  147. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (MilestoneRequest)null));
  148. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", new MilestoneRequest(), ApiOptions.None));
  149. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, new MilestoneRequest(), ApiOptions.None));
  150. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null, ApiOptions.None));
  151. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", new MilestoneRequest(), null));
  152. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, (ApiOptions)null));
  153. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, (MilestoneRequest)null));
  154. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, null, ApiOptions.None));
  155. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, new MilestoneRequest(), null));
  156. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name"));
  157. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", ""));
  158. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", ApiOptions.None));
  159. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", ApiOptions.None));
  160. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", new MilestoneRequest()));
  161. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", new MilestoneRequest()));
  162. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", new MilestoneRequest(), ApiOptions.None));
  163. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", new MilestoneRequest(), ApiOptions.None));
  164. }
  165. [Fact]
  166. public async Task ReturnsEveryPageOfMilestones()
  167. {
  168. var firstPageUrl = new Uri("repos/fake/repo/milestones", UriKind.Relative);
  169. var secondPageUrl = new Uri("https://example.com/page/2");
  170. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  171. var firstPageResponse = new ApiResponse<List<Milestone>>
  172. (
  173. CreateResponseWithApiInfo(firstPageLinks),
  174. new List<Milestone>
  175. {
  176. new Milestone(1),
  177. new Milestone(2),
  178. new Milestone(3)
  179. }
  180. );
  181. var thirdPageUrl = new Uri("https://example.com/page/3");
  182. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  183. var secondPageResponse = new ApiResponse<List<Milestone>>
  184. (
  185. CreateResponseWithApiInfo(secondPageLinks),
  186. new List<Milestone>
  187. {
  188. new Milestone(4),
  189. new Milestone(5),
  190. new Milestone(6)
  191. }
  192. );
  193. var lastPageResponse = new ApiResponse<List<Milestone>>
  194. (
  195. new Response(),
  196. new List<Milestone>
  197. {
  198. new Milestone(7)
  199. }
  200. );
  201. var gitHubClient = Substitute.For<IGitHubClient>();
  202. gitHubClient.Connection.Get<List<Milestone>>(firstPageUrl, Args.EmptyDictionary, null)
  203. .Returns(Task.Factory.StartNew<IApiResponse<List<Milestone>>>(() => firstPageResponse));
  204. gitHubClient.Connection.Get<List<Milestone>>(secondPageUrl, Args.EmptyDictionary, null)
  205. .Returns(Task.Factory.StartNew<IApiResponse<List<Milestone>>>(() => secondPageResponse));
  206. gitHubClient.Connection.Get<List<Milestone>>(thirdPageUrl, Args.EmptyDictionary, null)
  207. .Returns(Task.Factory.StartNew<IApiResponse<List<Milestone>>>(() => lastPageResponse));
  208. var client = new ObservableMilestonesClient(gitHubClient);
  209. var results = await client.GetAllForRepository("fake", "repo").ToArray();
  210. Assert.Equal(7, results.Length);
  211. Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
  212. Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
  213. Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
  214. }
  215. [Fact]
  216. public async Task SendsAppropriateParametersMulti()
  217. {
  218. var firstPageUrl = new Uri("repos/fake/repo/milestones", UriKind.Relative);
  219. var secondPageUrl = new Uri("https://example.com/page/2");
  220. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  221. var firstPageResponse = new ApiResponse<List<Milestone>>
  222. (
  223. CreateResponseWithApiInfo(firstPageLinks),
  224. new List<Milestone>
  225. {
  226. new Milestone(1),
  227. new Milestone(2),
  228. new Milestone(3)
  229. }
  230. );
  231. var thirdPageUrl = new Uri("https://example.com/page/3");
  232. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  233. var secondPageResponse = new ApiResponse<List<Milestone>>
  234. (
  235. CreateResponseWithApiInfo(secondPageLinks),
  236. new List<Milestone>
  237. {
  238. new Milestone(4),
  239. new Milestone(5),
  240. new Milestone(6)
  241. }
  242. );
  243. var lastPageResponse = new ApiResponse<List<Milestone>>
  244. (
  245. new Response { ApiInfo = new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>())) },
  246. new List<Milestone>
  247. {
  248. new Milestone(7)
  249. }
  250. );
  251. var gitHubClient = Substitute.For<IGitHubClient>();
  252. gitHubClient.Connection.Get<List<Milestone>>(Arg.Is(firstPageUrl),
  253. Arg.Is<Dictionary<string, string>>(d => d.Count == 3
  254. && d["direction"] == "desc"
  255. && d["state"] == "open"
  256. && d["sort"] == "due_date"), null)
  257. .Returns(Task.Factory.StartNew<IApiResponse<List<Milestone>>>(() => firstPageResponse));
  258. gitHubClient.Connection.Get<List<Milestone>>(secondPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 3
  259. && d["direction"] == "desc"
  260. && d["state"] == "open"
  261. && d["sort"] == "due_date"), null)
  262. .Returns(Task.Factory.StartNew<IApiResponse<List<Milestone>>>(() => secondPageResponse));
  263. gitHubClient.Connection.Get<List<Milestone>>(thirdPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 3
  264. && d["direction"] == "desc"
  265. && d["state"] == "open"
  266. && d["sort"] == "due_date"), null)
  267. .Returns(Task.Factory.StartNew<IApiResponse<List<Milestone>>>(() => lastPageResponse));
  268. var client = new ObservableMilestonesClient(gitHubClient);
  269. var results = await client.GetAllForRepository("fake", "repo", new MilestoneRequest { SortDirection = SortDirection.Descending }).ToArray();
  270. Assert.Equal(7, results.Length);
  271. Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
  272. Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
  273. Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
  274. }
  275. }
  276. public class TheCreateMethod
  277. {
  278. [Fact]
  279. public void CreatesFromClientIssueMilestone()
  280. {
  281. var newMilestone = new NewMilestone("some title");
  282. var gitHubClient = Substitute.For<IGitHubClient>();
  283. var client = new ObservableMilestonesClient(gitHubClient);
  284. client.Create("fake", "repo", newMilestone);
  285. gitHubClient.Issue.Milestone.Received().Create("fake", "repo", newMilestone);
  286. }
  287. [Fact]
  288. public void CreatesFromClientIssueMilestoneWithRepositoryId()
  289. {
  290. var newMilestone = new NewMilestone("some title");
  291. var gitHubClient = Substitute.For<IGitHubClient>();
  292. var client = new ObservableMilestonesClient(gitHubClient);
  293. client.Create(1, newMilestone);
  294. gitHubClient.Issue.Milestone.Received().Create(1, newMilestone);
  295. }
  296. [Fact]
  297. public void EnsuresNonNullArguments()
  298. {
  299. var gitHubClient = Substitute.For<IGitHubClient>();
  300. var client = new ObservableMilestonesClient(gitHubClient);
  301. Assert.Throws<ArgumentNullException>(() => client.Create(null, "name", new NewMilestone("x")));
  302. Assert.Throws<ArgumentNullException>(() => client.Create("owner", null, new NewMilestone("x")));
  303. Assert.Throws<ArgumentNullException>(() => client.Create("owner", "name", null));
  304. Assert.Throws<ArgumentNullException>(() => client.Create(1, null));
  305. Assert.Throws<ArgumentException>(() => client.Create("", "name", new NewMilestone("x")));
  306. Assert.Throws<ArgumentException>(() => client.Create("owner", "", new NewMilestone("x")));
  307. }
  308. }
  309. public class TheUpdateMethod
  310. {
  311. [Fact]
  312. public void UpdatesClientIssueMilestone()
  313. {
  314. var milestoneUpdate = new MilestoneUpdate();
  315. var gitHubClient = Substitute.For<IGitHubClient>();
  316. var client = new ObservableMilestonesClient(gitHubClient);
  317. client.Update("fake", "repo", 42, milestoneUpdate);
  318. gitHubClient.Issue.Milestone.Received().Update("fake", "repo", 42, milestoneUpdate);
  319. }
  320. [Fact]
  321. public void UpdatesClientIssueMilestoneWithRepositoryId()
  322. {
  323. var milestoneUpdate = new MilestoneUpdate();
  324. var gitHubClient = Substitute.For<IGitHubClient>();
  325. var client = new ObservableMilestonesClient(gitHubClient);
  326. client.Update(1, 42, milestoneUpdate);
  327. gitHubClient.Issue.Milestone.Received().Update(1, 42, milestoneUpdate);
  328. }
  329. [Fact]
  330. public void EnsuresNonNullArguments()
  331. {
  332. var gitHubClient = Substitute.For<IGitHubClient>();
  333. var client = new ObservableMilestonesClient(gitHubClient);
  334. Assert.Throws<ArgumentNullException>(() => client.Update(null, "name", 42, new MilestoneUpdate()));
  335. Assert.Throws<ArgumentNullException>(() => client.Update("owner", null, 42, new MilestoneUpdate()));
  336. Assert.Throws<ArgumentNullException>(() => client.Update("owner", "name", 42, null));
  337. Assert.Throws<ArgumentNullException>(() => client.Update(1, 42, null));
  338. Assert.Throws<ArgumentException>(() => client.Update("", "name", 42, new MilestoneUpdate()));
  339. Assert.Throws<ArgumentException>(() => client.Update("owner", "", 42, new MilestoneUpdate()));
  340. }
  341. }
  342. public class TheDeleteMethod
  343. {
  344. [Fact]
  345. public void DeletesFromClientIssueMilestone()
  346. {
  347. var gitHubClient = Substitute.For<IGitHubClient>();
  348. var client = new ObservableMilestonesClient(gitHubClient);
  349. client.Delete("fake", "repo", 42);
  350. gitHubClient.Issue.Milestone.Received().Delete("fake", "repo", 42);
  351. }
  352. [Fact]
  353. public void DeletesFromClientIssueMilestoneWithRepositoryId()
  354. {
  355. var gitHubClient = Substitute.For<IGitHubClient>();
  356. var client = new ObservableMilestonesClient(gitHubClient);
  357. client.Delete(1, 42);
  358. gitHubClient.Issue.Milestone.Received().Delete(1, 42);
  359. }
  360. [Fact]
  361. public void EnsuresNonNullArguments()
  362. {
  363. var gitHubClient = Substitute.For<IGitHubClient>();
  364. var client = new ObservableMilestonesClient(gitHubClient);
  365. Assert.Throws<ArgumentNullException>(() => client.Delete(null, "name", 42));
  366. Assert.Throws<ArgumentNullException>(() => client.Delete("owner", null, 42));
  367. Assert.Throws<ArgumentException>(() => client.Delete("", "name", 42));
  368. Assert.Throws<ArgumentException>(() => client.Delete("owner", "", 42));
  369. }
  370. }
  371. public class TheCtor
  372. {
  373. [Fact]
  374. public void EnsuresNonNullArguments()
  375. {
  376. Assert.Throws<ArgumentNullException>(() => new ObservableMilestonesClient(null));
  377. }
  378. }
  379. static IResponse CreateResponseWithApiInfo(IDictionary<string, Uri> links)
  380. {
  381. var response = Substitute.For<IResponse>();
  382. response.ApiInfo.Returns(new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>())));
  383. return response;
  384. }
  385. }
  386. }