From ad6f7515b639a6583639a977cba5bd05fd72351b Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Sun, 13 Nov 2022 11:31:38 +0200 Subject: [PATCH 1/9] Start working on JSON SetFile --- src/NRedisStack/Json/JsonCommands.cs | 7 ++ tests/NRedisStack.Tests/Json/JsonTests.cs | 64 +++++++++++++------ .../Json/PersonJsonFile.json | 5 ++ .../Json/PersonJsonTextFile.txt | 5 ++ 4 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 tests/NRedisStack.Tests/Json/PersonJsonFile.json create mode 100644 tests/NRedisStack.Tests/Json/PersonJsonTextFile.txt diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 78fa6627..ea064c6c 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -60,6 +60,13 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When return true; } + /// // TODO: Add this in IJsonCommands + public bool SetFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) + { + string fileContent = File.ReadAllText(filePath); // TODO: check this + return Set(key, path, fileContent, when); + } + /// public long?[] 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 b35fb9ab..1d3b438a 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -20,6 +20,30 @@ public void Dispose() redisFixture.Redis.GetDatabase().KeyDelete(_testName); } + [Fact] + public void TestSetFile() + { + //arrange + var conn = redisFixture.Redis; + var db = conn.GetDatabase(); + JsonCommands commands = new JsonCommands(db); //TODO: change left side to IJsonCommands + var keys = CreateKeyNames(1); + + //creating json string: + var obj = new Person { Name = "Shachar", Age = 23 }; + string json = JsonSerializer.Serialize(obj); + + //create file: + string path = Path.GetFullPath(Path.GetTempPath() + "testFile.txt"); + File.Create(path); + + //writing json to file: + File.WriteAllText(path, json); + + commands.SetFile(keys[0], "$", path); + File.Delete(path); + } + [Fact] public void TestJsonSetNotExist() { @@ -87,7 +111,7 @@ public void TestResp() Assert.Equal(33, (long)respResult[i]!); conn.GetDatabase().KeyDelete(key); } - + [Fact] public async Task TestRespAsync() { @@ -140,7 +164,7 @@ public void TestStringAppend() Assert.Null(nullResult[0]); Assert.Equal(6, simpleKeyResult[0]); } - + [Fact] public async Task TestStringAppendAsync() { @@ -193,7 +217,7 @@ public void StringLength() Assert.Null(nullResult[0]); Assert.Equal(3,simpleResult[0]); } - + [Fact] public async Task StringLengthAsync() { @@ -240,7 +264,7 @@ public void Toggle() Assert.True(result[1]); Assert.False(simpleResult[0]); } - + [Fact] public async Task ToggleAsync() { @@ -286,7 +310,7 @@ public void Type() result = commands.Type(simpleKey); Assert.Equal(JsonType.BOOLEAN, result[0]); } - + [Fact] public async Task TypeAsync() { @@ -328,7 +352,7 @@ public void ArrayAppend() result = commands.ArrAppend(complexKey, "$.people", new { name = "bob" }); Assert.Equal(2, result[0]); } - + [Fact] public async Task ArrayAppendAsync() { @@ -360,7 +384,7 @@ public void ArrayIndex() Assert.Equal(1,res[0]); Assert.Equal(-1,res[1]); } - + [Fact] public async Task ArrayIndexAsync() { @@ -391,7 +415,7 @@ public void ArrayInsert() result = commands.ArrInsert(simpleKey, "$", 1, "Lys"); Assert.Equal(4, result[0]); } - + [Fact] public async Task ArrayInsertAsync() { @@ -424,7 +448,7 @@ public void ArrayLength() result = commands.ArrLen(simpleKey); Assert.Equal(3, result[0]); } - + [Fact] public async Task ArrayLengthAsync() { @@ -474,7 +498,7 @@ public void ArrayTrim() result = commands.ArrTrim(simpleKey, "$", 0, 1); Assert.Equal(2,result[0]); } - + [Fact] public async Task ArrayTrimAsync() { @@ -506,7 +530,7 @@ public void Clear() result = commands.Clear(simpleKey); Assert.Equal(1,result); } - + [Fact] public async Task ClearAsync() { @@ -538,7 +562,7 @@ public void Del() result = commands.Del(simpleKey); Assert.Equal(1,result); } - + [Fact] public async Task DelAsync() { @@ -570,7 +594,7 @@ public void Forget() result = commands.Forget(simpleKey); Assert.Equal(1,result); } - + [Fact] public async Task ForgetAsync() { @@ -606,7 +630,7 @@ public void Get() Assert.Equal("Alice", people[1]!.Name); Assert.Equal(35, people[1]!.Age); } - + [Fact] public async Task GetAsync() { @@ -641,7 +665,7 @@ public void MGet() Assert.Equal("[\"hello\"]", result[0].ToString()); Assert.Equal("[\"world\"]", result[1].ToString()); } - + [Fact] public async Task MGetAsync() { @@ -669,7 +693,7 @@ public void NumIncrby() Assert.Equal(36, result[1]); Assert.Null(result[2]); } - + [Fact] public async Task NumIncrbyAsync() { @@ -700,7 +724,7 @@ public void ObjectKeys() Assert.Contains("a", result[1]); Assert.Contains("b", result[1]); } - + [Fact] public async Task ObjectKeysAsync() { @@ -734,7 +758,7 @@ public void ObjectLength() Assert.Null(result[2]); } - + [Fact] public async Task ObjectLengthAsync() { @@ -774,7 +798,7 @@ public void TestMultiPathGet() Assert.True(obj["$.b"]![0]!["a"]!.ToString() == "world"); } - + [Fact] public async Task TestMultiPathGetAsync() { @@ -812,7 +836,7 @@ public void Memory() res = commands.DebugMemory("non-existent key"); Assert.Equal(0,res); } - + [Fact] public async Task MemoryAsync() { diff --git a/tests/NRedisStack.Tests/Json/PersonJsonFile.json b/tests/NRedisStack.Tests/Json/PersonJsonFile.json new file mode 100644 index 00000000..75e655fc --- /dev/null +++ b/tests/NRedisStack.Tests/Json/PersonJsonFile.json @@ -0,0 +1,5 @@ +{ + "FirstName": "John", + "LastName": "Doe", + "JobTitle": "Developer" +} diff --git a/tests/NRedisStack.Tests/Json/PersonJsonTextFile.txt b/tests/NRedisStack.Tests/Json/PersonJsonTextFile.txt new file mode 100644 index 00000000..75e655fc --- /dev/null +++ b/tests/NRedisStack.Tests/Json/PersonJsonTextFile.txt @@ -0,0 +1,5 @@ +{ + "FirstName": "John", + "LastName": "Doe", + "JobTitle": "Developer" +} From 3cd8a5926f2b11235bfee9df5f1237e324d610ea Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Sun, 13 Nov 2022 18:31:38 +0200 Subject: [PATCH 2/9] Add SetFile to IJsonCommands --- src/NRedisStack/Json/IJsonCommands.cs | 11 +++++++++++ src/NRedisStack/Json/JsonCommands.cs | 2 +- tests/NRedisStack.Tests/Json/JsonTests.cs | 6 +++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index f8d5cd70..8b9fc37d 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -205,6 +205,17 @@ public interface IJsonCommands /// bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When.Always); + /// + /// Set's json file from to the provided file Path. + /// + /// The key. + /// The path to set within the key. + /// The path of the file to set. + /// When to set the value. + /// The disposition of the command + /// + bool SetFile(RedisKey key, RedisValue path, string filePath, When when = When.Always); + /// /// Appends the provided string to the string(s) at the provided path. /// diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index ea064c6c..3581b731 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -60,7 +60,7 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When return true; } - /// // TODO: Add this in IJsonCommands + /// public bool SetFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) { string fileContent = File.ReadAllText(filePath); // TODO: check this diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 1d3b438a..381153e3 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -26,7 +26,7 @@ public void TestSetFile() //arrange var conn = redisFixture.Redis; var db = conn.GetDatabase(); - JsonCommands commands = new JsonCommands(db); //TODO: change left side to IJsonCommands + IJsonCommands commands = new JsonCommands(db); var keys = CreateKeyNames(1); //creating json string: @@ -34,11 +34,11 @@ public void TestSetFile() string json = JsonSerializer.Serialize(obj); //create file: - string path = Path.GetFullPath(Path.GetTempPath() + "testFile.txt"); + string path = Path.GetFullPath("testFile.txt"); File.Create(path); //writing json to file: - File.WriteAllText(path, json); + File.WriteAllText(path, json); // TODO: figure out how to write json to file commands.SetFile(keys[0], "$", path); File.Delete(path); From f6a0fb31003ec131a87a8d2d2da41ca5febce874 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Sun, 13 Nov 2022 19:15:05 +0200 Subject: [PATCH 3/9] Fix TestSetFile --- tests/NRedisStack.Tests/Json/JsonTests.cs | 108 +++++++++--------- .../Json/PersonJsonFile.json | 5 - .../Json/PersonJsonTextFile.txt | 5 - 3 files changed, 54 insertions(+), 64 deletions(-) delete mode 100644 tests/NRedisStack.Tests/Json/PersonJsonFile.json delete mode 100644 tests/NRedisStack.Tests/Json/PersonJsonTextFile.txt diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 381153e3..3bea17ac 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -33,15 +33,15 @@ public void TestSetFile() var obj = new Person { Name = "Shachar", Age = 23 }; string json = JsonSerializer.Serialize(obj); - //create file: - string path = Path.GetFullPath("testFile.txt"); - File.Create(path); + var file = "testFile.json"; //writing json to file: - File.WriteAllText(path, json); // TODO: figure out how to write json to file + File.WriteAllText(file, json); - commands.SetFile(keys[0], "$", path); - File.Delete(path); + commands.SetFile(keys[0], "$", file); + var actual = commands.Get(keys[0]); + Assert.Equal(json, actual.ToString()); + File.Delete(file); } [Fact] @@ -147,7 +147,7 @@ public void TestStringAppend() var keys = CreateKeyNames(2); var key = keys[0]; - commands.Set(key, "$", new { name = "Steve", sibling = new {name = "christopher"}, age = 33}); + commands.Set(key, "$", new { name = "Steve", sibling = new { name = "christopher" }, age = 33 }); var simpleStringKey = keys[1]; commands.Set(simpleStringKey, "$", "\"foo\""); @@ -175,7 +175,7 @@ public async Task TestStringAppendAsync() var keys = CreateKeyNames(2); var key = keys[0]; - await commands.SetAsync(key, "$", new { name = "Steve", sibling = new {name = "christopher"}, age = 33}); + await commands.SetAsync(key, "$", new { name = "Steve", sibling = new { name = "christopher" }, age = 33 }); var simpleStringKey = keys[1]; await commands.SetAsync(simpleStringKey, "$", "\"foo\""); @@ -204,7 +204,7 @@ public void StringLength() var key = keys[0]; var simpleStringKey = keys[1]; - commands.Set(key, "$", new { name = "Steve", sibling = new {name = "christopher"}, age = 33}); + commands.Set(key, "$", new { name = "Steve", sibling = new { name = "christopher" }, age = 33 }); commands.Set(simpleStringKey, "$", "\"foo\""); var normalResult = commands.StrLen(key, "$..name"); @@ -215,7 +215,7 @@ public void StringLength() Assert.Equal(5, normalResult[i++]); Assert.Equal(11, normalResult[i]); Assert.Null(nullResult[0]); - Assert.Equal(3,simpleResult[0]); + Assert.Equal(3, simpleResult[0]); } [Fact] @@ -229,7 +229,7 @@ public async Task StringLengthAsync() var key = keys[0]; var simpleStringKey = keys[1]; - await commands.SetAsync(key, "$", new { name = "Steve", sibling = new {name = "christopher"}, age = 33}); + await commands.SetAsync(key, "$", new { name = "Steve", sibling = new { name = "christopher" }, age = 33 }); await commands.SetAsync(simpleStringKey, "$", "\"foo\""); var normalResult = await commands.StrLenAsync(key, "$..name"); @@ -240,7 +240,7 @@ public async Task StringLengthAsync() Assert.Equal(5, normalResult[i++]); Assert.Equal(11, normalResult[i]); Assert.Null(nullResult[0]); - Assert.Equal(3,simpleResult[0]); + Assert.Equal(3, simpleResult[0]); } [Fact] @@ -254,7 +254,7 @@ public void Toggle() var key = keys[0]; var simpleKey = keys[1]; - commands.Set(key, "$", new { @bool = true, other = new {@bool = false}, age = 33}); + commands.Set(key, "$", new { @bool = true, other = new { @bool = false }, age = 33 }); commands.Set(simpleKey, "$", true); var result = commands.Toggle(key, "$..bool"); @@ -276,7 +276,7 @@ public async Task ToggleAsync() var key = keys[0]; var simpleKey = keys[1]; - await commands.SetAsync(key, "$", new { @bool = true, other = new {@bool = false}, age = 33}); + await commands.SetAsync(key, "$", new { @bool = true, other = new { @bool = false }, age = 33 }); await commands.SetAsync(simpleKey, "$", true); var result = await commands.ToggleAsync(key, "$..bool"); @@ -297,7 +297,7 @@ public void Type() var keys = CreateKeyNames(2); var key = keys[0]; var simpleKey = keys[1]; - commands.Set(key, "$", new { name = "Steve", sibling = new {name = "christopher"}, age = 33, aDouble = 3.5}); + commands.Set(key, "$", new { name = "Steve", sibling = new { name = "christopher" }, age = 33, aDouble = 3.5 }); commands.Set(simpleKey, "$", "true"); var result = commands.Type(key, "$..name"); @@ -321,7 +321,7 @@ public async Task TypeAsync() var keys = CreateKeyNames(2); var key = keys[0]; var simpleKey = keys[1]; - await commands.SetAsync(key, "$", new { name = "Steve", sibling = new {name = "christopher"}, age = 33, aDouble = 3.5}); + await commands.SetAsync(key, "$", new { name = "Steve", sibling = new { name = "christopher" }, age = 33, aDouble = 3.5 }); await commands.SetAsync(simpleKey, "$", "true"); var result = await commands.TypeAsync(key, "$..name"); @@ -347,7 +347,7 @@ public void ArrayAppend() commands.Set(key, "$", new { name = "Elizabeth", nickNames = new[] { "Beth" } }); commands.Set(complexKey, "$", new { name = "foo", people = new[] { new { name = "steve" } } }); - var result = commands.ArrAppend(key, "$.nickNames", "Elle", "Liz","Betty"); + var result = commands.ArrAppend(key, "$.nickNames", "Elle", "Liz", "Betty"); Assert.Equal(4, result[0]); result = commands.ArrAppend(complexKey, "$.people", new { name = "bob" }); Assert.Equal(2, result[0]); @@ -365,7 +365,7 @@ public async Task ArrayAppendAsync() await commands.SetAsync(key, "$", new { name = "Elizabeth", nickNames = new[] { "Beth" } }); await commands.SetAsync(complexKey, "$", new { name = "foo", people = new[] { new { name = "steve" } } }); - var result = await commands.ArrAppendAsync(key, "$.nickNames", "Elle", "Liz","Betty"); + var result = await commands.ArrAppendAsync(key, "$.nickNames", "Elle", "Liz", "Betty"); Assert.Equal(4, result[0]); result = await commands.ArrAppendAsync(complexKey, "$.people", new { name = "bob" }); Assert.Equal(2, result[0]); @@ -379,10 +379,10 @@ public void ArrayIndex() IJsonCommands commands = new JsonCommands(db); var keys = CreateKeyNames(1); var key = keys[0]; - commands.Set(key, "$", new { name = "Elizabeth", nicknames = new[] { "Beth", "Betty", "Liz" }, sibling = new {name="Johnathan", nicknames = new [] {"Jon", "Johnny"}} }); - var res = commands.ArrIndex(key, "$..nicknames", "Betty", 0,5); - Assert.Equal(1,res[0]); - Assert.Equal(-1,res[1]); + commands.Set(key, "$", new { name = "Elizabeth", nicknames = new[] { "Beth", "Betty", "Liz" }, sibling = new { name = "Johnathan", nicknames = new[] { "Jon", "Johnny" } } }); + var res = commands.ArrIndex(key, "$..nicknames", "Betty", 0, 5); + Assert.Equal(1, res[0]); + Assert.Equal(-1, res[1]); } [Fact] @@ -393,10 +393,10 @@ public async Task ArrayIndexAsync() IJsonCommands commands = new JsonCommands(db); var keys = CreateKeyNames(1); var key = keys[0]; - await commands.SetAsync(key, "$", new { name = "Elizabeth", nicknames = new[] { "Beth", "Betty", "Liz" }, sibling = new {name="Johnathan", nicknames = new [] {"Jon", "Johnny"}} }); - var res = await commands.ArrIndexAsync(key, "$..nicknames", "Betty", 0,5); - Assert.Equal(1,res[0]); - Assert.Equal(-1,res[1]); + await commands.SetAsync(key, "$", new { name = "Elizabeth", nicknames = new[] { "Beth", "Betty", "Liz" }, sibling = new { name = "Johnathan", nicknames = new[] { "Jon", "Johnny" } } }); + var res = await commands.ArrIndexAsync(key, "$..nicknames", "Betty", 0, 5); + Assert.Equal(1, res[0]); + Assert.Equal(-1, res[1]); } [Fact] @@ -411,7 +411,7 @@ public void ArrayInsert() commands.Set(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = commands.ArrInsert(key, $"$.nicknames", 1, "Lys"); - Assert.Equal(4,result[0]); + Assert.Equal(4, result[0]); result = commands.ArrInsert(simpleKey, "$", 1, "Lys"); Assert.Equal(4, result[0]); } @@ -428,7 +428,7 @@ public async Task ArrayInsertAsync() await commands.SetAsync(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = await commands.ArrInsertAsync(key, $"$.nicknames", 1, "Lys"); - Assert.Equal(4,result[0]); + Assert.Equal(4, result[0]); result = await commands.ArrInsertAsync(simpleKey, "$", 1, "Lys"); Assert.Equal(4, result[0]); } @@ -494,9 +494,9 @@ public void ArrayTrim() commands.Set(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = commands.ArrTrim(key, "$.nicknames", 0, 0); - Assert.Equal(1,result[0]); + Assert.Equal(1, result[0]); result = commands.ArrTrim(simpleKey, "$", 0, 1); - Assert.Equal(2,result[0]); + Assert.Equal(2, result[0]); } [Fact] @@ -510,9 +510,9 @@ public async Task ArrayTrimAsync() await commands.SetAsync(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = await commands.ArrTrimAsync(key, "$.nicknames", 0, 0); - Assert.Equal(1,result[0]); + Assert.Equal(1, result[0]); result = await commands.ArrTrimAsync(simpleKey, "$", 0, 1); - Assert.Equal(2,result[0]); + Assert.Equal(2, result[0]); } [Fact] @@ -526,9 +526,9 @@ public void Clear() commands.Set(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = commands.Clear(key, "$.nicknames"); - Assert.Equal(1,result); + Assert.Equal(1, result); result = commands.Clear(simpleKey); - Assert.Equal(1,result); + Assert.Equal(1, result); } [Fact] @@ -542,9 +542,9 @@ public async Task ClearAsync() await commands.SetAsync(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = await commands.ClearAsync(key, "$.nicknames"); - Assert.Equal(1,result); + Assert.Equal(1, result); result = await commands.ClearAsync(simpleKey); - Assert.Equal(1,result); + Assert.Equal(1, result); } [Fact] @@ -558,9 +558,9 @@ public void Del() commands.Set(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = commands.Del(key, "$.nicknames"); - Assert.Equal(1,result); + Assert.Equal(1, result); result = commands.Del(simpleKey); - Assert.Equal(1,result); + Assert.Equal(1, result); } [Fact] @@ -574,9 +574,9 @@ public async Task DelAsync() await commands.SetAsync(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = await commands.DelAsync(key, "$.nicknames"); - Assert.Equal(1,result); + Assert.Equal(1, result); result = await commands.DelAsync(simpleKey); - Assert.Equal(1,result); + Assert.Equal(1, result); } [Fact] @@ -590,9 +590,9 @@ public void Forget() commands.Set(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = commands.Forget(key, "$.nicknames"); - Assert.Equal(1,result); + Assert.Equal(1, result); result = commands.Forget(simpleKey); - Assert.Equal(1,result); + Assert.Equal(1, result); } [Fact] @@ -606,9 +606,9 @@ public async Task ForgetAsync() await commands.SetAsync(simpleKey, "$", new[] { "Al", "Ali", "Ally" }); var result = await commands.ForgetAsync(key, "$.nicknames"); - Assert.Equal(1,result); + Assert.Equal(1, result); result = await commands.ForgetAsync(simpleKey); - Assert.Equal(1,result); + Assert.Equal(1, result); } [Fact] @@ -618,8 +618,8 @@ public void Get() var keys = CreateKeyNames(2); var key = keys[0]; var complexKey = keys[1]; - commands.Set(key, "$", new Person(){Age = 35, Name = "Alice"}); - commands.Set(complexKey, "$", new {a=new Person(){Age = 35, Name = "Alice"}, b = new {a = new Person(){Age = 35, Name = "Alice"}}}); + commands.Set(key, "$", new Person() { Age = 35, Name = "Alice" }); + commands.Set(complexKey, "$", new { a = new Person() { Age = 35, Name = "Alice" }, b = new { a = new Person() { Age = 35, Name = "Alice" } } }); var result = commands.Get(key); Assert.Equal("Alice", result!.Name); Assert.Equal(35, result.Age); @@ -638,8 +638,8 @@ public async Task GetAsync() var keys = CreateKeyNames(2); var key = keys[0]; var complexKey = keys[1]; - await commands.SetAsync(key, "$", new Person(){Age = 35, Name = "Alice"}); - await commands.SetAsync(complexKey, "$", new {a=new Person(){Age = 35, Name = "Alice"}, b = new {a = new Person(){Age = 35, Name = "Alice"}}}); + await commands.SetAsync(key, "$", new Person() { Age = 35, Name = "Alice" }); + await commands.SetAsync(complexKey, "$", new { a = new Person() { Age = 35, Name = "Alice" }, b = new { a = new Person() { Age = 35, Name = "Alice" } } }); var result = await commands.GetAsync(key); Assert.Equal("Alice", result!.Name); Assert.Equal(35, result.Age); @@ -687,7 +687,7 @@ public void NumIncrby() IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); var keys = CreateKeyNames(1); var key = keys[0]; - commands.Set(key, "$", new { age = 33, a = new { age = 34 }, b = new {age = "cat"} }); + commands.Set(key, "$", new { age = 33, a = new { age = 34 }, b = new { age = "cat" } }); var result = commands.NumIncrby(key, "$..age", 2); Assert.Equal(35, result[0]); Assert.Equal(36, result[1]); @@ -700,7 +700,7 @@ public async Task NumIncrbyAsync() IJsonCommands commands = new JsonCommands(redisFixture.Redis.GetDatabase()); var keys = CreateKeyNames(1); var key = keys[0]; - await commands.SetAsync(key, "$", new { age = 33, a = new { age = 34 }, b = new {age = "cat"} }); + await commands.SetAsync(key, "$", new { age = 33, a = new { age = 34 }, b = new { age = "cat" } }); var result = await commands.NumIncrbyAsync(key, "$..age", 2); Assert.Equal(35, result[0]); Assert.Equal(36, result[1]); @@ -830,11 +830,11 @@ public void Memory() var keys = CreateKeyNames(1); var key = keys[0]; - commands.Set(key, "$", new {a="hello", b=new {a="world"}}); + commands.Set(key, "$", new { a = "hello", b = new { a = "world" } }); var res = commands.DebugMemory(key); Assert.Equal(45, res); res = commands.DebugMemory("non-existent key"); - Assert.Equal(0,res); + Assert.Equal(0, res); } [Fact] @@ -844,10 +844,10 @@ public async Task MemoryAsync() var keys = CreateKeyNames(1); var key = keys[0]; - await commands.SetAsync(key, "$", new {a="hello", b=new {a="world"}}); + await commands.SetAsync(key, "$", new { a = "hello", b = new { a = "world" } }); var res = await commands.DebugMemoryAsync(key); Assert.Equal(45, res); res = await commands.DebugMemoryAsync("non-existent key"); - Assert.Equal(0,res); + Assert.Equal(0, res); } } \ No newline at end of file diff --git a/tests/NRedisStack.Tests/Json/PersonJsonFile.json b/tests/NRedisStack.Tests/Json/PersonJsonFile.json deleted file mode 100644 index 75e655fc..00000000 --- a/tests/NRedisStack.Tests/Json/PersonJsonFile.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "FirstName": "John", - "LastName": "Doe", - "JobTitle": "Developer" -} diff --git a/tests/NRedisStack.Tests/Json/PersonJsonTextFile.txt b/tests/NRedisStack.Tests/Json/PersonJsonTextFile.txt deleted file mode 100644 index 75e655fc..00000000 --- a/tests/NRedisStack.Tests/Json/PersonJsonTextFile.txt +++ /dev/null @@ -1,5 +0,0 @@ -{ - "FirstName": "John", - "LastName": "Doe", - "JobTitle": "Developer" -} From 1477751dfb5fb4e72d7c183ca98445c3a075d3fd Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 14 Nov 2022 10:28:47 +0200 Subject: [PATCH 4/9] Add SetFileAsync + Test --- src/NRedisStack/Json/IJsonCommands.cs | 11 ++++++++++ src/NRedisStack/Json/JsonCommands.cs | 7 ++++++ tests/NRedisStack.Tests/Json/JsonTests.cs | 26 +++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index 8b9fc37d..b351cfb6 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -462,6 +462,17 @@ public interface IJsonCommands /// Task SetAsync(RedisKey key, RedisValue path, RedisValue json, When when = When.Always); + /// + /// Set's json file from to the provided file Path. + /// + /// The key. + /// The path to set within the key. + /// The path of the file to set. + /// When to set the value. + /// The disposition of the command + /// + Task SetFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always); + /// /// Appends the provided string to the string(s) at the provided path. /// diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 3581b731..73e2a945 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -409,6 +409,13 @@ public async Task SetAsync(RedisKey key, RedisValue path, RedisValue json, return true; } + /// // TODO: check way asnyc methods dont have documenation + public async Task SetFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always) + { + string fileContent = File.ReadAllText(filePath); + return await SetAsync(key, path, fileContent, when); + } + public async Task StrAppendAsync(RedisKey key, string value, string? path = null) { RedisResult result; diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 3bea17ac..d9a892e1 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -40,6 +40,7 @@ public void TestSetFile() commands.SetFile(keys[0], "$", file); var actual = commands.Get(keys[0]); + Assert.Equal(json, actual.ToString()); File.Delete(file); } @@ -850,4 +851,29 @@ public async Task MemoryAsync() res = await commands.DebugMemoryAsync("non-existent key"); Assert.Equal(0, res); } + + [Fact] + public async Task TestSetFileAsync() + { + //arrange + var conn = redisFixture.Redis; + var db = conn.GetDatabase(); + IJsonCommands commands = new JsonCommands(db); + var keys = CreateKeyNames(1); + + //creating json string: + var obj = new Person { Name = "Shachar", Age = 23 }; + string json = JsonSerializer.Serialize(obj); + + var file = "testFile.json"; + + //writing json to file: + File.WriteAllText(file, json); + + commands.SetFileAsync(keys[0], "$", file); + var actual = commands.Get(keys[0]); + + Assert.Equal(json, actual.ToString()); + File.Delete(file); + } } \ No newline at end of file From 3899e27690485aae6b241c84836122f8bff7561d Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 14 Nov 2022 10:38:23 +0200 Subject: [PATCH 5/9] Fix Documentation --- src/NRedisStack/Json/IJsonCommands.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index b351cfb6..f3245d23 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -206,7 +206,7 @@ public interface IJsonCommands bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When.Always); /// - /// Set's json file from to the provided file Path. + /// Set json file from the provided file Path. /// /// The key. /// The path to set within the key. @@ -463,7 +463,7 @@ public interface IJsonCommands Task SetAsync(RedisKey key, RedisValue path, RedisValue json, When when = When.Always); /// - /// Set's json file from to the provided file Path. + /// Set json file from the provided file Path. /// /// The key. /// The path to set within the key. From 78e611064084585645bb6cbcc05bbcba7e636bea Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 14 Nov 2022 17:25:49 +0200 Subject: [PATCH 6/9] Add SetFiles command --- src/NRedisStack/Json/IJsonCommands.cs | 20 ++++ src/NRedisStack/Json/JsonCommands.cs | 47 ++++++++++ tests/NRedisStack.Tests/Json/JsonTests.cs | 106 ++++++++++++++++++++++ 3 files changed, 173 insertions(+) diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index f3245d23..30f7694a 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -216,6 +216,16 @@ public interface IJsonCommands /// bool SetFile(RedisKey key, RedisValue path, string filePath, When when = When.Always); + /// + /// Set all json files in the provided file Path. + /// + /// The path to set within the file name as key. + /// The path of the file to set. + /// When to set the value. + /// The number of files that have been set + /// + int SetFiles(RedisValue path, string filesPath, When when = When.Always); + /// /// Appends the provided string to the string(s) at the provided path. /// @@ -473,6 +483,16 @@ public interface IJsonCommands /// Task SetFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always); + /// + /// Set all json files in the provided file Path. + /// + /// The path to set within the file name as key. + /// The path of the file to set. + /// When to set the value. + /// The number of files that have been set + /// + Task SetFilesAsync(RedisValue path, string filesPath, When when = When.Always); + /// /// Appends the provided string to the string(s) at the provided path. /// diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 73e2a945..0b697beb 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -67,6 +67,29 @@ public bool SetFile(RedisKey key, RedisValue path, string filePath, When when = return Set(key, path, fileContent, when); } + /// + public int SetFiles(RedisValue path, string filesPath, When when = When.Always) + { + int inserted = 0; + string key; + var files = Directory.EnumerateFiles(filesPath, "*.json"); + foreach (var filePath in files) + { + key = filePath.Substring(0, filePath.IndexOf(".")); + if(SetFile(key, path, filePath, when)) + { + inserted++; + } + } + + foreach (var dirPath in Directory.EnumerateDirectories(filesPath)) + { + inserted += SetFiles(path, dirPath, when); + } + + return inserted; + } + /// public long?[] StrAppend(RedisKey key, string value, string? path = null) { @@ -416,6 +439,30 @@ public async Task SetFileAsync(RedisKey key, RedisValue path, string fileP return await SetAsync(key, path, fileContent, when); } + /// + public async Task SetFilesAsync(RedisValue path, string filesPath, When when = When.Always) + { + int inserted = 0; + string key; + var files = Directory.EnumerateFiles(filesPath, "*.json"); + foreach (var filePath in files) + { + key = filePath.Substring(0, filePath.IndexOf(".")); + if(await SetFileAsync(key, path, filePath, when)) + { + inserted++; + } + } + + foreach (var dirPath in Directory.EnumerateDirectories(filesPath)) + { + inserted += await SetFilesAsync(path, dirPath, when); + } + + return inserted; + } + + public async Task StrAppendAsync(RedisKey key, string value, string? path = null) { RedisResult result; diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index d9a892e1..822633b1 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -45,6 +45,59 @@ public void TestSetFile() File.Delete(file); } + [Fact] + public void TestSetFiles() + { + //arrange + var conn = redisFixture.Redis; + var db = conn.GetDatabase(); + IJsonCommands commands = new JsonCommands(db); + + //creating json string: + object[] persons = new object[10]; + string[] jsons = new string[10]; + string[] notJsons = new string[10]; + for (int i = 1; i <= 10; i++) + { + persons[i - 1] = new Person { Name = $"Person{i}", Age = i * 10 }; + jsons[i - 1] = JsonSerializer.Serialize(persons[i - 1]); + notJsons[i - 1] = $"notjson{i}"; + } + + //creating directorys: + Directory.CreateDirectory(Path.Combine("BaseDir", "DirNumber2", "DirNumber3")); + + //creating files in directorys: + for (int i = 1; i <= 10; i++) + { + string jsonPath; + string notJsonPath; + if (i <= 3) + { + jsonPath = Path.Combine("BaseDir", $"jsonFile{i}.json"); + notJsonPath = Path.Combine("BaseDir", $"notJsonFile{i}.txt"); + } + else if (i <= 6) + { + jsonPath = Path.Combine("BaseDir", "DirNumber2", $"jsonFile{i}.json"); + notJsonPath = Path.Combine("BaseDir", "DirNumber2", $"notJsonFile{i}.txt"); + } + else + { + jsonPath = Path.Combine("BaseDir", "DirNumber2", "DirNumber3", $"jsonFile{i}.json"); + notJsonPath = Path.Combine("BaseDir", "DirNumber2", "DirNumber3", $"notJsonFile{i}.txt"); + } + File.WriteAllText(Path.GetFullPath(jsonPath), jsons[i - 1]); + File.WriteAllText(Path.GetFullPath(notJsonPath), notJsons[i - 1]); + } + + Assert.Equal(10, commands.SetFiles("$", "BaseDir")); + + var actual = commands.Get(Path.Combine("BaseDir", "DirNumber2", "DirNumber3", $"jsonFile7")); + Assert.Equal(jsons[6], actual.ToString()); + Directory.Delete("BaseDir", true); + } + [Fact] public void TestJsonSetNotExist() { @@ -876,4 +929,57 @@ public async Task TestSetFileAsync() Assert.Equal(json, actual.ToString()); File.Delete(file); } + + [Fact] + public async Task TestSetFilesAsync() + { + //arrange + var conn = redisFixture.Redis; + var db = conn.GetDatabase(); + IJsonCommands commands = new JsonCommands(db); + + //creating json string: + object[] persons = new object[10]; + string[] jsons = new string[10]; + string[] notJsons = new string[10]; + for (int i = 1; i <= 10; i++) + { + persons[i - 1] = new Person { Name = $"Person{i}", Age = i * 10 }; + jsons[i - 1] = JsonSerializer.Serialize(persons[i - 1]); + notJsons[i - 1] = $"notjson{i}"; + } + + //creating directorys: + Directory.CreateDirectory(Path.Combine("BaseDir", "DirNumber2", "DirNumber3")); + + //creating files in directorys: + for (int i = 1; i <= 10; i++) + { + string jsonPath; + string notJsonPath; + if (i <= 3) + { + jsonPath = Path.Combine("BaseDir", $"jsonFile{i}.json"); + notJsonPath = Path.Combine("BaseDir", $"notJsonFile{i}.txt"); + } + else if (i <= 6) + { + jsonPath = Path.Combine("BaseDir", "DirNumber2", $"jsonFile{i}.json"); + notJsonPath = Path.Combine("BaseDir", "DirNumber2", $"notJsonFile{i}.txt"); + } + else + { + jsonPath = Path.Combine("BaseDir", "DirNumber2", "DirNumber3", $"jsonFile{i}.json"); + notJsonPath = Path.Combine("BaseDir", "DirNumber2", "DirNumber3", $"notJsonFile{i}.txt"); + } + File.WriteAllText(Path.GetFullPath(jsonPath), jsons[i - 1]); + File.WriteAllText(Path.GetFullPath(notJsonPath), notJsons[i - 1]); + } + + Assert.Equal(10, await commands.SetFilesAsync("$", "BaseDir")); + + var actual = commands.Get(Path.Combine("BaseDir", "DirNumber2", "DirNumber3", $"jsonFile7")); + Assert.Equal(jsons[6], actual.ToString()); + Directory.Delete("BaseDir", true); + } } \ No newline at end of file From dcf68fdee2d6a869deace8168846c5146b08ac83 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 14 Nov 2022 17:38:06 +0200 Subject: [PATCH 7/9] Naming --- src/NRedisStack/Json/IJsonCommands.cs | 8 ++++---- src/NRedisStack/Json/JsonCommands.cs | 16 ++++++++-------- tests/NRedisStack.Tests/Json/JsonTests.cs | 16 ++++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index 30f7694a..80be63ef 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -214,7 +214,7 @@ public interface IJsonCommands /// When to set the value. /// The disposition of the command /// - bool SetFile(RedisKey key, RedisValue path, string filePath, When when = When.Always); + bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always); /// /// Set all json files in the provided file Path. @@ -224,7 +224,7 @@ public interface IJsonCommands /// When to set the value. /// The number of files that have been set /// - int SetFiles(RedisValue path, string filesPath, When when = When.Always); + int SetFromDirectory(RedisValue path, string filesPath, When when = When.Always); /// /// Appends the provided string to the string(s) at the provided path. @@ -481,7 +481,7 @@ public interface IJsonCommands /// When to set the value. /// The disposition of the command /// - Task SetFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always); + Task SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always); /// /// Set all json files in the provided file Path. @@ -491,7 +491,7 @@ public interface IJsonCommands /// When to set the value. /// The number of files that have been set /// - Task SetFilesAsync(RedisValue path, string filesPath, When when = When.Always); + Task SetFromDirectoryAsync(RedisValue path, string filesPath, When when = When.Always); /// /// Appends the provided string to the string(s) at the provided path. diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 0b697beb..19040ef9 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -61,14 +61,14 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When } /// - public bool SetFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) + public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) { string fileContent = File.ReadAllText(filePath); // TODO: check this return Set(key, path, fileContent, when); } /// - public int SetFiles(RedisValue path, string filesPath, When when = When.Always) + public int SetFromDirectory(RedisValue path, string filesPath, When when = When.Always) { int inserted = 0; string key; @@ -76,7 +76,7 @@ public int SetFiles(RedisValue path, string filesPath, When when = When.Always) foreach (var filePath in files) { key = filePath.Substring(0, filePath.IndexOf(".")); - if(SetFile(key, path, filePath, when)) + if(SetFromFile(key, path, filePath, when)) { inserted++; } @@ -84,7 +84,7 @@ public int SetFiles(RedisValue path, string filesPath, When when = When.Always) foreach (var dirPath in Directory.EnumerateDirectories(filesPath)) { - inserted += SetFiles(path, dirPath, when); + inserted += SetFromDirectory(path, dirPath, when); } return inserted; @@ -433,14 +433,14 @@ public async Task SetAsync(RedisKey key, RedisValue path, RedisValue json, } /// // TODO: check way asnyc methods dont have documenation - public async Task SetFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always) + public async Task SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always) { string fileContent = File.ReadAllText(filePath); return await SetAsync(key, path, fileContent, when); } /// - public async Task SetFilesAsync(RedisValue path, string filesPath, When when = When.Always) + public async Task SetFromDirectoryAsync(RedisValue path, string filesPath, When when = When.Always) { int inserted = 0; string key; @@ -448,7 +448,7 @@ public async Task SetFilesAsync(RedisValue path, string filesPath, When whe foreach (var filePath in files) { key = filePath.Substring(0, filePath.IndexOf(".")); - if(await SetFileAsync(key, path, filePath, when)) + if(await SetFromFileAsync(key, path, filePath, when)) { inserted++; } @@ -456,7 +456,7 @@ public async Task SetFilesAsync(RedisValue path, string filesPath, When whe foreach (var dirPath in Directory.EnumerateDirectories(filesPath)) { - inserted += await SetFilesAsync(path, dirPath, when); + inserted += await SetFromDirectoryAsync(path, dirPath, when); } return inserted; diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 822633b1..52d1c39c 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -21,7 +21,7 @@ public void Dispose() } [Fact] - public void TestSetFile() + public void TestSetFromFile() { //arrange var conn = redisFixture.Redis; @@ -38,7 +38,7 @@ public void TestSetFile() //writing json to file: File.WriteAllText(file, json); - commands.SetFile(keys[0], "$", file); + commands.SetFromFile(keys[0], "$", file); var actual = commands.Get(keys[0]); Assert.Equal(json, actual.ToString()); @@ -46,7 +46,7 @@ public void TestSetFile() } [Fact] - public void TestSetFiles() + public void TestSetFromDirectory() { //arrange var conn = redisFixture.Redis; @@ -91,7 +91,7 @@ public void TestSetFiles() File.WriteAllText(Path.GetFullPath(notJsonPath), notJsons[i - 1]); } - Assert.Equal(10, commands.SetFiles("$", "BaseDir")); + Assert.Equal(10, commands.SetFromDirectory("$", "BaseDir")); var actual = commands.Get(Path.Combine("BaseDir", "DirNumber2", "DirNumber3", $"jsonFile7")); Assert.Equal(jsons[6], actual.ToString()); @@ -906,7 +906,7 @@ public async Task MemoryAsync() } [Fact] - public async Task TestSetFileAsync() + public async Task TestSetFromFileAsync() { //arrange var conn = redisFixture.Redis; @@ -923,7 +923,7 @@ public async Task TestSetFileAsync() //writing json to file: File.WriteAllText(file, json); - commands.SetFileAsync(keys[0], "$", file); + commands.SetFromFileAsync(keys[0], "$", file); var actual = commands.Get(keys[0]); Assert.Equal(json, actual.ToString()); @@ -931,7 +931,7 @@ public async Task TestSetFileAsync() } [Fact] - public async Task TestSetFilesAsync() + public async Task TestSetFromDirectoryAsync() { //arrange var conn = redisFixture.Redis; @@ -976,7 +976,7 @@ public async Task TestSetFilesAsync() File.WriteAllText(Path.GetFullPath(notJsonPath), notJsons[i - 1]); } - Assert.Equal(10, await commands.SetFilesAsync("$", "BaseDir")); + Assert.Equal(10, await commands.SetFromDirectoryAsync("$", "BaseDir")); var actual = commands.Get(Path.Combine("BaseDir", "DirNumber2", "DirNumber3", $"jsonFile7")); Assert.Equal(jsons[6], actual.ToString()); From 291aede7faf58d4f175527a5023fe6b05d0da5c0 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Mon, 14 Nov 2022 17:59:13 +0200 Subject: [PATCH 8/9] Add TODO --- src/NRedisStack/Json/JsonCommands.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 19040ef9..d6980a10 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -63,7 +63,8 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When /// public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) { - string fileContent = File.ReadAllText(filePath); // TODO: check this + string fileContent = File.ReadAllText(filePath); + // TODO: Check if the content is valid JSON return Set(key, path, fileContent, when); } From 88fa8bd602d75e86cc2f86781e0593874e76e757 Mon Sep 17 00:00:00 2001 From: Shachar Pashchur Date: Wed, 16 Nov 2022 14:39:58 +0200 Subject: [PATCH 9/9] Add File Exsistence --- src/NRedisStack/Json/JsonCommands.cs | 11 ++++++++++- tests/NRedisStack.Tests/Json/JsonTests.cs | 10 ++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index d6980a10..35dd2dae 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -63,8 +63,12 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When /// public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always) { + if(!File.Exists(filePath)) + { + throw new FileNotFoundException($"File {filePath} not found."); + } + string fileContent = File.ReadAllText(filePath); - // TODO: Check if the content is valid JSON return Set(key, path, fileContent, when); } @@ -436,6 +440,11 @@ public async Task SetAsync(RedisKey key, RedisValue path, RedisValue json, /// // TODO: check way asnyc methods dont have documenation public async Task SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always) { + if (!File.Exists(filePath)) + { + throw new FileNotFoundException($"File {filePath} not found."); + } + string fileContent = File.ReadAllText(filePath); return await SetAsync(key, path, fileContent, when); } diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 52d1c39c..1396881c 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -38,11 +38,14 @@ public void TestSetFromFile() //writing json to file: File.WriteAllText(file, json); - commands.SetFromFile(keys[0], "$", file); + Assert.True(commands.SetFromFile(keys[0], "$", file)); var actual = commands.Get(keys[0]); Assert.Equal(json, actual.ToString()); File.Delete(file); + + //test not existing file: + Assert.Throws(() => commands.SetFromFile(keys[0], "$", "notExistingFile.json")); } [Fact] @@ -923,11 +926,14 @@ public async Task TestSetFromFileAsync() //writing json to file: File.WriteAllText(file, json); - commands.SetFromFileAsync(keys[0], "$", file); + Assert.True(await commands.SetFromFileAsync(keys[0], "$", file)); var actual = commands.Get(keys[0]); Assert.Equal(json, actual.ToString()); File.Delete(file); + + //test not existing file: + await Assert.ThrowsAsync(async () => await commands.SetFromFileAsync(keys[0], "$", "notExistingFile.json")); } [Fact]