Skip to content

Commit

Permalink
Merge pull request #149 from albumprinter/issue-141
Browse files Browse the repository at this point in the history
issue-141 Adding possibility to add customheaders to a single cypher …
  • Loading branch information
cskardon committed Jan 22, 2016
2 parents f9cfda6 + c41acb3 commit 59600ca
Show file tree
Hide file tree
Showing 29 changed files with 595 additions and 75 deletions.
64 changes: 64 additions & 0 deletions Neo4jClient.Tests/Cypher/CypherFluentQueryCustomHeaderTests.cs
@@ -0,0 +1,64 @@
using System.Collections.Specialized;
using Neo4jClient.Cypher;
using NSubstitute;
using NUnit.Framework;

namespace Neo4jClient.Test.Cypher
{
[TestFixture]
public class CypherFluentQueryCustomHeaderTests
{
[Test]
public void SetsMaxExecutionTimeAndCustomHeader_WhenUsingAReturnTypeQuery()
{
const string headerName = "HeaderName";
const string headerValue = "TestHeaderValue";
var client = Substitute.For<IRawGraphClient>();
var customHeaders = new NameValueCollection {{headerName, headerValue}};

var query = new CypherFluentQuery(client)
.MaxExecutionTime(100)
.CustomHeaders(customHeaders)
.Match("n")
.Return<object>("n")
.Query;

Assert.AreEqual(100, query.MaxExecutionTime);
Assert.AreEqual(customHeaders, query.CustomHeaders);
}

[Test]
public void SetsCustomHeader_WhenUsingAReturnTypeQuery()
{
const string headerName = "HeaderName";
const string headerValue = "TestHeaderValue";
var client = Substitute.For<IRawGraphClient>();
var customHeaders = new NameValueCollection { { headerName, headerValue } };

var query = new CypherFluentQuery(client)
.CustomHeaders(customHeaders)
.Match("n")
.Return<object>("n")
.Query;

Assert.AreEqual(customHeaders, query.CustomHeaders);
}

[Test]
public void SetsCustomHeader_WhenUsingANonReturnTypeQuery()
{
const string headerName = "HeaderName";
const string headerValue = "TestHeaderValue";
var customHeaders = new NameValueCollection { { headerName, headerValue } };

var client = Substitute.For<IRawGraphClient>();
var query = new CypherFluentQuery(client)
.CustomHeaders(customHeaders)
.Match("n")
.Set("n.Value = 'value'")
.Query;

Assert.AreEqual(customHeaders, query.CustomHeaders);
}
}
}
37 changes: 37 additions & 0 deletions Neo4jClient.Tests/Cypher/CypherFluentQueryMaxExecutionTimeTests.cs
@@ -0,0 +1,37 @@
using NSubstitute;
using NUnit.Framework;
using Neo4jClient.Cypher;

