-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTestDistributedFileStoreCacheClass.cs
More file actions
206 lines (167 loc) · 6.54 KB
/
Copy pathTestDistributedFileStoreCacheClass.cs
File metadata and controls
206 lines (167 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using Microsoft.Extensions.DependencyInjection;
using Net.DistributedFileStoreCache;
using Test.TestHelpers;
using TestSupport.Helpers;
using Xunit;
using Xunit.Abstractions;
using Xunit.Extensions.AssertExtensions;
namespace Test.UnitTests;
// see https://stackoverflow.com/questions/1408175/execute-unit-tests-serially-rather-than-in-parallel
[Collection("Sequential")]
public class TestDistributedFileStoreCacheClass
{
private readonly IDistributedFileStoreCacheClass _fsCache;
private readonly DistributedFileStoreCacheOptions _options;
private readonly ITestOutputHelper _output;
public TestDistributedFileStoreCacheClass(ITestOutputHelper output)
{
_output = output;
var services = new ServiceCollection();
_options = services.AddDistributedFileStoreCache(options =>
{
options.WhichVersion = FileStoreCacheVersions.Class;
options.PathToCacheFileDirectory = TestData.GetTestDataDir();
options.SecondPartOfCacheFileName = GetType().Name;
options.TurnOffStaticFilePathCheck = true;
});
var serviceProvider = services.BuildServiceProvider();
_fsCache = serviceProvider.GetRequiredService<IDistributedFileStoreCacheClass>();
}
private class JsonClass1
{
public int MyInt { get; set; }
public string MyString { get; set; }
}
private class JsonClass2
{
public JsonClass1 MyClass1 { get; set; }
public int MyInt2 { get; set; }
}
[Fact]
public void DistributedFileStoreCacheSetClass_SetJsonClass1()
{
//SETUP
_fsCache.ClearAll();
//ATTEMPT
_fsCache.SetClass("test", new JsonClass1{MyInt = 1, MyString = "Hello"});
//VERIFY
var allValues = _fsCache.GetAllKeyValues();
allValues.Count.ShouldEqual(1);
allValues["test"].ShouldEqual("{\"MyInt\":1,\"MyString\":\"Hello\"}");
_options.DisplayCacheFile(_output);
}
[Fact]
public void DistributedFileStoreCacheSetClass_GetClassFromStringJsonClass1()
{
//SETUP
_fsCache.ClearAll();
_fsCache.SetClass("test", new JsonClass1 { MyInt = 1, MyString = "Hello" });
//ATTEMPT
var allValuesDict = _fsCache.GetAllKeyValues();
var jsonClass1 = _fsCache.GetClassFromString<JsonClass1>(allValuesDict ["test"]);
//VERIFY
jsonClass1.ShouldBeType<JsonClass1>();
jsonClass1.ShouldNotBeNull();
jsonClass1.MyInt.ShouldEqual(1);
jsonClass1.MyString.ShouldEqual("Hello");
_options.DisplayCacheFile(_output);
}
[Fact]
public void DistributedFileStoreCacheSetClass_GetJsonClass1()
{
//SETUP
_fsCache.ClearAll();
_fsCache.SetClass("test", new JsonClass1 { MyInt = 1, MyString = "Hello" });
//ATTEMPT
var jsonClass1 = _fsCache.GetClass<JsonClass1>("test");
//VERIFY
jsonClass1.ShouldBeType<JsonClass1>();
jsonClass1.ShouldNotBeNull();
jsonClass1.MyInt.ShouldEqual(1);
jsonClass1.MyString.ShouldEqual("Hello");
_options.DisplayCacheFile(_output);
}
[Fact]
public void DistributedFileStoreCacheSetClass_Bad()
{
//SETUP
_fsCache.ClearAll();
_fsCache.SetClass("test", new JsonClass1 { MyInt = 1, MyString = "Hello" });
//ATTEMPT
var jsonClass2 = _fsCache.GetClass<JsonClass2>("test");
//VERIFY
jsonClass2.ShouldBeType<JsonClass2>();
jsonClass2.ShouldNotBeNull();
jsonClass2.MyInt2.ShouldEqual(default);
jsonClass2.MyClass1.ShouldEqual(null);
}
[Fact]
public void DistributedFileStoreCacheSetClass_JsonClass2()
{
//SETUP
_fsCache.ClearAll();
//ATTEMPT
_fsCache.SetClass("test", new JsonClass2 { MyInt2 = 3, MyClass1 = new JsonClass1{ MyInt = 1, MyString = "Hello" } });
//VERIFY
var allValues = _fsCache.GetAllKeyValues();
allValues.Count.ShouldEqual(1);
allValues["test"].ShouldEqual("{\"MyClass1\":{\"MyInt\":1,\"MyString\":\"Hello\"},\"MyInt2\":3}");
_options.DisplayCacheFile(_output);
}
[Fact]
public void DistributedFileStoreCacheSetClass_Unicode()
{
//SETUP
_fsCache.ClearAll();
var unicode = "בָּרוּךְ אַתָּה ה' אֱ-לֹהֵינוּ, מֶלֶך הָעוֹלָם";
//ATTEMPT
_fsCache.SetClass("test", new JsonClass1 { MyInt = 1, MyString = unicode });
//VERIFY
var allValues = _fsCache.GetAllKeyValues();
allValues.Count.ShouldEqual(1);
allValues["test"].ShouldEqual("{\"MyInt\":1,\"MyString\":\"" + unicode + "\"}");
_options.DisplayCacheFile(_output);
}
[Fact]
public void DistributedFileStoreCacheSetClass_JsonClass_Example()
{
//SETUP
_fsCache.ClearAll();
//ATTEMPT
_fsCache.SetClass("test", new JsonClass2 { MyInt2 = 3,
MyClass1 = new JsonClass1 { MyInt = 1, MyString = "Hello" } });
//VERIFY
_fsCache.Get("test").ShouldEqual(
"{\"MyClass1\":{\"MyInt\":1,\"MyString\":\"Hello\"},\"MyInt2\":3}");
var jsonClass2 = _fsCache.GetClass<JsonClass2>("test");
jsonClass2.ShouldBeType<JsonClass2>();
jsonClass2.ShouldNotBeNull();
jsonClass2.MyInt2.ShouldEqual(3);
jsonClass2.MyClass1.ShouldNotBeNull();
jsonClass2.MyClass1.MyInt.ShouldEqual(1);
jsonClass2.MyClass1.MyString.ShouldEqual("Hello");
_options.DisplayCacheFile(_output);
//Example if no UnsafeRelaxedJsonEscaping
//"{\u0022MyClass1\u0022:{\u0022MyInt\u0022:1,\u0022MyString\u0022:\u0022Hello\u0022},\u0022MyInt\u0022:3}"}
}
[Fact]
public void DistributedFileStoreCacheSetManyClass_JsonClass2()
{
//SETUP
_fsCache.ClearAll();
//ATTEMPT
_fsCache.SetManyClass(new List<KeyValuePair<string, JsonClass1>>
{
new("test1", new JsonClass1 { MyInt = 1, MyString = "Hello" }),
new("test2", new JsonClass1 { MyInt = 2, MyString = "Goodbye" })
});
//VERIFY
var allValues = _fsCache.GetAllKeyValues();
allValues.Count.ShouldEqual(2);
allValues["test1"].ShouldEqual("{\"MyInt\":1,\"MyString\":\"Hello\"}");
allValues["test2"].ShouldEqual("{\"MyInt\":2,\"MyString\":\"Goodbye\"}");
_options.DisplayCacheFile(_output);
}
}