|
| 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 | +} |
0 commit comments