namespace Neo4jClient.Test.Cypher
{
[TestFixture]
public class CypherFluentQueryMaxExecutionTimeTests
{
[Test]
public void SetsMaxExecutionTime_WhenUsingAReturnTypeQuery()
{
var client = Substitute.For<IRawGraphClient>();
var query = new CypherFluentQuery(client)
.MaxExecutionTime(100)
.Match("n")
.Return<object>("n")
.Query;

Assert.AreEqual(100, query.MaxExecutionTime);
}

[Test]
public void SetsMaxExecutionTime_WhenUsingANonReturnTypeQuery()
{
var client = Substitute.For<IRawGraphClient>();
var query = new CypherFluentQuery(client)
.MaxExecutionTime(100)
.Match("n")
.Set("n.Value = 'value'")
.Query;

Assert.AreEqual(100, query.MaxExecutionTime);
}

}
}
3 changes: 1 addition & 2 deletions Neo4jClient.Tests/GraphClientTests/ConnectTests.cs
Expand Up @@ -37,7 +37,6 @@ public void ShouldRetrieveApiEndpoints()
using (var testHarness = new RestTestHarness())
{
var graphClient = (GraphClient)testHarness.CreateAndConnectGraphClient();

Assert.AreEqual("/node", graphClient.RootApiResponse.Node);
Assert.AreEqual("/index/node", graphClient.RootApiResponse.NodeIndex);
Assert.AreEqual("/index/relationship", graphClient.RootApiResponse.RelationshipIndex);
Expand Down Expand Up @@ -186,7 +185,7 @@ public void PassesCorrectStreamHeader_WhenUseStreamIsTrue()
.Returns(callInfo => { throw new NotImplementedException(); });

var graphClient = new GraphClient(new Uri("http://username:password@foo/db/data"), httpClient);

try
{
graphClient.Connect();
Expand Down
162 changes: 162 additions & 0 deletions Neo4jClient.Tests/GraphClientTests/Cypher/ExecuteCypherTests.cs
@@ -1,9 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using NUnit.Framework;
using Neo4jClient.ApiModels.Cypher;
using Neo4jClient.Cypher;
using NSubstitute;

namespace Neo4jClient.Test.GraphClientTests.Cypher
{
Expand Down Expand Up @@ -191,5 +196,162 @@ public void WhenExecuteCypherFails_ShouldRaiseCompletedWithException()
Assert.AreEqual(-1, eventArgs.ResourcesReturned);
}
}

/// <summary>
/// #75
/// </summary>
[Test]
public void SendsCommandWithCorrectTimeout()
{
const string queryText = "MATCH n SET n.Value = 'value'";
const int expectedMaxExecutionTime = 100;

var cypherQuery = new CypherQuery(queryText, new Dictionary<string, object>(), CypherResultMode.Set,CypherResultFormat.DependsOnEnvironment , maxExecutionTime: expectedMaxExecutionTime);
var cypherApiQuery = new CypherApiQuery(cypherQuery);

using (var testHarness = new RestTestHarness
{
{
MockRequest.Get(""),
MockResponse.NeoRoot()
},
{
MockRequest.PostObjectAsJson("/cypher", cypherApiQuery),
MockResponse.Http((int) HttpStatusCode.OK)
}
})
{
var httpClient = testHarness.GenerateHttpClient(testHarness.BaseUri);
var graphClient = new GraphClient(new Uri(testHarness.BaseUri), httpClient);
graphClient.Connect();

httpClient.ClearReceivedCalls();
((IRawGraphClient)graphClient).ExecuteCypher(cypherQuery);

var call = httpClient.ReceivedCalls().Single();
var requestMessage = (HttpRequestMessage)call.GetArguments()[0];
var maxExecutionTimeHeader = requestMessage.Headers.Single(h => h.Key == "max-execution-time");
Assert.AreEqual(expectedMaxExecutionTime.ToString(CultureInfo.InvariantCulture), maxExecutionTimeHeader.Value.Single());
}
}

/// <summary>
/// #75
/// </summary>
[Test]
public void DoesntSetMaxExecutionTime_WhenNotSet()
{
const string queryText = "MATCH n SET n.Value = 'value'";

var cypherQuery = new CypherQuery(queryText, new Dictionary<string, object>(), CypherResultMode.Set);
var cypherApiQuery = new CypherApiQuery(cypherQuery);

using (var testHarness = new RestTestHarness
{
{
MockRequest.Get(""),
MockResponse.NeoRoot()
},
{
MockRequest.PostObjectAsJson("/cypher", cypherApiQuery),
MockResponse.Http((int) HttpStatusCode.OK)
}
})
{
var httpClient = testHarness.GenerateHttpClient(testHarness.BaseUri);
var graphClient = new GraphClient(new Uri(testHarness.BaseUri), httpClient);
graphClient.Connect();

httpClient.ClearReceivedCalls();
((IRawGraphClient)graphClient).ExecuteCypher(cypherQuery);

var call = httpClient.ReceivedCalls().Single();
var requestMessage = (HttpRequestMessage)call.GetArguments()[0];
Assert.IsFalse(requestMessage.Headers.Any(h => h.Key == "max-execution-time"));
}
}

/// <summary>
/// #141
/// </summary>
[Test]
public void SendsCommandWithCustomHeaders()
{
const string queryText = "MATCH n SET n.Value = 'value'";
const int expectedMaxExecutionTime = 100;
const string headerName = "MyTestHeader";
const string headerValue = "myTestHeaderValue";
var customHeaders = new NameValueCollection();
customHeaders.Add(headerName, headerValue);

var cypherQuery = new CypherQuery(queryText, new Dictionary<string, object>(), CypherResultMode.Set, CypherResultFormat.DependsOnEnvironment, maxExecutionTime: expectedMaxExecutionTime, customHeaders: customHeaders);

var cypherApiQuery = new CypherApiQuery(cypherQuery);

using (var testHarness = new RestTestHarness
{
{
MockRequest.Get(""),
MockResponse.NeoRoot()
},
{
MockRequest.PostObjectAsJson("/cypher", cypherApiQuery),
MockResponse.Http((int) HttpStatusCode.OK)
}
})
{
var httpClient = testHarness.GenerateHttpClient(testHarness.BaseUri);
var graphClient = new GraphClient(new Uri(testHarness.BaseUri), httpClient);
graphClient.Connect();

httpClient.ClearReceivedCalls();
((IRawGraphClient)graphClient).ExecuteCypher(cypherQuery);

var call = httpClient.ReceivedCalls().Single();
var requestMessage = (HttpRequestMessage)call.GetArguments()[0];
var maxExecutionTimeHeader = requestMessage.Headers.Single(h => h.Key == "max-execution-time");
Assert.AreEqual(expectedMaxExecutionTime.ToString(CultureInfo.InvariantCulture), maxExecutionTimeHeader.Value.Single());
var customHeader = requestMessage.Headers.Single(h => h.Key == headerName);
Assert.IsNotNull(customHeader);
Assert.AreEqual(headerValue, customHeader.Value.Single());
}
}


/// <summary>
/// #141
/// </summary>
[Test]
public void DoesntSetHeaders_WhenNotSet()
{
const string queryText = "MATCH n SET n.Value = 'value'";

var cypherQuery = new CypherQuery(queryText, new Dictionary<string, object>(), CypherResultMode.Set);
var cypherApiQuery = new CypherApiQuery(cypherQuery);

using (var testHarness = new RestTestHarness
{
{
MockRequest.Get(""),
MockResponse.NeoRoot()
},
{
MockRequest.PostObjectAsJson("/cypher", cypherApiQuery),
MockResponse.Http((int) HttpStatusCode.OK)
}
})
{
var httpClient = testHarness.GenerateHttpClient(testHarness.BaseUri);
var graphClient = new GraphClient(new Uri(testHarness.BaseUri), httpClient);
graphClient.Connect();

httpClient.ClearReceivedCalls();
((IRawGraphClient)graphClient).ExecuteCypher(cypherQuery);

var call = httpClient.ReceivedCalls().Single();
var requestMessage = (HttpRequestMessage)call.GetArguments()[0];
Assert.IsFalse(requestMessage.Headers.Any(h => h.Key == "max-execution-time"));
}
}
}
}

0 comments on commit 59600ca

Please sign in to comment.