PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests.Integration/Clients/SearchClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 560 lines | 369 code | 136 blank | 55 comment | 15 complexity | 0e83945cd6520be1c13080e7dce6c212 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Octokit;
  6. using Octokit.Tests.Integration;
  7. using Xunit;
  8. public class SearchClientTests
  9. {
  10. readonly IGitHubClient _gitHubClient;
  11. public SearchClientTests()
  12. {
  13. _gitHubClient = Helper.GetAuthenticatedClient();
  14. }
  15. [IntegrationTest]
  16. public async Task SearchForCSharpRepositories()
  17. {
  18. var request = new SearchRepositoriesRequest("csharp");
  19. var repos = await _gitHubClient.Search.SearchRepo(request);
  20. Assert.NotEmpty(repos.Items);
  21. }
  22. [IntegrationTest]
  23. public async Task SearchForGitHub()
  24. {
  25. var request = new SearchUsersRequest("github");
  26. var repos = await _gitHubClient.Search.SearchUsers(request);
  27. Assert.NotEmpty(repos.Items);
  28. }
  29. [IntegrationTest]
  30. public async Task SearchForFunctionInCode()
  31. {
  32. var request = new SearchCodeRequest("addClass", "jquery", "jquery");
  33. var repos = await _gitHubClient.Search.SearchCode(request);
  34. Assert.NotEmpty(repos.Items);
  35. }
  36. [IntegrationTest]
  37. public async Task SearchForFileNameInCode()
  38. {
  39. var request = new SearchCodeRequest("GitHub")
  40. {
  41. FileName = "readme.md",
  42. Repos = new RepositoryCollection { "octokit/octokit.net" }
  43. };
  44. var repos = await _gitHubClient.Search.SearchCode(request);
  45. Assert.NotEmpty(repos.Items);
  46. }
  47. [IntegrationTest]
  48. public async Task SearchForFileNameInCodeWithoutTerm()
  49. {
  50. var request = new SearchCodeRequest()
  51. {
  52. FileName = "readme.md",
  53. Repos = new RepositoryCollection { "octokit/octokit.net" }
  54. };
  55. var repos = await _gitHubClient.Search.SearchCode(request);
  56. Assert.NotEmpty(repos.Items);
  57. }
  58. [IntegrationTest(Skip = "As this repository has been renamed, you cannot search for it's results.")]
  59. public async Task SearchForFileNameInCodeWithoutTerm2()
  60. {
  61. var request = new SearchCodeRequest()
  62. {
  63. FileName = "project.json",
  64. Repos = new RepositoryCollection { "adamcaudill/Psychson" }
  65. };
  66. var repos = await _gitHubClient.Search.SearchCode(request);
  67. Assert.Empty(repos.Items);
  68. }
  69. [IntegrationTest]
  70. public async Task SearchForFileNameInCodeWithoutTermWithUnderscore()
  71. {
  72. var request = new SearchCodeRequest()
  73. {
  74. FileName = "readme.md",
  75. Repos = new RepositoryCollection { "Cultural-Rogue/_51Wp.XinFengSDK.Demo" }
  76. };
  77. var repos = await _gitHubClient.Search.SearchCode(request);
  78. Assert.Empty(repos.Items);
  79. }
  80. [IntegrationTest]
  81. public async Task SearchForWordInCode()
  82. {
  83. var request = new SearchIssuesRequest("windows");
  84. request.Repos = new RepositoryCollection {
  85. { "aspnet", "dnx" },
  86. { "aspnet", "dnvm" }
  87. };
  88. request.SortField = IssueSearchSort.Created;
  89. request.Order = SortDirection.Descending;
  90. var repos = await _gitHubClient.Search.SearchIssues(request);
  91. Assert.NotEmpty(repos.Items);
  92. }
  93. [IntegrationTest]
  94. public async Task SearchForOpenIssues()
  95. {
  96. var request = new SearchIssuesRequest("phone");
  97. request.Repos.Add("caliburn-micro", "caliburn.micro");
  98. request.State = ItemState.Open;
  99. var issues = await _gitHubClient.Search.SearchIssues(request);
  100. Assert.NotEmpty(issues.Items);
  101. }
  102. [IntegrationTest]
  103. public async Task SearchForAllIssues()
  104. {
  105. var request = new SearchIssuesRequest("phone");
  106. request.Repos.Add("caliburn-micro", "caliburn.micro");
  107. var issues = await _gitHubClient.Search.SearchIssues(request);
  108. Assert.NotEmpty(issues.Items);
  109. }
  110. [IntegrationTest]
  111. public async Task SearchForAllIssuesWithoutUsingTerm()
  112. {
  113. var request = new SearchIssuesRequest();
  114. request.Repos.Add("caliburn-micro/caliburn.micro");
  115. var issues = await _gitHubClient.Search.SearchIssues(request);
  116. var closedIssues = issues.Items.Where(x => x.State == ItemState.Closed);
  117. var openedIssues = issues.Items.Where(x => x.State == ItemState.Open);
  118. Assert.NotEmpty(closedIssues);
  119. Assert.NotEmpty(openedIssues);
  120. }
  121. [IntegrationTest]
  122. public async Task SearchForAllIssuesUsingTerm()
  123. {
  124. var request = new SearchIssuesRequest("phone");
  125. request.Repos.Add("caliburn-micro", "caliburn.micro");
  126. var issues = await _gitHubClient.Search.SearchIssues(request);
  127. var closedIssues = issues.Items.Where(x => x.State == ItemState.Closed);
  128. var openedIssues = issues.Items.Where(x => x.State == ItemState.Open);
  129. Assert.NotEmpty(closedIssues);
  130. Assert.NotEmpty(openedIssues);
  131. }
  132. [IntegrationTest]
  133. public async Task SearchForMergedPullRequests()
  134. {
  135. var allRequest = new SearchIssuesRequest();
  136. allRequest.Repos.Add("octokit", "octokit.net");
  137. allRequest.Type = IssueTypeQualifier.PullRequest;
  138. var mergedRequest = new SearchIssuesRequest();
  139. mergedRequest.Repos.Add("octokit", "octokit.net");
  140. mergedRequest.Is = new List<IssueIsQualifier> { IssueIsQualifier.PullRequest, IssueIsQualifier.Merged };
  141. var allPullRequests = await _gitHubClient.Search.SearchIssues(allRequest);
  142. var mergedPullRequests = await _gitHubClient.Search.SearchIssues(mergedRequest);
  143. Assert.NotEmpty(allPullRequests.Items);
  144. Assert.NotEmpty(mergedPullRequests.Items);
  145. Assert.NotEqual(allPullRequests.TotalCount, mergedPullRequests.TotalCount);
  146. }
  147. [IntegrationTest]
  148. public async Task SearchForMissingMetadata()
  149. {
  150. var allRequest = new SearchIssuesRequest();
  151. allRequest.Repos.Add("octokit", "octokit.net");
  152. var noAssigneeRequest = new SearchIssuesRequest();
  153. noAssigneeRequest.Repos.Add("octokit", "octokit.net");
  154. noAssigneeRequest.No = IssueNoMetadataQualifier.Assignee;
  155. var allIssues = await _gitHubClient.Search.SearchIssues(allRequest);
  156. var noAssigneeIssues = await _gitHubClient.Search.SearchIssues(noAssigneeRequest);
  157. Assert.NotEmpty(allIssues.Items);
  158. Assert.NotEmpty(noAssigneeIssues.Items);
  159. Assert.NotEqual(allIssues.TotalCount, noAssigneeIssues.TotalCount);
  160. }
  161. [IntegrationTest]
  162. public async Task SearchForExcludedAuthor()
  163. {
  164. var author = "shiftkey";
  165. // Search for issues by include filter
  166. var request = new SearchIssuesRequest();
  167. request.Repos.Add("octokit", "octokit.net");
  168. request.Author = author;
  169. var issues = await _gitHubClient.Search.SearchIssues(request);
  170. // Ensure we found issues
  171. Assert.NotEmpty(issues.Items);
  172. // Search for issues by exclude filter
  173. var excludeRequest = new SearchIssuesRequest();
  174. excludeRequest.Repos.Add("octokit", "octokit.net");
  175. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  176. {
  177. Author = author
  178. };
  179. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  180. // Ensure we found issues
  181. Assert.NotEmpty(otherIssues.Items);
  182. // Ensure no items from the first search are in the results for the second
  183. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  184. }
  185. [IntegrationTest]
  186. public async Task SearchForExcludedAssignee()
  187. {
  188. var assignee = "shiftkey";
  189. // Search for issues by include filter
  190. var request = new SearchIssuesRequest();
  191. request.Repos.Add("octokit", "octokit.net");
  192. request.Assignee = assignee;
  193. var issues = await _gitHubClient.Search.SearchIssues(request);
  194. // Ensure we found issues
  195. Assert.NotEmpty(issues.Items);
  196. // Search for issues by exclude filter
  197. var excludeRequest = new SearchIssuesRequest();
  198. excludeRequest.Repos.Add("octokit", "octokit.net");
  199. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  200. {
  201. Assignee = assignee
  202. };
  203. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  204. // Ensure we found issues
  205. Assert.NotEmpty(otherIssues.Items);
  206. // Ensure no items from the first search are in the results for the second
  207. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  208. }
  209. [IntegrationTest]
  210. public async Task SearchForExcludedMentions()
  211. {
  212. var mentioned = "shiftkey";
  213. // Search for issues by include filter
  214. var request = new SearchIssuesRequest();
  215. request.Repos.Add("octokit", "octokit.net");
  216. request.Mentions = mentioned;
  217. var issues = await _gitHubClient.Search.SearchIssues(request);
  218. // Ensure we found issues
  219. Assert.NotEmpty(issues.Items);
  220. // Search for issues by exclude filter
  221. var excludeRequest = new SearchIssuesRequest();
  222. excludeRequest.Repos.Add("octokit", "octokit.net");
  223. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  224. {
  225. Mentions = mentioned
  226. };
  227. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  228. // Ensure we found issues
  229. Assert.NotEmpty(otherIssues.Items);
  230. // Ensure no items from the first search are in the results for the second
  231. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  232. }
  233. [IntegrationTest]
  234. public async Task SearchForExcludedCommenter()
  235. {
  236. var commenter = "shiftkey";
  237. // Search for issues by include filter
  238. var request = new SearchIssuesRequest();
  239. request.Repos.Add("octokit", "octokit.net");
  240. request.Commenter = commenter;
  241. var issues = await _gitHubClient.Search.SearchIssues(request);
  242. // Ensure we found issues
  243. Assert.NotEmpty(issues.Items);
  244. // Search for issues by exclude filter
  245. var excludeRequest = new SearchIssuesRequest();
  246. excludeRequest.Repos.Add("octokit", "octokit.net");
  247. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  248. {
  249. Commenter = commenter
  250. };
  251. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  252. // Ensure we found issues
  253. Assert.NotEmpty(otherIssues.Items);
  254. // Ensure no items from the first search are in the results for the second
  255. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  256. }
  257. [IntegrationTest]
  258. public async Task SearchForExcludedInvolves()
  259. {
  260. var involves = "shiftkey";
  261. // Search for issues by include filter
  262. var request = new SearchIssuesRequest();
  263. request.Repos.Add("octokit", "octokit.net");
  264. request.Involves = involves;
  265. var issues = await _gitHubClient.Search.SearchIssues(request);
  266. // Ensure we found issues
  267. Assert.NotEmpty(issues.Items);
  268. // Search for issues by exclude filter
  269. var excludeRequest = new SearchIssuesRequest();
  270. excludeRequest.Repos.Add("octokit", "octokit.net");
  271. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  272. {
  273. Involves = involves
  274. };
  275. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  276. // Ensure we found issues
  277. Assert.NotEmpty(otherIssues.Items);
  278. // Ensure no items from the first search are in the results for the second
  279. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  280. }
  281. [IntegrationTest]
  282. public async Task SearchForExcludedState()
  283. {
  284. var state = ItemState.Open;
  285. // Search for issues by include filter
  286. var request = new SearchIssuesRequest();
  287. request.Repos.Add("octokit", "octokit.net");
  288. request.State = state;
  289. var issues = await _gitHubClient.Search.SearchIssues(request);
  290. // Ensure we found issues
  291. Assert.NotEmpty(issues.Items);
  292. // Search for issues by exclude filter
  293. var excludeRequest = new SearchIssuesRequest();
  294. excludeRequest.Repos.Add("octokit", "octokit.net");
  295. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  296. {
  297. State = state
  298. };
  299. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  300. // Ensure we found issues
  301. Assert.NotEmpty(otherIssues.Items);
  302. // Ensure no items from the first search are in the results for the second
  303. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  304. }
  305. [IntegrationTest]
  306. public async Task SearchForExcludedLabels()
  307. {
  308. var label1 = "up-for-grabs";
  309. var label2 = "feature";
  310. // Search for issues by include filter
  311. var request = new SearchIssuesRequest();
  312. request.Repos.Add("octokit", "octokit.net");
  313. request.Labels = new[] { label1, label2 };
  314. var issues = await _gitHubClient.Search.SearchIssues(request);
  315. // Ensure we found issues
  316. Assert.NotEmpty(issues.Items);
  317. // Search for issues by exclude filter
  318. var excludeRequest = new SearchIssuesRequest();
  319. excludeRequest.Repos.Add("octokit", "octokit.net");
  320. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  321. {
  322. Labels = new[] { label1, label2 }
  323. };
  324. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  325. // Ensure we found issues
  326. Assert.NotEmpty(otherIssues.Items);
  327. // Ensure no items from the first search are in the results for the second
  328. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  329. }
  330. [IntegrationTest]
  331. public async Task SearchForExcludedLanguage()
  332. {
  333. var language = Language.CSharp;
  334. // Search for issues by include filter
  335. var request = new SearchIssuesRequest("octokit");
  336. request.Language = language;
  337. var issues = await _gitHubClient.Search.SearchIssues(request);
  338. // Ensure we found issues
  339. Assert.NotEmpty(issues.Items);
  340. // Search for issues by exclude filter
  341. var excludeRequest = new SearchIssuesRequest("octokit");
  342. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  343. {
  344. Language = language
  345. };
  346. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  347. // Ensure we found issues
  348. Assert.NotEmpty(otherIssues.Items);
  349. // Ensure no items from the first search are in the results for the second
  350. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  351. }
  352. [IntegrationTest]
  353. public async Task SearchForExcludedStatus()
  354. {
  355. var status = CommitState.Success;
  356. // Search for issues by include filter
  357. var request = new SearchIssuesRequest();
  358. request.Repos.Add("octokit", "octokit.net");
  359. request.Status = status;
  360. var issues = await _gitHubClient.Search.SearchIssues(request);
  361. // Ensure we found issues
  362. Assert.NotEmpty(issues.Items);
  363. // Search for issues by exclude filter
  364. var excludeRequest = new SearchIssuesRequest();
  365. excludeRequest.Repos.Add("octokit", "octokit.net");
  366. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  367. {
  368. Status = status
  369. };
  370. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  371. // Ensure we found issues
  372. Assert.NotEmpty(otherIssues.Items);
  373. // Ensure no items from the first search are in the results for the second
  374. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  375. }
  376. [IntegrationTest]
  377. public async Task SearchForExcludedHead()
  378. {
  379. var branch = "search-issues";
  380. // Search for issues by source branch
  381. var request = new SearchIssuesRequest();
  382. request.Repos.Add("octokit", "octokit.net");
  383. request.Head = branch;
  384. var issues = await _gitHubClient.Search.SearchIssues(request);
  385. // Ensure we found issues
  386. Assert.NotEmpty(issues.Items);
  387. // Search for issues excluding source branch
  388. var excludeRequest = new SearchIssuesRequest();
  389. excludeRequest.Repos.Add("octokit", "octokit.net");
  390. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  391. {
  392. Head = branch
  393. };
  394. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  395. // Ensure we found issues
  396. Assert.NotEmpty(otherIssues.Items);
  397. // Ensure no items from the first search are in the results for the second
  398. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  399. }
  400. [IntegrationTest]
  401. public async Task SearchForExcludedBase()
  402. {
  403. var branch = "master";
  404. // Search for issues by target branch
  405. var request = new SearchIssuesRequest();
  406. request.Repos.Add("octokit", "octokit.net");
  407. request.Base = branch;
  408. var issues = await _gitHubClient.Search.SearchIssues(request);
  409. // Ensure we found issues
  410. Assert.NotEmpty(issues.Items);
  411. // Search for issues excluding target branch
  412. var excludeRequest = new SearchIssuesRequest();
  413. excludeRequest.Repos.Add("octokit", "octokit.net");
  414. excludeRequest.Exclusions = new SearchIssuesRequestExclusions
  415. {
  416. Base = branch
  417. };
  418. var otherIssues = await _gitHubClient.Search.SearchIssues(excludeRequest);
  419. // Ensure we found issues
  420. Assert.NotEmpty(otherIssues.Items);
  421. // Ensure no items from the first search are in the results for the second
  422. Assert.DoesNotContain(issues.Items, x1 => otherIssues.Items.Any(x2 => x2.Id == x1.Id));
  423. }
  424. }