1
+ using System ;
2
+ using System . Linq ;
3
+ using Albelli . Extensions . Caching . DynamoDb ;
4
+ using Amazon . DynamoDBv2 ;
5
+ using Microsoft . Extensions . Caching . Distributed ;
6
+ using Microsoft . Extensions . DependencyInjection ;
7
+ using Moq ;
8
+ using Xunit ;
9
+
10
+ namespace Albelli . Extensions . Caching . Integration
11
+ {
12
+ public class DynamoDbCacheServiceCollectionExtensionsTests
13
+ {
14
+ [ Fact ]
15
+ public void DynamoDb_Is_Registered_Correctly ( )
16
+ {
17
+ var serviceCollection = new ServiceCollection ( ) ;
18
+ serviceCollection . AddDynamoDbCache ( o =>
19
+ {
20
+ o . TableName = "test" ;
21
+ } ) ;
22
+ var distributedCache = serviceCollection . FirstOrDefault ( desc => desc . ServiceType == typeof ( IDistributedCache ) ) ;
23
+ Assert . NotNull ( distributedCache ) ;
24
+ Assert . Equal ( ServiceLifetime . Singleton , distributedCache . Lifetime ) ;
25
+ var systemClock = serviceCollection . FirstOrDefault ( desc => desc . ServiceType == typeof ( ISystemClock ) ) ;
26
+ Assert . NotNull ( systemClock ) ;
27
+ Assert . Equal ( ServiceLifetime . Singleton , systemClock . Lifetime ) ;
28
+ }
29
+
30
+ [ Fact ]
31
+ public void ServiceProvider_Throws_Without_A_DynamoDbClient_Registered_In_the_DI ( )
32
+ {
33
+ var serviceCollection = new ServiceCollection ( ) ;
34
+ serviceCollection . AddDynamoDbCache ( o =>
35
+ {
36
+ o . TableName = "test" ;
37
+ } ) ;
38
+ var serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
39
+ Assert . Throws < ArgumentNullException > ( ( ) => serviceProvider . GetService ( typeof ( IDistributedCache ) ) ) ;
40
+ }
41
+
42
+ [ Fact ]
43
+ public void ServiceProvider_Is_Retrieved_Successfully_With_A_DynamoDbClient_Registered_In_the_DI ( )
44
+ {
45
+ var serviceCollection = new ServiceCollection ( ) ;
46
+ serviceCollection . AddDynamoDbCache ( o =>
47
+ {
48
+ o . TableName = "test" ;
49
+ } ) ;
50
+ serviceCollection . AddSingleton < IAmazonDynamoDB > ( m => new Mock < AmazonDynamoDBClient > ( ) . Object ) ;
51
+ var serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
52
+ Assert . NotNull ( serviceProvider . GetService ( typeof ( IDistributedCache ) ) ) ;
53
+ }
54
+
55
+ [ Fact ]
56
+ public void ServiceProvider_Is_Retrieved_Successfully_With_A_Manually_Provided_DynamoDBClient ( )
57
+ {
58
+ var serviceCollection = new ServiceCollection ( ) ;
59
+ serviceCollection . AddDynamoDbCache ( o =>
60
+ {
61
+ o . TableName = "test" ;
62
+ o . CustomDynamoDbClient = new Mock < AmazonDynamoDBClient > ( ) . Object ;
63
+ } ) ;
64
+ var serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
65
+ Assert . NotNull ( serviceProvider . GetService ( typeof ( IDistributedCache ) ) ) ;
66
+ }
67
+ }
68
+ }
0 commit comments