Skip to content

Commit

Permalink
TestKit backend: except txMeta as Cypher types
Browse files Browse the repository at this point in the history
  • Loading branch information
robsdedude committed Dec 7, 2022
1 parent 723273d commit 6205336
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public override T ReadJson(JsonReader reader, Type objectType,
protected void SetBaseValues(JObject jsonObject, T baseSession)
{
baseSession.sessionId = jsonObject["sessionId"]?.Value<string>();
baseSession.txMeta = jsonObject["txMeta"]?.ToObject<Dictionary<string, object>>()
?? new Dictionary<string, object>();
baseSession.txMeta = JsonCypherParameterParser.ParseParameters(jsonObject["txMeta"])
?? new Dictionary<string, CypherToNativeObject>();
baseSession.timeout = jsonObject["timeout"]?.Value<int?>();
baseSession.TimeoutSet = jsonObject.ContainsKey("timeout");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Neo4j.Driver.Tests.TestBackend
Expand All @@ -8,12 +9,43 @@ internal abstract class BaseSessionType
public string sessionId { get; set; }

[JsonProperty(Required = Required.AllowNull)]
public Dictionary<string, object> txMeta { get; set; } = new Dictionary<string, object>();
[JsonConverter(typeof(QueryParameterConverter))]
public Dictionary<string, CypherToNativeObject> txMeta { get; set; } = new Dictionary<string, CypherToNativeObject>();

[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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ await controller.Process(false, e =>
}
});

}, TransactionConfig);
}, data.TransactionConfig);
}

public override string Respond()
Expand All @@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,11 @@ public class SessionRunType : BaseSessionType
public Dictionary<string, CypherToNativeObject> parameters { get; set; } = new Dictionary<string, CypherToNativeObject>();
}

private Dictionary<string, object> ConvertParameters(Dictionary<string, CypherToNativeObject> source)
{
if (data.parameters == null)
return null;

Dictionary<string, object> newParams = new Dictionary<string, object>();

foreach(KeyValuePair<string, CypherToNativeObject> 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>();
result.ResultCursor = cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ await controller.Process(false, e =>
}
});

}, TransactionConfig);
}, data.TransactionConfig);
}

public override string Respond()
Expand All @@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,14 @@ public class TransactionRunType
public Dictionary<string, CypherToNativeObject> parameters { get; set; } = new Dictionary<string, CypherToNativeObject>();
}

private Dictionary<string, object> ConvertParameters(Dictionary<string, CypherToNativeObject> source)
{
if (data.parameters == null)
return null;

Dictionary<string, object> newParams = new Dictionary<string, object>();

foreach(KeyValuePair<string, CypherToNativeObject> element in source)
{
newParams.Add(element.Key, CypherToNative.Convert(element.Value));
}

return newParams;
}

public override async Task Process(Controller controller)
{
try
{
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ internal class CypherToNativeObject
{
public string name { get; set; }
public object data { get; set; }

public static Dictionary<string, object> ConvertDitctionaryToNative(Dictionary<string, CypherToNativeObject> source)
{
if (source == null)
return null;

Dictionary<string, object> newDict = new Dictionary<string, object>();

foreach(KeyValuePair<string, CypherToNativeObject> element in source)
{
newDict.Add(element.Key, CypherToNative.Convert(element.Value));
}

return newDict;
}
}

internal class SimpleValue
Expand Down

0 comments on commit 6205336

Please sign in to comment.