Skip to content

Commit 6906193

Browse files
authored
TestKit backend: except txMeta as Cypher types (#662)
1 parent d7deb79 commit 6906193

File tree

8 files changed

+70
-123
lines changed

8 files changed

+70
-123
lines changed

Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/JsonConverters/BaseSessionTypeJsonConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public override T ReadJson(JsonReader reader, Type objectType,
2222
protected void SetBaseValues(JObject jsonObject, T baseSession)
2323
{
2424
baseSession.sessionId = jsonObject["sessionId"]?.Value<string>();
25-
baseSession.txMeta = jsonObject["txMeta"]?.ToObject<Dictionary<string, object>>()
26-
?? new Dictionary<string, object>();
25+
baseSession.txMeta = JsonCypherParameterParser.ParseParameters(jsonObject["txMeta"]) ??
26+
new Dictionary<string, CypherToNativeObject>();
2727
baseSession.timeout = jsonObject["timeout"]?.Value<int?>();
2828
baseSession.TimeoutSet = jsonObject.ContainsKey("timeout");
2929
}
3030
}
31-
}
31+
}
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using Newtonsoft.Json;
34

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

1011
[JsonProperty(Required = Required.AllowNull)]
11-
public Dictionary<string, object> txMeta { get; set; } = new Dictionary<string, object>();
12+
[JsonConverter(typeof(QueryParameterConverter))]
13+
public Dictionary<string, CypherToNativeObject> txMeta { get; set; } = new();
1214

1315
[JsonProperty(Required = Required.AllowNull)]
1416
public int? timeout { get; set; }
1517

1618
[JsonIgnore]
1719
public bool TimeoutSet { get; set; }
20+
21+
public TransactionConfigBuilder ConfigureTxTimeout(TransactionConfigBuilder configBuilder)
22+
{
23+
try
24+
{
25+
if (TimeoutSet)
26+
{
27+
var timeout = this.timeout.HasValue
28+
? TimeSpan.FromMilliseconds(this.timeout.Value)
29+
: default(TimeSpan?);
30+
31+
configBuilder.WithTimeout(timeout);
32+
}
33+
}
34+
catch (ArgumentOutOfRangeException e) when ((timeout ?? 0) < 0 && e.ParamName == "value")
35+
{
36+
throw new DriverExceptionWrapper(e);
37+
}
38+
39+
return configBuilder;
40+
}
41+
42+
public TransactionConfigBuilder ConfigureTxMetadata(TransactionConfigBuilder configBuilder)
43+
{
44+
if (txMeta.Count > 0) configBuilder.WithMetadata(CypherToNativeObject.ConvertDictionaryToNative(txMeta));
45+
return configBuilder;
46+
}
47+
48+
public void TransactionConfig(TransactionConfigBuilder configBuilder)
49+
{
50+
ConfigureTxMetadata(ConfigureTxTimeout(configBuilder));
51+
}
1852
}
19-
}
53+
}

Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionBeginTransaction.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,10 @@ public class SessionBeginTransactionType : BaseSessionType
1616
{
1717
}
1818

19-
void TransactionConfig(TransactionConfigBuilder configBuilder)
20-
{
21-
try
22-
{
23-
if (data.TimeoutSet)
24-
{
25-
var timeout = data.timeout.HasValue
26-
? TimeSpan.FromMilliseconds(data.timeout.Value)
27-
: default(TimeSpan?);
28-
configBuilder.WithTimeout(timeout);
29-
}
30-
}
31-
catch (ArgumentOutOfRangeException e) when ((data.timeout ?? 0) < 0 && e.ParamName == "value")
32-
{
33-
throw new DriverExceptionWrapper(e);
34-
}
35-
36-
if (data.txMeta.Count > 0) configBuilder.WithMetadata(data.txMeta);
37-
}
38-
3919
public override async Task Process(Controller controller)
4020
{
4121
var sessionContainer = (NewSession)ObjManager.GetObject(data.sessionId);
42-
var transaction = await sessionContainer.Session.BeginTransactionAsync(TransactionConfig);
22+
var transaction = await sessionContainer.Session.BeginTransactionAsync(data.TransactionConfig);
4323
TransactionId = controller.TransactionManager.AddTransaction(new TransactionWrapper(transaction, async cursor =>
4424
{
4525
var result = ProtocolObjectFactory.CreateObject<Result>();

Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionReadTransaction.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ await controller.Process(false, e =>
5454
}
5555
});
5656

57-
}, TransactionConfig);
57+
}, data.TransactionConfig);
5858
}
5959

6060
public override string Respond()
@@ -77,25 +77,5 @@ public override string Respond()
7777

7878
return new ProtocolResponse("RetryableDone", new { }).Encode();
7979
}
80-
81-
void TransactionConfig(TransactionConfigBuilder configBuilder)
82-
{
83-
if (data.txMeta.Count > 0) configBuilder.WithMetadata(data.txMeta);
84-
85-
try
86-
{
87-
if (data.TimeoutSet)
88-
{
89-
var timeout = data.timeout.HasValue
90-
? TimeSpan.FromMilliseconds(data.timeout.Value)
91-
: default(TimeSpan?);
92-
configBuilder.WithTimeout(timeout);
93-
}
94-
}
95-
catch (ArgumentOutOfRangeException e) when ((data.timeout ?? 0) < 0 && e.ParamName == "value")
96-
{
97-
throw new DriverExceptionWrapper(e);
98-
}
99-
}
10080
}
10181
}

Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionRun.cs

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,15 @@ public class SessionRunType : BaseSessionType
2323
public Dictionary<string, CypherToNativeObject> parameters { get; set; } = new Dictionary<string, CypherToNativeObject>();
2424
}
2525

