SetPathValue needs to use ResolveMemoryScope() like GetPathValue does (#3075)

* SetPathValue needs to use ResolveMemoryScope() like GetPathValue does
Added unit tests

* clean up set path

* add same check for RemoveValue
This commit is contained in:
Tom Laird-McConnell 2019-12-04 16:10:30 -08:00 коммит произвёл GitHub
Родитель dd34f0a806
Коммит 49671f82e1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 94 добавлений и 2 удалений

Просмотреть файл

@ -289,7 +289,20 @@ namespace Microsoft.Bot.Builder.Dialogs.Memory
}
path = TransformPath(path ?? throw new ArgumentNullException(nameof(path)));
ObjectPath.SetPathValue(this, path, value);
var memoryScope = ResolveMemoryScope(path, out var remainingPath);
if (memoryScope == null)
{
throw new ArgumentException($"{path} does not resolve to a memory scope: {string.Join(",", this.Configuration.MemoryScopes.Select(ms => ms.Name))}");
}
if (string.IsNullOrEmpty(remainingPath))
{
memoryScope.SetMemory(this.dialogContext, value);
}
else
{
ObjectPath.SetPathValue(this, path, value);
}
// Every set will increase version
version++;
@ -302,7 +315,21 @@ namespace Microsoft.Bot.Builder.Dialogs.Memory
public void RemoveValue(string path)
{
path = TransformPath(path ?? throw new ArgumentNullException(nameof(path)));
ObjectPath.RemovePathValue(this, path);
//var memoryScope = ResolveMemoryScope(path, out var remainingPath);
//if (memoryScope == null)
//{
// throw new ArgumentException($"{path} does not resolve to a memory scope: {string.Join(",", this.Configuration.MemoryScopes.Select(ms => ms.Name))}");
//}
//if (string.IsNullOrEmpty(remainingPath))
//{
// throw new ArgumentException("You cannot remove a root memory scope.");
//}
//else
{
ObjectPath.RemovePathValue(this, path);
}
}
/// <summary>

Просмотреть файл

@ -283,6 +283,71 @@ namespace Microsoft.Bot.Builder.Dialogs.Tests
}).StartTestAsync();
}
[TestMethod]
public async Task TestSetValue_RootScope()
{
await CreateDialogContext(async (dc, ct) =>
{
try
{
dc.GetState().SetValue(null, 13);
Assert.Fail("Should have thrown with null memory scope");
}
catch (ArgumentNullException err)
{
Assert.IsTrue(err.Message.Contains("path"));
}
try
{
// complex type paths
dc.GetState().SetValue("xxx", 13);
Assert.Fail("Should have thrown with unknown memory scope");
}
catch (ArgumentOutOfRangeException err)
{
Assert.IsTrue(err.Message.Contains("does not match memory scope"));
}
}).StartTestAsync();
}
[TestMethod]
public async Task TestRemoveValue_RootScope()
{
await CreateDialogContext(async (dc, ct) =>
{
try
{
dc.GetState().RemoveValue(null);
Assert.Fail("Should have thrown with null memory scope");
}
catch (ArgumentNullException err)
{
Assert.IsTrue(err.Message.Contains("path"));
}
try
{
dc.GetState().RemoveValue("user");
Assert.Fail("Should have thrown with known root memory scope");
}
catch (ArgumentNullException err)
{
Assert.IsTrue(err.Message.Contains("cannot be null"));
}
try
{
dc.GetState().RemoveValue("xxx");
Assert.Fail("Should have thrown with unknown memory scope");
}
catch (ArgumentOutOfRangeException err)
{
Assert.IsTrue(err.Message.Contains("does not match memory scope"));
}
}).StartTestAsync();
}
[TestMethod]
public async Task TestHashResolver()
{