Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-141 Adding possibility to add customheaders to a single cypher … #149

Merged
merged 1 commit into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions Neo4jClient.Tests/Cypher/CypherFluentQueryCustomHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}
}