From 62053366391f23c191411c90d7100459ab8cd366 Mon Sep 17 00:00:00 2001 From: Robsdedude Date: Wed, 7 Dec 2022 16:31:15 +0100 Subject: [PATCH] TestKit backend: except txMeta as Cypher types Depends on: * https://github.com/neo4j-drivers/testkit/pull/538 --- .../BaseSessionTypeJsonConverter.cs | 4 +- .../Protocol/Session/BaseSessionType.cs | 36 ++++++++++++++++- .../Session/SessionBeginTransaction.cs | 22 +---------- .../Session/SessionReadTransaction.cs | 22 +---------- .../Protocol/Session/SessionRun.cs | 39 +------------------ .../Session/SessionWriteTransaction.cs | 22 +---------- .../Protocol/Transaction/TransactionRun.cs | 17 +------- .../Types/CypherToNative.cs | 15 +++++++ 8 files changed, 57 insertions(+), 120 deletions(-) diff --git a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/JsonConverters/BaseSessionTypeJsonConverter.cs b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/JsonConverters/BaseSessionTypeJsonConverter.cs index f9bf87e99..3cc96ab5f 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/JsonConverters/BaseSessionTypeJsonConverter.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/JsonConverters/BaseSessionTypeJsonConverter.cs @@ -22,8 +22,8 @@ public override T ReadJson(JsonReader reader, Type objectType, protected void SetBaseValues(JObject jsonObject, T baseSession) { baseSession.sessionId = jsonObject["sessionId"]?.Value(); - baseSession.txMeta = jsonObject["txMeta"]?.ToObject>() - ?? new Dictionary(); + baseSession.txMeta = JsonCypherParameterParser.ParseParameters(jsonObject["txMeta"]) + ?? new Dictionary(); baseSession.timeout = jsonObject["timeout"]?.Value(); baseSession.TimeoutSet = jsonObject.ContainsKey("timeout"); } diff --git a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/BaseSessionType.cs b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/BaseSessionType.cs index 450093277..2791eaec5 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/BaseSessionType.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/BaseSessionType.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Newtonsoft.Json; namespace Neo4j.Driver.Tests.TestBackend @@ -8,12 +9,43 @@ internal abstract class BaseSessionType public string sessionId { get; set; } [JsonProperty(Required = Required.AllowNull)] - public Dictionary txMeta { get; set; } = new Dictionary(); + [JsonConverter(typeof(QueryParameterConverter))] + public Dictionary txMeta { get; set; } = new Dictionary(); [JsonProperty(Required = Required.AllowNull)] public int? timeout { get; set; } [JsonIgnore] public bool TimeoutSet { get; set; } + + public TransactionConfigBuilder ConfigureTxTimeout(TransactionConfigBuilder configBuilder) + { + try + { + if (TimeoutSet) + { + var timeout = this.timeout.HasValue + ? TimeSpan.FromMilliseconds(this.timeout.Value) + : default(TimeSpan?); + configBuilder.WithTimeout(timeout); + } + } + catch (ArgumentOutOfRangeException e) when ((timeout ?? 0) < 0 && e.ParamName == "value") + { + throw new DriverExceptionWrapper(e); + } + return configBuilder; + } + + public TransactionConfigBuilder ConfigureTxMetadata(TransactionConfigBuilder configBuilder) + { + if (txMeta.Count > 0) configBuilder.WithMetadata(CypherToNativeObject.ConvertDitctionaryToNative(txMeta)); + return configBuilder; + } + + public void TransactionConfig(TransactionConfigBuilder configBuilder) + { + ConfigureTxMetadata(ConfigureTxTimeout(configBuilder)); + } } } \ No newline at end of file diff --git a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionBeginTransaction.cs b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionBeginTransaction.cs index c0cf65852..073b4a732 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionBeginTransaction.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionBeginTransaction.cs @@ -16,30 +16,10 @@ public class SessionBeginTransactionType : BaseSessionType { } - void TransactionConfig(TransactionConfigBuilder configBuilder) - { - try - { - if (data.TimeoutSet) - { - var timeout = data.timeout.HasValue - ? TimeSpan.FromMilliseconds(data.timeout.Value) - : default(TimeSpan?); - configBuilder.WithTimeout(timeout); - } - } - catch (ArgumentOutOfRangeException e) when ((data.timeout ?? 0) < 0 && e.ParamName == "value") - { - throw new DriverExceptionWrapper(e); - } - - if (data.txMeta.Count > 0) configBuilder.WithMetadata(data.txMeta); - } - public override async Task Process(Controller controller) { var sessionContainer = (NewSession)ObjManager.GetObject(data.sessionId); - var transaction = await sessionContainer.Session.BeginTransactionAsync(TransactionConfig); + var transaction = await sessionContainer.Session.BeginTransactionAsync(data.TransactionConfig); TransactionId = controller.TransactionManager.AddTransaction(new TransactionWrapper(transaction, async cursor => { var result = ProtocolObjectFactory.CreateObject(); diff --git a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionReadTransaction.cs b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionReadTransaction.cs index 41261d0ab..a716754df 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionReadTransaction.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionReadTransaction.cs @@ -54,7 +54,7 @@ await controller.Process(false, e => } }); - }, TransactionConfig); + }, data.TransactionConfig); } public override string Respond() @@ -77,25 +77,5 @@ public override string Respond() return new ProtocolResponse("RetryableDone", new { }).Encode(); } - - void TransactionConfig(TransactionConfigBuilder configBuilder) - { - if (data.txMeta.Count > 0) configBuilder.WithMetadata(data.txMeta); - - try - { - if (data.TimeoutSet) - { - var timeout = data.timeout.HasValue - ? TimeSpan.FromMilliseconds(data.timeout.Value) - : default(TimeSpan?); - configBuilder.WithTimeout(timeout); - } - } - catch (ArgumentOutOfRangeException e) when ((data.timeout ?? 0) < 0 && e.ParamName == "value") - { - throw new DriverExceptionWrapper(e); - } - } } } diff --git a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionRun.cs b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionRun.cs index cf0a3cd0d..446908738 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionRun.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionRun.cs @@ -23,46 +23,11 @@ public class SessionRunType : BaseSessionType public Dictionary parameters { get; set; } = new Dictionary(); } - private Dictionary ConvertParameters(Dictionary source) - { - if (data.parameters == null) - return null; - - Dictionary newParams = new Dictionary(); - - foreach(KeyValuePair element in source) - { - newParams.Add(element.Key, CypherToNative.Convert(element.Value)); - } - - return newParams; - } - - void TransactionConfig(TransactionConfigBuilder configBuilder) - { - try - { - if (data.TimeoutSet) - { - var timeout = data.timeout.HasValue - ? TimeSpan.FromMilliseconds(data.timeout.Value) - : default(TimeSpan?); - configBuilder.WithTimeout(timeout); - } - } - catch (ArgumentOutOfRangeException e) when ((data.timeout ?? 0) < 0 && e.ParamName == "value") - { - throw new DriverExceptionWrapper(e); - } - - if (data.txMeta.Count > 0) - configBuilder.WithMetadata(data.txMeta); - } - public override async Task Process() { var newSession = (NewSession)ObjManager.GetObject(data.sessionId); - IResultCursor cursor = await newSession.Session.RunAsync(data.cypher, ConvertParameters(data.parameters), TransactionConfig).ConfigureAwait(false); + IResultCursor cursor = await newSession.Session + .RunAsync(data.cypher, CypherToNativeObject.ConvertDitctionaryToNative(data.parameters), data.TransactionConfig).ConfigureAwait(false); var result = ProtocolObjectFactory.CreateObject(); result.ResultCursor = cursor; diff --git a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionWriteTransaction.cs b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionWriteTransaction.cs index b00ee0f39..2afa569f3 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionWriteTransaction.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionWriteTransaction.cs @@ -52,7 +52,7 @@ await controller.Process(false, e => } }); - }, TransactionConfig); + }, data.TransactionConfig); } public override string Respond() @@ -75,25 +75,5 @@ public override string Respond() return new ProtocolResponse("RetryableDone", new { }).Encode(); } - - void TransactionConfig(TransactionConfigBuilder configBuilder) - { - if (data.txMeta.Count > 0) configBuilder.WithMetadata(data.txMeta); - - try - { - if (data.TimeoutSet) - { - var timeout = data.timeout.HasValue - ? TimeSpan.FromMilliseconds(data.timeout.Value) - : default(TimeSpan?); - configBuilder.WithTimeout(timeout); - } - } - catch (ArgumentOutOfRangeException e) when ((data.timeout ?? 0) < 0 && e.ParamName == "value") - { - throw new DriverExceptionWrapper(e); - } - } } } diff --git a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Transaction/TransactionRun.cs b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Transaction/TransactionRun.cs index 8ab67247d..c2d1920b1 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Transaction/TransactionRun.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Transaction/TransactionRun.cs @@ -21,21 +21,6 @@ public class TransactionRunType public Dictionary parameters { get; set; } = new Dictionary(); } - private Dictionary ConvertParameters(Dictionary source) - { - if (data.parameters == null) - return null; - - Dictionary newParams = new Dictionary(); - - foreach(KeyValuePair element in source) - { - newParams.Add(element.Key, CypherToNative.Convert(element.Value)); - } - - return newParams; - } - public override async Task Process(Controller controller) { try @@ -43,7 +28,7 @@ public override async Task Process(Controller controller) var transactionWrapper = controller.TransactionManager.FindTransaction(data.txId); IResultCursor cursor = await transactionWrapper.Transaction - .RunAsync(data.cypher, ConvertParameters(data.parameters)).ConfigureAwait(false); + .RunAsync(data.cypher, CypherToNativeObject.ConvertDitctionaryToNative(data.parameters)).ConfigureAwait(false); ResultId = await transactionWrapper.ProcessResults(cursor); diff --git a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Types/CypherToNative.cs b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Types/CypherToNative.cs index e4568a8b6..2e959cb48 100644 --- a/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Types/CypherToNative.cs +++ b/Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Types/CypherToNative.cs @@ -10,6 +10,21 @@ internal class CypherToNativeObject { public string name { get; set; } public object data { get; set; } + + public static Dictionary ConvertDitctionaryToNative(Dictionary source) + { + if (source == null) + return null; + + Dictionary newDict = new Dictionary(); + + foreach(KeyValuePair element in source) + { + newDict.Add(element.Key, CypherToNative.Convert(element.Value)); + } + + return newDict; + } } internal class SimpleValue