Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit ad65938

Browse files
committed
Add unit tests for SimpleApiClient
Want to start adding test coverage so others feel safe making changes.
1 parent d33b726 commit ad65938

File tree

3 files changed

+168
-2
lines changed

3 files changed

+168
-2
lines changed

src/GitHub.Api/SimpleApiClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class SimpleApiClient : ISimpleApiClient
1313
public HostAddress HostAddress { get; }
1414
public UriString OriginalUrl { get; }
1515

16-
readonly GitHubClient client;
16+
readonly IGitHubClient client;
1717
readonly Lazy<IEnterpriseProbeTask> enterpriseProbe;
1818
readonly Lazy<IWikiProbe> wikiProbe;
1919
static readonly SemaphoreSlim sem = new SemaphoreSlim(1);
@@ -23,7 +23,7 @@ public class SimpleApiClient : ISimpleApiClient
2323
bool? isEnterprise;
2424
bool? hasWiki;
2525

26-
internal SimpleApiClient(HostAddress hostAddress, UriString repoUrl, GitHubClient githubClient,
26+
public SimpleApiClient(HostAddress hostAddress, UriString repoUrl, IGitHubClient githubClient,
2727
Lazy<IEnterpriseProbeTask> enterpriseProbe, Lazy<IWikiProbe> wikiProbe)
2828
{
2929
HostAddress = hostAddress;
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using GitHub.Api;
4+
using GitHub.Primitives;
5+
using GitHub.Services;
6+
using NSubstitute;
7+
using Octokit;
8+
using Xunit;
9+
10+
public class SimpleApiClientTests
11+
{
12+
public class TheGetRepositoryMethod
13+
{
14+
[Fact]
15+
public async Task RetrievesRepositoryFromWeb()
16+
{
17+
var gitHubHost = HostAddress.GitHubDotComHostAddress;
18+
var gitHubClient = Substitute.For<IGitHubClient>();
19+
var repository = new Repository(42);
20+
gitHubClient.Repository.Get("github", "visualstudio").Returns(Task.FromResult(repository));
21+
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
22+
var wikiProbe = Substitute.For<IWikiProbe>();
23+
var client = new SimpleApiClient(
24+
gitHubHost,
25+
"https://github.com/github/visualstudio",
26+
gitHubClient,
27+
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
28+
new Lazy<IWikiProbe>(() => wikiProbe));
29+
30+
var result = await client.GetRepository();
31+
32+
Assert.Equal(42, result.Id);
33+
}
34+
35+
[Fact]
36+
public async Task RetrievesCachedRepositoryForSubsequentCalls()
37+
{
38+
var gitHubHost = HostAddress.GitHubDotComHostAddress;
39+
var gitHubClient = Substitute.For<IGitHubClient>();
40+
var repository = new Repository(42);
41+
gitHubClient.Repository.Get("github", "visualstudio")
42+
.Returns(_ => Task.FromResult(repository), _ => { throw new Exception("Should only be called once."); });
43+
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
44+
var wikiProbe = Substitute.For<IWikiProbe>();
45+
var client = new SimpleApiClient(
46+
gitHubHost,
47+
"https://github.com/github/visualstudio",
48+
gitHubClient,
49+
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
50+
new Lazy<IWikiProbe>(() => wikiProbe));
51+
await client.GetRepository();
52+
53+
var result = await client.GetRepository();
54+
55+
Assert.Equal(42, result.Id);
56+
}
57+
}
58+
59+
public class TheHasWikiMethod
60+
{
61+
[Theory]
62+
[InlineData(WikiProbeResult.Ok, true)]
63+
[InlineData(WikiProbeResult.Failed, false)]
64+
[InlineData(WikiProbeResult.NotFound, false)]
65+
public async Task ReturnsTrueWhenWikiProbeReturnsOk(WikiProbeResult probeResult, bool expected)
66+
{
67+
var gitHubHost = HostAddress.GitHubDotComHostAddress;
68+
var gitHubClient = Substitute.For<IGitHubClient>();
69+
var repository = CreateRepository(42, true);
70+
gitHubClient.Repository.Get("github", "visualstudio").Returns(Task.FromResult(repository));
71+
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
72+
var wikiProbe = Substitute.For<IWikiProbe>();
73+
wikiProbe.ProbeAsync(repository)
74+
.Returns(_ => Task.FromResult(probeResult), _ => { throw new Exception("Only call it once"); });
75+
var client = new SimpleApiClient(
76+
gitHubHost,
77+
"https://github.com/github/visualstudio",
78+
gitHubClient,
79+
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
80+
new Lazy<IWikiProbe>(() => wikiProbe));
81+
await client.GetRepository();
82+
83+
var result = client.HasWiki();
84+
85+
Assert.Equal(expected, result);
86+
Assert.Equal(expected, client.HasWiki());
87+
}
88+
89+
[Fact]
90+
public void ReturnsFalseWhenWeHaveNotRequestedRepository()
91+
{
92+
var gitHubHost = HostAddress.GitHubDotComHostAddress;
93+
var gitHubClient = Substitute.For<IGitHubClient>();
94+
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
95+
var wikiProbe = Substitute.For<IWikiProbe>();
96+
var client = new SimpleApiClient(
97+
gitHubHost,
98+
"https://github.com/github/visualstudio",
99+
gitHubClient,
100+
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
101+
new Lazy<IWikiProbe>(() => wikiProbe));
102+
103+
var result = client.HasWiki();
104+
105+
Assert.False(result);
106+
}
107+
}
108+
109+
public class TheIsEnterpriseMethod
110+
{
111+
[Theory]
112+
[InlineData(EnterpriseProbeResult.Ok, true)]
113+
[InlineData(EnterpriseProbeResult.Failed, false)]
114+
[InlineData(EnterpriseProbeResult.NotFound, false)]
115+
public async Task ReturnsTrueWhenEnterpriseProbeReturnsOk(EnterpriseProbeResult probeResult, bool expected)
116+
{
117+
var gitHubHost = HostAddress.GitHubDotComHostAddress;
118+
var gitHubClient = Substitute.For<IGitHubClient>();
119+
var repository = CreateRepository(42, true);
120+
gitHubClient.Repository.Get("github", "visualstudio").Returns(Task.FromResult(repository));
121+
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
122+
enterpriseProbe.ProbeAsync(Args.Uri)
123+
.Returns(_ => Task.FromResult(probeResult), _ => { throw new Exception("Only call it once"); });
124+
var wikiProbe = Substitute.For<IWikiProbe>();
125+
var client = new SimpleApiClient(
126+
gitHubHost,
127+
"https://github.com/github/visualstudio",
128+
gitHubClient,
129+
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
130+
new Lazy<IWikiProbe>(() => wikiProbe));
131+
await client.GetRepository();
132+
133+
var result = client.IsEnterprise();
134+
135+
Assert.Equal(expected, result);
136+
Assert.Equal(expected, client.IsEnterprise());
137+
}
138+
139+
[Fact]
140+
public void ReturnsFalseWhenWeHaveNotRequestedRepository()
141+
{
142+
var gitHubHost = HostAddress.GitHubDotComHostAddress;
143+
var gitHubClient = Substitute.For<IGitHubClient>();
144+
var enterpriseProbe = Substitute.For<IEnterpriseProbeTask>();
145+
var wikiProbe = Substitute.For<IWikiProbe>();
146+
var client = new SimpleApiClient(
147+
gitHubHost,
148+
"https://github.com/github/visualstudio",
149+
gitHubClient,
150+
new Lazy<IEnterpriseProbeTask>(() => enterpriseProbe),
151+
new Lazy<IWikiProbe>(() => wikiProbe));
152+
153+
var result = client.IsEnterprise();
154+
155+
Assert.False(result);
156+
}
157+
}
158+
159+
private static Repository CreateRepository(int id, bool hasWiki)
160+
{
161+
return new Repository("", "", "", "", "", "", "", id, new User(), "", "", "", "", "", false, false, 0, 0, 0, "",
162+
0, null, DateTimeOffset.Now, DateTimeOffset.Now, new RepositoryPermissions(), new User(), null, null, false,
163+
hasWiki, false);
164+
}
165+
}

src/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
</ItemGroup>
136136
<ItemGroup>
137137
<Compile Include="Args.cs" />
138+
<Compile Include="GitHub.App\Api\SimpleApiClientTests.cs" />
138139
<Compile Include="GitHub.App\Caches\ImageCacheTests.cs" />
139140
<Compile Include="GitHub.App\Models\ModelServiceTests.cs" />
140141
<Compile Include="GitHub.App\Controllers\UIControllerTests.cs" />

0 commit comments

Comments
 (0)