Skip to content

Commit 79e77ba

Browse files
committed
Change project Name, Add JSON.GET command + tests
1 parent 06a8c35 commit 79e77ba

File tree

9 files changed

+105
-23
lines changed

9 files changed

+105
-23
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"type": "coreclr",
1010
"request": "launch",
1111
"preLaunchTask": "build",
12-
"program": "${workspaceFolder}/tests/RediSharp.Tests/bin/Debug/net6.0/RediSharp.Tests.dll",
12+
"program": "${workspaceFolder}/tests/NRedisStack.Tests/bin/Debug/net6.0/NRedisStack.Tests.dll",
1313
"args": [],
14-
"cwd": "${workspaceFolder}/tests/RediSharp.Tests",
14+
"cwd": "${workspaceFolder}/tests/NRedisStack.Tests",
1515
"console": "internalConsole",
1616
"stopAtEntry": false
1717
},

.vscode/tasks.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "process",
88
"args": [
99
"build",
10-
"${workspaceFolder}/tests/RediSharp.Tests/RediSharp.Tests.csproj",
10+
"${workspaceFolder}/tests/NRedisStack.Tests/NRedisStack.Tests.csproj",
1111
"/property:GenerateFullPaths=true",
1212
"/consoleloggerparameters:NoSummary"
1313
],
@@ -19,7 +19,7 @@
1919
"type": "process",
2020
"args": [
2121
"publish",
22-
"${workspaceFolder}/tests/RediSharp.Tests/RediSharp.Tests.csproj",
22+
"${workspaceFolder}/tests/NRedisStack.Tests/NRedisStack.Tests.csproj",
2323
"/property:GenerateFullPaths=true",
2424
"/consoleloggerparameters:NoSummary"
2525
],
@@ -33,7 +33,7 @@
3333
"watch",
3434
"run",
3535
"--project",
36-
"${workspaceFolder}/tests/RediSharp.Tests/RediSharp.Tests.csproj"
36+
"${workspaceFolder}/tests/NRedisStack.Tests/NRedisStack.Tests.csproj"
3737
],
3838
"problemMatcher": "$msCompile"
3939
}

RediSharp.sln renamed to NRedisStack.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.30114.105
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RediSharp.Core", "src\RediSharp.Core\RediSharp.Core.csproj", "{6F5E84A5-DFC1-4747-AF7D-97BDAFA9102D}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NRedisStack.Core", "src\NRedisStack.Core\NRedisStack.Core.csproj", "{6F5E84A5-DFC1-4747-AF7D-97BDAFA9102D}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RediSharp.Tests", "tests\RediSharp.Tests\RediSharp.Tests.csproj", "{73B56C35-BC92-4ACD-B077-7D55CD67B95F}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NRedisStack.Tests", "tests\NRedisStack.Tests\NRedisStack.Tests.csproj", "{73B56C35-BC92-4ACD-B077-7D55CD67B95F}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/RediSharp.Core/RedisJsonCommands.cs renamed to src/NRedisStack.Core/RedisJsonCommands.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using StackExchange.Redis;
22
using System.Text.Json;
33
using System.Text.Json.Serialization;
4-
namespace RediSharp.Core;
4+
namespace NRedisStack.Core;
55

