Skip to content

Commit

Permalink
Add batch unit tests and refactor Batch class.
Browse files Browse the repository at this point in the history
Roel van Dijk committed Sep 20, 2012
1 parent 2dd4b16 commit 12488e5
Showing 2 changed files with 476 additions and 58 deletions.
291 changes: 233 additions & 58 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ def not_streaming(*args)

def batch(accept_header, *args)
batch = []
Array(args).each_with_index do |c,i|
Array(args).each_with_index do |c, i|
batch << {:id => i }.merge(get_batch(c))
end
options = {
@@ -34,66 +34,241 @@ def batch(accept_header, *args)
end

def get_batch(args)
case args[0]
when :get_node
{:method => "GET", :to => "/node/#{get_id(args[1])}"}
when :create_node
{:method => "POST", :to => "/node/", :body => args[1]}
when :create_unique_node
{:method => "POST", :to => "/index/node/#{args[1]}?unique", :body => {:key => args[2], :value => args[3], :properties => args[4]}}
when :set_node_property
{:method => "PUT", :to => "/node/#{get_id(args[1])}/properties/#{args[2].keys.first}", :body => args[2].values.first}
when :reset_node_properties
{:method => "PUT", :to => "/node/#{get_id(args[1])}/properties", :body => args[2]}
when :get_relationship
{:method => "GET", :to => "/relationship/#{get_id(args[1])}"}
when :create_relationship
{:method => "POST", :to => (args[2].is_a?(String) && args[2].start_with?("{") ? "" : "/node/") + "#{get_id(args[2])}/relationships", :body => {:to => (args[3].is_a?(String) && args[3].start_with?("{") ? "" : "/node/") + "#{get_id(args[3])}", :type => args[1], :data => args[4] } }
when :create_unique_relationship
{:method => "POST", :to => "/index/relationship/#{args[1]}?unique", :body => {:key => args[2], :value => args[3], :type => args[4], :start => (args[5].is_a?(String) && args[5].start_with?("{") ? "" : "/node/") + "#{get_id(args[5])}", :end=> (args[6].is_a?(String) && args[6].start_with?("{") ? "" : "/node/") + "#{get_id(args[6])}"} }
when :delete_relationship
{:method => "DELETE", :to => "/relationship/#{get_id(args[1])}"}
when :set_relationship_property
{:method => "PUT", :to => "/relationship/#{get_id(args[1])}/properties/#{args[2].keys.first}", :body => args[2].values.first}
when :reset_relationship_properties
{:method => "PUT", :to => (args[1].is_a?(String) && args[1].start_with?("{") ? "" : "/relationship/") + "#{get_id(args[1])}/properties", :body => args[2]}
when :add_node_to_index
{:method => "POST", :to => "/index/node/#{args[1]}", :body => {:uri => (args[4].is_a?(String) && args[4].start_with?("{") ? "" : "/node/") + "#{get_id(args[4])}", :key => args[2], :value => args[3] } }
when :add_relationship_to_index
{:method => "POST", :to => "/index/relationship/#{args[1]}", :body => {:uri => (args[4].is_a?(String) && args[4].start_with?("{") ? "" : "/relationship/") + "#{get_id(args[4])}", :key => args[2], :value => args[3] } }
when :get_node_index
{:method => "GET", :to => "/index/node/#{args[1]}/#{args[2]}/#{args[3]}"}
when :get_relationship_index
{:method => "GET", :to => "/index/relationship/#{args[1]}/#{args[2]}/#{args[3]}"}
when :get_node_relationships
{:method => "GET", :to => "/node/#{get_id(args[1])}/relationships/#{args[2] || 'all'}"}
when :execute_script
{:method => "POST", :to => @connection.gremlin_path, :body => {:script => args[1], :params => args[2]}}
when :execute_query
if args[2]
{:method => "POST", :to => @connection.cypher_path, :body => {:query => args[1], :params => args[2]}}
else
{:method => "POST", :to => @connection.cypher_path, :body => {:query => args[1]}}
end
when :remove_node_from_index
case args.size
when 5 then {:method => "DELETE", :to => "/index/node/#{args[1]}/#{args[2]}/#{args[3]}/#{get_id(args[4])}" }
when 4 then {:method => "DELETE", :to => "/index/node/#{args[1]}/#{args[2]}/#{get_id(args[3])}" }
when 3 then {:method => "DELETE", :to => "/index/node/#{args[1]}/#{get_id(args[2])}" }
end
when :remove_relationship_from_index
case args.size
when 5 then {:method => "DELETE", :to => "/index/relationship/#{args[1]}/#{args[2]}/#{args[3]}/#{get_id(args[4])}" }
when 4 then {:method => "DELETE", :to => "/index/relationship/#{args[1]}/#{args[2]}/#{get_id(args[3])}" }
when 3 then {:method => "DELETE", :to => "/index/relationship/#{args[1]}/#{get_id(args[2])}" }
end
when :delete_node
{:method => "DELETE", :to => "/node/#{get_id(args[1])}"}
else
raise "Unknown option #{args[0]}"
if respond_to?(args[0].to_sym, true)
send(args[0].to_sym, args)
else
raise "Unknown option #{args[0]}"
end
end

def get_node(args)
{
:method => "GET",
:to => Nodes.base_path(:id => get_id(args[1]))
}
end

def create_node(args)
{
:method => "POST",
:to => Nodes.index_path,
:body => args[1]
}
end

def delete_node(args)
{
:method => "DELETE",
:to => Nodes.base_path(:id => get_id(args[1]))
}
end

def create_unique_node(args)
{
:method => "POST",
:to => NodeIndexes.unique_path(:index => args[1]),
:body => {
:key => args[2],
:value => args[3],
:properties => args[4]
}
}
end

def add_node_to_index(args)
{
:method => "POST",
:to => "/index/node/#{args[1]}",
:body => {
:uri => build_node_uri(args[4]),
:key => args[2],
:value => args[3]
}
}
end

def get_node_index(args)
{
:method => "GET",
:to => "/index/node/#{args[1]}/#{args[2]}/#{args[3]}"
}
end

def remove_node_from_index(args)
path = case args.size
when 5
NodeIndexes.value_path(:index => args[1], :key => args[2], :value => args[3], :id => get_id(args[4]))
when 4
NodeIndexes.key_path(:index => args[1], :key => args[2], :id => get_id(args[3]))
when 3
NodeIndexes.id_path(:index => args[1], :id => get_id(args[2]))
end

{
:method => "DELETE",
:to => path
}
end

def set_node_property(args)
{
:method => "PUT",
:to => NodeProperties.single_path(:id => get_id(args[1]), :property => args[2].keys.first),
:body => args[2].values.first
}
end

def reset_node_properties(args)
{
:method => "PUT",
:to => NodeProperties.all_path(:id => get_id(args[1])),
:body => args[2]
}
end

def get_node_relationships(args)
{
:method => "GET",
:to => NodeRelationships.direction_path(:id => get_id(args[1]), :direction => args[2] || 'all'),
}
end

def get_relationship(args)
{
:method => "GET",
:to => Relationships.base_path(:id => get_id(args[1]))
}
end

def create_relationship(args)
{
:method => "POST",
:to => build_node_uri(args[2]) + "/relationships",
:body => {
:to => build_node_uri(args[3]),
:type => args[1],
:data => args[4]
}
}
end

def delete_relationship(args)
{
:method => "DELETE",
:to => Relationships.base_path(:id =>get_id(args[1]))
}
end

def create_unique_relationship(args)
{
:method => "POST",
:to => "/index/relationship/#{args[1]}?unique",
:body => {
:key => args[2],
:value => args[3],
:type => args[4],
:start => build_node_uri(args[5]),
:end => build_node_uri(args[6])
}
}
end

def add_relationship_to_index(args)
{
:method => "POST",
:to => "/index/relationship/#{args[1]}",
:body => {
:uri => build_relationship_uri(args[4]),
:key => args[2],
:value => args[3]
}
}
end

def get_relationship_index(args)
{
:method => "GET",
:to => "/index/relationship/#{args[1]}/#{args[2]}/#{args[3]}"
}
end

def set_relationship_property(args)
{
:method => "PUT",
:to => RelationshipProperties.single_path(:id => get_id(args[1]), :property => args[2].keys.first),
:body => args[2].values.first
}
end

def reset_relationship_properties(args)
{
:method => "PUT",
:to => build_relationship_uri(args[1]) + "/properties",
:body => args[2]
}
end

def execute_query(args)
request = {
:method => "POST",
:to => @connection.cypher_path,
:body => {
:query => args[1]
}
}

request[:body].merge!({ :params => args[2] }) if args[2]

request
end

def remove_relationship_from_index(args)
path = case args.size
when 5
RelationshipIndexes.value_path(:index => args[1], :key => args[2], :value => args[3], :id => get_id(args[4]))
when 4
RelationshipIndexes.key_path(:index => args[1], :key => args[2], :id => get_id(args[3]))
when 3
RelationshipIndexes.id_path(:index => args[1], :id => get_id(args[2]))
end

{
:method => "DELETE",
:to => path
}
end

def build_node_uri(value)
build_uri(value, "node")
end

def build_relationship_uri(value)
build_uri(value, "relationship")
end

def build_uri(value, type)
path_or_variable(value, type) + "#{get_id(value)}"
end

def path_or_variable(value, type)
if value.is_a?(String) && value.start_with?("{")
""
else
"/#{type}/"
end
end

def execute_script(args)
{
:method => "POST",
:to => @connection.gremlin_path,
:body => {
:script => args[1],
:params => args[2]
}
}
end

end
end
end
Loading

0 comments on commit 12488e5

Please sign in to comment.