Skip to content

Commit c4afb50

Browse files
committed
Update unit tests to apply IDisposables
1 parent c25115e commit c4afb50

21 files changed

+655
-373
lines changed

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/DbCommandInterceptorProcessorTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.Options;
66
using Moq;
77
using Moq.Protected;
8+
using Assert = Xunit.Assert;
89

910
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
1011

@@ -348,7 +349,7 @@ public void ProcessExecutedCommands_NotifiesCachingSkippedEvent_WhenIsCrudComman
348349
public void ProcessExecutedCommands_ReturnsCachedTableRows()
349350
{
350351
// Arrange
351-
var expected = new EFTableRowsDataReader(new EFTableRows(), new EFCoreSecondLevelCacheSettings());
352+
using var expected = new EFTableRowsDataReader(new EFTableRows(), new EFCoreSecondLevelCacheSettings());
352353
var commandMock = new Mock<DbCommand>();
353354
var transaction = Mock.Of<DbTransaction>();
354355
var context = Mock.Of<DbContext>();
@@ -374,7 +375,7 @@ public void ProcessExecutedCommands_ReturnsCachedTableRows()
374375
public void ProcessExecutedCommands_NotifiesCacheHitEvent_WhenReturningCachedTableRows()
375376
{
376377
// Arrange
377-
var result = new EFTableRowsDataReader(new EFTableRows
378+
using var result = new EFTableRowsDataReader(new EFTableRows
378379
{
379380
TableName = string.Empty
380381
}, new EFCoreSecondLevelCacheSettings());

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFCacheDependenciesProcessorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.Extensions.Options;
33
using Moq;
4+
using Assert = Xunit.Assert;
45

56
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
67

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFCacheKeyPrefixProviderTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.Options;
22
using Moq;
3+
using Assert = Xunit.Assert;
34

45
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
56

@@ -16,14 +17,15 @@ public void Constructor_ThrowsArgumentNullException_WhenCacheSettingsIsNull()
1617
void Act() => new EFCacheKeyPrefixProvider(serviceProvider, null!);
1718

1819
// Act && Assert
19-
Assert.Throws<ArgumentNullException>("cacheSettings", Act);
20+
Assert.Throws<ArgumentNullException>(paramName: "cacheSettings", Act);
2021
}
2122

2223
[Fact]
2324
public void GetCacheKeyPrefix_ReturnsCacheKeyPrefixSelectorResult_WhenSelectorIsNotNull()
2425
{
2526
// Arrange
2627
var serviceProvider = new Mock<IServiceProvider>().Object;
28+
2729
var cacheSettings = Options.Create(new EFCoreSecondLevelCacheSettings
2830
{
2931
CacheKeyPrefixSelector = _ => "CustomPrefix"
@@ -35,14 +37,15 @@ public void GetCacheKeyPrefix_ReturnsCacheKeyPrefixSelectorResult_WhenSelectorIs
3537
var actual = provider.GetCacheKeyPrefix();
3638

3739
// Assert
38-
Assert.Equal("CustomPrefix", actual);
40+
Assert.Equal(expected: "CustomPrefix", actual);
3941
}
4042

4143
[Fact]
4244
public void GetCacheKeyPrefix_ReturnsCacheKeyPrefix_WhenSelectorIsNull()
4345
{
4446
// Arrange
4547
var serviceProvider = new Mock<IServiceProvider>().Object;
48+
4649
var cacheSettings = Options.Create(new EFCoreSecondLevelCacheSettings
4750
{
4851
CacheKeyPrefix = "DefaultPrefix",
@@ -55,6 +58,6 @@ public void GetCacheKeyPrefix_ReturnsCacheKeyPrefix_WhenSelectorIsNull()
5558
var actual = provider.GetCacheKeyPrefix();
5659

5760
// Assert
58-
Assert.Equal("DefaultPrefix", actual);
61+
Assert.Equal(expected: "DefaultPrefix", actual);
5962
}
6063
}

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFCacheKeyProviderTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.Options;
77
using Moq;
88
using Moq.Protected;
9+
using Assert = Xunit.Assert;
910

1011
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
1112

@@ -192,7 +193,8 @@ public void GetEFCacheKey_ReturnsExpectedCacheKey()
192193
.SetupGet<DbParameterCollection>(propertyName: "DbParameterCollection")
193194
.Returns(dbParameterCollectionMock.Object);
194195

195-
dbParameterCollectionMock.Setup(x => x.GetEnumerator()).Returns(parameters.GetEnumerator());
196+
using var enumerator = parameters.GetEnumerator();
197+
dbParameterCollectionMock.Setup(x => x.GetEnumerator()).Returns(enumerator);
196198

197199
_cacheDependenciesProcessorMock.Setup(x => x.GetCacheDependencies(commandMock.Object, context, cachePolicy))
198200
.Returns(expected.CacheDependencies as SortedSet<string>);
@@ -246,7 +248,8 @@ public void GetEFCacheKey_ReturnsExpectedCacheKeyWithPrefix()
246248
.SetupGet<DbParameterCollection>(propertyName: "DbParameterCollection")
247249
.Returns(dbParameterCollectionMock.Object);
248250

249-
dbParameterCollectionMock.Setup(x => x.GetEnumerator()).Returns(parameters.GetEnumerator());
251+
using var enumerator = parameters.GetEnumerator();
252+
dbParameterCollectionMock.Setup(x => x.GetEnumerator()).Returns(enumerator);
250253

251254
_cacheDependenciesProcessorMock.Setup(x => x.GetCacheDependencies(commandMock.Object, context, cachePolicy))
252255
.Returns(expected.CacheDependencies as SortedSet<string>);

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFCacheKeyTests.cs

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.EntityFrameworkCore;
22
using Moq;
3+
using Assert = Xunit.Assert;
34

45
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
56

@@ -10,13 +11,19 @@ public class EFCacheKeyTests
1011
public void Equals_ReturnsTrue_WhenKeyHashAndDbContextAreEqual()
1112
{
1213
// Arrange
13-
var cacheKey1 = new EFCacheKey(new HashSet<string> { "Entity1" })
14+
var cacheKey1 = new EFCacheKey(new HashSet<string>
15+
{
16+
"Entity1"
17+
})
1418
{
1519
KeyHash = "hash1",
1620
DbContext = typeof(DbContext)
1721
};
1822

19-
var cacheKey2 = new EFCacheKey(new HashSet<string> { "Entity2" })
23+
var cacheKey2 = new EFCacheKey(new HashSet<string>
24+
{
25+
"Entity2"
26+
})
2027
{
2128
KeyHash = "hash1",
2229
DbContext = typeof(DbContext)
@@ -33,12 +40,15 @@ public void Equals_ReturnsTrue_WhenKeyHashAndDbContextAreEqual()
3340
public void Equals_ReturnsFalse_WhenObjIsNotEFCacheKey()
3441
{
3542
// Arrange
36-
var cacheKey1 = new EFCacheKey(new HashSet<string> { "Entity1" })
43+
var cacheKey1 = new EFCacheKey(new HashSet<string>
44+
{
45+
"Entity1"
46+
})
3747
{
3848
KeyHash = "hash1",
3949
DbContext = typeof(DbContext)
4050
};
41-
51+
4252
var obj = new object();
4353

4454
// Act
@@ -52,13 +62,19 @@ public void Equals_ReturnsFalse_WhenObjIsNotEFCacheKey()
5262
public void Equals_ReturnsFalse_WhenKeyHashIsDifferent()
5363
{
5464
// Arrange
55-
var cacheKey1 = new EFCacheKey(new HashSet<string> { "Entity1" })
65+
var cacheKey1 = new EFCacheKey(new HashSet<string>
66+
{
67+
"Entity1"
68+
})
5669
{
5770
KeyHash = "hash1",
5871
DbContext = typeof(DbContext)
5972
};
60-
61-
var cacheKey2 = new EFCacheKey(new HashSet<string> { "Entity2" })
73+
74+
var cacheKey2 = new EFCacheKey(new HashSet<string>
75+
{
76+
"Entity2"
77+
})
6278
{
6379
KeyHash = "hash2",
6480
DbContext = typeof(DbContext)
@@ -75,13 +91,19 @@ public void Equals_ReturnsFalse_WhenKeyHashIsDifferent()
7591
public void Equals_ReturnsFalse_WhenDbContextIsDifferent()
7692
{
7793
// Arrange
78-
var cacheKey1 = new EFCacheKey(new HashSet<string> { "Entity1" })
94+
var cacheKey1 = new EFCacheKey(new HashSet<string>
95+
{
96+
"Entity1"
97+
})
7998
{
8099
KeyHash = "hash1",
81100
DbContext = typeof(DbContext)
82101
};
83102

84-
var cacheKey2 = new EFCacheKey(new HashSet<string> { "Entity2" })
103+
var cacheKey2 = new EFCacheKey(new HashSet<string>
104+
{
105+
"Entity2"
106+
})
85107
{
86108
KeyHash = "hash1",
87109
DbContext = Mock.Of<DbContext>().GetType()
@@ -98,13 +120,19 @@ public void Equals_ReturnsFalse_WhenDbContextIsDifferent()
98120
public void GetHashCode_ReturnsSameHashCode_ForEqualObjects()
99121
{
100122
// Arrange
101-
var cacheKey1 = new EFCacheKey(new HashSet<string> { "Entity1" })
123+
var cacheKey1 = new EFCacheKey(new HashSet<string>
124+
{
125+
"Entity1"
126+
})
102127
{
103-
KeyHash = "hash1",
128+
KeyHash = "hash1",
104129
DbContext = typeof(DbContext)
105130
};
106131

107-
var cacheKey2 = new EFCacheKey(new HashSet<string> { "Entity2" })
132+
var cacheKey2 = new EFCacheKey(new HashSet<string>
133+
{
134+
"Entity2"
135+
})
108136
{
109137
KeyHash = "hash1",
110138
DbContext = typeof(DbContext)
@@ -122,13 +150,19 @@ public void GetHashCode_ReturnsSameHashCode_ForEqualObjects()
122150
public void GetHashCode_ReturnsDifferentHashCode_ForDifferentObjects()
123151
{
124152
// Arrange
125-
var cacheKey1 = new EFCacheKey(new HashSet<string> { "Entity1" })
153+
var cacheKey1 = new EFCacheKey(new HashSet<string>
154+
{
155+
"Entity1"
156+
})
126157
{
127158
KeyHash = "hash1",
128159
DbContext = typeof(DbContext)
129160
};
130161

131-
var cacheKey2 = new EFCacheKey(new HashSet<string> { "Entity2" })
162+
var cacheKey2 = new EFCacheKey(new HashSet<string>
163+
{
164+
"Entity2"
165+
})
132166
{
133167
KeyHash = "hash2",
134168
DbContext = typeof(DbContext)
@@ -146,7 +180,11 @@ public void GetHashCode_ReturnsDifferentHashCode_ForDifferentObjects()
146180
public void ToString_ReturnsExpectedFormat()
147181
{
148182
// Arrange
149-
var cacheKey = new EFCacheKey(new HashSet<string> { "Entity1", "Entity2" })
183+
var cacheKey = new EFCacheKey(new HashSet<string>
184+
{
185+
"Entity1",
186+
"Entity2"
187+
})
150188
{
151189
KeyHash = "hash1",
152190
DbContext = typeof(DbContext)
@@ -156,6 +194,6 @@ public void ToString_ReturnsExpectedFormat()
156194
var actual = cacheKey.ToString();
157195

158196
// Assert
159-
Assert.Equal("KeyHash: hash1, DbContext: DbContext, CacheDependencies: Entity1, Entity2.", actual);
197+
Assert.Equal(expected: "KeyHash: hash1, DbContext: DbContext, CacheDependencies: Entity1, Entity2.", actual);
160198
}
161199
}

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFCachePolicyParserTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.Extensions.Options;
33
using Moq;
4+
using Assert = Xunit.Assert;
45

56
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
67

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFCacheServiceCheckTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.Extensions.Options;
33
using Moq;
4+
using Assert = Xunit.Assert;
45

56
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
67

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/EFCacheServiceProviderTests.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Caching.Memory;
22
using Microsoft.Extensions.DependencyInjection;
33
using Moq;
4+
using Assert = Xunit.Assert;
45

56
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
67

@@ -9,7 +10,7 @@ public class EFCacheServiceProviderTests
910
[Fact]
1011
public void TestCacheInvalidationWithTwoRoots()
1112
{
12-
var efCacheServiceProvider = CreateMemoryCacheServiceProvider();
13+
var (efCacheServiceProvider, _) = CreateMemoryCacheServiceProvider();
1314

1415
var efCachePolicy = new EFCachePolicy().Timeout(TimeSpan.FromMinutes(value: 10))
1516
.ExpirationMode(CacheExpirationMode.Absolute);
@@ -66,7 +67,7 @@ public void TestCacheInvalidationWithTwoRoots()
6667
[Fact]
6768
public void TestCacheInvalidationWithOneRoot()
6869
{
69-
var efCacheServiceProvider = CreateMemoryCacheServiceProvider();
70+
var (efCacheServiceProvider, _) = CreateMemoryCacheServiceProvider();
7071

7172
var efCachePolicy = new EFCachePolicy().Timeout(TimeSpan.FromMinutes(value: 10))
7273
.ExpirationMode(CacheExpirationMode.Absolute);
@@ -121,7 +122,7 @@ public void TestCacheInvalidationWithOneRoot()
121122
[Fact]
122123
public void TestObjectCacheInvalidationWithOneRoot()
123124
{
124-
var efCacheServiceProvider = CreateMemoryCacheServiceProvider();
125+
var (efCacheServiceProvider, _) = CreateMemoryCacheServiceProvider();
125126

126127
var efCachePolicy = new EFCachePolicy().Timeout(TimeSpan.FromMinutes(value: 10))
127128
.ExpirationMode(CacheExpirationMode.Absolute);
@@ -186,7 +187,7 @@ public void TestObjectCacheInvalidationWithOneRoot()
186187
[Fact]
187188
public void TestCacheInvalidationWithSimilarRoots()
188189
{
189-
var efCacheServiceProvider = CreateMemoryCacheServiceProvider();
190+
var (efCacheServiceProvider, _) = CreateMemoryCacheServiceProvider();
190191

191192
var efCachePolicy = new EFCachePolicy().Timeout(TimeSpan.FromMinutes(value: 10))
192193
.ExpirationMode(CacheExpirationMode.Absolute);
@@ -242,7 +243,7 @@ public void TestCacheInvalidationWithSimilarRoots()
242243
[Fact]
243244
public void TestInsertingNullValues()
244245
{
245-
var efCacheServiceProvider = CreateMemoryCacheServiceProvider();
246+
var (efCacheServiceProvider, _) = CreateMemoryCacheServiceProvider();
246247

247248
var efCachePolicy = new EFCachePolicy().Timeout(TimeSpan.FromMinutes(value: 10))
248249
.ExpirationMode(CacheExpirationMode.Absolute);
@@ -266,7 +267,7 @@ public void TestInsertingNullValues()
266267
public async Task TestConcurrentCacheInsertAndInvalidation()
267268
{
268269
const string rootKey = "entity1";
269-
var efCacheServiceProvider = CreateMemoryCacheServiceProvider();
270+
var (efCacheServiceProvider, _) = CreateMemoryCacheServiceProvider();
270271

271272
var efCachePolicy = new EFCachePolicy().Timeout(TimeSpan.FromMinutes(value: 10))
272273
.ExpirationMode(CacheExpirationMode.Absolute);
@@ -318,7 +319,7 @@ void InvalidateCacheDependencies()
318319
public void TestParallelCacheInsertAndInvalidation()
319320
{
320321
const string rootKey = "entity1";
321-
var efCacheServiceProvider = CreateMemoryCacheServiceProvider();
322+
var (efCacheServiceProvider, _) = CreateMemoryCacheServiceProvider();
322323

323324
var efCachePolicy = new EFCachePolicy().Timeout(TimeSpan.FromMinutes(value: 10))
324325
.ExpirationMode(CacheExpirationMode.Absolute);
@@ -367,7 +368,7 @@ public void TestParallelCacheInsertAndInvalidation()
367368
[Fact]
368369
public void TestParallelInsertsAndRemoves()
369370
{
370-
var efCacheServiceProvider = CreateMemoryCacheServiceProvider();
371+
var (efCacheServiceProvider, _) = CreateMemoryCacheServiceProvider();
371372

372373
var efCachePolicy = new EFCachePolicy().Timeout(TimeSpan.FromMinutes(value: 10))
373374
.ExpirationMode(CacheExpirationMode.Absolute);
@@ -431,17 +432,18 @@ public void TestParallelInsertsAndRemoves()
431432
Assert.Null(value1);
432433
}
433434

434-
private static IEFCacheServiceProvider CreateMemoryCacheServiceProvider()
435+
private static (IEFCacheServiceProvider, ServiceProvider) CreateMemoryCacheServiceProvider()
435436
{
436437
var services = new ServiceCollection();
437438
services.AddMemoryCache();
438439
services.AddSingleton<IMemoryCacheChangeTokenProvider, EFMemoryCacheChangeTokenProvider>();
439440
services.AddSingleton<IEFDebugLogger, EFDebugLogger>();
440-
var serviceProvider = services.BuildServiceProvider();
441441

442+
var serviceProvider = services.BuildServiceProvider();
443+
var memoryCache = serviceProvider.GetRequiredService<IMemoryCache>();
444+
var changeTokenProvider = serviceProvider.GetRequiredService<IMemoryCacheChangeTokenProvider>();
442445
var loggerMock = new Mock<IEFDebugLogger>();
443446

444-
return new EFMemoryCacheServiceProvider(serviceProvider.GetRequiredService<IMemoryCache>(),
445-
serviceProvider.GetRequiredService<IMemoryCacheChangeTokenProvider>(), loggerMock.Object);
447+
return (new EFMemoryCacheServiceProvider(memoryCache, changeTokenProvider, loggerMock.Object), serviceProvider);
446448
}
447449
}

0 commit comments

Comments
 (0)