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