Skip to content

Commit

Permalink
updated batch tests to include creating unique node in an index to allow
Browse files Browse the repository at this point in the history
the updating of unique key values.  this would be easier if the AddToIndex
had the create-or-fail attribut.  this is a hack until this attribue is
added.

added batch SetParameter functionality

removed obsolete index query parameters
  • Loading branch information
unknown authored and unknown committed Apr 18, 2013
1 parent 1a7637f commit 443da17
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Neo4jRestNet.Test/App.config
Expand Up @@ -6,7 +6,7 @@

<neo4jRestNet>
<databases>
<add name="neo4j" default="true" https="false" domain="localhost" port="7475" />
<add name="neo4j" default="true" https="false" domain="localhost" port="7474" />
</databases>
</neo4jRestNet>

Expand Down
169 changes: 125 additions & 44 deletions Neo4jRestNet.Test/BatchStoreTest.cs
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo4jRestNet.Core;
using Neo4jRestNet.Core.CypherQuery;
using Neo4jRestNet.Core.Exceptions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
Expand Down Expand Up @@ -693,50 +694,6 @@ public void RemoveRelationshipToIndex4()
Assert.IsTrue(relationships3.First() == relationship1);
}

[TestMethod]
public void AddNodeToIndexUnique1()
{
var batch = new BatchStore();

var batchNode = Node.CreateNode(batch);

var value1 = UniqueValue();

var batchUniqueNode = batchNode.AddToIndex("nodes", "a", value1, true);

Assert.IsTrue(batch.Execute());

var restNode = batch.GetGraphObject(batchUniqueNode);

var nodes = Node.GetNode("nodes", "a", value1);
Assert.IsTrue(nodes.Count() == 1);
Assert.IsTrue(nodes.First() == restNode);
}

[TestMethod]
public void AddNodeToIndexUnique2()
{
var batch = new BatchStore();

var batchNode1 = Node.CreateNode(batch);
var batchNode2 = Node.CreateNode(batch);

var value1 = UniqueValue();

var batchUniqueNode1 = batchNode1.AddToIndex("nodes", "a", value1, true);
var batchUniqueNode2 = batchNode2.AddToIndex("nodes", "a", value1, true);

Assert.IsTrue(batch.Execute());

var restNode1 = batch.GetGraphObject(batchUniqueNode1);
var restNode2 = batch.GetGraphObject(batchUniqueNode2);

var nodes = Node.GetNode("nodes", "a", value1);
Assert.IsTrue(nodes.Count() == 1);
Assert.IsTrue(nodes.First() == restNode1);
Assert.IsTrue(restNode1 == restNode2);
}

[TestMethod]
public void CreateUniqueNodeInIndex1()
{
Expand Down Expand Up @@ -803,5 +760,129 @@ public void CreateUniqueNodeInIndex3()


}

[TestMethod]
public void CreateUniqueNodeUpdateIndexValue()
{
var batch = new BatchStore();

var value1 = UniqueValue();

var props1 = new Properties();
props1.SetProperty("name", "jack");
props1.SetProperty("a", value1);

var batchNode1 = Node.CreateUniqueNode("nodes", "a", value1, IndexUniqueness.CreateOrFail, props1, batch);

var insertResult = batch.Execute();

Assert.IsTrue(insertResult);

var graphNode = batch.GetGraphObject(batchNode1);


// try to update unique index value.
batch = new BatchStore();
var value2 = UniqueValue();

// create node with new value - will fail if value already exists.
var fakeNode = Node.CreateUniqueNode("nodes", "a", value2, IndexUniqueness.CreateOrFail, null, batch);

// delete old node from index
Node.RemoveFromIndex(graphNode, "nodes", "a", null, batch);

// update properties on node in batch
Node.SetProperty(graphNode, "a", value2, batch);
Node.SetProperty(graphNode, "name", "tom", batch);


// add node to index with new key/value for index
Node.AddToIndex(graphNode, "nodes", "a", value2, batch);

// execute batch
var updateResult = batch.Execute();

Assert.IsTrue(updateResult);

fakeNode = batch.GetGraphObject(fakeNode);

// remove fake node
fakeNode.RemoveFromIndex("nodes");
fakeNode.Delete();

//verify key is in index
var findNode = Node.GetNode("nodes", "a", value2);

Assert.IsTrue(findNode.Count() == 1);
Assert.IsTrue(findNode.First().GetProperty("a").ToString() == value2);


//verify old key is not in index
var missingNode = Node.GetNode("nodes", "a", value1);

Assert.IsFalse(missingNode.Any());
}

[TestMethod]
public void CreateUniqueNodeUpdateIndexValueDupFail()
{
var batch = new BatchStore();

var value1 = UniqueValue();

var props1 = new Properties();
props1.SetProperty("name", "jack");
props1.SetProperty("a", value1);

var batchNode1 = Node.CreateUniqueNode("nodes", "a", value1, IndexUniqueness.CreateOrFail, props1, batch);

var insertResult = batch.Execute();

Assert.IsTrue(insertResult);

var graphNode = batch.GetGraphObject(batchNode1);

// try to update unique index value.
batch = new BatchStore();
var value2 = UniqueValue();

// create node with new value - will fail because value already exists.
var fakeNode = Node.CreateUniqueNode("nodes", "a", value1, IndexUniqueness.CreateOrFail, null, batch);

// delete old node from index
Node.RemoveFromIndex(graphNode, "nodes", "a", null, batch);

// update properties on node
Node.SetProperty(graphNode, "a", value2,batch);
Node.SetProperty(graphNode, "name", "tom", batch);

// add node to index with new key/value for index
Node.AddToIndex(graphNode, "nodes", "a", value2, batch);

// execute batch
var updateResult = batch.Execute();

if (updateResult)
{
fakeNode = batch.GetGraphObject(fakeNode);
// remove fake node
fakeNode.RemoveFromIndex("nodes");
fakeNode.Delete();
}

Assert.IsFalse(updateResult);

//verify old key is in index
var findNode = Node.GetNode("nodes", "a", value1);

Assert.IsTrue(findNode.Count() == 1);
Assert.IsTrue(findNode.First().GetProperty("a").ToString() == value1);


//verify new key is not in index
var missingNode = Node.GetNode("nodes", "a", value2);

Assert.IsFalse(missingNode.Any());
}
}
}
40 changes: 1 addition & 39 deletions Neo4jRestNet.Test/NodeClassTest.cs
Expand Up @@ -366,25 +366,6 @@ public void CreateUniqueNode()
Assert.IsNull(node2);
}

[TestMethod]
public void AddNodeToIndexUnique()
{

var node1 = Node.CreateNode();

var node2 = Node.AddToIndex(node1, "people", "id", node1.Id, true);
Assert.IsNotNull(node2);

var node3 = node1.AddToIndex("people", "id", node1.Id, true);

Assert.IsNull(node3);

var node4 = node1.AddToIndex("people", "id", node1.Id);

Assert.IsNotNull(node4);
}


[TestMethod]
public void CreateUniqueRelationship()
{
Expand All @@ -399,26 +380,7 @@ public void CreateUniqueRelationship()
var relationship2 = Relationship.CreateUniqueRelationship(node1, node2, "knows", "rels", "friends", value, IndexUniqueness.GetOrCreate);
Assert.IsNull(relationship2);
}

[TestMethod]
public void AddRelationshipToIndexUnique()
{
var node1 = Node.CreateNode();
var node2 = Node.CreateNode();
var relationship1 = Relationship.CreateRelationship(node1, node2, "knows");

var relationship2 = relationship1.AddToIndex("rels", "id", relationship1.Id, true);
Assert.IsNotNull(relationship2);

var relationship3 = relationship1.AddToIndex("rels", "id", relationship1.Id, true);

Assert.IsNull(relationship3);

var relationship4 = relationship1.AddToIndex("rels", "id", relationship1.Id);

Assert.IsNotNull(relationship4);
}


[TestMethod]
public void GetRelationships4()
{
Expand Down
9 changes: 7 additions & 2 deletions Neo4jRestNet/Core/BatchNodeStore.cs
Expand Up @@ -68,6 +68,11 @@ public Properties GetProperties()
throw new NotImplementedException();
}

public void SetProperty<T>(Node node, string key, T value)
{
_batchStore.SetProperty(node, key, value);
}

public void SaveProperties(Properties properties)
{
_batchStore.SaveProperties(this, properties);
Expand All @@ -83,9 +88,9 @@ public IEnumerable<Relationship> GetRelationships(RelationshipDirection directio
throw new BatchGetRelationshipsNotSupportedException();
}

public Node AddToIndex(ConnectionElement connection, Node node, string indexName, string key, object value, bool unique = false)
public Node AddToIndex(ConnectionElement connection, Node node, string indexName, string key, object value)
{
return _batchStore.AddToIndex(connection, node, indexName, key, value, unique);
return _batchStore.AddToIndex(connection, node, indexName, key, value);
}

public bool RemoveFromIndex(ConnectionElement connection, Node node, string indexName)
Expand Down
4 changes: 2 additions & 2 deletions Neo4jRestNet/Core/BatchRelationshipStore.cs
Expand Up @@ -84,9 +84,9 @@ public string Type
get { throw new NotImplementedException(); }
}

public Relationship AddToIndex(ConnectionElement connection, Relationship relationship, string indexName, string key, object value, bool unique = false)
public Relationship AddToIndex(ConnectionElement connection, Relationship relationship, string indexName, string key, object value)
{
return _batchStore.AddToIndex(connection, relationship, indexName, key, value, unique);
return _batchStore.AddToIndex(connection, relationship, indexName, key, value);
}

public bool RemoveFromIndex(ConnectionElement connection, Relationship relationship, string indexName)
Expand Down
46 changes: 42 additions & 4 deletions Neo4jRestNet/Core/BatchStore.cs
Expand Up @@ -18,6 +18,7 @@ class BatchJob
public HttpRest.Method Method { get; set; }
public string To { get; set; }
public JObject Body { get; set; }
public object BodyValue { get; set; }
public object GraphObject { get; set; }
public long Id { get; set; }
public bool ReloadProperties { get; set; }
Expand Down Expand Up @@ -67,6 +68,10 @@ public bool Execute(ConnectionElement connection)
{
jsonJobDescription.Add("body", job.Body);
}
else if (job.BodyValue != null)
{
jsonJobDescription.Add("body", JToken.FromObject(job.BodyValue));
}

jsonJobs.Add(jsonJobDescription);

Expand Down Expand Up @@ -306,6 +311,22 @@ public Properties GetProperties()
throw new NotImplementedException();
}

public void SetProperty<T>(string key, T value)
{
var index = _jobs.Count();
_jobs.Add(new BatchJob
{
Method = HttpRest.Method.Put,
To = string.Format("/node/{0}/properties", 1),
Id = index,
});
}

public void SetProperty<T>(Enum key, T value)
{
SetProperty(key.ToString(), value);
}

public void SaveProperties(Properties properties)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -347,7 +368,7 @@ public IEnumerable<Relationship> GetRelationships(RelationshipDirection directio
throw new NotImplementedException();
}

public Node AddToIndex(ConnectionElement connection, Node node, string indexName, string key, object value, bool unique = false)
public Node AddToIndex(ConnectionElement connection, Node node, string indexName, string key, object value)
{
var uriFormat = IsBatchObject(node) ? "{{{0}}}" : "/node/{0}";

Expand All @@ -362,7 +383,7 @@ public Node AddToIndex(ConnectionElement connection, Node node, string indexName
_jobs.Add(new BatchJob
{
Method = HttpRest.Method.Post,
To = string.Format("/index/node/{0}{1}", indexName, unique ? "?unique" : string.Empty),
To = string.Format("/index/node/{0}", indexName),
Id = index,
Body = body
});
Expand Down Expand Up @@ -539,7 +560,7 @@ public string Type
get { throw new NotImplementedException(); }
}

public Relationship AddToIndex(ConnectionElement connection, Relationship relationship, string indexName, string key, object value, bool unique = false)
public Relationship AddToIndex(ConnectionElement connection, Relationship relationship, string indexName, string key, object value)
{
var uriFormat = IsBatchObject(relationship) ? "{{{0}}}" : "/relationship/{0}";

Expand All @@ -554,7 +575,7 @@ public Relationship AddToIndex(ConnectionElement connection, Relationship relati
_jobs.Add(new BatchJob
{
Method = HttpRest.Method.Post,
To = string.Format("/index/relationship/{0}{1}", indexName, unique ? "?unique" : string.Empty),
To = string.Format("/index/relationship/{0}", indexName),
Id = index,
Body = body
});
Expand Down Expand Up @@ -602,6 +623,23 @@ public bool RemoveFromIndex(ConnectionElement connection, Relationship relations

#region Node Batch Methods

public void SetProperty<T>(Node node, string key, T value)
{
if (IsBatchObject(node))
{
throw new BatchSetPropertyNotSupportedException();
}

var index = _jobs.Count();
_jobs.Add(new BatchJob
{
Method = HttpRest.Method.Put,
To = string.Format("/node/{0}/properties/{1}", node.Id, key),
BodyValue = value,
Id = index,
});
}

public void SaveProperties(BatchNodeStore node, Properties properties)
{
var index = _jobs.Count();
Expand Down

0 comments on commit 443da17

Please sign in to comment.