microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Common.Tests/Storage/LocalStorageTests.cs
120lines · modecode
| 1 | |
| 2 | using Microsoft.Teams.Common.Storage; |
| 3 | |
| 4 | namespace Microsoft.Teams.Common.Tests.Storage; |
| 5 | |
| 6 | public class LocalStorageTests |
| 7 | { |
| 8 | [Fact] |
| 9 | public void LocalStorage_SizeAndKeys_WorkAsExpected() |
| 10 | { |
| 11 | var storage = new LocalStorage<int>(); |
| 12 | storage.Set("a", 1); |
| 13 | storage.Set("b", 2); |
| 14 | |
| 15 | Assert.Equal(2, storage.Size); |
| 16 | Assert.Contains("a", storage.Keys); |
| 17 | Assert.Contains("b", storage.Keys); |
| 18 | } |
| 19 | |
| 20 | [Fact] |
| 21 | public void LocalStorage_ExistsAndGet_WorkAsExpected() |
| 22 | { |
| 23 | var storage = new LocalStorage<string>(); |
| 24 | storage.Set("foo", "bar"); |
| 25 | |
| 26 | Assert.True(storage.Exists("foo")); |
| 27 | Assert.Equal("bar", storage.Get("foo")); |
| 28 | Assert.Null(storage.Get("baz")); |
| 29 | } |
| 30 | |
| 31 | [Fact] |
| 32 | public async Task LocalStorage_AsyncMethods_WorkAsExpected() |
| 33 | { |
| 34 | var storage = new LocalStorage<string>(); |
| 35 | await storage.SetAsync("foo", "bar"); |
| 36 | |
| 37 | Assert.True(await storage.ExistsAsync("foo")); |
| 38 | Assert.Equal("bar", await storage.GetAsync("foo")); |
| 39 | } |
| 40 | |
| 41 | [Fact] |
| 42 | public void LocalStorage_GetGeneric_ReturnsCorrectType() |
| 43 | { |
| 44 | var storage = new LocalStorage<object>(); |
| 45 | storage.Set("num", 42); |
| 46 | |
| 47 | int? value = storage.Get<int>("num"); |
| 48 | Assert.Equal(42, value); |
| 49 | } |
| 50 | |
| 51 | [Fact] |
| 52 | public async Task LocalStorage_GetAsyncGeneric_ReturnsCorrectType() |
| 53 | { |
| 54 | var storage = new LocalStorage<object>(); |
| 55 | await storage.SetAsync("num", 42); |
| 56 | |
| 57 | int? value = await storage.GetAsync<int>("num"); |
| 58 | Assert.Equal(42, value); |
| 59 | } |
| 60 | |
| 61 | [Fact] |
| 62 | public void LocalStorage_Delete_RemovesKey() |
| 63 | { |
| 64 | var storage = new LocalStorage<string>(); |
| 65 | storage.Set("foo", "bar"); |
| 66 | storage.Delete("foo"); |
| 67 | |
| 68 | Assert.False(storage.Exists("foo")); |
| 69 | Assert.Null(storage.Get("foo")); |
| 70 | } |
| 71 | |
| 72 | [Fact] |
| 73 | public async Task LocalStorage_DeleteAsync_RemovesKey() |
| 74 | { |
| 75 | var storage = new LocalStorage<string>(); |
| 76 | await storage.SetAsync("foo", "bar"); |
| 77 | await storage.DeleteAsync("foo"); |
| 78 | |
| 79 | Assert.False(await storage.ExistsAsync("foo")); |
| 80 | Assert.Null(await storage.GetAsync("foo")); |
| 81 | } |
| 82 | |
| 83 | [Fact] |
| 84 | public void LocalStorage_Eviction_WorksWhenMaxIsSet() |
| 85 | { |
| 86 | var storage = new LocalStorage<int>(max: 2); |
| 87 | storage.Set("a", 1); |
| 88 | storage.Set("b", 2); |
| 89 | storage.Set("c", 3); // Should evict "a" |
| 90 | |
| 91 | Assert.False(storage.Exists("a")); |
| 92 | Assert.True(storage.Exists("b")); |
| 93 | Assert.True(storage.Exists("c")); |
| 94 | Assert.Equal(2, storage.Size); |
| 95 | } |
| 96 | |
| 97 | [Fact] |
| 98 | public void LocalStorage_Hit_MovesKeyToEnd() |
| 99 | { |
| 100 | var storage = new LocalStorage<int>(); |
| 101 | storage.Set("a", 1); |
| 102 | storage.Set("b", 2); |
| 103 | storage.Set("c", 3); |
| 104 | |
| 105 | // Access "b" to move it to the end |
| 106 | var result = storage.Get("b"); |
| 107 | Assert.Equal(2, result); |
| 108 | } |
| 109 | |
| 110 | [Fact] |
| 111 | public void LocalStorage_ConstructorWithData_InitializesCorrectly() |
| 112 | { |
| 113 | var data = new Dictionary<string, int> { { "x", 10 }, { "y", 20 } }; |
| 114 | var storage = new LocalStorage<int>(data, max: 5); |
| 115 | |
| 116 | Assert.True(storage.Exists("x")); |
| 117 | Assert.True(storage.Exists("y")); |
| 118 | Assert.Equal(2, storage.Size); |
| 119 | } |
| 120 | } |