|
| 1 | +using Microsoft.Extensions.Caching.Distributed; |
| 2 | +using Microsoft.Extensions.Caching.Memory; |
| 3 | +using Microsoft.Extensions.Caching.StackExchangeRedis; |
| 4 | +using Microsoft.Extensions.Options; |
| 5 | +using Promact.Core.Caching; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Promact.Caching.Test |
| 13 | +{ |
| 14 | + [TestClass] |
| 15 | + public class InMemoryCacheTests |
| 16 | + { |
| 17 | + private IDistributedCache _distributedCache; |
| 18 | + private ICachingService _cacheService; |
| 19 | + private string[] _keys = new string[] { "TestKey1", "TestKey2", "TestKey3" }; |
| 20 | + [TestInitialize] |
| 21 | + public void Setup() |
| 22 | + { |
| 23 | + IOptions<MemoryDistributedCacheOptions> options = Options.Create(new MemoryDistributedCacheOptions()); |
| 24 | + _distributedCache = new MemoryDistributedCache(options); |
| 25 | + _cacheService = new DistributedCachingServices(_distributedCache); |
| 26 | + } |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public void AddDataSuccessTest() |
| 30 | + { |
| 31 | + var data = new TestData() { Name = "Test", Age = 20 }; |
| 32 | + _cacheService.Set("TestKey1", data); |
| 33 | + |
| 34 | + var result = _cacheService.Get<TestData>("TestKey1"); |
| 35 | + Assert.IsNotNull(result); |
| 36 | + Assert.AreEqual(data.Name, result.Name); |
| 37 | + Assert.AreEqual(data.Age, result.Age); |
| 38 | + } |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + public void GetAllKeysSuccessTest() |
| 42 | + { |
| 43 | + var data = new TestData() { Name = "Test", Age = 20 }; |
| 44 | + _cacheService.Set("TestKey1", data); |
| 45 | + _cacheService.Set("TestKey2", data); |
| 46 | + _cacheService.Set("TestKey3", data); |
| 47 | + |
| 48 | + var keys = _cacheService.GetAllKeys(); |
| 49 | + Assert.IsNotNull(keys); |
| 50 | + Assert.AreEqual(3, keys.Count); |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + public void GetAllDataSuccessTest() |
| 55 | + { |
| 56 | + var data = new TestData() { Name = "Test", Age = 20 }; |
| 57 | + _cacheService.Set("TestKey1", data); |
| 58 | + _cacheService.Set("TestKey2", data); |
| 59 | + _cacheService.Set("TestKey3", data); |
| 60 | + |
| 61 | + var result = _cacheService.GetAll(); |
| 62 | + Assert.IsNotNull(result); |
| 63 | + Assert.AreEqual(3, result.Count); |
| 64 | + } |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void NullDataTest() |
| 68 | + { |
| 69 | + Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Set<TestData>("TestKey1", null)); |
| 70 | + Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Set<TestData>(null, null)); |
| 71 | + Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Get<TestData>(null)); |
| 72 | + Assert.ThrowsException<ArgumentNullException>(() => _cacheService.Remove(null)); |
| 73 | + } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void AddDataRetriveNullTest() |
| 77 | + { |
| 78 | + var data = new TestData() { Name = "Test", Age = 20 }; |
| 79 | + _cacheService.Set("TestKey1", data); |
| 80 | + |
| 81 | + var result = _cacheService.Get<TestData>("TestKey2"); |
| 82 | + Assert.IsNull(result); |
| 83 | + } |
| 84 | + |
| 85 | + [TestCleanup] |
| 86 | + public void CleanUp() |
| 87 | + { |
| 88 | + _keys.ToList().ForEach(key => _cacheService.Remove(key)); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments