|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Amazon; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace ServiceStack.CacheAccess.AwsDynamoDb.Tests |
| 10 | +{ |
| 11 | + [TestFixture] |
| 12 | + public class DynamoDbCacheClientTests |
| 13 | + { |
| 14 | + // Replace these with your prod/test AWS account to run unit tests. |
| 15 | + private const string yourAwsAccountKey = "SOME.KEY"; |
| 16 | + private const string yourAwsSecretKey = "SOME.SECRET.KEY"; |
| 17 | + |
| 18 | + public class DummyObject |
| 19 | + { |
| 20 | + public string UserId { get; set; } |
| 21 | + public string Email { get; set; } |
| 22 | + public string Phone { get; set; } |
| 23 | + public List<string> Friends { get; set; } |
| 24 | + } |
| 25 | + |
| 26 | + private ICacheClient _client; |
| 27 | + private DummyObject _item; |
| 28 | + private string _itemCacheKey = "urn:dummyobject:john.doe"; |
| 29 | + private string _counterCacheKey = "urn:counter"; |
| 30 | + |
| 31 | + [SetUp] |
| 32 | + public void SetupTests() |
| 33 | + { |
| 34 | + |
| 35 | + _client = new DynamoDbCacheClient(yourAwsAccountKey, yourAwsSecretKey, |
| 36 | + RegionEndpoint.USEast1, "ICacheClientDynamoDb", 10, 5, true); |
| 37 | + // The primary item we'll be caching in the tests. |
| 38 | + _item = new DummyObject |
| 39 | + { |
| 40 | + UserId = "john.doe", |
| 41 | + Email = "john.doe@servicestack.net", |
| 42 | + Phone = "555-555-9876", |
| 43 | + Friends = new List<string> {"jane.doe", "jack.doe", "some.friend"} |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void DyanmoDb_ExerciseCacheClient() |
| 49 | + { |
| 50 | + // Expecting the Set operation to succeed |
| 51 | + bool setResponse = _client.Set<DummyObject>(_itemCacheKey, _item); |
| 52 | + Assert.AreEqual(true, setResponse); |
| 53 | + |
| 54 | + // Expecting the Get to return the item cached above |
| 55 | + var actual = _client.Get<DummyObject>(_itemCacheKey); |
| 56 | + Assert.IsNotNull(actual); |
| 57 | + Assert.AreEqual(_item.UserId, actual.UserId); |
| 58 | + |
| 59 | + // Expecting Add to return false since the item is already cached |
| 60 | + bool addResponse = _client.Add<DummyObject>(_itemCacheKey, _item); |
| 61 | + Assert.AreEqual(false, addResponse); |
| 62 | + |
| 63 | + // Expecting remove to succeed |
| 64 | + bool removeResponse = _client.Remove(_itemCacheKey); |
| 65 | + Assert.AreEqual(true, removeResponse); |
| 66 | + |
| 67 | + // Add the item back, expecting success |
| 68 | + addResponse = _client.Add<DummyObject>(_itemCacheKey, _item); |
| 69 | + Assert.AreEqual(true, addResponse); |
| 70 | + |
| 71 | + // Remove it again |
| 72 | + removeResponse = _client.Remove(_itemCacheKey); |
| 73 | + |
| 74 | + // Clear the counter if it exists |
| 75 | + removeResponse = _client.Remove(_counterCacheKey); |
| 76 | + |
| 77 | + // Initialize the counter, incResponse should be equal to 0 since the counter doesn't exist |
| 78 | + long incResponse = _client.Increment(_counterCacheKey, 0); |
| 79 | + |
| 80 | + // Increment by 1 |
| 81 | + long updatedIncResponse = _client.Increment(_counterCacheKey, 1); |
| 82 | + Assert.AreEqual(incResponse + 1, updatedIncResponse); |
| 83 | + // Decrement by 1 |
| 84 | + long decResponse = _client.Decrement(_counterCacheKey, 1); |
| 85 | + Assert.AreEqual(incResponse, decResponse); |
| 86 | + |
| 87 | + // Clear out the cache - this will cause a very long delete/re-create DynamoDB table sequence |
| 88 | + _client.FlushAll(); |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments