PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests/Reactive/ObservableRepositoryCommentsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 358 lines | 286 code | 72 blank | 0 comment | 8 complexity | 42036612f549044c377ecde6f43336aa MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using NSubstitute;
  4. using Octokit.Reactive;
  5. using Xunit;
  6. namespace Octokit.Tests.Reactive
  7. {
  8. public class ObservableRepositoryCommentsClientTests
  9. {
  10. public class TheCtor
  11. {
  12. [Fact]
  13. public void EnsuresNonNullArguments()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => new ObservableRepositoryCommentsClient(null));
  16. }
  17. }
  18. public class TheGetMethod
  19. {
  20. [Fact]
  21. public void RequestsCorrectUrl()
  22. {
  23. var gitHub = Substitute.For<IGitHubClient>();
  24. var client = new ObservableRepositoryCommentsClient(gitHub);
  25. client.Get("fake", "repo", 42);
  26. gitHub.Received().Repository.Comment.Get("fake", "repo", 42);
  27. }
  28. [Fact]
  29. public void RequestsCorrectUrlWithRepositoryId()
  30. {
  31. var gitHub = Substitute.For<IGitHubClient>();
  32. var client = new ObservableRepositoryCommentsClient(gitHub);
  33. client.Get(1, 42);
  34. gitHub.Received().Repository.Comment.Get(1, 42);
  35. }
  36. [Fact]
  37. public void EnsuresNonNullArguments()
  38. {
  39. var client = new ObservableRepositoryCommentsClient(Substitute.For<IGitHubClient>());
  40. Assert.Throws<ArgumentNullException>(() => client.Get(null, "name", 1));
  41. Assert.Throws<ArgumentNullException>(() => client.Get("owner", null, 1));
  42. Assert.Throws<ArgumentException>(() => client.Get("", "name", 1));
  43. Assert.Throws<ArgumentException>(() => client.Get("owner", "", 1));
  44. }
  45. }
  46. public class TheGetAllForRepositoryMethod
  47. {
  48. [Fact]
  49. public void RequestsCorrectUrl()
  50. {
  51. var githubClient = Substitute.For<IGitHubClient>();
  52. var client = new ObservableRepositoryCommentsClient(githubClient);
  53. client.GetAllForRepository("fake", "repo");
  54. githubClient.Connection.Received(1).Get<List<CommitComment>>(Arg.Is<Uri>(uri => uri.ToString() == "repos/fake/repo/comments"),
  55. Args.EmptyDictionary, "application/vnd.github.squirrel-girl-preview");
  56. }
  57. [Fact]
  58. public void RequestsCorrectUrlWithRepositoryId()
  59. {
  60. var githubClient = Substitute.For<IGitHubClient>();
  61. var client = new ObservableRepositoryCommentsClient(githubClient);
  62. client.GetAllForRepository(1);
  63. githubClient.Connection.Received(1).Get<List<CommitComment>>(Arg.Is<Uri>(uri => uri.ToString() == "repositories/1/comments"),
  64. Args.EmptyDictionary, "application/vnd.github.squirrel-girl-preview");
  65. }
  66. [Fact]
  67. public void RequestsCorrectUrlWithApiOptions()
  68. {
  69. var githubClient = Substitute.For<IGitHubClient>();
  70. var client = new ObservableRepositoryCommentsClient(githubClient);
  71. var options = new ApiOptions
  72. {
  73. StartPage = 1,
  74. PageCount = 1,
  75. PageSize = 1
  76. };
  77. client.GetAllForRepository("fake", "repo", options);
  78. githubClient.Connection.Received(1).Get<List<CommitComment>>(Arg.Is<Uri>(uri => uri.ToString() == "repos/fake/repo/comments"),
  79. Arg.Is<Dictionary<string, string>>(dictionary => dictionary.Count == 2), "application/vnd.github.squirrel-girl-preview");
  80. }
  81. [Fact]
  82. public void RequestsCorrectUrlWithRepositoryIdWithApiOptions()
  83. {
  84. var githubClient = Substitute.For<IGitHubClient>();
  85. var client = new ObservableRepositoryCommentsClient(githubClient);
  86. var options = new ApiOptions
  87. {
  88. StartPage = 1,
  89. PageCount = 1,
  90. PageSize = 1
  91. };
  92. client.GetAllForRepository(1, options);
  93. githubClient.Connection.Received(1).Get<List<CommitComment>>(Arg.Is<Uri>(uri => uri.ToString() == "repositories/1/comments"),
  94. Arg.Is<Dictionary<string, string>>(dictionary => dictionary.Count == 2), "application/vnd.github.squirrel-girl-preview");
  95. }
  96. [Fact]
  97. public void EnsuresNonNullArguments()
  98. {
  99. var githubClient = Substitute.For<IGitHubClient>();
  100. var client = new ObservableRepositoryCommentsClient(githubClient);
  101. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", ApiOptions.None));
  102. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, ApiOptions.None));
  103. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null));
  104. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name"));
  105. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
  106. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, null));
  107. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name"));
  108. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", ""));
  109. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", ApiOptions.None));
  110. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", ApiOptions.None));
  111. }
  112. }
  113. public class TheGetAllForCommitMethod
  114. {
  115. [Fact]
  116. public void RequestsCorrectUrl()
  117. {
  118. var githubClient = Substitute.For<IGitHubClient>();
  119. var client = new ObservableRepositoryCommentsClient(githubClient);
  120. client.GetAllForCommit("fake", "repo", "sha");
  121. githubClient.Connection.Received().Get<List<CommitComment>>(Arg.Is(new Uri("repos/fake/repo/commits/sha/comments", UriKind.Relative)),
  122. Args.EmptyDictionary, "application/vnd.github.squirrel-girl-preview");
  123. }
  124. [Fact]
  125. public void RequestsCorrectUrlWithRepositoryId()
  126. {
  127. var githubClient = Substitute.For<IGitHubClient>();
  128. var client = new ObservableRepositoryCommentsClient(githubClient);
  129. client.GetAllForCommit(1, "sha");
  130. githubClient.Connection.Received().Get<List<CommitComment>>(Arg.Is(new Uri("repositories/1/commits/sha/comments", UriKind.Relative)),
  131. Args.EmptyDictionary, "application/vnd.github.squirrel-girl-preview");
  132. }
  133. [Fact]
  134. public void RequestsCorrectUrlWithApiOptions()
  135. {
  136. var githubClient = Substitute.For<IGitHubClient>();
  137. var client = new ObservableRepositoryCommentsClient(githubClient);
  138. var options = new ApiOptions
  139. {
  140. StartPage = 1,
  141. PageCount = 1,
  142. PageSize = 1
  143. };
  144. client.GetAllForCommit("fake", "repo", "sha", options);
  145. githubClient.Connection.Received().Get<List<CommitComment>>(Arg.Is(new Uri("repos/fake/repo/commits/sha/comments", UriKind.Relative)),
  146. Arg.Is<IDictionary<string, string>>(d => d.Count == 2), "application/vnd.github.squirrel-girl-preview");
  147. }
  148. [Fact]
  149. public void RequestsCorrectUrlWithRepositoryIdWithApiOptions()
  150. {
  151. var githubClient = Substitute.For<IGitHubClient>();
  152. var client = new ObservableRepositoryCommentsClient(githubClient);
  153. var options = new ApiOptions
  154. {
  155. StartPage = 1,
  156. PageCount = 1,
  157. PageSize = 1
  158. };
  159. client.GetAllForCommit(1, "sha", options);
  160. githubClient.Connection.Received().Get<List<CommitComment>>(Arg.Is(new Uri("repositories/1/commits/sha/comments", UriKind.Relative)),
  161. Arg.Is<IDictionary<string, string>>(d => d.Count == 2), "application/vnd.github.squirrel-girl-preview");
  162. }
  163. [Fact]
  164. public void EnsuresNonNullArguments()
  165. {
  166. var githubClient = Substitute.For<IGitHubClient>();
  167. var client = new ObservableRepositoryCommentsClient(githubClient);
  168. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit(null, "name", "sha"));
  169. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit("owner", null, "sha"));
  170. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit("owner", "name", null));
  171. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit(null, "name", "sha", ApiOptions.None));
  172. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit("owner", null, "sha", ApiOptions.None));
  173. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit("owner", "name", null, ApiOptions.None));
  174. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit("owner", "name", "sha", null));
  175. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit(1, null, ApiOptions.None));
  176. Assert.Throws<ArgumentNullException>(() => client.GetAllForCommit(1, "sha", null));
  177. Assert.Throws<ArgumentException>(() => client.GetAllForCommit("", "name", "sha"));
  178. Assert.Throws<ArgumentException>(() => client.GetAllForCommit("owner", "", "sha"));
  179. Assert.Throws<ArgumentException>(() => client.GetAllForCommit("owner", "name", ""));
  180. Assert.Throws<ArgumentException>(() => client.GetAllForCommit("", "name", "sha", ApiOptions.None));
  181. Assert.Throws<ArgumentException>(() => client.GetAllForCommit("owner", "", "sha", ApiOptions.None));
  182. Assert.Throws<ArgumentException>(() => client.GetAllForCommit("owner", "name", "", ApiOptions.None));
  183. Assert.Throws<ArgumentException>(() => client.GetAllForCommit(1, "", ApiOptions.None));
  184. }
  185. }
  186. public class TheCreateMethod
  187. {
  188. [Fact]
  189. public void PostsToCorrectUrl()
  190. {
  191. var newComment = new NewCommitComment("body");
  192. var githubClient = Substitute.For<IGitHubClient>();
  193. var client = new ObservableRepositoryCommentsClient(githubClient);
  194. client.Create("fake", "repo", "sha", newComment);
  195. githubClient.Repository.Comment.Received().Create("fake", "repo", "sha", newComment);
  196. }
  197. [Fact]
  198. public void PostsToCorrectUrlWithRepositoryId()
  199. {
  200. var newComment = new NewCommitComment("body");
  201. var githubClient = Substitute.For<IGitHubClient>();
  202. var client = new ObservableRepositoryCommentsClient(githubClient);
  203. client.Create(1, "sha", newComment);
  204. githubClient.Repository.Comment.Received().Create(1, "sha", newComment);
  205. }
  206. [Fact]
  207. public void EnsuresNonNullArguments()
  208. {
  209. var connection = Substitute.For<IApiConnection>();
  210. var client = new RepositoryCommentsClient(connection);
  211. Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(null, "name", "sha", new NewCommitComment("body")));
  212. Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("owner", null, "sha", new NewCommitComment("body")));
  213. Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("owner", "name", null, new NewCommitComment("body")));
  214. Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("owner", "name", "sha", null));
  215. Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(1, null, new NewCommitComment("body")));
  216. Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(1, "sha", null));
  217. Assert.ThrowsAsync<ArgumentException>(() => client.Create("", "name", "sha", new NewCommitComment("body")));
  218. Assert.ThrowsAsync<ArgumentException>(() => client.Create("owner", "", "sha", new NewCommitComment("body")));
  219. Assert.ThrowsAsync<ArgumentException>(() => client.Create("owner", "name", "", new NewCommitComment("body")));
  220. Assert.ThrowsAsync<ArgumentException>(() => client.Create(1, "", new NewCommitComment("body")));
  221. }
  222. }
  223. public class TheUpdateMethod
  224. {
  225. [Fact]
  226. public void PostsToCorrectUrl()
  227. {
  228. const string issueCommentUpdate = "updated comment";
  229. var githubClient = Substitute.For<IGitHubClient>();
  230. var client = new ObservableRepositoryCommentsClient(githubClient);
  231. client.Update("fake", "repo", 42, issueCommentUpdate);
  232. githubClient.Repository.Comment.Received().Update("fake", "repo", 42, issueCommentUpdate);
  233. }
  234. [Fact]
  235. public void PostsToCorrectUrlWithRepositoryId()
  236. {
  237. const string issueCommentUpdate = "updated comment";
  238. var githubClient = Substitute.For<IGitHubClient>();
  239. var client = new ObservableRepositoryCommentsClient(githubClient);
  240. client.Update(1, 42, issueCommentUpdate);
  241. githubClient.Repository.Comment.Received().Update(1, 42, issueCommentUpdate);
  242. }
  243. [Fact]
  244. public void EnsuresNonNullArguments()
  245. {
  246. var githubClient = Substitute.For<IGitHubClient>();
  247. var client = new ObservableRepositoryCommentsClient(githubClient);
  248. Assert.Throws<ArgumentNullException>(() => client.Update(null, "name", 42, "updated comment"));
  249. Assert.Throws<ArgumentNullException>(() => client.Update("owner", null, 42, "updated comment"));
  250. Assert.Throws<ArgumentNullException>(() => client.Update("owner", "name", 42, null));
  251. Assert.Throws<ArgumentNullException>(() => client.Update(1, 42, null));
  252. Assert.Throws<ArgumentException>(() => client.Update("", "name", 42, "updated comment"));
  253. Assert.Throws<ArgumentException>(() => client.Update("owner", "", 42, "updated comment"));
  254. }
  255. }
  256. public class TheDeleteMethod
  257. {
  258. [Fact]
  259. public void DeletesCorrectUrl()
  260. {
  261. var githubClient = Substitute.For<IGitHubClient>();
  262. var client = new ObservableRepositoryCommentsClient(githubClient);
  263. client.Delete("fake", "repo", 42);
  264. githubClient.Repository.Comment.Received().Delete("fake", "repo", 42);
  265. }
  266. [Fact]
  267. public void DeletesCorrectUrlWithRepositoryId()
  268. {
  269. var githubClient = Substitute.For<IGitHubClient>();
  270. var client = new ObservableRepositoryCommentsClient(githubClient);
  271. client.Delete(1, 42);
  272. githubClient.Repository.Comment.Received().Delete(1, 42);
  273. }
  274. [Fact]
  275. public void EnsuresNonNullArgumentsOrEmpty()
  276. {
  277. var githubClient = Substitute.For<IGitHubClient>();
  278. var client = new ObservableRepositoryCommentsClient(githubClient);
  279. Assert.Throws<ArgumentNullException>(() => client.Delete(null, "name", 42));
  280. Assert.Throws<ArgumentNullException>(() => client.Delete("owner", null, 42));
  281. Assert.Throws<ArgumentException>(() => client.Delete("", "name", 42));
  282. Assert.Throws<ArgumentException>(() => client.Delete("owner", "", 42));
  283. }
  284. }
  285. }
  286. }