From 5f88ebd66b51b5bcc02a4cb63936079bc6616d57 Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Sat, 8 Sep 2012 17:24:23 +0200 Subject: [PATCH] Add spec for REST node properties. --- lib/neography/rest/node_properties.rb | 14 ++--- spec/spec_helper.rb | 7 ++- spec/unit/rest/node_properties_spec.rb | 80 ++++++++++++++++++++++++++ spec/unit/rest/nodes_spec.rb | 6 -- 4 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 spec/unit/rest/node_properties_spec.rb diff --git a/lib/neography/rest/node_properties.rb b/lib/neography/rest/node_properties.rb index f5480b8..29f3f2a 100644 --- a/lib/neography/rest/node_properties.rb +++ b/lib/neography/rest/node_properties.rb @@ -11,6 +11,13 @@ def initialize(connection) @connection = connection end + def set(id, properties) + properties.each do |property, value| + options = { :body => value.to_json, :headers => json_content_type } + @connection.put(single_path(:id => get_id(id), :property => property), options) + end + end + def reset(id, properties) options = { :body => properties.to_json, :headers => json_content_type } @connection.put(all_path(:id => get_id(id)), options) @@ -48,13 +55,6 @@ def remove_each(id, *properties) end end - def set(id, properties) - properties.each do |property, value| - options = { :body => value.to_json, :headers => json_content_type } - @connection.put(single_path(:id => get_id(id), :property => property), options) - end - end - end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 78437da..917b593 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,4 +15,9 @@ def generate_text(length=8) RSpec.configure do |c| c.filter_run_excluding :slow => true, :break_gremlin => true -end \ No newline at end of file +end + +def json_content_type + {"Content-Type"=>"application/json"} +end + diff --git a/spec/unit/rest/node_properties_spec.rb b/spec/unit/rest/node_properties_spec.rb new file mode 100644 index 0000000..19a3a97 --- /dev/null +++ b/spec/unit/rest/node_properties_spec.rb @@ -0,0 +1,80 @@ +require 'spec_helper' + +module Neography + class Rest + describe NodeProperties do + + let(:connection) { stub } + subject { NodeProperties.new(connection) } + + it "sets properties" do + options1 = { + :body => '"bar"', + :headers => json_content_type + } + options2 = { + :body => '"qux"', + :headers => json_content_type + } + connection.should_receive(:put).with("/node/42/properties/foo", options1) + connection.should_receive(:put).with("/node/42/properties/baz", options2) + subject.set("42", {:foo => "bar", :baz => "qux"}) + end + + it "resets properties" do + options = { + :body => '{"foo":"bar"}', + :headers => json_content_type + } + connection.should_receive(:put).with("/node/42/properties", options) + subject.reset("42", {:foo => "bar"}) + end + + context "getting properties" do + + it "gets all properties" do + connection.should_receive(:get).with("/node/42/properties") + subject.get("42") + end + + it "gets multiple properties" do + connection.should_receive(:get).with("/node/42/properties/foo") + connection.should_receive(:get).with("/node/42/properties/bar") + subject.get("42", "foo", "bar") + end + + it "returns multiple properties as a hash" do + connection.stub(:get).and_return("baz", "qux") + subject.get("42", "foo", "bar").should == { "foo" => "baz", "bar" => "qux" } + end + + it "returns nil if no properties were found" do + connection.stub(:get).and_return(nil, nil) + subject.get("42", "foo", "bar").should be_nil + end + + it "returns hash without nil return values" do + connection.stub(:get).and_return("baz", nil) + subject.get("42", "foo", "bar").should == { "foo" => "baz" } + end + + end + + context "removing properties" do + + it "removes all properties" do + connection.should_receive(:delete).with("/node/42/properties") + subject.remove("42") + end + + it "removes multiple properties" do + connection.should_receive(:delete).with("/node/42/properties/foo") + connection.should_receive(:delete).with("/node/42/properties/bar") + subject.remove("42", "foo", "bar") + end + + end + + end + end +end diff --git a/spec/unit/rest/nodes_spec.rb b/spec/unit/rest/nodes_spec.rb index 916b281..efe0775 100644 --- a/spec/unit/rest/nodes_spec.rb +++ b/spec/unit/rest/nodes_spec.rb @@ -183,12 +183,6 @@ class Rest end - private - - def json_content_type - {"Content-Type"=>"application/json"} - end - end end end