Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit ea46c8a

Browse files
committed
Add tests for dynamically calling typed APIs with a runtime type
1 parent e3f8796 commit ea46c8a

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/ServiceStack.Redis.Tests/RedisBasicPersistenceProviderTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Linq;
44
using NUnit.Framework;
55
using ServiceStack.Model;
6+
using ServiceStack.Redis.Generic;
7+
using ServiceStack.Script;
68
using ServiceStack.Text;
79

810
namespace ServiceStack.Redis.Tests
@@ -158,6 +160,66 @@ public void Can_DeleteAll()
158160
Assert.That(idsRemaining, Is.Empty);
159161
}
160162

163+
[Test]
164+
public void Can_DeleteAll_with_runtime_type()
165+
{
166+
Redis.StoreAll(testModels);
167+
168+
var mi = Redis.GetType().GetMethod(nameof(RedisClient.DeleteAll));
169+
var genericMi = mi.MakeGenericMethod(typeof(TestModel));
170+
genericMi.Invoke(Redis, TypeConstants.EmptyObjectArray);
171+
172+
var allModels = Redis.GetAll<TestModel>();
173+
Assert.That(allModels, Is.Empty);
174+
var idsRemaining = Redis.GetAllItemsFromSet(TestModelIdsSetKey);
175+
Assert.That(idsRemaining, Is.Empty);
176+
}
177+
178+
[Test]
179+
public void Can_As_DeleteAll_with_runtime_type()
180+
{
181+
Redis.StoreAll(testModels);
182+
183+
var mi = Redis.GetType().GetMethod(nameof(RedisClient.As));
184+
var genericMi = mi.MakeGenericMethod(typeof(TestModel));
185+
var typedClient = genericMi.Invoke(Redis, TypeConstants.EmptyObjectArray);
186+
var deleteMi = typedClient.GetType().GetMethod(nameof(IRedisTypedClient<Type>.DeleteAll));
187+
deleteMi.Invoke(typedClient, TypeConstants.EmptyObjectArray);
188+
189+
var allModels = Redis.GetAll<TestModel>();
190+
Assert.That(allModels, Is.Empty);
191+
var idsRemaining = Redis.GetAllItemsFromSet(TestModelIdsSetKey);
192+
Assert.That(idsRemaining, Is.Empty);
193+
}
194+
195+
[Test]
196+
public void Can_As_DeleteAll_with_script()
197+
{
198+
Redis.StoreAll(testModels);
199+
200+
var context = new ScriptContext {
201+
ScriptLanguages = { ScriptLisp.Language },
202+
AllowScriptingOfAllTypes = true,
203+
ScriptMethods = {
204+
new ProtectedScripts()
205+
},
206+
Args = {
207+
["redis"] = Redis
208+
}
209+
}.Init();
210+
211+
var type = typeof(TestModel).FullName;
212+
context.EvaluateCode($"redis.call('DeleteAll<{type}>') |> return");
213+
// context.EvaluateCode($"redis.call('As<{type}>').call('DeleteAll') |> return");
214+
// context.RenderLisp($"(call redis \"DeleteAll<{type}>\")");
215+
// context.RenderLisp($"(call (call redis \"As<{type}>\") \"DeleteAll\")");
216+
217+
var allModels = Redis.GetAll<TestModel>();
218+
Assert.That(allModels, Is.Empty);
219+
var idsRemaining = Redis.GetAllItemsFromSet(TestModelIdsSetKey);
220+
Assert.That(idsRemaining, Is.Empty);
221+
}
222+
161223
[Test]
162224
public void Can_DeleteByIds()
163225
{

0 commit comments

Comments
 (0)