|
| 1 | +using NUnit.Framework; |
| 2 | +using StackExchange.Redis.StackExchange.Redis.KeyspaceIsolation; |
| 3 | +using System; |
| 4 | + |
| 5 | +namespace StackExchange.Redis.Tests |
| 6 | +{ |
| 7 | + |
| 8 | + [TestFixture] |
| 9 | + public class WithKeyPrefixTests : TestBase |
| 10 | + { |
| 11 | + [Test] |
| 12 | + public void BlankPrefixYieldsSame_Bytes() |
| 13 | + { |
| 14 | + using (var conn = Create()) |
| 15 | + { |
| 16 | + var raw = conn.GetDatabase(1); |
| 17 | + var prefixed = raw.WithKeyPrefix(new byte[0]); |
| 18 | + Assert.AreSame(raw, prefixed); |
| 19 | + } |
| 20 | + } |
| 21 | + [Test] |
| 22 | + public void BlankPrefixYieldsSame_String() |
| 23 | + { |
| 24 | + using (var conn = Create()) |
| 25 | + { |
| 26 | + var raw = conn.GetDatabase(1); |
| 27 | + var prefixed = raw.WithKeyPrefix(""); |
| 28 | + Assert.AreSame(raw, prefixed); |
| 29 | + } |
| 30 | + } |
| 31 | + [Test, ExpectedException(typeof(ArgumentNullException))] |
| 32 | + public void NullPrefixIsError_Bytes() |
| 33 | + { |
| 34 | + using (var conn = Create()) |
| 35 | + { |
| 36 | + var raw = conn.GetDatabase(1); |
| 37 | + var prefixed = raw.WithKeyPrefix((byte[])null); |
| 38 | + } |
| 39 | + } |
| 40 | + [Test, ExpectedException(typeof(ArgumentNullException))] |
| 41 | + public void NullPrefixIsError_String() |
| 42 | + { |
| 43 | + using (var conn = Create()) |
| 44 | + { |
| 45 | + var raw = conn.GetDatabase(1); |
| 46 | + var prefixed = raw.WithKeyPrefix((string)null); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + [Test, ExpectedException(typeof(ArgumentNullException))] |
| 51 | + [TestCase("abc")] |
| 52 | + [TestCase("")] |
| 53 | + [TestCase(null)] |
| 54 | + public void NullDatabaseIsError(string prefix) |
| 55 | + { |
| 56 | + IDatabase raw = null; |
| 57 | + var prefixed = raw.WithKeyPrefix(prefix); |
| 58 | + } |
| 59 | + [Test] |
| 60 | + public void BasicSmokeTest() |
| 61 | + { |
| 62 | + using(var conn = Create()) |
| 63 | + { |
| 64 | + var raw = conn.GetDatabase(1); |
| 65 | + |
| 66 | + var foo = raw.WithKeyPrefix("foo"); |
| 67 | + var foobar = foo.WithKeyPrefix("bar"); |
| 68 | + |
| 69 | + string key = Me(); |
| 70 | + |
| 71 | + string s = Guid.NewGuid().ToString(), t = Guid.NewGuid().ToString(); |
| 72 | + |
| 73 | + foo.StringSet(key, s); |
| 74 | + var val = (string)foo.StringGet(key); |
| 75 | + Assert.AreEqual(s, val); // fooBasicSmokeTest |
| 76 | + |
| 77 | + foobar.StringSet(key, t); |
| 78 | + val = (string)foobar.StringGet(key); |
| 79 | + Assert.AreEqual(t, val); // foobarBasicSmokeTest |
| 80 | + |
| 81 | + val = (string)foo.StringGet("bar" + key); |
| 82 | + Assert.AreEqual(t, val); // foobarBasicSmokeTest |
| 83 | + |
| 84 | + val = (string)raw.StringGet("foo" + key); |
| 85 | + Assert.AreEqual(s, val); // fooBasicSmokeTest |
| 86 | + |
| 87 | + val = (string)raw.StringGet("foobar" + key); |
| 88 | + Assert.AreEqual(t, val); // foobarBasicSmokeTest |
| 89 | + } |
| 90 | + } |
| 91 | + [Test] |
| 92 | + public void ConditionTest() |
| 93 | + { |
| 94 | + using(var conn = Create()) |
| 95 | + { |
| 96 | + var raw = conn.GetDatabase(2); |
| 97 | + |
| 98 | + var foo = raw.WithKeyPrefix("tran:"); |
| 99 | + |
| 100 | + raw.KeyDelete("tran:abc"); |
| 101 | + raw.KeyDelete("tran:i"); |
| 102 | + |
| 103 | + // execute while key exists |
| 104 | + raw.StringSet("tran:abc", "def"); |
| 105 | + var tran = foo.CreateTransaction(); |
| 106 | + tran.AddCondition(Condition.KeyExists("abc")); |
| 107 | + tran.StringIncrementAsync("i"); |
| 108 | + tran.Execute(); |
| 109 | + |
| 110 | + int i = (int)raw.StringGet("tran:i"); |
| 111 | + Assert.AreEqual(1, i); |
| 112 | + |
| 113 | + // repeat without key |
| 114 | + raw.KeyDelete("tran:abc"); |
| 115 | + tran = foo.CreateTransaction(); |
| 116 | + tran.AddCondition(Condition.KeyExists("abc")); |
| 117 | + tran.StringIncrementAsync("i"); |
| 118 | + tran.Execute(); |
| 119 | + |
| 120 | + i = (int)raw.StringGet("tran:i"); |
| 121 | + Assert.AreEqual(1, i); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments