Skip to content

Commit

Permalink
more work on adding spatial to batch... failing test however...
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Jan 31, 2014
1 parent 07e63dc commit f1e172e
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 52 deletions.
109 changes: 109 additions & 0 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,115 @@ def remove_from_index_path(klass, index, key_or_id, value_or_id = nil, id = nil)
end
end

# Spatial

def get_spatial
get Spatial.index_path
end

def add_point_layer(layer, lat = nil, lon = nil)
post Spatial.add_simple_point_layer_path do
{
:layer => layer,
:lat => lat || "lat",
:lon => lon || "lon"
}
end
end

def add_editable_layer(layer, format = "WKT", node_property_name = "wkt")
post Spatial.add_editable_layer_path do
{
:layer => layer,
:format => format,
:nodePropertyName => node_property_name
}
end
end

def get_layer(layer)
post Spatial.get_layer_path do
{
:layer => layer
}
end
end

def add_geometry_to_layer(layer, geometry)
post Spatial.add_geometry_to_layer_path do
{
:layer => layer,
:geometry => geometry
}
end
end

def edit_geometry_from_layer(layer, geometry, node)
post Spatial.edit_geometry_from_layer_path do
{
:layer => layer,
:geometry => geometry,
:geometryNodeId => get_id(node)
}
end
end

def add_node_to_layer(layer, node)
post Spatial.add_node_to_layer_path do
{
:layer => layer,
:node => get_id(node)
}
end
end

def find_geometries_in_bbox(layer, minx, maxx, miny, maxy)
post Spatial.find_geometries_in_bbox_path do
{
:layer => layer,
:minx => minx,
:maxx => maxx,
:miny => miny,
:maxy => maxy
}
end
end

def find_geometries_within_distance(layer, pointx, pointy, distance)
post Spatial.find_geometries_within_distance_path do
{
:layer => layer,
:pointX => pointx,
:pointY => pointy,
:distanceInKm => distance
}
end
end

def create_spatial_index(name, type, lat, lon)
post NodeIndexes.all_path do
{
:name => name,
:config => {
:provider => "spatial",
:geometry_type => type || "point",
:lat => lat || "lat",
:lon => lon || "lon"
}
}
end
end

def add_node_to_spatial_index(index, id)
post NodeIndexes.base_path(:index => index) do
{
:uri => build_node_uri(id),
:key => "k",
:value => "v"
}
end
end

def get(to, &block)
request "GET", to, &block
end
Expand Down
139 changes: 87 additions & 52 deletions spec/integration/rest_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,58 +304,93 @@
batch_result.first["body"]["data"][0][0]["self"].split('/').last.should == id
end

it "can delete a node in batch" do
node1 = @neo.create_node
node2 = @neo.create_node
id1 = node1['self'].split('/').last
id2 = node2['self'].split('/').last
batch_result = @neo.batch [:delete_node, id1 ], [:delete_node, id2]
expect {
@neo.get_node(node1).should be_nil
}.to raise_error Neography::NodeNotFoundException
expect {
@neo.get_node(node2).should be_nil
}.to raise_error Neography::NodeNotFoundException
end

it "can remove a node from an index in batch " do
index = generate_text(6)
key = generate_text(6)
value1 = generate_text
value2 = generate_text
value3 = generate_text

node1 = @neo.create_unique_node(index, key, value1, { "name" => "Max" })
node2 = @neo.create_unique_node(index, key, value2, { "name" => "Neo" })
node3 = @neo.create_unique_node(index, key, value3, { "name" => "Samir"})

batch_result = @neo.batch [:remove_node_from_index, index, key, value1, node1 ],
[:remove_node_from_index, index, key, node2 ],
[:remove_node_from_index, index, node3 ]

@neo.get_node_index(index, key, value1).should be_nil
@neo.get_node_index(index, key, value2).should be_nil
@neo.get_node_index(index, key, value3).should be_nil
end

it "can remove a relationship from an index in batch" do
index = generate_text(6)
key = generate_text(6)
value1 = generate_text
value2 = generate_text

node1 = @neo.create_node
node2 = @neo.create_node
relationship1 = @neo.create_unique_relationship(index, key, value1, "friends", node1, node2)
relationship2 = @neo.create_unique_relationship(index, key, value2, "friends", node2, node1)

batch_result = @neo.batch [:remove_relationship_from_index, index, key, relationship1],
[:remove_relationship_from_index, index, key, relationship2]

@neo.get_relationship_index(index, key, value1).should be_nil
@neo.get_relationship_index(index, key, value2).should be_nil
end

it "can delete a node in batch" do
node1 = @neo.create_node
node2 = @neo.create_node
id1 = node1['self'].split('/').last
id2 = node2['self'].split('/').last
batch_result = @neo.batch [:delete_node, id1 ], [:delete_node, id2]
expect {
@neo.get_node(node1).should be_nil
}.to raise_error Neography::NodeNotFoundException
expect {
@neo.get_node(node2).should be_nil
}.to raise_error Neography::NodeNotFoundException
end

it "can remove a node from an index in batch " do
index = generate_text(6)
key = generate_text(6)
value1 = generate_text
value2 = generate_text
value3 = generate_text

node1 = @neo.create_unique_node(index, key, value1, { "name" => "Max" })
node2 = @neo.create_unique_node(index, key, value2, { "name" => "Neo" })
node3 = @neo.create_unique_node(index, key, value3, { "name" => "Samir"})

batch_result = @neo.batch [:remove_node_from_index, index, key, value1, node1 ],
[:remove_node_from_index, index, key, node2 ],
[:remove_node_from_index, index, node3 ]

@neo.get_node_index(index, key, value1).should be_nil
@neo.get_node_index(index, key, value2).should be_nil
@neo.get_node_index(index, key, value3).should be_nil
end

it "can remove a relationship from an index in batch" do
index = generate_text(6)
key = generate_text(6)
value1 = generate_text
value2 = generate_text

node1 = @neo.create_node
node2 = @neo.create_node
relationship1 = @neo.create_unique_relationship(index, key, value1, "friends", node1, node2)
relationship2 = @neo.create_unique_relationship(index, key, value2, "friends", node2, node1)

batch_result = @neo.batch [:remove_relationship_from_index, index, key, relationship1],
[:remove_relationship_from_index, index, key, relationship2]

@neo.get_relationship_index(index, key, value1).should be_nil
@neo.get_relationship_index(index, key, value2).should be_nil
end

it "can do spatial in batch" do
properties = {:lat => 60.1, :lon => 15.2}
node = @neo.create_node(properties)
batch_result = @neo.batch [:add_point_layer, "restaurantsbatch"],
[:add_node_to_layer, "restaurantsbatch", node],
[:get_layer, "restaurantsbatch"],
[:find_geometries_within_distance, "restaurantsbatch", 60.0,15.0, 100.0],
[:find_geometries_in_bbox, "restaurantsbatch", 60.0, 60.2, 15.0, 15.3]
# getting The transaction is marked for rollback only. errors
puts batch_result.inspect
batch_result[0].first["data"]["layer"].should == "restaurantsbatch"
batch_result[1].first["data"]["lat"].should == properties[:lat]
batch_result[1].first["data"]["lon"].should == properties[:lon]
batch_result[2].first["data"]["layer"].should == "restaurantsbatch"
batch_result[3].first["data"].should_not be_empty
batch_result[4].first["data"].should_not be_empty
end

it "can do spatial via Cypher in batch" do
properties = {:lat => 60.1, :lon => 15.2}
node = @neo.create_node(properties)
batch_result = @neo.batch [:create_spatial_index, "geobatchcypher", "point", "lat", "lon"],
[:add_node_to_spatial_index, "geobatchcypher", node],
[:execute_query, "start n = node:geobatchcypher({withinDistance}) return n", {:withinDistance => "withinDistance:[60.0,15.0,100.0]"}],
[:execute_query, "start n = node:geobatchcypher({bbox}) return n", {:bbox => "bbox:[15.0,15.3,60.0,60.2]"}]

batch_result[0]["body"]["provider"].should == "spatial"
batch_result[0]["body"]["geometry_type"].should == "point"
batch_result[0]["body"]["lat"].should == "lat"
batch_result[0]["body"]["lon"].should == "lon"
batch_result[1]["from"].should == "/index/node/geobatchcypher"
batch_result[1]["body"]["data"].should == {"lat" => 60.1, "lon" => 15.2}
batch_result[2]["body"]["data"].should_not be_empty
batch_result[3]["body"]["data"].should_not be_empty
end
end

describe "referenced batch" do
Expand Down

0 comments on commit f1e172e

Please sign in to comment.