PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 982 lines | 796 code | 180 blank | 6 comment | 56 complexity | 144b711e08170f88b394512de199befb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Linq;
  4. using System.Threading.Tasks;
  5. using NSubstitute;
  6. using Octokit.Internal;
  7. using Octokit.Reactive;
  8. using Xunit;
  9. namespace Octokit.Tests.Reactive
  10. {
  11. public class ObservableRepositoriesClientTests
  12. {
  13. public class TheCtor
  14. {
  15. [Fact]
  16. public void EnsuresNonNullArguments()
  17. {
  18. Assert.Throws<ArgumentNullException>(
  19. () => new ObservableRepositoriesClient(null));
  20. }
  21. }
  22. public class TheDeleteMethod
  23. {
  24. [Fact]
  25. public async Task RequestsCorrectUrl()
  26. {
  27. var gitHubClient = Substitute.For<IGitHubClient>();
  28. var client = new ObservableRepositoriesClient(gitHubClient);
  29. await client.Delete("owner", "name");
  30. gitHubClient.Received().Repository.Delete("owner", "name");
  31. }
  32. [Fact]
  33. public async Task RequestsCorrectUrlWithRepositoryId()
  34. {
  35. var gitHubClient = Substitute.For<IGitHubClient>();
  36. var client = new ObservableRepositoriesClient(gitHubClient);
  37. await client.Delete(1);
  38. gitHubClient.Received().Repository.Delete(1);
  39. }
  40. [Fact]
  41. public async Task EnsuresNonNullArguments()
  42. {
  43. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  44. Assert.Throws<ArgumentNullException>(() => client.Delete(null, "name"));
  45. Assert.Throws<ArgumentNullException>(() => client.Delete("owner", null));
  46. Assert.Throws<ArgumentException>(() => client.Delete("", "name"));
  47. Assert.Throws<ArgumentException>(() => client.Delete("owner", ""));
  48. }
  49. }
  50. public class TheGetMethod
  51. {
  52. [Fact]
  53. public async Task RequestsCorrectUrl()
  54. {
  55. var gitHubClient = Substitute.For<IGitHubClient>();
  56. var client = new ObservableRepositoriesClient(gitHubClient);
  57. await client.Get("owner", "name");
  58. gitHubClient.Received().Repository.Get("owner", "name");
  59. }
  60. [Fact]
  61. public async Task RequestsCorrectUrlWithRepositoryId()
  62. {
  63. var gitHubClient = Substitute.For<IGitHubClient>();
  64. var client = new ObservableRepositoriesClient(gitHubClient);
  65. await client.Get(1);
  66. gitHubClient.Repository.Get(1);
  67. }
  68. // This isn't really a test specific to this method. This is just as good a place as any to test
  69. // that our API methods returns the right kind of observables.
  70. [Fact]
  71. public async Task IsALukeWarmObservable()
  72. {
  73. var repository = new Repository();
  74. var response = Task.Factory.StartNew<IApiResponse<Repository>>(() =>
  75. new ApiResponse<Repository>(new Response(), repository));
  76. var connection = Substitute.For<IConnection>();
  77. connection.Get<Repository>(Args.Uri, null, null).Returns(response);
  78. var gitHubClient = new GitHubClient(connection);
  79. var client = new ObservableRepositoriesClient(gitHubClient);
  80. var observable = client.Get("stark", "ned");
  81. connection.Received(1).Get<Repository>(Args.Uri, null, null);
  82. var result = await observable;
  83. connection.Received(1).Get<Repository>(Args.Uri, null, null);
  84. var result2 = await observable;
  85. // TODO: If we change this to a warm observable, we'll need to change this to Received(2)
  86. connection.Received(1).Get<Repository>(Args.Uri, null, null);
  87. Assert.Same(repository, result);
  88. Assert.Same(repository, result2);
  89. }
  90. // This isn't really a test specific to this method. This is just as good a place as any to test
  91. // that our API methods returns the right kind of observables.
  92. [Fact]
  93. public async Task IsALukeWarmObservableWithRepositoryId()
  94. {
  95. var repository = new Repository();
  96. var response = Task.Factory.StartNew<IApiResponse<Repository>>(() =>
  97. new ApiResponse<Repository>(new Response(), repository));
  98. var connection = Substitute.For<IConnection>();
  99. connection.Get<Repository>(Args.Uri, null, null).Returns(response);
  100. var gitHubClient = new GitHubClient(connection);
  101. var client = new ObservableRepositoriesClient(gitHubClient);
  102. var observable = client.Get(1);
  103. connection.Received(1).Get<Repository>(Args.Uri, null, null);
  104. var result = await observable;
  105. connection.Received(1).Get<Repository>(Args.Uri, null, null);
  106. var result2 = await observable;
  107. // TODO: If we change this to a warm observable, we'll need to change this to Received(2)
  108. connection.Received(1).Get<Repository>(Args.Uri, null, null);
  109. Assert.Same(repository, result);
  110. Assert.Same(repository, result2);
  111. }
  112. [Fact]
  113. public async Task EnsuresNonNullArguments()
  114. {
  115. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  116. Assert.Throws<ArgumentNullException>(() => client.Get(null, "name"));
  117. Assert.Throws<ArgumentNullException>(() => client.Get("owner", null));
  118. Assert.Throws<ArgumentException>(() => client.Get("", "name"));
  119. Assert.Throws<ArgumentException>(() => client.Get("owner", ""));
  120. }
  121. }
  122. public class TheGetAllForCurrentMethod
  123. {
  124. [Fact]
  125. public async Task ReturnsEveryPageOfRepositories()
  126. {
  127. var firstPageUrl = new Uri("user/repos", UriKind.Relative);
  128. var secondPageUrl = new Uri("https://example.com/page/2");
  129. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  130. var firstPageResponse = new ApiResponse<List<Repository>>(
  131. CreateResponseWithApiInfo(firstPageLinks),
  132. new List<Repository>
  133. {
  134. new Repository(1),
  135. new Repository(2),
  136. new Repository(3)
  137. });
  138. var thirdPageUrl = new Uri("https://example.com/page/3");
  139. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  140. var secondPageResponse = new ApiResponse<List<Repository>>
  141. (
  142. CreateResponseWithApiInfo(secondPageLinks),
  143. new List<Repository>
  144. {
  145. new Repository(4),
  146. new Repository(5),
  147. new Repository(6)
  148. });
  149. var lastPageResponse = new ApiResponse<List<Repository>>(
  150. new Response(),
  151. new List<Repository>
  152. {
  153. new Repository(7)
  154. });
  155. var gitHubClient = Substitute.For<IGitHubClient>();
  156. gitHubClient.Connection.Get<List<Repository>>(firstPageUrl, Arg.Any<IDictionary<string, string>>(), null)
  157. .Returns(Task.Factory.StartNew<IApiResponse<List<Repository>>>(() => firstPageResponse));
  158. gitHubClient.Connection.Get<List<Repository>>(secondPageUrl, Arg.Any<IDictionary<string, string>>(), null)
  159. .Returns(Task.Factory.StartNew<IApiResponse<List<Repository>>>(() => secondPageResponse));
  160. gitHubClient.Connection.Get<List<Repository>>(thirdPageUrl, Arg.Any<IDictionary<string, string>>(), null)
  161. .Returns(Task.Factory.StartNew<IApiResponse<List<Repository>>>(() => lastPageResponse));
  162. var repositoriesClient = new ObservableRepositoriesClient(gitHubClient);
  163. var results = await repositoriesClient.GetAllForCurrent().ToArray();
  164. Assert.Equal(7, results.Length);
  165. gitHubClient.Connection.Received(1).Get<List<Repository>>(firstPageUrl, Arg.Any<IDictionary<string, string>>(), null);
  166. gitHubClient.Connection.Received(1).Get<List<Repository>>(secondPageUrl, Arg.Any<IDictionary<string, string>>(), null);
  167. gitHubClient.Connection.Received(1).Get<List<Repository>>(thirdPageUrl, Arg.Any<IDictionary<string, string>>(), null);
  168. }
  169. [Fact(Skip = "See https://github.com/octokit/octokit.net/issues/1011 for issue to investigate this further")]
  170. public async Task StopsMakingNewRequestsWhenTakeIsFulfilled()
  171. {
  172. var firstPageUrl = new Uri("user/repos", UriKind.Relative);
  173. var secondPageUrl = new Uri("https://example.com/page/2");
  174. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  175. var firstPageResponse = new ApiResponse<List<Repository>>
  176. (
  177. CreateResponseWithApiInfo(firstPageLinks),
  178. new List<Repository>
  179. {
  180. new Repository(1),
  181. new Repository(2),
  182. new Repository(3)
  183. }
  184. );
  185. var thirdPageUrl = new Uri("https://example.com/page/3");
  186. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  187. var secondPageResponse = new ApiResponse<List<Repository>>
  188. (
  189. CreateResponseWithApiInfo(secondPageLinks),
  190. new List<Repository>
  191. {
  192. new Repository(4),
  193. new Repository(5),
  194. new Repository(6)
  195. }
  196. );
  197. var fourthPageUrl = new Uri("https://example.com/page/4");
  198. var thirdPageLinks = new Dictionary<string, Uri> { { "next", fourthPageUrl } };
  199. var thirdPageResponse = new ApiResponse<List<Repository>>
  200. (
  201. CreateResponseWithApiInfo(thirdPageLinks),
  202. new List<Repository>
  203. {
  204. new Repository(7)
  205. }
  206. );
  207. var lastPageResponse = new ApiResponse<List<Repository>>
  208. (
  209. new Response(),
  210. new List<Repository>
  211. {
  212. new Repository(8)
  213. }
  214. );
  215. var gitHubClient = Substitute.For<IGitHubClient>();
  216. gitHubClient.Connection.GetResponse<List<Repository>>(firstPageUrl)
  217. .Returns(Task.Factory.StartNew<IApiResponse<List<Repository>>>(() => firstPageResponse));
  218. gitHubClient.Connection.GetResponse<List<Repository>>(secondPageUrl)
  219. .Returns(Task.Factory.StartNew<IApiResponse<List<Repository>>>(() => secondPageResponse));
  220. gitHubClient.Connection.GetResponse<List<Repository>>(thirdPageUrl)
  221. .Returns(Task.Factory.StartNew<IApiResponse<List<Repository>>>(() => thirdPageResponse));
  222. gitHubClient.Connection.GetResponse<List<Repository>>(fourthPageUrl)
  223. .Returns(Task.Factory.StartNew<IApiResponse<List<Repository>>>(() => lastPageResponse));
  224. var repositoriesClient = new ObservableRepositoriesClient(gitHubClient);
  225. var results = await repositoriesClient.GetAllForCurrent().Take(4).ToArray();
  226. Assert.Equal(4, results.Length);
  227. gitHubClient.Connection.Received(1).Get<List<Repository>>(firstPageUrl, null, null);
  228. gitHubClient.Connection.Received(1).Get<List<Repository>>(secondPageUrl, null, null);
  229. gitHubClient.Connection.Received(0).Get<List<Repository>>(thirdPageUrl, null, null);
  230. gitHubClient.Connection.Received(0).Get<List<Repository>>(fourthPageUrl, null, null);
  231. }
  232. }
  233. public class TheGetAllPublicRepositoriesSinceMethod
  234. {
  235. [Fact]
  236. public async Task ReturnsEveryPageOfRepositories()
  237. {
  238. var firstPageUrl = new Uri("repositories?since=364", UriKind.Relative);
  239. var secondPageUrl = new Uri("https://example.com/page/2");
  240. var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
  241. IApiResponse<List<Repository>> firstPageResponse = new ApiResponse<List<Repository>>(
  242. CreateResponseWithApiInfo(firstPageLinks),
  243. new List<Repository>
  244. {
  245. new Repository(364),
  246. new Repository(365),
  247. new Repository(366)
  248. });
  249. var thirdPageUrl = new Uri("https://example.com/page/3");
  250. var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
  251. IApiResponse<List<Repository>> secondPageResponse = new ApiResponse<List<Repository>>
  252. (
  253. CreateResponseWithApiInfo(secondPageLinks),
  254. new List<Repository>
  255. {
  256. new Repository(367),
  257. new Repository(368),
  258. new Repository(369)
  259. });
  260. IApiResponse<List<Repository>> lastPageResponse = new ApiResponse<List<Repository>>(
  261. new Response(),
  262. new List<Repository>
  263. {
  264. new Repository(370)
  265. });
  266. var gitHubClient = Substitute.For<IGitHubClient>();
  267. gitHubClient.Connection.Get<List<Repository>>(firstPageUrl, null, null)
  268. .Returns(Task.FromResult(firstPageResponse));
  269. gitHubClient.Connection.Get<List<Repository>>(secondPageUrl, null, null)
  270. .Returns(Task.FromResult(secondPageResponse));
  271. gitHubClient.Connection.Get<List<Repository>>(thirdPageUrl, null, null)
  272. .Returns(Task.FromResult(lastPageResponse));
  273. var repositoriesClient = new ObservableRepositoriesClient(gitHubClient);
  274. var results = await repositoriesClient.GetAllPublic(new PublicRepositoryRequest(364)).ToArray();
  275. Assert.Equal(7, results.Length);
  276. gitHubClient.Connection.Received(1).Get<List<Repository>>(firstPageUrl, null, null);
  277. gitHubClient.Connection.Received(1).Get<List<Repository>>(secondPageUrl, null, null);
  278. gitHubClient.Connection.Received(1).Get<List<Repository>>(thirdPageUrl, null, null);
  279. }
  280. }
  281. public class TheGetAllBranchesMethod
  282. {
  283. [Fact]
  284. public void RequestsTheCorrectUrl()
  285. {
  286. var gitHubClient = Substitute.For<IGitHubClient>();
  287. var client = new ObservableRepositoriesClient(gitHubClient);
  288. var expected = new Uri("repos/owner/repo/branches", UriKind.Relative);
  289. client.GetAllBranches("owner", "repo");
  290. gitHubClient.Connection.Received(1).Get<List<Branch>>(expected, Args.EmptyDictionary, null);
  291. }
  292. [Fact]
  293. public void RequestsTheCorrectUrlWithRepositoryId()
  294. {
  295. var gitHubClient = Substitute.For<IGitHubClient>();
  296. var client = new ObservableRepositoriesClient(gitHubClient);
  297. var expected = new Uri("repositories/1/branches", UriKind.Relative);
  298. client.GetAllBranches(1);
  299. gitHubClient.Connection.Received(1).Get<List<Branch>>(expected, Args.EmptyDictionary, null);
  300. }
  301. [Fact]
  302. public void RequestsTheCorrectUrlWithApiOptions()
  303. {
  304. var gitHubClient = Substitute.For<IGitHubClient>();
  305. var client = new ObservableRepositoriesClient(gitHubClient);
  306. var expected = new Uri("repos/owner/name/branches", UriKind.Relative);
  307. var options = new ApiOptions
  308. {
  309. PageCount = 1,
  310. StartPage = 1,
  311. PageSize = 1
  312. };
  313. client.GetAllBranches("owner", "name", options);
  314. gitHubClient.Connection.Received(1).Get<List<Branch>>(expected, Arg.Is<IDictionary<string, string>>(d => d.Count == 2 && d["page"] == "1" && d["per_page"] == "1"), null);
  315. }
  316. [Fact]
  317. public void RequestsTheCorrectUrlWithRepositoryIdWithApiOptions()
  318. {
  319. var gitHubClient = Substitute.For<IGitHubClient>();
  320. var client = new ObservableRepositoriesClient(gitHubClient);
  321. var expected = new Uri("repositories/1/branches", UriKind.Relative);
  322. var options = new ApiOptions
  323. {
  324. PageCount = 1,
  325. StartPage = 1,
  326. PageSize = 1
  327. };
  328. client.GetAllBranches(1, options);
  329. gitHubClient.Connection.Received(1).Get<List<Branch>>(expected, Arg.Is<IDictionary<string, string>>(d => d.Count == 2 && d["page"] == "1" && d["per_page"] == "1"), null);
  330. }
  331. [Fact]
  332. public void EnsuresNonNullArguments()
  333. {
  334. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  335. Assert.Throws<ArgumentNullException>(() => client.GetAllBranches(null, "name"));
  336. Assert.Throws<ArgumentNullException>(() => client.GetAllBranches("owner", null));
  337. Assert.Throws<ArgumentNullException>(() => client.GetAllBranches(null, "name", ApiOptions.None));
  338. Assert.Throws<ArgumentNullException>(() => client.GetAllBranches("owner", null, ApiOptions.None));
  339. Assert.Throws<ArgumentNullException>(() => client.GetAllBranches("owner", "name", null));
  340. Assert.Throws<ArgumentNullException>(() => client.GetAllBranches(1, null));
  341. Assert.Throws<ArgumentException>(() => client.GetAllBranches("", "name"));
  342. Assert.Throws<ArgumentException>(() => client.GetAllBranches("owner", ""));
  343. Assert.Throws<ArgumentException>(() => client.GetAllBranches("", "name", ApiOptions.None));
  344. Assert.Throws<ArgumentException>(() => client.GetAllBranches("owner", "", ApiOptions.None));
  345. }
  346. }
  347. public class TheGetCommitMethod
  348. {
  349. [Fact]
  350. public void EnsuresNonNullArguments()
  351. {
  352. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  353. Assert.Throws<ArgumentNullException>(() => client.Commit.Get(null, "repo", "reference"));
  354. Assert.Throws<ArgumentNullException>(() => client.Commit.Get("owner", null, "reference"));
  355. Assert.Throws<ArgumentNullException>(() => client.Commit.Get("owner", "repo", null));
  356. Assert.Throws<ArgumentException>(() => client.Commit.Get("", "repo", "reference"));
  357. Assert.Throws<ArgumentException>(() => client.Commit.Get("owner", "", "reference"));
  358. Assert.Throws<ArgumentException>(() => client.Commit.Get("owner", "repo", ""));
  359. }
  360. [Fact]
  361. public void CallsCorrectApi()
  362. {
  363. var github = Substitute.For<IGitHubClient>();
  364. var client = new ObservableRepositoriesClient(github);
  365. client.Commit.Get("owner", "repo", "reference");
  366. github.Repository.Commit.Received(1).Get("owner", "repo", "reference");
  367. }
  368. }
  369. public class TheGetAllCommitsMethod
  370. {
  371. [Fact]
  372. public void EnsuresNonNullArguments()
  373. {
  374. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  375. Assert.Throws<ArgumentNullException>(() => client.Commit.GetAll(null, "repo"));
  376. Assert.Throws<ArgumentNullException>(() => client.Commit.GetAll("owner", null));
  377. Assert.Throws<ArgumentNullException>(() => client.Commit.GetAll("owner", "repo", null, ApiOptions.None));
  378. Assert.Throws<ArgumentException>(() => client.Commit.GetAll("", "repo"));
  379. Assert.Throws<ArgumentException>(() => client.Commit.GetAll("owner", ""));
  380. }
  381. [Fact]
  382. public void RequestsTheCorrectUrl()
  383. {
  384. var github = Substitute.For<IGitHubClient>();
  385. var client = new ObservableRepositoriesClient(github);
  386. var expected = new Uri("repos/owner/repo/commits", UriKind.Relative);
  387. client.Commit.GetAll("owner", "repo");
  388. github.Connection.Received(1).Get<List<GitHubCommit>>(expected, Arg.Any<IDictionary<string, string>>(), null);
  389. }
  390. }
  391. public class TheGetAllContributorsMethod
  392. {
  393. [Fact]
  394. public void RequestsTheCorrectUrl()
  395. {
  396. var gitHubClient = Substitute.For<IGitHubClient>();
  397. var client = new ObservableRepositoriesClient(gitHubClient);
  398. var expected = new Uri("repos/owner/repo/contributors", UriKind.Relative);
  399. client.GetAllContributors("owner", "repo");
  400. gitHubClient.Connection.Received(1)
  401. .Get<List<RepositoryContributor>>(expected,
  402. Args.EmptyDictionary,
  403. null);
  404. }
  405. [Fact]
  406. public void RequestsTheCorrectUrlWithRepositoryId()
  407. {
  408. var gitHubClient = Substitute.For<IGitHubClient>();
  409. var client = new ObservableRepositoriesClient(gitHubClient);
  410. var expected = new Uri("repositories/1/contributors", UriKind.Relative);
  411. client.GetAllContributors(1);
  412. gitHubClient.Connection.Received(1)
  413. .Get<List<RepositoryContributor>>(expected,
  414. Args.EmptyDictionary,
  415. null);
  416. }
  417. [Fact]
  418. public void RequestsTheCorrectUrlWithApiOptions()
  419. {
  420. var gitHubClient = Substitute.For<IGitHubClient>();
  421. var client = new ObservableRepositoriesClient(gitHubClient);
  422. var expected = new Uri("repos/owner/repo/contributors", UriKind.Relative);
  423. var options = new ApiOptions
  424. {
  425. PageCount = 1,
  426. StartPage = 1,
  427. PageSize = 1
  428. };
  429. client.GetAllContributors("owner", "repo", options);
  430. gitHubClient.Connection.Received(1)
  431. .Get<List<RepositoryContributor>>(expected,
  432. Arg.Is<IDictionary<string, string>>(d => d.Count == 2 && d["page"] == "1" && d["per_page"] == "1"),
  433. null);
  434. }
  435. [Fact]
  436. public void RequestsTheCorrectUrlWithApiOptionsWithRepositoryId()
  437. {
  438. var gitHubClient = Substitute.For<IGitHubClient>();
  439. var client = new ObservableRepositoriesClient(gitHubClient);
  440. var expected = new Uri("repositories/1/contributors", UriKind.Relative);
  441. var options = new ApiOptions
  442. {
  443. PageCount = 1,
  444. StartPage = 1,
  445. PageSize = 1
  446. };
  447. client.GetAllContributors(1, options);
  448. gitHubClient.Connection.Received(1)
  449. .Get<List<RepositoryContributor>>(expected,
  450. Arg.Is<IDictionary<string, string>>(d => d.Count == 2),
  451. null);
  452. }
  453. [Fact]
  454. public void RequestsTheCorrectUrlIncludeAnonymous()
  455. {
  456. var gitHubClient = Substitute.For<IGitHubClient>();
  457. var client = new ObservableRepositoriesClient(gitHubClient);
  458. client.GetAllContributors("owner", "name", true);
  459. gitHubClient.Connection.Received()
  460. .Get<List<RepositoryContributor>>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/contributors"), Arg.Is<IDictionary<string, string>>(d => d["anon"] == "1"), null);
  461. }
  462. [Fact]
  463. public void RequestsTheCorrectUrlWithRepositoryIdIncludeAnonymous()
  464. {
  465. var gitHubClient = Substitute.For<IGitHubClient>();
  466. var client = new ObservableRepositoriesClient(gitHubClient);
  467. client.GetAllContributors(1, true);
  468. gitHubClient.Connection.Received()
  469. .Get<List<RepositoryContributor>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contributors"), Arg.Is<IDictionary<string, string>>(d => d["anon"] == "1"), null);
  470. }
  471. [Fact]
  472. public void RequestsTheCorrectUrlWithApiOptionsIncludeAnonymous()
  473. {
  474. var gitHubClient = Substitute.For<IGitHubClient>();
  475. var client = new ObservableRepositoriesClient(gitHubClient);
  476. var options = new ApiOptions
  477. {
  478. PageCount = 1,
  479. StartPage = 1,
  480. PageSize = 1
  481. };
  482. client.GetAllContributors("owner", "name", true, options);
  483. gitHubClient.Connection.Received()
  484. .Get<List<RepositoryContributor>>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/contributors"), Arg.Is<IDictionary<string, string>>(d => d.Count == 3 && d["anon"] == "1" && d["page"] == "1" && d["per_page"] == "1"), null);
  485. }
  486. [Fact]
  487. public void RequestsTheCorrectUrlWithRepositoryIdWithApiOptionsIncludeAnonymous()
  488. {
  489. var gitHubClient = Substitute.For<IGitHubClient>();
  490. var client = new ObservableRepositoriesClient(gitHubClient);
  491. var options = new ApiOptions
  492. {
  493. PageCount = 1,
  494. StartPage = 1,
  495. PageSize = 1
  496. };
  497. client.GetAllContributors(1, true, options);
  498. gitHubClient.Connection.Received()
  499. .Get<List<RepositoryContributor>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contributors"), Arg.Is<IDictionary<string, string>>(d => d.Count == 3 && d["anon"] == "1" && d["page"] == "1" && d["per_page"] == "1"), null);
  500. }
  501. [Fact]
  502. public void EnsuresNonNullArguments()
  503. {
  504. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  505. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors(null, "repo"));
  506. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors("owner", null));
  507. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors(null, "repo", ApiOptions.None));
  508. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors("owner", null, ApiOptions.None));
  509. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors("owner", "repo", null));
  510. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors(null, "repo", false, ApiOptions.None));
  511. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors("owner", null, false, ApiOptions.None));
  512. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors("owner", "repo", false, null));
  513. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors(1, null));
  514. Assert.Throws<ArgumentNullException>(() => client.GetAllContributors(1, false, null));
  515. Assert.Throws<ArgumentException>(() => client.GetAllContributors("", "repo"));
  516. Assert.Throws<ArgumentException>(() => client.GetAllContributors("owner", ""));
  517. Assert.Throws<ArgumentException>(() => client.GetAllContributors("", "repo", ApiOptions.None));
  518. Assert.Throws<ArgumentException>(() => client.GetAllContributors("owner", "", ApiOptions.None));
  519. Assert.Throws<ArgumentException>(() => client.GetAllContributors("", "repo", false, ApiOptions.None));
  520. Assert.Throws<ArgumentException>(() => client.GetAllContributors("owner", "", false, ApiOptions.None));
  521. }
  522. }
  523. public class TheGetAllLanguagesMethod
  524. {
  525. [Fact]
  526. public void RequestsTheCorrectUrl()
  527. {
  528. var gitHubClient = Substitute.For<IGitHubClient>();
  529. var client = new ObservableRepositoriesClient(gitHubClient);
  530. var expected = new Uri("repos/owner/repo/languages", UriKind.Relative);
  531. client.GetAllLanguages("owner", "repo");
  532. gitHubClient.Connection.Received(1).GetResponse<List<Tuple<string, long>>>(expected);
  533. }
  534. [Fact]
  535. public void RequestsTheCorrectUrlWithRepositoryId()
  536. {
  537. var gitHubClient = Substitute.For<IGitHubClient>();
  538. var client = new ObservableRepositoriesClient(gitHubClient);
  539. var expected = new Uri("repositories/1/languages", UriKind.Relative);
  540. client.GetAllLanguages(1);
  541. gitHubClient.Connection.Received(1).GetResponse<List<Tuple<string, long>>>(expected);
  542. }
  543. [Fact]
  544. public void EnsuresNonNullArguments()
  545. {
  546. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  547. Assert.Throws<ArgumentNullException>(() => client.GetAllLanguages(null, "repo"));
  548. Assert.Throws<ArgumentNullException>(() => client.GetAllLanguages("owner", null));
  549. Assert.Throws<ArgumentException>(() => client.GetAllLanguages("", "repo"));
  550. Assert.Throws<ArgumentException>(() => client.GetAllLanguages("owner", ""));
  551. }
  552. }
  553. public class TheGetAllTeamsMethod
  554. {
  555. [Fact]
  556. public void RequestsTheCorrectUrl()
  557. {
  558. var gitHubClient = Substitute.For<IGitHubClient>();
  559. var client = new ObservableRepositoriesClient(gitHubClient);
  560. var expected = new Uri("repos/owner/repo/teams", UriKind.Relative);
  561. client.GetAllTeams("owner", "repo");
  562. gitHubClient.Connection.Received(1).Get<List<Team>>(expected,
  563. Arg.Any<IDictionary<string, string>>(),
  564. Arg.Any<string>());
  565. }
  566. [Fact]
  567. public void RequestsTheCorrectUrlWithRepositoryId()
  568. {
  569. var gitHubClient = Substitute.For<IGitHubClient>();
  570. var client = new ObservableRepositoriesClient(gitHubClient);
  571. var expected = new Uri("repositories/1/teams", UriKind.Relative);
  572. client.GetAllTeams(1);
  573. gitHubClient.Connection.Received(1).Get<List<Team>>(expected,
  574. Arg.Any<IDictionary<string, string>>(),
  575. Arg.Any<string>());
  576. }
  577. [Fact]
  578. public void RequestsTheCorrectUrlWithApiOptions()
  579. {
  580. var gitHubClient = Substitute.For<IGitHubClient>();
  581. var client = new ObservableRepositoriesClient(gitHubClient);
  582. var expected = new Uri("repos/owner/repo/teams", UriKind.Relative);
  583. var options = new ApiOptions
  584. {
  585. PageCount = 1,
  586. StartPage = 1,
  587. PageSize = 1
  588. };
  589. client.GetAllTeams("owner", "repo", options);
  590. gitHubClient.Connection.Received(1).Get<List<Team>>(expected,
  591. Arg.Is<IDictionary<string, string>>(d => d.Count == 2 && d["page"] == "1" && d["per_page"] == "1"),
  592. Arg.Any<string>());
  593. }
  594. [Fact]
  595. public void RequestsTheCorrectUrlWithRepositoryIdWithApiOptions()
  596. {
  597. var gitHubClient = Substitute.For<IGitHubClient>();
  598. var client = new ObservableRepositoriesClient(gitHubClient);
  599. var expected = new Uri("repositories/1/teams", UriKind.Relative);
  600. var options = new ApiOptions
  601. {
  602. PageCount = 1,
  603. StartPage = 1,
  604. PageSize = 1
  605. };
  606. client.GetAllTeams(1, options);
  607. gitHubClient.Connection.Received(1).Get<List<Team>>(expected,
  608. Arg.Is<IDictionary<string, string>>(d => d.Count == 2 && d["page"] == "1" && d["per_page"] == "1"),
  609. Arg.Any<string>());
  610. }
  611. [Fact]
  612. public void EnsuresNonNullArguments()
  613. {
  614. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  615. Assert.Throws<ArgumentNullException>(() => client.GetAllTeams(null, "repo"));
  616. Assert.Throws<ArgumentNullException>(() => client.GetAllTeams("owner", null));
  617. Assert.Throws<ArgumentNullException>(() => client.GetAllTeams(null, "repo", ApiOptions.None));
  618. Assert.Throws<ArgumentNullException>(() => client.GetAllTeams("owner", null, ApiOptions.None));
  619. Assert.Throws<ArgumentNullException>(() => client.GetAllTeams("owner", "repo", null));
  620. Assert.Throws<ArgumentNullException>(() => client.GetAllTeams(1, null));
  621. Assert.Throws<ArgumentException>(() => client.GetAllTeams("", "repo"));
  622. Assert.Throws<ArgumentException>(() => client.GetAllTeams("owner", ""));
  623. Assert.Throws<ArgumentException>(() => client.GetAllTeams("", "repo", ApiOptions.None));
  624. Assert.Throws<ArgumentException>(() => client.GetAllTeams("owner", "", ApiOptions.None));
  625. }
  626. }
  627. public class TheGetAllTagsMethod
  628. {
  629. [Fact]
  630. public void RequestsTheCorrectUrl()
  631. {
  632. var gitHubClient = Substitute.For<IGitHubClient>();
  633. var client = new ObservableRepositoriesClient(gitHubClient);
  634. var expected = new Uri("repos/owner/repo/tags", UriKind.Relative);
  635. client.GetAllTags("owner", "repo");
  636. gitHubClient.Connection.Received(1).Get<List<RepositoryTag>>(expected, Arg.Any<IDictionary<string, string>>(), null);
  637. }
  638. [Fact]
  639. public void RequestsTheCorrectUrlWithRepositoryId()
  640. {
  641. var gitHubClient = Substitute.For<IGitHubClient>();
  642. var client = new ObservableRepositoriesClient(gitHubClient);
  643. var expected = new Uri("repositories/1/tags", UriKind.Relative);
  644. client.GetAllTags(1);
  645. gitHubClient.Connection.Received(1).Get<List<RepositoryTag>>(expected, Arg.Any<IDictionary<string, string>>(), null);
  646. }
  647. [Fact]
  648. public void RequestsTheCorrectUrlWithApiOptions()
  649. {
  650. var gitHubClient = Substitute.For<IGitHubClient>();
  651. var client = new ObservableRepositoriesClient(gitHubClient);
  652. var expected = new Uri("repos/owner/repo/tags", UriKind.Relative);
  653. var options = new ApiOptions
  654. {
  655. PageCount = 1,
  656. StartPage = 1,
  657. PageSize = 1
  658. };
  659. client.GetAllTags("owner", "repo", options);
  660. gitHubClient.Connection.Received(1).Get<List<RepositoryTag>>(expected, Arg.Is<IDictionary<string, string>>(d => d.Count == 2 && d["page"] == "1" && d["per_page"] == "1"), null);
  661. }
  662. [Fact]
  663. public void RequestsTheCorrectUrlWithRepositoryIdWithApiOptions()
  664. {
  665. var gitHubClient = Substitute.For<IGitHubClient>();
  666. var client = new ObservableRepositoriesClient(gitHubClient);
  667. var expected = new Uri("repositories/1/tags", UriKind.Relative);
  668. var options = new ApiOptions
  669. {
  670. PageCount = 1,
  671. StartPage = 1,
  672. PageSize = 1
  673. };
  674. client.GetAllTags(1, options);
  675. gitHubClient.Connection.Received(1).Get<List<RepositoryTag>>(expected, Arg.Is<IDictionary<string, string>>(d => d.Count == 2 && d["page"] == "1" && d["per_page"] == "1"), null);
  676. }
  677. [Fact]
  678. public void EnsuresNonNullArguments()
  679. {
  680. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  681. Assert.Throws<ArgumentNullException>(() => client.GetAllTags(null, "repo"));
  682. Assert.Throws<ArgumentNullException>(() => client.GetAllTags("owner", null));
  683. Assert.Throws<ArgumentNullException>(() => client.GetAllTags(null, "repo", ApiOptions.None));
  684. Assert.Throws<ArgumentNullException>(() => client.GetAllTags("owner", null, ApiOptions.None));
  685. Assert.Throws<ArgumentNullException>(() => client.GetAllTags("owner", "repo", null));
  686. Assert.Throws<ArgumentNullException>(() => client.GetAllTags(1, null));
  687. Assert.Throws<ArgumentException>(() => client.GetAllTags("", "repo"));
  688. Assert.Throws<ArgumentException>(() => client.GetAllTags("owner", ""));
  689. Assert.Throws<ArgumentException>(() => client.GetAllTags("", "repo", ApiOptions.None));
  690. Assert.Throws<ArgumentException>(() => client.GetAllTags("owner", "", ApiOptions.None));
  691. }
  692. }
  693. public class TheGetBranchMethod
  694. {
  695. [Fact]
  696. public void RequestsTheCorrectUrl()
  697. {
  698. var github = Substitute.For<IGitHubClient>();
  699. var client = new ObservableRepositoriesClient(github);
  700. client.GetBranch("owner", "repo", "branch");
  701. github.Repository.Branch.Received(1).Get("owner", "repo", "branch");
  702. }
  703. [Fact]
  704. public void RequestsTheCorrectUrlWithRepositoryId()
  705. {
  706. var github = Substitute.For<IGitHubClient>();
  707. var client = new ObservableRepositoriesClient(github);
  708. client.GetBranch(1, "branch");
  709. github.Repository.Branch.Received(1).Get(1, "branch");
  710. }
  711. [Fact]
  712. public async Task EnsuresNonNullArguments()
  713. {
  714. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  715. Assert.Throws<ArgumentNullException>(() => client.GetBranch(null, "repo", "branch"));
  716. Assert.Throws<ArgumentNullException>(() => client.GetBranch("owner", null, "branch"));
  717. Assert.Throws<ArgumentNullException>(() => client.GetBranch("owner", "repo", null));
  718. Assert.Throws<ArgumentNullException>(() => client.GetBranch(1, null));
  719. Assert.Throws<ArgumentException>(() => client.GetBranch("", "repo", "branch"));
  720. Assert.Throws<ArgumentException>(() => client.GetBranch("owner", "", "branch"));
  721. Assert.Throws<ArgumentException>(() => client.GetBranch("owner", "repo", ""));
  722. Assert.Throws<ArgumentException>(() => client.GetBranch(1, ""));
  723. }
  724. }
  725. public class TheEditMethod
  726. {
  727. [Fact]
  728. public void PatchsTheCorrectUrl()
  729. {
  730. var github = Substitute.For<IGitHubClient>();
  731. var client = new ObservableRepositoriesClient(github);
  732. var update = new RepositoryUpdate();
  733. client.Edit("owner", "repo", update);
  734. github.Repository.Received(1).Edit("owner", "repo", update);
  735. }
  736. [Fact]
  737. public void PatchsTheCorrectUrlWithRepositoryId()
  738. {
  739. var github = Substitute.For<IGitHubClient>();
  740. var client = new ObservableRepositoriesClient(github);
  741. var update = new RepositoryUpdate();
  742. client.Edit(1, update);
  743. github.Repository.Received(1).Edit(1, update);
  744. }
  745. [Fact]
  746. public async Task EnsuresNonNullArguments()
  747. {
  748. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  749. var update = new RepositoryUpdate();
  750. Assert.Throws<ArgumentNullException>(() => client.Edit(null, "repo", update));
  751. Assert.Throws<ArgumentNullException>(() => client.Edit("owner", null, update));
  752. Assert.Throws<ArgumentNullException>(() => client.Edit("owner", "repo", null));
  753. Assert.Throws<ArgumentNullException>(() => client.Edit(1, null));
  754. Assert.Throws<ArgumentException>(() => client.Edit("", "repo", update));
  755. Assert.Throws<ArgumentException>(() => client.Edit("owner", "", update));
  756. }
  757. }
  758. public class TheEditBranchMethod
  759. {
  760. [Fact]
  761. public void PatchsTheCorrectUrl()
  762. {
  763. var github = Substitute.For<IGitHubClient>();
  764. var client = new ObservableRepositoriesClient(github);
  765. var update = new BranchUpdate();
  766. client.EditBranch("owner", "repo", "branch", update);
  767. github.Repository.Branch.Received(1).Edit("owner", "repo", "branch", update);
  768. }
  769. [Fact]
  770. public void PatchsTheCorrectUrlWithRepositoryId()
  771. {
  772. var github = Substitute.For<IGitHubClient>();
  773. var client = new ObservableRepositoriesClient(github);
  774. var update = new BranchUpdate();
  775. client.EditBranch(1, "branch", update);
  776. github.Repository.Branch.Received(1).Edit(1, "branch", update);
  777. }
  778. [Fact]
  779. public async Task EnsuresNonNullArguments()
  780. {
  781. var client = new ObservableRepositoriesClient(Substitute.For<IGitHubClient>());
  782. var update = new BranchUpdate();
  783. Assert.Throws<ArgumentNullException>(() => client.EditBranch(null, "repo", "branch", update));
  784. Assert.Throws<ArgumentNullException>(() => client.EditBranch("owner", null, "branch", update));
  785. Assert.Throws<ArgumentNullException>(() => client.EditBranch("owner", "repo", null, update));
  786. Assert.Throws<ArgumentNullException>(() => client.EditBranch("owner", "repo", "branch", null));
  787. Assert.Throws<ArgumentNullException>(() => client.EditBranch(1, null, update));
  788. Assert.Throws<ArgumentNullException>(() => client.EditBranch(1, "branch", null));
  789. Assert.Throws<ArgumentException>(() => client.EditBranch("", "repo", "branch", update));
  790. Assert.Throws<ArgumentException>(() => client.EditBranch("owner", "", "branch", update));
  791. Assert.Throws<ArgumentException>(() => client.EditBranch("owner", "repo", "", update));
  792. Assert.Throws<ArgumentException>(() => client.EditBranch(1, "", update));
  793. }
  794. }
  795. static IResponse CreateResponseWithApiInfo(IDictionary<string, Uri> links)
  796. {
  797. var response = Substitute.For<IResponse>();
  798. response.ApiInfo.Returns(new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>())));
  799. return response;
  800. }
  801. }
  802. }