26-
private Dictionary<string, object> ConvertParameters(Dictionary<string, CypherToNativeObject> source)
27-
{
28-
if (data.parameters == null)
29-
return null;
30-
31-
Dictionary<string, object> newParams = new Dictionary<string, object>();
32-
33-
foreach(KeyValuePair<string, CypherToNativeObject> element in source)
34-
{
35-
newParams.Add(element.Key, CypherToNative.Convert(element.Value));
36-
}
37-
38-
return newParams;
39-
}
40-
41-
void TransactionConfig(TransactionConfigBuilder configBuilder)
42-
{
43-
try
44-
{
45-
if (data.TimeoutSet)
46-
{
47-
var timeout = data.timeout.HasValue
48-
? TimeSpan.FromMilliseconds(data.timeout.Value)
49-
: default(TimeSpan?);
50-
configBuilder.WithTimeout(timeout);
51-
}
52-
}
53-
catch (ArgumentOutOfRangeException e) when ((data.timeout ?? 0) < 0 && e.ParamName == "value")
54-
{
55-
throw new DriverExceptionWrapper(e);
56-
}
57-
58-
if (data.txMeta.Count > 0)
59-
configBuilder.WithMetadata(data.txMeta);
60-
}
61-
6226
public override async Task Process()
6327
{
6428
var newSession = (NewSession)ObjManager.GetObject(data.sessionId);
65-
IResultCursor cursor = await newSession.Session.RunAsync(data.cypher, ConvertParameters(data.parameters), TransactionConfig).ConfigureAwait(false);
29+
var cursor = await newSession.Session
30+
.RunAsync(
31+
data.cypher,
32+
CypherToNativeObject.ConvertDictionaryToNative(data.parameters),
33+
data.TransactionConfig)
34+
.ConfigureAwait(false);
6635

6736
var result = ProtocolObjectFactory.CreateObject<Result>();
6837
result.ResultCursor = cursor;

Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Session/SessionWriteTransaction.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ await controller.Process(false, e =>
5252
}
5353
});
5454

55-
}, TransactionConfig);
55+
}, data.TransactionConfig);
5656
}
5757

5858
public override string Respond()
@@ -75,25 +75,5 @@ public override string Respond()
7575

7676
return new ProtocolResponse("RetryableDone", new { }).Encode();
7777
}
78-
79-
void TransactionConfig(TransactionConfigBuilder configBuilder)
80-
{
81-
if (data.txMeta.Count > 0) configBuilder.WithMetadata(data.txMeta);
82-
83-
try
84-
{
85-
if (data.TimeoutSet)
86-
{
87-
var timeout = data.timeout.HasValue
88-
? TimeSpan.FromMilliseconds(data.timeout.Value)
89-
: default(TimeSpan?);
90-
configBuilder.WithTimeout(timeout);
91-
}
92-
}
93-
catch (ArgumentOutOfRangeException e) when ((data.timeout ?? 0) < 0 && e.ParamName == "value")
94-
{
95-
throw new DriverExceptionWrapper(e);
96-
}
97-
}
9878
}
9979
}

Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Protocol/Transaction/TransactionRun.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,15 @@ public class TransactionRunType
2121
public Dictionary<string, CypherToNativeObject> parameters { get; set; } = new Dictionary<string, CypherToNativeObject>();
2222
}
2323

24-
private Dictionary<string, object> ConvertParameters(Dictionary<string, CypherToNativeObject> source)
25-
{
26-
if (data.parameters == null)
27-
return null;
28-
29-
Dictionary<string, object> newParams = new Dictionary<string, object>();
30-
31-
foreach(KeyValuePair<string, CypherToNativeObject> element in source)
32-
{
33-
newParams.Add(element.Key, CypherToNative.Convert(element.Value));
34-
}
35-
36-
return newParams;
37-
}
38-
3924
public override async Task Process(Controller controller)
4025
{
4126
try
4227
{
4328
var transactionWrapper = controller.TransactionManager.FindTransaction(data.txId);
4429

45-
IResultCursor cursor = await transactionWrapper.Transaction
46-
.RunAsync(data.cypher, ConvertParameters(data.parameters)).ConfigureAwait(false);
30+
var cursor = await transactionWrapper.Transaction
31+
.RunAsync(data.cypher, CypherToNativeObject.ConvertDictionaryToNative(data.parameters))
32+
.ConfigureAwait(false);
4733

4834
ResultId = await transactionWrapper.ProcessResults(cursor);
4935

Neo4j.Driver/Neo4j.Driver.Tests.TestBackend/Types/CypherToNative.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ internal class CypherToNativeObject
1010
{
1111
public string name { get; set; }
1212
public object data { get; set; }
13+
14+
public static Dictionary<string, object> ConvertDictionaryToNative(
15+
Dictionary<string, CypherToNativeObject> source)
16+
{
17+
if (source == null)
18+
{
19+
return null;
20+
}
21+
22+
var newDict = new Dictionary<string, object>();
23+
24+
foreach (var element in source)
25+
{
26+
newDict.Add(element.Key, CypherToNative.Convert(element.Value));
27+
}
28+
29+
return newDict;
30+
}
1331
}
1432

1533
internal class SimpleValue

0 commit comments

Comments
 (0)