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

/Octokit.Tests.Integration/Clients/IssueCommentsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 617 lines | 469 code | 136 blank | 12 comment | 4 complexity | 7b2bd1c2b417aa55031d054e7ebde695 MD5 | raw file
  1. using System.Threading.Tasks;
  2. using Octokit;
  3. using Octokit.Tests.Integration;
  4. using Octokit.Tests.Integration.Helpers;
  5. using Xunit;
  6. using System;
  7. using System.Linq;
  8. using System.Collections.Generic;
  9. public class IssueCommentsClientTests
  10. {
  11. public class TheGetMethod
  12. {
  13. readonly IGitHubClient _github;
  14. readonly IIssueCommentsClient _issueCommentsClient;
  15. const string owner = "octokit";
  16. const string name = "octokit.net";
  17. const int id = 12067722;
  18. const int repositoryId = 7528679;
  19. public TheGetMethod()
  20. {
  21. _github = Helper.GetAuthenticatedClient();
  22. _issueCommentsClient = _github.Issue.Comment;
  23. }
  24. [IntegrationTest]
  25. public async Task ReturnsIssueComment()
  26. {
  27. var comment = await _issueCommentsClient.Get(owner, name, id);
  28. Assert.NotNull(comment);
  29. }
  30. [IntegrationTest]
  31. public async Task ReturnsIssueCommentWithRepositoryId()
  32. {
  33. var comment = await _issueCommentsClient.Get(repositoryId, id);
  34. Assert.NotNull(comment);
  35. }
  36. [IntegrationTest]
  37. public async Task CanGetReactionPayload()
  38. {
  39. using (var context = await _github.CreateRepositoryContext(Helper.MakeNameWithTimestamp("IssueCommentsReactionTests")))
  40. {
  41. // Create a test issue
  42. var issueNumber = await HelperCreateIssue(context.RepositoryOwner, context.RepositoryName);
  43. // Create a test comment with reactions
  44. var commentId = await HelperCreateIssueCommentWithReactions(context.RepositoryOwner, context.RepositoryName, issueNumber);
  45. // Retrieve the comment
  46. var retrieved = await _issueCommentsClient.Get(context.RepositoryOwner, context.RepositoryName, commentId);
  47. // Check the reactions
  48. Assert.True(retrieved.Id > 0);
  49. Assert.Equal(6, retrieved.Reactions.TotalCount);
  50. Assert.Equal(1, retrieved.Reactions.Plus1);
  51. Assert.Equal(1, retrieved.Reactions.Hooray);
  52. Assert.Equal(1, retrieved.Reactions.Heart);
  53. Assert.Equal(1, retrieved.Reactions.Laugh);
  54. Assert.Equal(1, retrieved.Reactions.Confused);
  55. Assert.Equal(1, retrieved.Reactions.Minus1);
  56. }
  57. }
  58. }
  59. public class TheGetAllForRepositoryMethod
  60. {
  61. readonly IGitHubClient _github;
  62. readonly IIssueCommentsClient _issueCommentsClient;
  63. const string owner = "octokit";
  64. const string name = "octokit.net";
  65. const int repositoryId = 7528679;
  66. public TheGetAllForRepositoryMethod()
  67. {
  68. _github = Helper.GetAuthenticatedClient();
  69. _issueCommentsClient = _github.Issue.Comment;
  70. }
  71. [IntegrationTest]
  72. public async Task ReturnsIssueComments()
  73. {
  74. var issueComments = await _issueCommentsClient.GetAllForRepository(owner, name);
  75. Assert.NotEmpty(issueComments);
  76. }
  77. [IntegrationTest]
  78. public async Task ReturnsIssueCommentsWithRepositoryId()
  79. {
  80. var issueComments = await _issueCommentsClient.GetAllForRepository(repositoryId);
  81. Assert.NotEmpty(issueComments);
  82. }
  83. [IntegrationTest]
  84. public async Task ReturnsCorrectCountOfIssueCommentsWithoutStart()
  85. {
  86. var options = new ApiOptions
  87. {
  88. PageSize = 5,
  89. PageCount = 1
  90. };
  91. var issueComments = await _issueCommentsClient.GetAllForRepository(owner, name, options);
  92. Assert.Equal(5, issueComments.Count);
  93. }
  94. [IntegrationTest]
  95. public async Task ReturnsCorrectCountOfIssueCommentsWithRepositoryIdWithoutStart()
  96. {
  97. var options = new ApiOptions
  98. {
  99. PageSize = 5,
  100. PageCount = 1
  101. };
  102. var issueComments = await _issueCommentsClient.GetAllForRepository(repositoryId, options);
  103. Assert.Equal(5, issueComments.Count);
  104. }
  105. [IntegrationTest]
  106. public async Task ReturnsCorrectCountOfIssueCommentsWithStart()
  107. {
  108. var options = new ApiOptions
  109. {
  110. PageSize = 5,
  111. PageCount = 1,
  112. StartPage = 2
  113. };
  114. var issueComments = await _issueCommentsClient.GetAllForRepository(owner, name, options);
  115. Assert.Equal(5, issueComments.Count);
  116. }
  117. [IntegrationTest]
  118. public async Task ReturnsCorrectCountOfIssueCommentsWithRepositoryIdWithStart()
  119. {
  120. var options = new ApiOptions
  121. {
  122. PageSize = 5,
  123. PageCount = 1,
  124. StartPage = 2
  125. };
  126. var issueComments = await _issueCommentsClient.GetAllForRepository(repositoryId, options);
  127. Assert.Equal(5, issueComments.Count);
  128. }
  129. [IntegrationTest]
  130. public async Task ReturnsDistinctResultsBasedOnStartPage()
  131. {
  132. var startOptions = new ApiOptions
  133. {
  134. PageSize = 5,
  135. PageCount = 1
  136. };
  137. var firstPageIssueComments = await _issueCommentsClient.GetAllForRepository(owner, name, startOptions);
  138. var skipStartOptions = new ApiOptions
  139. {
  140. PageSize = 5,
  141. PageCount = 1,
  142. StartPage = 2
  143. };
  144. var secondPageIssueComments = await _issueCommentsClient.GetAllForRepository(owner, name, skipStartOptions);
  145. Assert.NotEqual(firstPageIssueComments[0].Id, secondPageIssueComments[0].Id);
  146. Assert.NotEqual(firstPageIssueComments[1].Id, secondPageIssueComments[1].Id);
  147. Assert.NotEqual(firstPageIssueComments[2].Id, secondPageIssueComments[2].Id);
  148. Assert.NotEqual(firstPageIssueComments[3].Id, secondPageIssueComments[3].Id);
  149. Assert.NotEqual(firstPageIssueComments[4].Id, secondPageIssueComments[4].Id);
  150. }
  151. [IntegrationTest]
  152. public async Task ReturnsDistinctResultsBasedOnStartPageWithRepositoryId()
  153. {
  154. var startOptions = new ApiOptions
  155. {
  156. PageSize = 5,
  157. PageCount = 1
  158. };
  159. var firstPageIssueComments = await _issueCommentsClient.GetAllForRepository(repositoryId, startOptions);
  160. var skipStartOptions = new ApiOptions
  161. {
  162. PageSize = 5,
  163. PageCount = 1,
  164. StartPage = 2
  165. };
  166. var secondPageIssueComments = await _issueCommentsClient.GetAllForRepository(repositoryId, skipStartOptions);
  167. Assert.NotEqual(firstPageIssueComments[0].Id, secondPageIssueComments[0].Id);
  168. Assert.NotEqual(firstPageIssueComments[1].Id, secondPageIssueComments[1].Id);
  169. Assert.NotEqual(firstPageIssueComments[2].Id, secondPageIssueComments[2].Id);
  170. Assert.NotEqual(firstPageIssueComments[3].Id, secondPageIssueComments[3].Id);
  171. Assert.NotEqual(firstPageIssueComments[4].Id, secondPageIssueComments[4].Id);
  172. }
  173. [IntegrationTest]
  174. public async Task CanGetReactionPayload()
  175. {
  176. var numberToCreate = 2;
  177. using (var context = await _github.CreateRepositoryContext(Helper.MakeNameWithTimestamp("IssueCommentsReactionTests")))
  178. {
  179. var commentIds = new List<int>();
  180. // Create multiple test issues
  181. for (int count = 1; count <= numberToCreate; count++)
  182. {
  183. var issueNumber = await HelperCreateIssue(context.RepositoryOwner, context.RepositoryName);
  184. // Each with a comment with reactions
  185. var commentId = await HelperCreateIssueCommentWithReactions(context.RepositoryOwner, context.RepositoryName, issueNumber);
  186. commentIds.Add(commentId);
  187. }
  188. Assert.Equal(numberToCreate, commentIds.Count);
  189. // Retrieve all issue comments for the repo
  190. var issueComments = await _issueCommentsClient.GetAllForRepository(context.RepositoryOwner, context.RepositoryName);
  191. // Check the reactions
  192. foreach (var commentId in commentIds)
  193. {
  194. var retrieved = issueComments.FirstOrDefault(x => x.Id == commentId);
  195. Assert.NotNull(retrieved);
  196. Assert.Equal(6, retrieved.Reactions.TotalCount);
  197. Assert.Equal(1, retrieved.Reactions.Plus1);
  198. Assert.Equal(1, retrieved.Reactions.Hooray);
  199. Assert.Equal(1, retrieved.Reactions.Heart);
  200. Assert.Equal(1, retrieved.Reactions.Laugh);
  201. Assert.Equal(1, retrieved.Reactions.Confused);
  202. Assert.Equal(1, retrieved.Reactions.Minus1);
  203. }
  204. }
  205. }
  206. }
  207. public class TheGetAllForIssueMethod
  208. {
  209. readonly IGitHubClient _github;
  210. readonly IIssueCommentsClient _issueCommentsClient;
  211. const string owner = "octokit";
  212. const string name = "octokit.net";
  213. const int number = 1115;
  214. const int repositoryId = 7528679;
  215. public TheGetAllForIssueMethod()
  216. {
  217. _github = Helper.GetAuthenticatedClient();
  218. _issueCommentsClient = _github.Issue.Comment;
  219. }
  220. [IntegrationTest]
  221. public async Task ReturnsIssueComments()
  222. {
  223. var issueComments = await _issueCommentsClient.GetAllForIssue(owner, name, number);
  224. Assert.NotEmpty(issueComments);
  225. }
  226. [IntegrationTest]
  227. public async Task ReturnsIssueCommentsWithRepositoryId()
  228. {
  229. var issueComments = await _issueCommentsClient.GetAllForIssue(repositoryId, number);
  230. Assert.NotEmpty(issueComments);
  231. }
  232. [IntegrationTest]
  233. public async Task ReturnsCorrectCountOfIssueCommentsWithoutStart()
  234. {
  235. var options = new ApiOptions
  236. {
  237. PageSize = 5,
  238. PageCount = 1
  239. };
  240. var issueComments = await _issueCommentsClient.GetAllForIssue(owner, name, number, options);
  241. Assert.Equal(5, issueComments.Count);
  242. }
  243. [IntegrationTest]
  244. public async Task ReturnsCorrectCountOfIssueCommentsWithRepositoryIdWithoutStart()
  245. {
  246. var options = new ApiOptions
  247. {
  248. PageSize = 5,
  249. PageCount = 1
  250. };
  251. var issueComments = await _issueCommentsClient.GetAllForIssue(repositoryId, number, options);
  252. Assert.Equal(5, issueComments.Count);
  253. }
  254. [IntegrationTest]
  255. public async Task ReturnsCorrectCountOfIssueCommentsWithStart()
  256. {
  257. var options = new ApiOptions
  258. {
  259. PageSize = 5,
  260. PageCount = 1,
  261. StartPage = 2
  262. };
  263. var issueComments = await _issueCommentsClient.GetAllForIssue(owner, name, number, options);
  264. Assert.Equal(5, issueComments.Count);
  265. }
  266. [IntegrationTest]
  267. public async Task ReturnsCorrectCountOfIssueCommentsWithRepositoryIdWithStart()
  268. {
  269. var options = new ApiOptions
  270. {
  271. PageSize = 5,
  272. PageCount = 1,
  273. StartPage = 2
  274. };
  275. var issueComments = await _issueCommentsClient.GetAllForIssue(repositoryId, number, options);
  276. Assert.Equal(5, issueComments.Count);
  277. }
  278. [IntegrationTest]
  279. public async Task ReturnsDistinctResultsBasedOnStartPage()
  280. {
  281. var startOptions = new ApiOptions
  282. {
  283. PageSize = 5,
  284. PageCount = 1
  285. };
  286. var firstPageIssueComments = await _issueCommentsClient.GetAllForIssue(owner, name, number, startOptions);
  287. var skipStartOptions = new ApiOptions
  288. {
  289. PageSize = 5,
  290. PageCount = 1,
  291. StartPage = 2
  292. };
  293. var secondPageIssueComments = await _issueCommentsClient.GetAllForIssue(owner, name, number, skipStartOptions);
  294. Assert.NotEqual(firstPageIssueComments[0].Id, secondPageIssueComments[0].Id);
  295. Assert.NotEqual(firstPageIssueComments[1].Id, secondPageIssueComments[1].Id);
  296. Assert.NotEqual(firstPageIssueComments[2].Id, secondPageIssueComments[2].Id);
  297. Assert.NotEqual(firstPageIssueComments[3].Id, secondPageIssueComments[3].Id);
  298. Assert.NotEqual(firstPageIssueComments[4].Id, secondPageIssueComments[4].Id);
  299. }
  300. [IntegrationTest]
  301. public async Task ReturnsDistinctResultsBasedOnStartPageWithRepositoryId()
  302. {
  303. var startOptions = new ApiOptions
  304. {
  305. PageSize = 5,
  306. PageCount = 1
  307. };
  308. var firstPageIssueComments = await _issueCommentsClient.GetAllForIssue(repositoryId, number, startOptions);
  309. var skipStartOptions = new ApiOptions
  310. {
  311. PageSize = 5,
  312. PageCount = 1,
  313. StartPage = 2
  314. };
  315. var secondPageIssueComments = await _issueCommentsClient.GetAllForIssue(repositoryId, number, skipStartOptions);
  316. Assert.NotEqual(firstPageIssueComments[0].Id, secondPageIssueComments[0].Id);
  317. Assert.NotEqual(firstPageIssueComments[1].Id, secondPageIssueComments[1].Id);
  318. Assert.NotEqual(firstPageIssueComments[2].Id, secondPageIssueComments[2].Id);
  319. Assert.NotEqual(firstPageIssueComments[3].Id, secondPageIssueComments[3].Id);
  320. Assert.NotEqual(firstPageIssueComments[4].Id, secondPageIssueComments[4].Id);
  321. }
  322. [IntegrationTest]
  323. public async Task CanGetReactionPayload()
  324. {
  325. var numberToCreate = 2;
  326. using (var context = await _github.CreateRepositoryContext(Helper.MakeNameWithTimestamp("IssueCommentsReactionTests")))
  327. {
  328. var commentIds = new List<int>();
  329. // Create a single test issue
  330. var issueNumber = await HelperCreateIssue(context.RepositoryOwner, context.RepositoryName);
  331. // With multiple comments with reactions
  332. for (int count = 1; count <= numberToCreate; count++)
  333. {
  334. var commentId = await HelperCreateIssueCommentWithReactions(context.RepositoryOwner, context.RepositoryName, issueNumber);
  335. commentIds.Add(commentId);
  336. }
  337. Assert.Equal(numberToCreate, commentIds.Count);
  338. // Retrieve all comments for the issue
  339. var issueComments = await _issueCommentsClient.GetAllForIssue(context.RepositoryOwner, context.RepositoryName, issueNumber);
  340. // Check the reactions
  341. foreach (var commentId in commentIds)
  342. {
  343. var retrieved = issueComments.FirstOrDefault(x => x.Id == commentId);
  344. Assert.NotNull(retrieved);
  345. Assert.Equal(6, retrieved.Reactions.TotalCount);
  346. Assert.Equal(1, retrieved.Reactions.Plus1);
  347. Assert.Equal(1, retrieved.Reactions.Hooray);
  348. Assert.Equal(1, retrieved.Reactions.Heart);
  349. Assert.Equal(1, retrieved.Reactions.Laugh);
  350. Assert.Equal(1, retrieved.Reactions.Confused);
  351. Assert.Equal(1, retrieved.Reactions.Minus1);
  352. }
  353. }
  354. }
  355. }
  356. public class TheCreateMethod
  357. {
  358. readonly IIssueCommentsClient _issueCommentsClient;
  359. readonly RepositoryContext _context;
  360. readonly IIssuesClient _issuesClient;
  361. public TheCreateMethod()
  362. {
  363. var gitHubClient = Helper.GetAuthenticatedClient();
  364. var repoName = Helper.MakeNameWithTimestamp("public-repo");
  365. _context = gitHubClient.CreateRepositoryContext(new NewRepository(repoName)).Result;
  366. _issuesClient = gitHubClient.Issue;
  367. _issueCommentsClient = gitHubClient.Issue.Comment;
  368. }
  369. [IntegrationTest]
  370. public async Task ReturnsIssueComment()
  371. {
  372. var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, new NewIssue("Super Issue 1"));
  373. var comment = await _issueCommentsClient.Create(_context.RepositoryOwner, _context.RepositoryName, issue.Number, "test comment 1");
  374. var loadedComment = await _issueCommentsClient.Get(_context.RepositoryOwner, _context.RepositoryName, comment.Id);
  375. Assert.Equal(comment.Id, loadedComment.Id);
  376. Assert.Equal(comment.Body, loadedComment.Body);
  377. }
  378. [IntegrationTest]
  379. public async Task ReturnsIssueCommentWithRepositoryId()
  380. {
  381. var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, new NewIssue("Super Issue 2"));
  382. var comment = await _issueCommentsClient.Create(_context.Repository.Id, issue.Number, "test comment 2");
  383. var loadedComment = await _issueCommentsClient.Get(_context.Repository.Id, comment.Id);
  384. Assert.Equal(comment.Id, loadedComment.Id);
  385. Assert.Equal(comment.Body, loadedComment.Body);
  386. }
  387. }
  388. public class TheUpdateMethod
  389. {
  390. readonly IIssueCommentsClient _issueCommentsClient;
  391. readonly RepositoryContext _context;
  392. readonly IIssuesClient _issuesClient;
  393. public TheUpdateMethod()
  394. {
  395. var gitHubClient = Helper.GetAuthenticatedClient();
  396. var repoName = Helper.MakeNameWithTimestamp("public-repo");
  397. _context = gitHubClient.CreateRepositoryContext(new NewRepository(repoName)).Result;
  398. _issuesClient = gitHubClient.Issue;
  399. _issueCommentsClient = gitHubClient.Issue.Comment;
  400. }
  401. [IntegrationTest]
  402. public async Task UpdateIssueComment()
  403. {
  404. var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, new NewIssue("Super Issue 1"));
  405. var comment = await _issueCommentsClient.Create(_context.RepositoryOwner, _context.RepositoryName, issue.Number, "test comment 1");
  406. var commentId = comment.Id;
  407. var beforeComment = await _issueCommentsClient.Get(_context.RepositoryOwner, _context.RepositoryName, commentId);
  408. await _issueCommentsClient.Update(_context.RepositoryOwner, _context.RepositoryName, commentId, "test comment 2");
  409. var afterComment = await _issueCommentsClient.Get(_context.RepositoryOwner, _context.RepositoryName, commentId);
  410. Assert.Equal(beforeComment.Id, afterComment.Id);
  411. Assert.NotEqual(beforeComment.Body, afterComment.Body);
  412. Assert.Equal("test comment 2", afterComment.Body);
  413. }
  414. [IntegrationTest]
  415. public async Task UpdateIssueWithRepositoryId()
  416. {
  417. var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, new NewIssue("Super Issue 1"));
  418. var comment = await _issueCommentsClient.Create(_context.Repository.Id, issue.Number, "test comment 1");
  419. var commentId = comment.Id;
  420. var beforeComment = await _issueCommentsClient.Get(_context.Repository.Id, commentId);
  421. await _issueCommentsClient.Update(_context.Repository.Id, commentId, "test comment 2");
  422. var afterComment = await _issueCommentsClient.Get(_context.Repository.Id, commentId);
  423. Assert.Equal(beforeComment.Id, afterComment.Id);
  424. Assert.NotEqual(beforeComment.Body, afterComment.Body);
  425. Assert.Equal("test comment 2", afterComment.Body);
  426. }
  427. }
  428. public class TheDeleteMethod
  429. {
  430. readonly IIssueCommentsClient _issueCommentsClient;
  431. readonly RepositoryContext _context;
  432. readonly IIssuesClient _issuesClient;
  433. public TheDeleteMethod()
  434. {
  435. var gitHubClient = Helper.GetAuthenticatedClient();
  436. var repoName = Helper.MakeNameWithTimestamp("public-repo");
  437. _context = gitHubClient.CreateRepositoryContext(new NewRepository(repoName)).Result;
  438. _issuesClient = gitHubClient.Issue;
  439. _issueCommentsClient = gitHubClient.Issue.Comment;
  440. }
  441. [IntegrationTest]
  442. public async Task DeleteIssueComment()
  443. {
  444. var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, new NewIssue("Super Issue 1"));
  445. var comment = await _issueCommentsClient.Create(_context.RepositoryOwner, _context.RepositoryName, issue.Number, "test comment 1");
  446. await _issueCommentsClient.Delete(_context.RepositoryOwner, _context.RepositoryName, comment.Id);
  447. await Assert.ThrowsAsync<NotFoundException>(() => _issueCommentsClient.Get(_context.RepositoryOwner, _context.RepositoryName, comment.Id));
  448. }
  449. [IntegrationTest]
  450. public async Task DeleteIssueWithRepositoryId()
  451. {
  452. var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, new NewIssue("Super Issue 1"));
  453. var comment = await _issueCommentsClient.Create(_context.Repository.Id, issue.Number, "test comment 1");
  454. await _issueCommentsClient.Delete(_context.Repository.Id, comment.Id);
  455. await Assert.ThrowsAsync<NotFoundException>(() => _issueCommentsClient.Get(_context.Repository.Id, comment.Id));
  456. }
  457. }
  458. async static Task<int> HelperCreateIssue(string owner, string repo)
  459. {
  460. var github = Helper.GetAuthenticatedClient();
  461. var newIssue = new NewIssue("A test issue") { Body = "A new unassigned issue" };
  462. var issue = await github.Issue.Create(owner, repo, newIssue);
  463. Assert.NotNull(issue);
  464. return issue.Number;
  465. }
  466. async static Task<int> HelperCreateIssueCommentWithReactions(string owner, string repo, int number)
  467. {
  468. var github = Helper.GetAuthenticatedClient();
  469. var issueComment = await github.Issue.Comment.Create(owner, repo, number, "A test issue comment");
  470. Assert.NotNull(issueComment);
  471. foreach (ReactionType reactionType in Enum.GetValues(typeof(ReactionType)))
  472. {
  473. var newReaction = new NewReaction(reactionType);
  474. var reaction = await github.Reaction.IssueComment.Create(owner, repo, issueComment.Id, newReaction);
  475. Assert.IsType<Reaction>(reaction);
  476. Assert.Equal(reactionType, reaction.Content);
  477. Assert.Equal(issueComment.User.Id, reaction.User.Id);
  478. }
  479. return issueComment.Id;
  480. }
  481. }