From ac44147e77906da8a8a7445972e88be3413f6baa Mon Sep 17 00:00:00 2001 From: shacharPash Date: Mon, 8 May 2023 14:58:23 +0300 Subject: [PATCH 01/28] Support JSON.MSET Command --- .../Json/DataTypes/KeyValuePath.cs | 34 +++++++++++++++++++ src/NRedisStack/Json/IJsonCommands.cs | 14 ++++++-- src/NRedisStack/Json/IJsonCommandsAsync.cs | 12 ++++++- src/NRedisStack/Json/JsonCommandBuilder.cs | 11 +++++- src/NRedisStack/Json/JsonCommands.cs | 9 ++++- src/NRedisStack/Json/JsonCommandsAsync.cs | 8 ++++- src/NRedisStack/Json/Literals/Commands.cs | 1 + 7 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 src/NRedisStack/Json/DataTypes/KeyValuePath.cs diff --git a/src/NRedisStack/Json/DataTypes/KeyValuePath.cs b/src/NRedisStack/Json/DataTypes/KeyValuePath.cs new file mode 100644 index 00000000..42bfb59f --- /dev/null +++ b/src/NRedisStack/Json/DataTypes/KeyValuePath.cs @@ -0,0 +1,34 @@ +using System.Text.Json; + +namespace NRedisStack.Json.DataTypes; + +public struct KeyValuePath +{ + public string Key { get; set; } + public object Value { get; set; } + public string Path { get; set; } + + public KeyValuePath(string key, object value, string path = "$") + { + if (!(value is string) && !(value is object)) + { + throw new ArgumentException("Value must be a string or an object."); + } + + if (key == null || value == null) + { + throw new ArgumentNullException("Key and value cannot be null."); + } + + Key = key; + Value = value; + Path = path; + } + + public override string ToString() + { + if (Value is string) + return $"{Key} {Path} {Value}"; + return $"{Key} {Path} {JsonSerializer.Serialize(Value)}"; + } +} \ No newline at end of file diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index d022cab6..cc6fd3ee 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -1,4 +1,5 @@ -using StackExchange.Redis; +using NRedisStack.Json.DataTypes; +using StackExchange.Redis; namespace NRedisStack; @@ -206,7 +207,16 @@ public interface IJsonCommands bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When.Always); /// - /// Set json file from the provided file Path. + /// Sets or updates the JSON value of one or more keys. + /// + /// The key, The value to set and + /// The path to set within the key, must be > 1 + /// The disposition of the command + /// + bool MSet(KeyValuePath[] keyValuePathList); + + /// + /// Sets or updates the JSON value of one or more keys. /// /// The key. /// The path to set within the key. diff --git a/src/NRedisStack/Json/IJsonCommandsAsync.cs b/src/NRedisStack/Json/IJsonCommandsAsync.cs index 2d038ae3..2ad45a49 100644 --- a/src/NRedisStack/Json/IJsonCommandsAsync.cs +++ b/src/NRedisStack/Json/IJsonCommandsAsync.cs @@ -1,4 +1,5 @@ -using StackExchange.Redis; +using NRedisStack.Json.DataTypes; +using StackExchange.Redis; namespace NRedisStack; @@ -205,6 +206,15 @@ public interface IJsonCommandsAsync /// Task SetAsync(RedisKey key, RedisValue path, RedisValue json, When when = When.Always); + /// + /// Sets or updates the JSON value of one or more keys. + /// + /// The key, The value to set and + /// The path to set within the key, must be > 1 + /// The disposition of the command + /// + Task MSetAsync(KeyValuePath[] keyValuePathList); + /// /// Set json file from the provided file Path. /// diff --git a/src/NRedisStack/Json/JsonCommandBuilder.cs b/src/NRedisStack/Json/JsonCommandBuilder.cs index fa06d3b1..941d3f46 100644 --- a/src/NRedisStack/Json/JsonCommandBuilder.cs +++ b/src/NRedisStack/Json/JsonCommandBuilder.cs @@ -1,4 +1,5 @@ -using NRedisStack.Json.Literals; +using NRedisStack.Json.DataTypes; +using NRedisStack.Json.Literals; using NRedisStack.RedisStackCommands; using StackExchange.Redis; using System.Text.Json; @@ -28,6 +29,14 @@ public static SerializedCommand Set(RedisKey key, RedisValue path, RedisValue js }; } + public static SerializedCommand MSet(KeyValuePath[] keyValuePathList) + { + if (keyValuePathList.Length < 1) + throw new ArgumentOutOfRangeException(nameof(keyValuePathList)); + var args = keyValuePathList.Select(x => x.ToString()); + return new SerializedCommand(JSON.MSET, string.Join(" ", args)); + } + public static SerializedCommand StrAppend(RedisKey key, string value, string? path = null) { if (path == null) diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 0add4087..876db349 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -1,4 +1,5 @@ -using StackExchange.Redis; +using NRedisStack.Json.DataTypes; +using StackExchange.Redis; using System.Text.Json; using System.Text.Json.Nodes; @@ -39,6 +40,12 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When return _db.Execute(JsonCommandBuilder.Set(key, path, json, when)).OKtoBoolean(); } + /// + public bool MSet(KeyValuePath[] keyValuePathList) + { + return _db.Execute(JsonCommandBuilder.MSet(keyValuePathList)).OKtoBoolean(); + } + /// public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) { diff --git a/src/NRedisStack/Json/JsonCommandsAsync.cs b/src/NRedisStack/Json/JsonCommandsAsync.cs index 12d0e5fc..3eb32026 100644 --- a/src/NRedisStack/Json/JsonCommandsAsync.cs +++ b/src/NRedisStack/Json/JsonCommandsAsync.cs @@ -1,4 +1,5 @@ -using StackExchange.Redis; +using NRedisStack.Json.DataTypes; +using StackExchange.Redis; using System.Text.Json; using System.Text.Json.Nodes; @@ -143,6 +144,11 @@ public async Task SetAsync(RedisKey key, RedisValue path, RedisValue json, return (await _db.ExecuteAsync(JsonCommandBuilder.Set(key, path, json, when))).OKtoBoolean(); } + public async Task MSetAsync(KeyValuePath[] keyValuePathList) + { + return (await _db.ExecuteAsync(JsonCommandBuilder.MSet(keyValuePathList))).OKtoBoolean(); + } + public async Task SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always) { if (!File.Exists(filePath)) diff --git a/src/NRedisStack/Json/Literals/Commands.cs b/src/NRedisStack/Json/Literals/Commands.cs index f1d34bdd..f748fae0 100644 --- a/src/NRedisStack/Json/Literals/Commands.cs +++ b/src/NRedisStack/Json/Literals/Commands.cs @@ -15,6 +15,7 @@ internal class JSON public const string FORGET = "JSON.FORGET"; public const string GET = "JSON.GET"; public const string MEMORY = "MEMORY"; + public const string MSET = "JSON.MSET"; public const string MGET = "JSON.MGET"; public const string NUMINCRBY = "JSON.NUMINCRBY"; public const string NUMMULTBY = "JSON.NUMMULTBY"; From bd52427e4f8f3081eb149d85f5a9b831613f0d9b Mon Sep 17 00:00:00 2001 From: shacharPash Date: Mon, 8 May 2023 16:50:10 +0300 Subject: [PATCH 02/28] add tests --- .../Json/DataTypes/KeyValuePath.cs | 14 ++--- src/NRedisStack/Json/JsonCommandBuilder.cs | 5 +- tests/NRedisStack.Tests/Json/JsonTests.cs | 56 ++++++++++++++++++- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/NRedisStack/Json/DataTypes/KeyValuePath.cs b/src/NRedisStack/Json/DataTypes/KeyValuePath.cs index 42bfb59f..52d4009d 100644 --- a/src/NRedisStack/Json/DataTypes/KeyValuePath.cs +++ b/src/NRedisStack/Json/DataTypes/KeyValuePath.cs @@ -10,11 +10,6 @@ public struct KeyValuePath public KeyValuePath(string key, object value, string path = "$") { - if (!(value is string) && !(value is object)) - { - throw new ArgumentException("Value must be a string or an object."); - } - if (key == null || value == null) { throw new ArgumentNullException("Key and value cannot be null."); @@ -24,11 +19,12 @@ public KeyValuePath(string key, object value, string path = "$") Value = value; Path = path; } - - public override string ToString() + public string[] ToArray() { if (Value is string) - return $"{Key} {Path} {Value}"; - return $"{Key} {Path} {JsonSerializer.Serialize(Value)}"; + { + return new string[] { Key, Path, Value.ToString() }; + } + return new string[] { Key, Path, JsonSerializer.Serialize(Value) }; } } \ No newline at end of file diff --git a/src/NRedisStack/Json/JsonCommandBuilder.cs b/src/NRedisStack/Json/JsonCommandBuilder.cs index 941d3f46..4120e693 100644 --- a/src/NRedisStack/Json/JsonCommandBuilder.cs +++ b/src/NRedisStack/Json/JsonCommandBuilder.cs @@ -33,8 +33,9 @@ public static SerializedCommand MSet(KeyValuePath[] keyValuePathList) { if (keyValuePathList.Length < 1) throw new ArgumentOutOfRangeException(nameof(keyValuePathList)); - var args = keyValuePathList.Select(x => x.ToString()); - return new SerializedCommand(JSON.MSET, string.Join(" ", args)); + + var args = keyValuePathList.SelectMany(x => x.ToArray()).ToArray(); + return new SerializedCommand(JSON.MSET, args[0], args.Skip(1)); } public static SerializedCommand StrAppend(RedisKey key, string value, string? path = null) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index bc05f720..f6ac4637 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -4,7 +4,7 @@ using StackExchange.Redis; using Moq; using NRedisStack.RedisStackCommands; - +using NRedisStack.Json.DataTypes; namespace NRedisStack.Tests; @@ -725,6 +725,60 @@ public async Task GetAsync() Assert.Equal(35, people[1]!.Age); } + [Fact] + public void TestMSet() + { + IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(2); + var key1 = keys[0]; + var key2 = keys[1]; + + KeyValuePath[] values = new[] + { + new KeyValuePath(key1, new { a = "hello" }), + new KeyValuePath(key2, new { a = "world" }) + }; + commands.MSet(values) +; + var result = commands.MGet(keys.Select(x => new RedisKey(x)).ToArray(), "$.a"); + + Assert.Equal("[\"hello\"]", result[0].ToString()); + Assert.Equal("[\"world\"]", result[1].ToString()); + + // test errors: + Assert.Throws(() => commands.MSet(new KeyValuePath[0])); + } + + [Fact] + public async Task MSetAsync() + { + IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(2); + var key1 = keys[0]; + var key2 = keys[1]; + KeyValuePath[] values = new[] + { + new KeyValuePath(key1, new { a = "hello" }), + new KeyValuePath(key2, new { a = "world" }) + }; + await commands.MSetAsync(values) +; + var result = await commands.MGetAsync(keys.Select(x => new RedisKey(x)).ToArray(), "$.a"); + + Assert.Equal("[\"hello\"]", result[0].ToString()); + Assert.Equal("[\"world\"]", result[1].ToString()); + + // test errors: + await Assert.ThrowsAsync(async () => await commands.MSetAsync(new KeyValuePath[0])); + } + + [Fact] + public void TestKeyValuePathErrors() + { + Assert.Throws(() => new KeyValuePath(null!, new { a = "hello" })); + Assert.Throws(() => new KeyValuePath("key", null!) ); + } + [Fact] public void MGet() { From 3936612eedb44a923132ea32d7a22f2bc0aac339 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Mon, 8 May 2023 17:05:42 +0300 Subject: [PATCH 03/28] fixes --- src/NRedisStack/Json/JsonCommandBuilder.cs | 2 +- tests/NRedisStack.Tests/Json/JsonTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NRedisStack/Json/JsonCommandBuilder.cs b/src/NRedisStack/Json/JsonCommandBuilder.cs index 4120e693..48211320 100644 --- a/src/NRedisStack/Json/JsonCommandBuilder.cs +++ b/src/NRedisStack/Json/JsonCommandBuilder.cs @@ -35,7 +35,7 @@ public static SerializedCommand MSet(KeyValuePath[] keyValuePathList) throw new ArgumentOutOfRangeException(nameof(keyValuePathList)); var args = keyValuePathList.SelectMany(x => x.ToArray()).ToArray(); - return new SerializedCommand(JSON.MSET, args[0], args.Skip(1)); + return new SerializedCommand(JSON.MSET, args); } public static SerializedCommand StrAppend(RedisKey key, string value, string? path = null) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index f6ac4637..ed28c3a4 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -726,7 +726,7 @@ public async Task GetAsync() } [Fact] - public void TestMSet() + public void MSet() { IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); var keys = CreateKeyNames(2); From 8407c90fcd546665629a24b93e236886d8fc2fd1 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Tue, 9 May 2023 12:50:39 +0300 Subject: [PATCH 04/28] Support JSON.MERGE Command --- src/NRedisStack/Json/IJsonCommands.cs | 10 ++++++++++ src/NRedisStack/Json/IJsonCommandsAsync.cs | 10 ++++++++++ src/NRedisStack/Json/JsonCommandBuilder.cs | 5 +++++ src/NRedisStack/Json/JsonCommands.cs | 6 ++++++ src/NRedisStack/Json/JsonCommandsAsync.cs | 6 ++++++ src/NRedisStack/Json/Literals/Commands.cs | 1 + tests/NRedisStack.Tests/Json/JsonTests.cs | 2 ++ 7 files changed, 40 insertions(+) diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index cc6fd3ee..3ce848e6 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -215,6 +215,16 @@ public interface IJsonCommands /// bool MSet(KeyValuePath[] keyValuePathList); + /// + /// Sets or updates the JSON value at a path. + /// + /// The key. + /// The path to set within the key. + /// The value to set. + /// The disposition of the command + /// + bool Merge(RedisKey key, RedisValue path, RedisValue json); + /// /// Sets or updates the JSON value of one or more keys. /// diff --git a/src/NRedisStack/Json/IJsonCommandsAsync.cs b/src/NRedisStack/Json/IJsonCommandsAsync.cs index 2ad45a49..f8303230 100644 --- a/src/NRedisStack/Json/IJsonCommandsAsync.cs +++ b/src/NRedisStack/Json/IJsonCommandsAsync.cs @@ -215,6 +215,16 @@ public interface IJsonCommandsAsync /// Task MSetAsync(KeyValuePath[] keyValuePathList); + /// + /// Sets or updates the JSON value at a path. + /// + /// The key. + /// The path to set within the key. + /// The value to set. + /// The disposition of the command + /// + Task MergeAsync(RedisKey key, RedisValue path, RedisValue json); + /// /// Set json file from the provided file Path. /// diff --git a/src/NRedisStack/Json/JsonCommandBuilder.cs b/src/NRedisStack/Json/JsonCommandBuilder.cs index 48211320..77446bb0 100644 --- a/src/NRedisStack/Json/JsonCommandBuilder.cs +++ b/src/NRedisStack/Json/JsonCommandBuilder.cs @@ -38,6 +38,11 @@ public static SerializedCommand MSet(KeyValuePath[] keyValuePathList) return new SerializedCommand(JSON.MSET, args); } + public static SerializedCommand Merge(RedisKey key, RedisValue path, RedisValue json) + { + return new SerializedCommand(JSON.MERGE, key, path, json); + } + public static SerializedCommand StrAppend(RedisKey key, string value, string? path = null) { if (path == null) diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 876db349..f72f79a8 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -46,6 +46,12 @@ public bool MSet(KeyValuePath[] keyValuePathList) return _db.Execute(JsonCommandBuilder.MSet(keyValuePathList)).OKtoBoolean(); } + /// + public bool Merge(RedisKey key, RedisValue path, RedisValue json) + { + return _db.Execute(JsonCommandBuilder.Merge(key, path, json)).OKtoBoolean(); + } + /// public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) { diff --git a/src/NRedisStack/Json/JsonCommandsAsync.cs b/src/NRedisStack/Json/JsonCommandsAsync.cs index 3eb32026..6a43c8bb 100644 --- a/src/NRedisStack/Json/JsonCommandsAsync.cs +++ b/src/NRedisStack/Json/JsonCommandsAsync.cs @@ -149,6 +149,12 @@ public async Task MSetAsync(KeyValuePath[] keyValuePathList) return (await _db.ExecuteAsync(JsonCommandBuilder.MSet(keyValuePathList))).OKtoBoolean(); } + /// + public async Task MergeAsync(RedisKey key, RedisValue path, RedisValue json) + { + return (await _db.ExecuteAsync(JsonCommandBuilder.Merge(key, path, json))).OKtoBoolean(); + } + public async Task SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always) { if (!File.Exists(filePath)) diff --git a/src/NRedisStack/Json/Literals/Commands.cs b/src/NRedisStack/Json/Literals/Commands.cs index f748fae0..b0bc9e87 100644 --- a/src/NRedisStack/Json/Literals/Commands.cs +++ b/src/NRedisStack/Json/Literals/Commands.cs @@ -15,6 +15,7 @@ internal class JSON public const string FORGET = "JSON.FORGET"; public const string GET = "JSON.GET"; public const string MEMORY = "MEMORY"; + public const string MERGE = "MERGE"; public const string MSET = "JSON.MSET"; public const string MGET = "JSON.MGET"; public const string NUMINCRBY = "JSON.NUMINCRBY"; diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index ed28c3a4..40ae2cc7 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -772,6 +772,8 @@ await commands.MSetAsync(values) await Assert.ThrowsAsync(async () => await commands.MSetAsync(new KeyValuePath[0])); } + // TODO: add json.merge sync/async tests here + [Fact] public void TestKeyValuePathErrors() { From ed2751f976a132daab7d0fbf39b97bcdcbe573da Mon Sep 17 00:00:00 2001 From: slorello89 Date: Tue, 9 May 2023 07:36:59 -0400 Subject: [PATCH 05/28] adding edge filter --- .github/workflows/integration.yml | 10 ++++++++-- tests/NRedisStack.Tests/Json/JsonTests.cs | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 624e3cd1..c3ed32f1 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -24,6 +24,7 @@ jobs: USER_NAME: ${{ secrets.USER_NAME }} PASSWORD: ${{ secrets.PASSWORD }} ENDPOINT: ${{ secrets.ENDPOINT }} + VERSION: ${{ matrix.redis-stack-version }} steps: - uses: actions/checkout@v3 - name: .NET Core 6 @@ -53,7 +54,12 @@ jobs: echo "${{secrets.REDIS_USER_CRT}}" > tests/NRedisStack.Tests/bin/Debug/net7.0/redis_user.crt echo "${{secrets.REDIS_USER_PRIVATE_KEY}}" > tests/NRedisStack.Tests/bin/Debug/net7.0/redis_user_private.key ls -R - dotnet test -f net7.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover + echo Version $VERSION + if [ "$VERSION" != "edge" ]; then + dotnet test -f net7.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --filter Category!=edge + else + dotnet test -f net7.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover + fi - name: Codecov uses: codecov/codecov-action@v3 with: @@ -98,4 +104,4 @@ jobs: shell: cmd run: | START wsl ./redis-stack-server-${{env.redis_stack_version}}/bin/redis-stack-server & - dotnet test -f net481 --no-build --verbosity normal + dotnet test -f net481 --no-build --verbosity normal --filter Category!=edge diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index ed28c3a4..6ae9ddde 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -726,6 +726,7 @@ public async Task GetAsync() } [Fact] + [Trait("Category","edge")] public void MSet() { IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); @@ -750,6 +751,7 @@ public void MSet() } [Fact] + [Trait("Category","edge")] public async Task MSetAsync() { IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); From fd16eebe74777df204a724f10ee0ed68d1e5559f Mon Sep 17 00:00:00 2001 From: slorello89 Date: Tue, 9 May 2023 07:45:37 -0400 Subject: [PATCH 06/28] bump --- tests/NRedisStack.Tests/Json/JsonTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 6ae9ddde..59f17e11 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -748,6 +748,7 @@ public void MSet() // test errors: Assert.Throws(() => commands.MSet(new KeyValuePath[0])); + } [Fact] From eabd24d2555066a2abab7c7e1a7b867cb6e43820 Mon Sep 17 00:00:00 2001 From: slorello89 Date: Tue, 9 May 2023 17:00:47 -0400 Subject: [PATCH 07/28] applying to .NET 6 too --- .github/workflows/integration.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index c3ed32f1..88230d3c 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -47,7 +47,11 @@ jobs: echo "${{secrets.REDIS_USER_CRT}}" > tests/NRedisStack.Tests/bin/Debug/net6.0/redis_user.crt echo "${{secrets.REDIS_USER_PRIVATE_KEY}}" > tests/NRedisStack.Tests/bin/Debug/net6.0/redis_user_private.key ls -R - dotnet test -f net6.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover + if [ "$VERSION" != "edge" ]; then + dotnet test -f net6.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --filter Category!=edge + else + dotnet test -f net6.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover + fi - name: Test run: | echo "${{secrets.REDIS_CA_PEM}}" > tests/NRedisStack.Tests/bin/Debug/net7.0/redis_ca.pem From f2ccf86566323b202d7b2340c3dcbd664b35ce4b Mon Sep 17 00:00:00 2001 From: slorello89 Date: Tue, 9 May 2023 17:09:17 -0400 Subject: [PATCH 08/28] think it might be picking up the version env var --- .github/workflows/integration.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 88230d3c..a7293fad 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -24,7 +24,7 @@ jobs: USER_NAME: ${{ secrets.USER_NAME }} PASSWORD: ${{ secrets.PASSWORD }} ENDPOINT: ${{ secrets.ENDPOINT }} - VERSION: ${{ matrix.redis-stack-version }} + REDIS_VERSION: ${{ matrix.redis-stack-version }} steps: - uses: actions/checkout@v3 - name: .NET Core 6 @@ -47,7 +47,7 @@ jobs: echo "${{secrets.REDIS_USER_CRT}}" > tests/NRedisStack.Tests/bin/Debug/net6.0/redis_user.crt echo "${{secrets.REDIS_USER_PRIVATE_KEY}}" > tests/NRedisStack.Tests/bin/Debug/net6.0/redis_user_private.key ls -R - if [ "$VERSION" != "edge" ]; then + if [ "$REDIS_VERSION" != "edge" ]; then dotnet test -f net6.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --filter Category!=edge else dotnet test -f net6.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover @@ -58,8 +58,7 @@ jobs: echo "${{secrets.REDIS_USER_CRT}}" > tests/NRedisStack.Tests/bin/Debug/net7.0/redis_user.crt echo "${{secrets.REDIS_USER_PRIVATE_KEY}}" > tests/NRedisStack.Tests/bin/Debug/net7.0/redis_user_private.key ls -R - echo Version $VERSION - if [ "$VERSION" != "edge" ]; then + if [ "$REDIS_VERSION" != "edge" ]; then dotnet test -f net7.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --filter Category!=edge else dotnet test -f net7.0 --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover From 6faac3bad863ff1511163b5873f7d736166e0737 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Wed, 10 May 2023 15:53:12 +0300 Subject: [PATCH 09/28] Sync test for json.merge --- tests/NRedisStack.Tests/Json/JsonTests.cs | 37 +++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index bbe827e1..8f52aa4a 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -726,7 +726,7 @@ public async Task GetAsync() } [Fact] - [Trait("Category","edge")] + [Trait("Category", "edge")] public void MSet() { IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); @@ -748,11 +748,10 @@ public void MSet() // test errors: Assert.Throws(() => commands.MSet(new KeyValuePath[0])); - } [Fact] - [Trait("Category","edge")] + [Trait("Category", "edge")] public async Task MSetAsync() { IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); @@ -775,13 +774,33 @@ await commands.MSetAsync(values) await Assert.ThrowsAsync(async () => await commands.MSetAsync(new KeyValuePath[0])); } - // TODO: add json.merge sync/async tests here + [Fact] + [Trait("Category", "edge")] + public void Merge() + { + IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(2); + var key1 = keys[0]; + var key2 = keys[1]; + + // Create a key + commands.Set("user", "$", "{}"); + + // Merge a value at a path + commands.Merge("user", "$.name", "John Doe"); + + // Get the value at the path + var name = commands.Get("user", path: "$.name"); + + // Assert that the value is correct + Assert.Equal("John Doe", name.ToString()); + } [Fact] public void TestKeyValuePathErrors() { Assert.Throws(() => new KeyValuePath(null!, new { a = "hello" })); - Assert.Throws(() => new KeyValuePath("key", null!) ); + Assert.Throws(() => new KeyValuePath("key", null!)); } [Fact] @@ -1069,18 +1088,18 @@ public async Task TestSetFromDirectoryAsync() public void TestJsonCommandBuilder() { var getBuild1 = JsonCommandBuilder.Get("key", "indent", "newline", "space", "path"); - var getBuild2 = JsonCommandBuilder.Get("key",new string[]{"path1", "path2", "path3"}, "indent", "newline", "space"); - var expectedArgs1 = new object[] { "key", "INDENT", "indent", "NEWLINE","newline", "SPACE", "space", "path" }; + var getBuild2 = JsonCommandBuilder.Get("key", new string[] { "path1", "path2", "path3" }, "indent", "newline", "space"); + var expectedArgs1 = new object[] { "key", "INDENT", "indent", "NEWLINE", "newline", "SPACE", "space", "path" }; var expectedArgs2 = new object[] { "key", "INDENT", "indent", "NEWLINE", "newline", "SPACE", "space", "path1", "path2", "path3" }; - for(int i = 0; i < expectedArgs1.Length; i++) + for (int i = 0; i < expectedArgs1.Length; i++) { Assert.Equal(expectedArgs1[i].ToString(), getBuild1.Args[i].ToString()); } Assert.Equal("JSON.GET", getBuild1.Command); - for(int i = 0; i < expectedArgs2.Length; i++) + for (int i = 0; i < expectedArgs2.Length; i++) { Assert.Equal(expectedArgs2[i].ToString(), getBuild2.Args[i].ToString()); } From 5812a160e2ea7a5ecad1ac0985a38c815fc5d0ee Mon Sep 17 00:00:00 2001 From: shacharPash Date: Wed, 10 May 2023 16:12:49 +0300 Subject: [PATCH 10/28] add async test --- tests/NRedisStack.Tests/Json/JsonTests.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 8f52aa4a..f5487a99 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -796,6 +796,28 @@ public void Merge() Assert.Equal("John Doe", name.ToString()); } + [Fact] + [Trait("Category", "edge")] + public async Task MergeAsync() + { + IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(2); + var key1 = keys[0]; + var key2 = keys[1]; + + // Create a key + await commands.SetAsync("user", "$", "{}"); + + // Merge a value at a path + await commands.MergeAsync("user", "$.name", "John Doe"); + + // Get the value at the path + var name = await commands.GetAsync("user", path: "$.name"); + + // Assert that the value is correct + Assert.Equal("John Doe", name.ToString()); + } + [Fact] public void TestKeyValuePathErrors() { From 0019127779018a3644209a3390b7a8f9acd77e52 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Wed, 10 May 2023 16:16:52 +0300 Subject: [PATCH 11/28] add JSON. to MERGE in commands.cs --- src/NRedisStack/Json/Literals/Commands.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NRedisStack/Json/Literals/Commands.cs b/src/NRedisStack/Json/Literals/Commands.cs index b0bc9e87..8ddbafe0 100644 --- a/src/NRedisStack/Json/Literals/Commands.cs +++ b/src/NRedisStack/Json/Literals/Commands.cs @@ -15,7 +15,7 @@ internal class JSON public const string FORGET = "JSON.FORGET"; public const string GET = "JSON.GET"; public const string MEMORY = "MEMORY"; - public const string MERGE = "MERGE"; + public const string MERGE = "JSON.MERGE"; public const string MSET = "JSON.MSET"; public const string MGET = "JSON.MGET"; public const string NUMINCRBY = "JSON.NUMINCRBY"; From 05273233c03531533f7f09c14f739eda313a590d Mon Sep 17 00:00:00 2001 From: shacharPash Date: Wed, 10 May 2023 17:07:26 +0300 Subject: [PATCH 12/28] test from redisJson --- tests/NRedisStack.Tests/Json/JsonTests.cs | 76 +++++++++++++++-------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index f5487a99..73d9ae43 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -774,49 +774,71 @@ await commands.MSetAsync(values) await Assert.ThrowsAsync(async () => await commands.MSetAsync(new KeyValuePath[0])); } - [Fact] - [Trait("Category", "edge")] - public void Merge() - { - IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - var keys = CreateKeyNames(2); - var key1 = keys[0]; - var key2 = keys[1]; + // [Fact] + // [Trait("Category", "edge")] + // public void Merge1() + // { + // IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + // var keys = CreateKeyNames(2); + // var key1 = keys[0]; + // var key2 = keys[1]; - // Create a key - commands.Set("user", "$", "{}"); + // // Create a key + // commands.Set("user", "$", "{}"); - // Merge a value at a path - commands.Merge("user", "$.name", "John Doe"); + // // Merge a value at a path + // commands.Merge("user", "$.name", "John Doe"); - // Get the value at the path - var name = commands.Get("user", path: "$.name"); + // // Get the value at the path + // var name = commands.Get("user", path: "$.name"); - // Assert that the value is correct - Assert.Equal("John Doe", name.ToString()); - } + // // Assert that the value is correct + // Assert.Equal("John Doe", name.ToString()); + // } [Fact] [Trait("Category", "edge")] - public async Task MergeAsync() + public void Merge() { - IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); var keys = CreateKeyNames(2); var key1 = keys[0]; var key2 = keys[1]; // Create a key - await commands.SetAsync("user", "$", "{}"); + commands.Set("test_merge", "$", "{\"a\":{\"b\":{\"c\":\"d\"}}}"); + Assert.True(commands.Merge("test_merge", "$", "{\"a\":{\"b\":{\"e\":\"f\"}}}")); + Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\"}}}", commands.Get("test_merge").ToString()); + // Test with root path path $.a.b + + Assert.True(commands.Merge("test_merge", "$.a.b", "{\"h\":\"i\"}")); + Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\",\"h\":\"i\"}}}", commands.Get("test_merge").ToString()); + // Test with null value to delete a value + Assert.True(commands.Merge("test_merge", "$.a.b", "{\"c\":null}")); + Assert.Equal("{\"a\":{\"b\":{\"h\":\"i\",\"e\":\"f\"}}}", commands.Get("test_merge").ToString()); + } - // Merge a value at a path - await commands.MergeAsync("user", "$.name", "John Doe"); + // [Fact] + // [Trait("Category", "edge")] + // public async Task MergeAsync() + // { + // IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + // var keys = CreateKeyNames(2); + // var key1 = keys[0]; + // var key2 = keys[1]; - // Get the value at the path - var name = await commands.GetAsync("user", path: "$.name"); + // // Create a key + // await commands.SetAsync("user", "$", "{}"); - // Assert that the value is correct - Assert.Equal("John Doe", name.ToString()); - } + // // Merge a value at a path + // await commands.MergeAsync("user", "$.name", "John Doe"); + + // // Get the value at the path + // var name = await commands.GetAsync("user", path: "$.name"); + + // // Assert that the value is correct + // Assert.Equal("John Doe", name.ToString()); + // } [Fact] public void TestKeyValuePathErrors() From 266692790fd56becda867805ffda4b5f78bd701e Mon Sep 17 00:00:00 2001 From: shacharPash Date: Wed, 10 May 2023 17:20:52 +0300 Subject: [PATCH 13/28] add async test --- tests/NRedisStack.Tests/Json/JsonTests.cs | 62 +++++++---------------- 1 file changed, 17 insertions(+), 45 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 73d9ae43..944693af 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -774,39 +774,14 @@ await commands.MSetAsync(values) await Assert.ThrowsAsync(async () => await commands.MSetAsync(new KeyValuePath[0])); } - // [Fact] - // [Trait("Category", "edge")] - // public void Merge1() - // { - // IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - // var keys = CreateKeyNames(2); - // var key1 = keys[0]; - // var key2 = keys[1]; - - // // Create a key - // commands.Set("user", "$", "{}"); - - // // Merge a value at a path - // commands.Merge("user", "$.name", "John Doe"); - - // // Get the value at the path - // var name = commands.Get("user", path: "$.name"); - - // // Assert that the value is correct - // Assert.Equal("John Doe", name.ToString()); - // } - [Fact] [Trait("Category", "edge")] public void Merge() { IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - var keys = CreateKeyNames(2); - var key1 = keys[0]; - var key2 = keys[1]; // Create a key - commands.Set("test_merge", "$", "{\"a\":{\"b\":{\"c\":\"d\"}}}"); + Assert.True(commands.Set("test_merge", "$", "{\"a\":{\"b\":{\"c\":\"d\"}}}")); Assert.True(commands.Merge("test_merge", "$", "{\"a\":{\"b\":{\"e\":\"f\"}}}")); Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\"}}}", commands.Get("test_merge").ToString()); // Test with root path path $.a.b @@ -818,27 +793,24 @@ public void Merge() Assert.Equal("{\"a\":{\"b\":{\"h\":\"i\",\"e\":\"f\"}}}", commands.Get("test_merge").ToString()); } - // [Fact] - // [Trait("Category", "edge")] - // public async Task MergeAsync() - // { - // IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - // var keys = CreateKeyNames(2); - // var key1 = keys[0]; - // var key2 = keys[1]; - - // // Create a key - // await commands.SetAsync("user", "$", "{}"); - - // // Merge a value at a path - // await commands.MergeAsync("user", "$.name", "John Doe"); + [Fact] + [Trait("Category", "edge")] + public async Task MergeAsync() + { + IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - // // Get the value at the path - // var name = await commands.GetAsync("user", path: "$.name"); + // Create a key + Assert.True(await commands.SetAsync("test_merge", "$", "{\"a\":{\"b\":{\"c\":\"d\"}}}")); + Assert.True(await commands.MergeAsync("test_merge", "$", "{\"a\":{\"b\":{\"e\":\"f\"}}}")); + Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\"}}}", (await commands.GetAsync("test_merge")).ToString()); + // Test with root path path $.a.b - // // Assert that the value is correct - // Assert.Equal("John Doe", name.ToString()); - // } + Assert.True(await commands.MergeAsync("test_merge", "$.a.b", "{\"h\":\"i\"}")); + Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\",\"h\":\"i\"}}}", (await commands.GetAsync("test_merge")).ToString()); + // Test with null value to delete a value + Assert.True(await commands.MergeAsync("test_merge", "$.a.b", "{\"c\":null}")); + Assert.Equal("{\"a\":{\"b\":{\"h\":\"i\",\"e\":\"f\"}}}", (await commands.GetAsync("test_merge")).ToString()); + } [Fact] public void TestKeyValuePathErrors() From 17534bf18f54d13cd65dc5c6586576dad9f6303f Mon Sep 17 00:00:00 2001 From: shacharPash Date: Wed, 10 May 2023 17:41:18 +0300 Subject: [PATCH 14/28] fix windows test --- .github/workflows/integration.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index b68fe16a..35445c53 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -105,4 +105,4 @@ jobs: shell: cmd run: | START wsl ./redis-stack-server-${{env.redis_stack_version}}/bin/redis-stack-server & - dotnet test -f net481 --no-build --verbosity normal + dotnet test -f net481 --no-build --verbosity normal --filter Categpry!=edge From 0adef839e8fe3e9b6cd604f9837bfad615684b1a Mon Sep 17 00:00:00 2001 From: shacharPash Date: Wed, 10 May 2023 17:42:06 +0300 Subject: [PATCH 15/28] fix word mistake --- .github/workflows/integration.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 35445c53..7a666e50 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -105,4 +105,4 @@ jobs: shell: cmd run: | START wsl ./redis-stack-server-${{env.redis_stack_version}}/bin/redis-stack-server & - dotnet test -f net481 --no-build --verbosity normal --filter Categpry!=edge + dotnet test -f net481 --no-build --verbosity normal --filter Category!=edge From c99e258f0b531f17d16466d0a1beebbe20f0ff76 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 10:47:09 +0300 Subject: [PATCH 16/28] check if its passing --- tests/NRedisStack.Tests/Json/JsonTests.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 944693af..1c5ab1df 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -778,19 +778,20 @@ await commands.MSetAsync(values) [Trait("Category", "edge")] public void Merge() { + // Create a connection to Redis IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); // Create a key - Assert.True(commands.Set("test_merge", "$", "{\"a\":{\"b\":{\"c\":\"d\"}}}")); - Assert.True(commands.Merge("test_merge", "$", "{\"a\":{\"b\":{\"e\":\"f\"}}}")); - Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\"}}}", commands.Get("test_merge").ToString()); + Assert.True(commands.Set("test_merge", "$", "{\"person\":{\"name\":\"John Doe\",\"age\":25,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}}")); + Assert.True(commands.Merge("test_merge", "$", "{\"person\":{\"age\":30}}")); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); // Test with root path path $.a.b - Assert.True(commands.Merge("test_merge", "$.a.b", "{\"h\":\"i\"}")); - Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\",\"h\":\"i\"}}}", commands.Get("test_merge").ToString()); + Assert.True(commands.Merge("test_merge", "$.person.address", "{\"city\":\"New York\"}")); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", db.Get("test_merge").ToString()); // Test with null value to delete a value - Assert.True(commands.Merge("test_merge", "$.a.b", "{\"c\":null}")); - Assert.Equal("{\"a\":{\"b\":{\"h\":\"i\",\"e\":\"f\"}}}", commands.Get("test_merge").ToString()); + Assert.True(commands.Merge("test_merge", "$.person.age", "null")); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":null,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", db.Get("test_merge").ToString()); } [Fact] From da7fa5f633e7c6f4b60ad528daafc393f1761289 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 10:50:58 +0300 Subject: [PATCH 17/28] check 2 --- tests/NRedisStack.Tests/Json/JsonTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 1c5ab1df..eb8d7c6e 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -788,10 +788,10 @@ public void Merge() // Test with root path path $.a.b Assert.True(commands.Merge("test_merge", "$.person.address", "{\"city\":\"New York\"}")); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", db.Get("test_merge").ToString()); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); // Test with null value to delete a value Assert.True(commands.Merge("test_merge", "$.person.age", "null")); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":null,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", db.Get("test_merge").ToString()); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":null,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); } [Fact] From a91e3ae55fa25d05ee350e91517513272b638ebc Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 11:20:46 +0300 Subject: [PATCH 18/28] check 3 --- tests/NRedisStack.Tests/Json/JsonTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index eb8d7c6e..793669a0 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -784,7 +784,7 @@ public void Merge() // Create a key Assert.True(commands.Set("test_merge", "$", "{\"person\":{\"name\":\"John Doe\",\"age\":25,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}}")); Assert.True(commands.Merge("test_merge", "$", "{\"person\":{\"age\":30}}")); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); // Test with root path path $.a.b Assert.True(commands.Merge("test_merge", "$.person.address", "{\"city\":\"New York\"}")); From 049162e319129d0bd5f95d4516a8435abd4cb53b Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 11:57:19 +0300 Subject: [PATCH 19/28] adding Merge input options --- src/NRedisStack/Json/IJsonCommands.cs | 19 +++++++++++++++++++ src/NRedisStack/Json/IJsonCommandsAsync.cs | 18 ++++++++++++++++++ src/NRedisStack/Json/JsonCommandBuilder.cs | 5 +++++ src/NRedisStack/Json/JsonCommands.cs | 13 +++++++++++++ src/NRedisStack/Json/JsonCommandsAsync.cs | 13 +++++++++++++ tests/NRedisStack.Tests/Json/JsonTests.cs | 12 ++++++------ 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index 3ce848e6..e32c8684 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -225,6 +225,25 @@ public interface IJsonCommands /// bool Merge(RedisKey key, RedisValue path, RedisValue json); + /// + /// Sets or updates the JSON value at a path. + /// + /// The key. + /// The path to set within the key. + /// The value to set. + /// The disposition of the command + /// + bool Merge(RedisKey key, RedisValue path, object obj); + + /// + /// Sets or updates the JSON value at a path. + /// + /// The key, The value to set and + /// The path to set within the key + /// The disposition of the command + /// + bool Merge(KeyValuePath keyValuePath); + /// /// Sets or updates the JSON value of one or more keys. /// diff --git a/src/NRedisStack/Json/IJsonCommandsAsync.cs b/src/NRedisStack/Json/IJsonCommandsAsync.cs index 0abb50bb..9555cb87 100644 --- a/src/NRedisStack/Json/IJsonCommandsAsync.cs +++ b/src/NRedisStack/Json/IJsonCommandsAsync.cs @@ -226,7 +226,25 @@ public interface IJsonCommandsAsync Task MergeAsync(RedisKey key, RedisValue path, RedisValue json); /// + /// Sets or updates the JSON value at a path. + /// + /// The key. + /// The path to set within the key. + /// The value to set. + /// The disposition of the command + /// + Task MergeAsync(RedisKey key, RedisValue path, object obj); + /// + /// Sets or updates the JSON value at a path. + /// + /// The key, The value to set and + /// The path to set within the key + /// The disposition of the command + /// + Task MergeAsync(KeyValuePath keyValuePath); + + /// /// Set json file from the provided file Path. /// /// The key. diff --git a/src/NRedisStack/Json/JsonCommandBuilder.cs b/src/NRedisStack/Json/JsonCommandBuilder.cs index 77446bb0..8bc13e50 100644 --- a/src/NRedisStack/Json/JsonCommandBuilder.cs +++ b/src/NRedisStack/Json/JsonCommandBuilder.cs @@ -43,6 +43,11 @@ public static SerializedCommand Merge(RedisKey key, RedisValue path, RedisValue return new SerializedCommand(JSON.MERGE, key, path, json); } + public static SerializedCommand Merge(KeyValuePath keyValuePath) + { + return new SerializedCommand(JSON.MERGE, keyValuePath.ToArray()); + } + public static SerializedCommand StrAppend(RedisKey key, string value, string? path = null) { if (path == null) diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index f72f79a8..c4729c47 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -52,6 +52,19 @@ public bool Merge(RedisKey key, RedisValue path, RedisValue json) return _db.Execute(JsonCommandBuilder.Merge(key, path, json)).OKtoBoolean(); } + /// + public bool Merge(RedisKey key, RedisValue path, object obj) + { + string json = JsonSerializer.Serialize(obj); + return _db.Execute(JsonCommandBuilder.Merge(key, path, json)).OKtoBoolean(); + } + + /// + public bool Merge(KeyValuePath keyValuePath) + { + return _db.Execute(JsonCommandBuilder.Merge(keyValuePath)).OKtoBoolean(); + } + /// public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) { diff --git a/src/NRedisStack/Json/JsonCommandsAsync.cs b/src/NRedisStack/Json/JsonCommandsAsync.cs index 6a43c8bb..4a764067 100644 --- a/src/NRedisStack/Json/JsonCommandsAsync.cs +++ b/src/NRedisStack/Json/JsonCommandsAsync.cs @@ -155,6 +155,19 @@ public async Task MergeAsync(RedisKey key, RedisValue path, RedisValue jso return (await _db.ExecuteAsync(JsonCommandBuilder.Merge(key, path, json))).OKtoBoolean(); } + /// + public async Task MergeAsync(RedisKey key, RedisValue path, object obj) + { + string json = JsonSerializer.Serialize(obj); + return (await _db.ExecuteAsync(JsonCommandBuilder.Merge(key, path, json))).OKtoBoolean(); + } + + /// + public async Task MergeAsync(KeyValuePath keyValuePath) + { + return (await _db.ExecuteAsync(JsonCommandBuilder.Merge(keyValuePath))).OKtoBoolean(); + } + public async Task SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always) { if (!File.Exists(filePath)) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 793669a0..83e88948 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -781,16 +781,16 @@ public void Merge() // Create a connection to Redis IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - // Create a key - Assert.True(commands.Set("test_merge", "$", "{\"person\":{\"name\":\"John Doe\",\"age\":25,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}}")); - Assert.True(commands.Merge("test_merge", "$", "{\"person\":{\"age\":30}}")); + Assert.True(commands.Set("test_merge", "$", new { person = new { name = "John Doe", age = 25, address = "123 Main Street", phone = "123-456-7890" } })); + Assert.True(commands.Merge("test_merge", "$", new { person = new { age = 30 } })); Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); - // Test with root path path $.a.b - Assert.True(commands.Merge("test_merge", "$.person.address", "{\"city\":\"New York\"}")); + // Test with root path path $.a.b + Assert.True(commands.Merge("test_merge", "$.person.address", new { city = "New York" })); Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); + // Test with null value to delete a value - Assert.True(commands.Merge("test_merge", "$.person.age", "null")); + Assert.True(commands.Merge("test_merge", "$.person.age", RedisValue.Null)); Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":null,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); } From 95793428e489a396fdcbe148420d46e76be2e6b5 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 13:48:24 +0300 Subject: [PATCH 20/28] change $.a.b test merge --- tests/NRedisStack.Tests/Json/JsonTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 83e88948..d6cb33fb 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -786,7 +786,7 @@ public void Merge() Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); // Test with root path path $.a.b - Assert.True(commands.Merge("test_merge", "$.person.address", new { city = "New York" })); + Assert.True(commands.Merge("test_merge", "$.person.address", "New York")); Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); // Test with null value to delete a value From 9ba17dfde7a68cfdbf78b226bf9859af03974879 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 14:21:26 +0300 Subject: [PATCH 21/28] trying somthing else --- tests/NRedisStack.Tests/Json/JsonTests.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index d6cb33fb..93ce4321 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -781,17 +781,17 @@ public void Merge() // Create a connection to Redis IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - Assert.True(commands.Set("test_merge", "$", new { person = new { name = "John Doe", age = 25, address = "123 Main Street", phone = "123-456-7890" } })); + Assert.True(commands.Set("test_merge", "$", new { person = new { name = "John Doe", age = 25, address = new {home = "123 Main Street"}, phone = "123-456-7890" } })); Assert.True(commands.Merge("test_merge", "$", new { person = new { age = 30 } })); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":\"123 Main Street\",\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); // Test with root path path $.a.b - Assert.True(commands.Merge("test_merge", "$.person.address", "New York")); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); + Assert.True(commands.Merge("test_merge", "$.person.", new {work = "Redis office"})); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"work\":\"Redis office\",\"home\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); // Test with null value to delete a value - Assert.True(commands.Merge("test_merge", "$.person.age", RedisValue.Null)); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":null,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); + // Assert.True(commands.Merge("test_merge", "$.person.age", RedisValue.Null)); + // Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":null,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); } [Fact] From d0abeabe59e8c51cb32f94cb43e6aaf85db592a0 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 14:24:45 +0300 Subject: [PATCH 22/28] fix mistake --- tests/NRedisStack.Tests/Json/JsonTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 93ce4321..825238a6 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -786,7 +786,7 @@ public void Merge() Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); // Test with root path path $.a.b - Assert.True(commands.Merge("test_merge", "$.person.", new {work = "Redis office"})); + Assert.True(commands.Merge("test_merge", "$.person.address", new {work = "Redis office"})); Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"work\":\"Redis office\",\"home\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); // Test with null value to delete a value From b25242dcc2c6940f3b87631ad341be34598ad981 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 14:30:59 +0300 Subject: [PATCH 23/28] change order --- tests/NRedisStack.Tests/Json/JsonTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 825238a6..a286246e 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -787,7 +787,7 @@ public void Merge() // Test with root path path $.a.b Assert.True(commands.Merge("test_merge", "$.person.address", new {work = "Redis office"})); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"work\":\"Redis office\",\"home\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); // Test with null value to delete a value // Assert.True(commands.Merge("test_merge", "$.person.age", RedisValue.Null)); From ae4e3c23157f70e3a5f4f347595e51bb81a857d8 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 14:44:02 +0300 Subject: [PATCH 24/28] fix } --- tests/NRedisStack.Tests/Json/JsonTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index a286246e..ac11ada8 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -787,7 +787,7 @@ public void Merge() // Test with root path path $.a.b Assert.True(commands.Merge("test_merge", "$.person.address", new {work = "Redis office"})); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); // Test with null value to delete a value // Assert.True(commands.Merge("test_merge", "$.person.age", RedisValue.Null)); From af083fb201197cbd4eb45eb47cd8eb75e9c9ddf4 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 15:19:56 +0300 Subject: [PATCH 25/28] change to KeyPathValue --- .../Json/DataTypes/KeyValuePath.cs | 13 ++++------- src/NRedisStack/Json/IJsonCommands.cs | 19 +++++---------- src/NRedisStack/Json/IJsonCommandsAsync.cs | 13 ++--------- src/NRedisStack/Json/JsonCommandBuilder.cs | 13 ++++------- src/NRedisStack/Json/JsonCommands.cs | 10 ++------ src/NRedisStack/Json/JsonCommandsAsync.cs | 10 ++------ tests/NRedisStack.Tests/Json/JsonTests.cs | 23 +++++++------------ 7 files changed, 28 insertions(+), 73 deletions(-) diff --git a/src/NRedisStack/Json/DataTypes/KeyValuePath.cs b/src/NRedisStack/Json/DataTypes/KeyValuePath.cs index 52d4009d..252b639e 100644 --- a/src/NRedisStack/Json/DataTypes/KeyValuePath.cs +++ b/src/NRedisStack/Json/DataTypes/KeyValuePath.cs @@ -2,22 +2,17 @@ namespace NRedisStack.Json.DataTypes; -public struct KeyValuePath +public struct KeyPathValue { public string Key { get; set; } - public object Value { get; set; } public string Path { get; set; } + public object Value { get; set; } - public KeyValuePath(string key, object value, string path = "$") + public KeyPathValue(string key, string path, object value) { - if (key == null || value == null) - { - throw new ArgumentNullException("Key and value cannot be null."); - } - Key = key; - Value = value; Path = path; + Value = value; } public string[] ToArray() { diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index e32c8684..bed3e52b 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -209,11 +209,11 @@ public interface IJsonCommands /// /// Sets or updates the JSON value of one or more keys. /// - /// The key, The value to set and + /// The key, The value to set and /// The path to set within the key, must be > 1 /// The disposition of the command /// - bool MSet(KeyValuePath[] keyValuePathList); + bool MSet(KeyPathValue[] KeyPathValueList); /// /// Sets or updates the JSON value at a path. @@ -235,15 +235,6 @@ public interface IJsonCommands /// bool Merge(RedisKey key, RedisValue path, object obj); - /// - /// Sets or updates the JSON value at a path. - /// - /// The key, The value to set and - /// The path to set within the key - /// The disposition of the command - /// - bool Merge(KeyValuePath keyValuePath); - /// /// Sets or updates the JSON value of one or more keys. /// @@ -271,7 +262,8 @@ public interface IJsonCommands /// The key to append to. /// The path of the string(s) to append to. /// The value to append. - /// The new length of the string(s) appended to, those lengths will be null if the path did not resolve ot a string. + /// The new length of the string(s) appended to, those lengths + /// will be null if the path did not resolve ot a string. /// long?[] StrAppend(RedisKey key, string value, string? path = null); @@ -280,7 +272,8 @@ public interface IJsonCommands /// /// The key of the json object. /// The path of the string(s) within the json object. - /// The length of the string(s) appended to, those lengths will be null if the path did not resolve ot a string. + /// The length of the string(s) appended to, those lengths + /// will be null if the path did not resolve ot a string. /// public long?[] StrLen(RedisKey key, string? path = null); diff --git a/src/NRedisStack/Json/IJsonCommandsAsync.cs b/src/NRedisStack/Json/IJsonCommandsAsync.cs index 9555cb87..81c1343f 100644 --- a/src/NRedisStack/Json/IJsonCommandsAsync.cs +++ b/src/NRedisStack/Json/IJsonCommandsAsync.cs @@ -209,11 +209,11 @@ public interface IJsonCommandsAsync /// /// Sets or updates the JSON value of one or more keys. /// - /// The key, The value to set and + /// The key, The value to set and /// The path to set within the key, must be > 1 /// The disposition of the command /// - Task MSetAsync(KeyValuePath[] keyValuePathList); + Task MSetAsync(KeyPathValue[] KeyPathValueList); /// /// Sets or updates the JSON value at a path. @@ -235,15 +235,6 @@ public interface IJsonCommandsAsync /// Task MergeAsync(RedisKey key, RedisValue path, object obj); - /// - /// Sets or updates the JSON value at a path. - /// - /// The key, The value to set and - /// The path to set within the key - /// The disposition of the command - /// - Task MergeAsync(KeyValuePath keyValuePath); - /// /// Set json file from the provided file Path. /// diff --git a/src/NRedisStack/Json/JsonCommandBuilder.cs b/src/NRedisStack/Json/JsonCommandBuilder.cs index 8bc13e50..b5505100 100644 --- a/src/NRedisStack/Json/JsonCommandBuilder.cs +++ b/src/NRedisStack/Json/JsonCommandBuilder.cs @@ -29,12 +29,12 @@ public static SerializedCommand Set(RedisKey key, RedisValue path, RedisValue js }; } - public static SerializedCommand MSet(KeyValuePath[] keyValuePathList) + public static SerializedCommand MSet(KeyPathValue[] KeyPathValueList) { - if (keyValuePathList.Length < 1) - throw new ArgumentOutOfRangeException(nameof(keyValuePathList)); + if (KeyPathValueList.Length < 1) + throw new ArgumentOutOfRangeException(nameof(KeyPathValueList)); - var args = keyValuePathList.SelectMany(x => x.ToArray()).ToArray(); + var args = KeyPathValueList.SelectMany(x => x.ToArray()).ToArray(); return new SerializedCommand(JSON.MSET, args); } @@ -43,11 +43,6 @@ public static SerializedCommand Merge(RedisKey key, RedisValue path, RedisValue return new SerializedCommand(JSON.MERGE, key, path, json); } - public static SerializedCommand Merge(KeyValuePath keyValuePath) - { - return new SerializedCommand(JSON.MERGE, keyValuePath.ToArray()); - } - public static SerializedCommand StrAppend(RedisKey key, string value, string? path = null) { if (path == null) diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index c4729c47..d1e588f7 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -41,9 +41,9 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When } /// - public bool MSet(KeyValuePath[] keyValuePathList) + public bool MSet(KeyPathValue[] KeyPathValueList) { - return _db.Execute(JsonCommandBuilder.MSet(keyValuePathList)).OKtoBoolean(); + return _db.Execute(JsonCommandBuilder.MSet(KeyPathValueList)).OKtoBoolean(); } /// @@ -59,12 +59,6 @@ public bool Merge(RedisKey key, RedisValue path, object obj) return _db.Execute(JsonCommandBuilder.Merge(key, path, json)).OKtoBoolean(); } - /// - public bool Merge(KeyValuePath keyValuePath) - { - return _db.Execute(JsonCommandBuilder.Merge(keyValuePath)).OKtoBoolean(); - } - /// public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) { diff --git a/src/NRedisStack/Json/JsonCommandsAsync.cs b/src/NRedisStack/Json/JsonCommandsAsync.cs index 4a764067..c42fcbd5 100644 --- a/src/NRedisStack/Json/JsonCommandsAsync.cs +++ b/src/NRedisStack/Json/JsonCommandsAsync.cs @@ -144,9 +144,9 @@ public async Task SetAsync(RedisKey key, RedisValue path, RedisValue json, return (await _db.ExecuteAsync(JsonCommandBuilder.Set(key, path, json, when))).OKtoBoolean(); } - public async Task MSetAsync(KeyValuePath[] keyValuePathList) + public async Task MSetAsync(KeyPathValue[] KeyPathValueList) { - return (await _db.ExecuteAsync(JsonCommandBuilder.MSet(keyValuePathList))).OKtoBoolean(); + return (await _db.ExecuteAsync(JsonCommandBuilder.MSet(KeyPathValueList))).OKtoBoolean(); } /// @@ -162,12 +162,6 @@ public async Task MergeAsync(RedisKey key, RedisValue path, object obj) return (await _db.ExecuteAsync(JsonCommandBuilder.Merge(key, path, json))).OKtoBoolean(); } - /// - public async Task MergeAsync(KeyValuePath keyValuePath) - { - return (await _db.ExecuteAsync(JsonCommandBuilder.Merge(keyValuePath))).OKtoBoolean(); - } - public async Task SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always) { if (!File.Exists(filePath)) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index ac11ada8..a78c8710 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -734,10 +734,10 @@ public void MSet() var key1 = keys[0]; var key2 = keys[1]; - KeyValuePath[] values = new[] + KeyPathValue[] values = new[] { - new KeyValuePath(key1, new { a = "hello" }), - new KeyValuePath(key2, new { a = "world" }) + new KeyPathValue(key1, "$", new { a = "hello" }), + new KeyPathValue(key2, "$", new { a = "world" }) }; commands.MSet(values) ; @@ -747,7 +747,7 @@ public void MSet() Assert.Equal("[\"world\"]", result[1].ToString()); // test errors: - Assert.Throws(() => commands.MSet(new KeyValuePath[0])); + Assert.Throws(() => commands.MSet(new KeyPathValue[0])); } [Fact] @@ -758,10 +758,10 @@ public async Task MSetAsync() var keys = CreateKeyNames(2); var key1 = keys[0]; var key2 = keys[1]; - KeyValuePath[] values = new[] + KeyPathValue[] values = new[] { - new KeyValuePath(key1, new { a = "hello" }), - new KeyValuePath(key2, new { a = "world" }) + new KeyPathValue(key1, "$", new { a = "hello" }), + new KeyPathValue(key2, "$", new { a = "world" }) }; await commands.MSetAsync(values) ; @@ -771,7 +771,7 @@ await commands.MSetAsync(values) Assert.Equal("[\"world\"]", result[1].ToString()); // test errors: - await Assert.ThrowsAsync(async () => await commands.MSetAsync(new KeyValuePath[0])); + await Assert.ThrowsAsync(async () => await commands.MSetAsync(new KeyPathValue[0])); } [Fact] @@ -813,13 +813,6 @@ public async Task MergeAsync() Assert.Equal("{\"a\":{\"b\":{\"h\":\"i\",\"e\":\"f\"}}}", (await commands.GetAsync("test_merge")).ToString()); } - [Fact] - public void TestKeyValuePathErrors() - { - Assert.Throws(() => new KeyValuePath(null!, new { a = "hello" })); - Assert.Throws(() => new KeyValuePath("key", null!)); - } - [Fact] public void MGet() { From a633e3bc254e26e5639e3ed1e9786a022dcc6096 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 15:27:27 +0300 Subject: [PATCH 26/28] null test --- tests/NRedisStack.Tests/Json/JsonTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index a78c8710..a49bc332 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -790,8 +790,8 @@ public void Merge() Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); // Test with null value to delete a value - // Assert.True(commands.Merge("test_merge", "$.person.age", RedisValue.Null)); - // Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":null,\"address\":{\"city\":\"New York\",\"street\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}}", commands.Get("test_merge").ToString()); + Assert.True(commands.Merge("test_merge", "$.person.age", RedisValue.Null)); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); } [Fact] From 53551f51ba074aaeb7de39d4e368b93d4c5b9494 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 15:37:10 +0300 Subject: [PATCH 27/28] async test + fixes --- tests/NRedisStack.Tests/Json/JsonTests.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index a49bc332..0a3e637d 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -790,7 +790,7 @@ public void Merge() Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); // Test with null value to delete a value - Assert.True(commands.Merge("test_merge", "$.person.age", RedisValue.Null)); + Assert.True(commands.Merge("test_merge", "$.person", "{\"age\":null}")); Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); } @@ -798,19 +798,20 @@ public void Merge() [Trait("Category", "edge")] public async Task MergeAsync() { + // Create a connection to Redis IJsonCommandsAsync commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - // Create a key - Assert.True(await commands.SetAsync("test_merge", "$", "{\"a\":{\"b\":{\"c\":\"d\"}}}")); - Assert.True(await commands.MergeAsync("test_merge", "$", "{\"a\":{\"b\":{\"e\":\"f\"}}}")); - Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\"}}}", (await commands.GetAsync("test_merge")).ToString()); + Assert.True(await commands.SetAsync("test_merge", "$", new { person = new { name = "John Doe", age = 25, address = new {home = "123 Main Street"}, phone = "123-456-7890" } })); + Assert.True(await commands.MergeAsync("test_merge", "$", new { person = new { age = 30 } })); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\"},\"phone\":\"123-456-7890\"}}", (await commands.GetAsync("test_merge")).ToString()); + // Test with root path path $.a.b + Assert.True(await commands.MergeAsync("test_merge", "$.person.address", new {work = "Redis office"})); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"age\":30,\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", (await commands.GetAsync("test_merge")).ToString()); - Assert.True(await commands.MergeAsync("test_merge", "$.a.b", "{\"h\":\"i\"}")); - Assert.Equal("{\"a\":{\"b\":{\"c\":\"d\",\"e\":\"f\",\"h\":\"i\"}}}", (await commands.GetAsync("test_merge")).ToString()); // Test with null value to delete a value - Assert.True(await commands.MergeAsync("test_merge", "$.a.b", "{\"c\":null}")); - Assert.Equal("{\"a\":{\"b\":{\"h\":\"i\",\"e\":\"f\"}}}", (await commands.GetAsync("test_merge")).ToString()); + Assert.True(await commands.MergeAsync("test_merge", "$.person", "{\"age\":null}")); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", (await commands.GetAsync("test_merge")).ToString()); } [Fact] From 82798206fb9d550e0439a287db6b23cd43b7954b Mon Sep 17 00:00:00 2001 From: shacharPash Date: Thu, 11 May 2023 15:48:48 +0300 Subject: [PATCH 28/28] change order --- tests/NRedisStack.Tests/Json/JsonTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 0a3e637d..18a66392 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -791,7 +791,7 @@ public void Merge() // Test with null value to delete a value Assert.True(commands.Merge("test_merge", "$.person", "{\"age\":null}")); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", commands.Get("test_merge").ToString()); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"phone\":\"123-456-7890\",\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"}}}", commands.Get("test_merge").ToString()); } [Fact] @@ -811,7 +811,7 @@ public async Task MergeAsync() // Test with null value to delete a value Assert.True(await commands.MergeAsync("test_merge", "$.person", "{\"age\":null}")); - Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"},\"phone\":\"123-456-7890\"}}", (await commands.GetAsync("test_merge")).ToString()); + Assert.Equal("{\"person\":{\"name\":\"John Doe\",\"phone\":\"123-456-7890\",\"address\":{\"home\":\"123 Main Street\",\"work\":\"Redis office\"}}}", (await commands.GetAsync("test_merge")).ToString()); } [Fact]