This repository was archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCypherSessionTransactionTests.cs
102 lines (85 loc) · 4.21 KB
/
CypherSessionTransactionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Transactions;
using CypherNet.Configuration;
using CypherNet.Dynamic;
using CypherNet.Graph;
using CypherNet.Http;
using CypherNet.Transaction;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace CypherNet.UnitTests
{
[TestClass]
public class CypherSessionTransactionTests
{
private const string BaseUri = "http://localhost:7474/db/data/";
private const string AutoCommitAddress = "http://localhost:7474/db/data/transaction/commit";
private const string BeginTransactionUri = "http://localhost:7474/db/data/transaction/";
private const string KeepAliveAddress = "http://localhost:7474/db/data/transaction/1";
private const string CommitAddress = "http://localhost:7474/db/data/transaction/1/commit";
private const string EmptyRequest = @"{""statements"":[]}";
private const string EmptyResponse = @"{""results"":[],""errors"":[]}";
private const string Name = "Marcus.T.Peeps";
private static readonly object Node = new {name = Name};
[TestMethod]
public void CreateNode__CreatesNode()
{
var mock = InitializeMockWebClient(AutoCommitAddress);
var session = new CypherSession(new ConnectionProperties(BaseUri), mock.Object);
var node = session.CreateNode(new {name = Name}, "person");
Assert.AreEqual(node.AsDynamic().name, Name);
Assert.AreEqual((node).Labels.First(), "person");
}
[TestMethod]
public void CreateNode_WithinCommitedTransaction_CallsCommit()
{
var mock = InitializeMockWebClient(BeginTransactionUri);
//Keep alive.
mock.Setup(m => m.PostAsync(KeepAliveAddress, EmptyRequest))
.Returns(() => BuildResponse(@"{""commit"":""" + CommitAddress + @""",""results"":[],""transaction"":{""expires"":""Wed, 02 Oct 2013 15:18:27 +0000""},""errors"":[]}"));
//Commit
mock.Setup(m => m.PostAsync(CommitAddress, EmptyRequest)).Returns(() => BuildResponse(EmptyResponse));
var session = new CypherSession(new ConnectionProperties(BaseUri), mock.Object);
using (var ts = new TransactionScope())
{
session.CreateNode(new {name = Name}, "person");
ts.Complete();
}
mock.Verify(m => m.PostAsync(KeepAliveAddress, EmptyRequest));
mock.Verify(m => m.PostAsync(CommitAddress, EmptyRequest));
}
[TestMethod]
public void CreateNode_WithinRollbackTransaction_CallsRollback()
{
var mock = InitializeMockWebClient(BeginTransactionUri);
//Rollback
mock.Setup(m => m.DeleteAsync(KeepAliveAddress)).Returns(() => BuildResponse(EmptyResponse));
var session = new CypherSession(new ConnectionProperties(BaseUri), mock.Object);
using (new TransactionScope())
{
session.CreateNode(new { name = Name }, "person");
}
mock.Verify(m => m.DeleteAsync(KeepAliveAddress));
}
private Mock<IWebClient> InitializeMockWebClient(string uri)
{
var mock = new Mock<IWebClient>();
mock.Setup(
m =>
m.PostAsync(uri,
@"{""statements"":[{""statement"":""CREATE (NewNode:person {param_0}) RETURN NewNode as NewNode, id(NewNode) as NewNode__Id, labels(NewNode) as NewNode__Labels;"",""parameters"":{""param_0"":{""name"":""" +
Name + @"""}}}]}"))
.Returns(
() =>
BuildResponse(@"{""commit"":""" + CommitAddress +
@""",""results"":[{""columns"":[""NewNode"",""NewNode__Id"",""NewNode__Labels""],""data"":[{""row"": [{""name"":""" + Name + @"""},15026,[""person""]]}]}],""errors"":[]}"));
return mock;
}
private Task<IHttpResponseMessage> BuildResponse(string response)
{
return Task.FromResult((IHttpResponseMessage) new MockHttpResponseMessage(response, HttpStatusCode.OK));
}
}
}