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

Commit 7e57621

Browse files
committed
Insert AccountCacheItem for connection.
When creating a `ModelService`, ensure that the correct `AccountCacheItem` is inserted.
1 parent 17aebf8 commit 7e57621

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/GitHub.App/Factories/ModelServiceFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel.Composition;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using GitHub.Caches;
67
using GitHub.Models;
78
using GitHub.Services;
89
using Microsoft.VisualStudio.Shell;
@@ -44,6 +45,8 @@ public async Task<IModelService> CreateAsync(IConnection connection)
4445
await apiClientFactory.Create(connection.HostAddress),
4546
await hostCacheFactory.Create(connection.HostAddress),
4647
avatarProvider);
48+
result.InsertUser(AccountCacheItem.Create(connection.User));
49+
cache.Add(connection, result);
4750
}
4851
}
4952
finally
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using GitHub.Caches;
4+
using GitHub.Extensions;
5+
using GitHub.Factories;
6+
using GitHub.Models;
7+
using GitHub.Primitives;
8+
using GitHub.Services;
9+
using NSubstitute;
10+
using Xunit;
11+
12+
namespace UnitTests.GitHub.App.Factories
13+
{
14+
public class ModelServiceFactoryTests : TestBaseClass
15+
{
16+
public class TheCreateAsyncMethod
17+
{
18+
[Fact]
19+
public async Task ShouldCreateDifferentModelServiceForDifferentHost()
20+
{
21+
var target = CreateTarget();
22+
var instance1 = await target.CreateAsync(CreateConnection("https://github.com"));
23+
var instance2 = await target.CreateAsync(CreateConnection("https://another.com"));
24+
25+
Assert.NotSame(instance1, instance2);
26+
}
27+
28+
[Fact]
29+
public async Task ShouldCreateDifferentModelServiceForDifferentConnectionsWithSameAddress()
30+
{
31+
var target = CreateTarget();
32+
var instance1 = await target.CreateAsync(CreateConnection("https://github.com"));
33+
var instance2 = await target.CreateAsync(CreateConnection("https://github.com"));
34+
35+
Assert.NotSame(instance1, instance2);
36+
}
37+
38+
[Fact]
39+
public async Task ShouldCacheModelServiceForHost()
40+
{
41+
var target = CreateTarget();
42+
var connection = CreateConnection("https://github.com");
43+
var instance1 = await target.CreateAsync(connection);
44+
var instance2 = await target.CreateAsync(connection);
45+
46+
Assert.Same(instance1, instance2);
47+
}
48+
49+
[Fact]
50+
public async Task ShouldInsertUser()
51+
{
52+
var hostCacheFactory = Substitute.For<IHostCacheFactory>();
53+
var target = CreateTarget(hostCacheFactory: hostCacheFactory);
54+
var connection = CreateConnection("https://github.com");
55+
var hostCache = await hostCacheFactory.Create(connection.HostAddress);
56+
var modelService = await target.CreateAsync(connection);
57+
58+
hostCache.Received().Insert("GitHub.Caches.AccountCacheItem___user", Arg.Any<byte[]>());
59+
}
60+
}
61+
62+
static ModelServiceFactory CreateTarget(
63+
IHostCacheFactory hostCacheFactory = null)
64+
{
65+
var apiClientFactory = Substitute.For<IApiClientFactory>();
66+
var avatarProvider = Substitute.For<IAvatarProvider>();
67+
68+
hostCacheFactory = hostCacheFactory ?? Substitute.For<IHostCacheFactory>();
69+
70+
return new ModelServiceFactory(
71+
apiClientFactory,
72+
hostCacheFactory,
73+
avatarProvider);
74+
}
75+
76+
static IConnection CreateConnection(string address, string login = "user")
77+
{
78+
var result = Substitute.For<IConnection>();
79+
var user = CreateOctokitUser(login, address);
80+
result.HostAddress.Returns(HostAddress.Create(address));
81+
result.User.Returns(user);
82+
return result;
83+
}
84+
}
85+
}

src/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
<Compile Include="GitHub.Api\SimpleApiClientTests.cs" />
224224
<Compile Include="GitHub.App\Caches\ImageCacheTests.cs" />
225225
<Compile Include="GitHub.App\Controllers\UIProviderTests.cs" />
226+
<Compile Include="GitHub.App\Factories\ModelServiceFactoryTests.cs" />
226227
<Compile Include="GitHub.App\Models\AccountModelTests.cs" />
227228
<Compile Include="GitHub.App\Models\ModelServiceTests.cs" />
228229
<Compile Include="GitHub.App\Controllers\UIControllerTests.cs" />

0 commit comments

Comments
 (0)