66
public static class RedisJsonCommands
77
{
@@ -11,7 +11,12 @@ public static class RedisJsonCommands
1111
};
1212
public static RedisResult JsonSet(this IDatabase db, RedisKey key, string path, object obj, When when = When.Always)
1313
{
14-
var json = JsonSerializer.Serialize(obj);
14+
string json = JsonSerializer.Serialize(obj);
15+
return JsonSet(db, key, path, json, when);
16+
}
17+
18+
public static RedisResult JsonSet(this IDatabase db, RedisKey key, string path, string json, When when = When.Always)
19+
{
1520
switch(when)
1621
{
1722
case When.Exists:
@@ -22,4 +27,9 @@ public static RedisResult JsonSet(this IDatabase db, RedisKey key, string path,
2227
return db.Execute("JSON.SET", key, path, json);
2328
}
2429
}
30+
31+
public static RedisResult JsonGet(this IDatabase db, RedisKey key)
32+
{
33+
return db.Execute("JSON.GET", key);
34+
}
2535
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
using StackExchange.Redis;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Runtime.CompilerServices;
7+
using System.Threading.Tasks;
8+
using Xunit;
9+
namespace NRedisStack.Tests
10+
{
11+
public abstract class AbstractNRedisStackTest : IClassFixture<RedisFixture>, IAsyncLifetime
12+
{
13+
protected internal RedisFixture redisFixture;
14+
15+
protected internal AbstractNRedisStackTest(RedisFixture redisFixture) => this.redisFixture = redisFixture;
16+
17+
private List<string> keyNames = new List<string>();
18+
19+
protected internal string CreateKeyName([CallerMemberName] string memberName = "") => CreateKeyNames(1, memberName)[0];
20+
21+
protected internal string[] CreateKeyNames(int count, [CallerMemberName] string memberName = "")
22+
{
23+
if (count < 1) throw new ArgumentOutOfRangeException(nameof(count), "Must be greater than zero.");
24+
25+
var newKeys = new string[count];
26+
for (var i = 0; i < count; i++)
27+
{
28+
newKeys[i] = $"{GetType().Name}:{memberName}:{i}";
29+
}
30+
31+
keyNames.AddRange(newKeys);
32+
33+
return newKeys;
34+
}
35+
36+
public Task InitializeAsync() => Task.CompletedTask;
37+
38+
public async Task DisposeAsync()
39+
{
40+
var redis = redisFixture.Redis.GetDatabase();
41+
await redis.KeyDeleteAsync(keyNames.Select(i => (RedisKey)i).ToArray());
42+
}
43+
}
44+
}

tests/RediSharp.Tests/UnitTest1.cs renamed to tests/NRedisStack.Tests/JsonTests.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
using StackExchange.Redis;
55
using System.Linq;
66
using System.IO;
7-
using RediSharp.Core;
7+
using NRedisStack.Core;
88
using Moq;
99

1010
using System.Text.Json;
1111
using System.Text.Json.Serialization;
1212

1313

14-
namespace RediSharp.Tests;
14+
namespace NRedisStack.Tests;
1515

16-
public class UnitTest1
16+
public class JsonTests : AbstractNRedisStackTest, IDisposable
1717
{
1818
Mock<IDatabase> _mock = new Mock<IDatabase>();
19+
private readonly string key = "JSON_TESTS";
20+
public JsonTests(RedisFixture redisFixture) : base(redisFixture) { }
21+
22+
public void Dispose()
23+
{
24+
redisFixture.Redis.GetDatabase().KeyDelete(key);
25+
}
26+
1927
public class Person
2028
{
2129
public string Name { get; set; }
@@ -24,14 +32,6 @@ public class Person
2432

2533
[Fact]
2634
public void TestJsonSet()
27-
{
28-
var obj = new Person { Name = "Shachar", Age = 23 };
29-
_mock.Object.JsonSet("Person:Shachar", "$", obj);
30-
_mock.Verify(x => x.Execute("JSON.SET", "Person:Shachar", "$", "{\"Name\":\"Shachar\",\"Age\":23}"));
31-
}
32-
33-
[Fact]
34-
public void TestJsonSetExist()
3535
{
3636
var obj = new Person { Name = "Shachar", Age = 23 };
3737
_mock.Object.JsonSet("Person:Shachar", "$", obj, When.Exists);
@@ -45,4 +45,15 @@ public void TestJsonSetNotExist()
4545
_mock.Object.JsonSet("Person:Shachar", "$", obj, When.NotExists);
4646
_mock.Verify(x => x.Execute("JSON.SET", "Person:Shachar", "$", "{\"Name\":\"Shachar\",\"Age\":23}", "NX"));
4747
}
48+
49+
[Fact]
50+
public void TestJsonGet()
51+
{
52+
var obj = new Person { Name = "Shachar", Age = 23 };
53+
IDatabase db = redisFixture.Redis.GetDatabase();
54+
55+
db.JsonSet(key, "$", obj);
56+
string expected = "{\"Name\":\"Shachar\",\"Age\":23}";
57+
Assert.Equal(db.JsonGet(key).ToString(), expected);
58+
}
4859
}

tests/RediSharp.Tests/RediSharp.Tests.csproj renamed to tests/NRedisStack.Tests/NRedisStack.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<PackageReference Include="Moq" Version="4.18.1" />
11+
<ItemGroup>
12+
<PackageReference Include="Moq" Version="4.18.1" />
1313
<PackageReference Include="coverlet.collector" Version="3.1.2" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
1515
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
@@ -19,7 +19,7 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<ProjectReference Include="..\..\src\RediSharp.Core\RediSharp.Core.csproj" />
22+
<ProjectReference Include="..\..\src\NRedisStack.Core\NRedisStack.Core.csproj" />
2323
</ItemGroup>
2424

2525
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using StackExchange.Redis;
3+
4+
namespace NRedisStack.Tests
5+
{
6+
public class RedisFixture : IDisposable
7+
{
8+
public RedisFixture() => Redis = ConnectionMultiplexer.Connect("localhost");
9+
10+
public void Dispose()
11+
{
12+
Redis.Close();
13+
}
14+
15+
public ConnectionMultiplexer Redis { get; private set; }
16+
}
17+
}

0 commit comments

Comments
 (0)