1+ using Microsoft . Extensions . DependencyInjection ;
2+ using NUnit . Framework ;
3+ using RocksDb . Extensions . Protobuf ;
4+ using RocksDb . Extensions . Tests . Utils ;
5+
6+ namespace RocksDb . Extensions . Tests ;
7+
8+ public class KeyedStoreTests
9+ {
10+ [ Test ]
11+ public void should_resolve_rocksdb_stores_as_keyed_services_when_registered_under_different_column_names ( )
12+ {
13+ // Arrange
14+ using var testFixture = TestFixture . Create ( rockDb =>
15+ {
16+ _ = rockDb . AddStore < CacheKey , CacheValue , RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-1" ) ;
17+ _ = rockDb . AddStore < CacheKey , CacheValue , RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-2" ) ;
18+ } , options =>
19+ {
20+ options . SerializerFactories . Clear ( ) ;
21+ options . SerializerFactories . Add ( new ProtobufSerializerFactory ( ) ) ;
22+ } ) ;
23+
24+ // Act
25+ var store1 = testFixture . ServiceProvider . GetRequiredKeyedService < RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-1" ) ;
26+ var store2 = testFixture . ServiceProvider . GetRequiredKeyedService < RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-2" ) ;
27+
28+ // Assert
29+ Assert . Multiple ( ( ) =>
30+ {
31+ Assert . That ( store1 , Is . Not . Null ) ;
32+ Assert . That ( store2 , Is . Not . Null ) ;
33+ Assert . That ( ReferenceEquals ( store1 , store2 ) , Is . False ) ;
34+ } ) ;
35+ }
36+
37+ [ Test ]
38+ public void should_throw_when_resolving_non_existent_store ( )
39+ {
40+ // Arrange
41+ using var testFixture = TestFixture . Create ( rockDb =>
42+ {
43+ _ = rockDb . AddStore < CacheKey , CacheValue , RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-1" ) ;
44+ } , options =>
45+ {
46+ options . SerializerFactories . Clear ( ) ;
47+ options . SerializerFactories . Add ( new ProtobufSerializerFactory ( ) ) ;
48+ } ) ;
49+
50+ // Act & Assert
51+ Assert . Throws < InvalidOperationException > ( ( ) => testFixture . ServiceProvider . GetRequiredKeyedService < RocksDbGenericStore < CacheKey , CacheValue > > ( "non-existent-store" ) ) ;
52+ }
53+
54+ [ Test ]
55+ public void should_resolve_default_store_as_first_registered_keyed_service ( )
56+ {
57+ // Arrange
58+ using var testFixture = TestFixture . Create ( rockDb =>
59+ {
60+ _ = rockDb . AddStore < CacheKey , CacheValue , RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-1" ) ;
61+ _ = rockDb . AddStore < CacheKey , CacheValue , RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-2" ) ;
62+ } , options =>
63+ {
64+ options . SerializerFactories . Clear ( ) ;
65+ options . SerializerFactories . Add ( new ProtobufSerializerFactory ( ) ) ;
66+ } ) ;
67+
68+ // Act
69+ var store1 = testFixture . ServiceProvider . GetRequiredKeyedService < RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-1" ) ;
70+ var store2 = testFixture . ServiceProvider . GetRequiredKeyedService < RocksDbGenericStore < CacheKey , CacheValue > > ( "my-store-2" ) ;
71+ var store = testFixture . ServiceProvider . GetRequiredService < RocksDbGenericStore < CacheKey , CacheValue > > ( ) ;
72+
73+ // Assert
74+ Assert . Multiple ( ( ) =>
75+ {
76+ Assert . That ( ReferenceEquals ( store1 , store ) , Is . True ) ;
77+ Assert . That ( ReferenceEquals ( store2 , store ) , Is . False ) ;
78+ } ) ;
79+ }
80+ }
0 commit comments