From 7a77fc4f36f739a490cfa0abe1cd5b1110b2dcf2 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Thu, 13 Feb 2014 00:10:21 -0600 Subject: [PATCH] adding constraints to fix #143 --- lib/neography/rest.rb | 24 ++++++++ lib/neography/rest/constraints.rb | 48 +++++++++++++++ spec/integration/rest_constraints_spec.rb | 72 +++++++++++++++++++++++ spec/unit/rest/constraints_spec.rb | 46 +++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 lib/neography/rest/constraints.rb create mode 100644 spec/integration/rest_constraints_spec.rb create mode 100644 spec/unit/rest/constraints_spec.rb diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 2b1d414..a27cb85 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -29,6 +29,7 @@ require 'neography/rest/clean' require 'neography/rest/transactions' require 'neography/rest/spatial' +require 'neography/rest/constraints' require 'neography/errors' @@ -71,6 +72,7 @@ def initialize(options = ENV['NEO4J_URL'] || {}) @clean = Clean.new(@connection) @transactions = Transactions.new(@connection) @spatial = Spatial.new(@connection) + @constraints = Constraints.new(@connection) end # meta-data @@ -123,6 +125,28 @@ def delete_schema_index(label, property) @schema_indexes.drop(label, property) end + # constraints + + def get_constraints(label=nil) + label.nil? ? @constraints.list : @constraints.get(label) + end + + def drop_constraint(label, property) + @constraints.drop(label, property) + end + + def get_uniqueness(label) + @constraints.get_uniqueness(label) + end + + def get_unique_constraint(label, property) + @constraints.get_unique(label, property) + end + + def create_unique_constraint(label, property) + @constraints.create_unique(label, property) + end + # transactions def begin_transaction(statements=nil) diff --git a/lib/neography/rest/constraints.rb b/lib/neography/rest/constraints.rb new file mode 100644 index 0000000..51d238b --- /dev/null +++ b/lib/neography/rest/constraints.rb @@ -0,0 +1,48 @@ +module Neography + class Rest + class Constraints + extend Neography::Rest::Paths + include Neography::Rest::Helpers + + add_path :base, "/schema/constraint/" + add_path :label, "/schema/constraint/:label" + add_path :uniqueness, "/schema/constraint/:label/uniqueness/" + add_path :unique, "/schema/constraint/:label/uniqueness/:property" + + def initialize(connection) + @connection = connection + end + + def drop(label, property) + @connection.delete(unique_path(:label => label, :property => property)) + end + + def list + @connection.get(base_path) + end + + def get(label) + @connection.get(label_path(:label => label)) + end + + def get_uniqueness(label) + @connection.get(uniqueness_path(:label => label)) + end + + def get_unique(label, property) + @connection.get(unique_path(:label => label, :property => property)) + end + + def create_unique(label, property) + options = { + :body => { + :property_keys => [property] + }.to_json, + :headers => json_content_type + } + @connection.post(uniqueness_path(:label => label), options) + end + + end + end +end diff --git a/spec/integration/rest_constraints_spec.rb b/spec/integration/rest_constraints_spec.rb new file mode 100644 index 0000000..62fa1ed --- /dev/null +++ b/spec/integration/rest_constraints_spec.rb @@ -0,0 +1,72 @@ +require 'spec_helper' + +describe Neography::Rest do + before(:each) do + @neo = Neography::Rest.new + end + + describe "create unique constraint" do + it "can create a unique constraint" do + label = "User" + property = "user_id" + uc = @neo.create_unique_constraint(label, property) + uc.should_not be_nil + uc["label"].should == label + uc["property_keys"].should == [property] + end + end + + describe "get a unique constraint" do + it "can get a unique constraint" do + label = "User" + property = "user_id" + uc = @neo.get_unique_constraint(label, property) + uc.should_not be_nil + uc.first["label"].should == label + uc.first["property_keys"].should == [property] + end + end + + describe "get unique constraints" do + it "can get unique constraints for a label" do + label = "User" + property = "user_id" + uc = @neo.get_uniqueness(label) + uc.should_not be_nil + uc.first["label"].should == label + uc.first["property_keys"].should == [property] + end + end + + describe "list constraints" do + it "can get a list of constraints" do + label = "User" + property = "user_id" + cs = @neo.get_constraints + cs.should_not be_nil + cs.first["label"].should == label + cs.first["property_keys"].should == [property] + end + + it "can get a list of constraints for a specifc label" do + label = "User" + property = "user_id" + cs = @neo.get_constraints(label) + cs.should_not be_nil + cs.first["label"].should == label + cs.first["property_keys"].should == [property] + end + end + + describe "drop a constraint" do + it "can drop a constraint" do + label = "User" + property = "user_id" + uc = @neo.drop_constraint(label, property) + uc.should be_nil + cs = @neo.get_constraints(label) + cs.should be_empty + end + end + +end \ No newline at end of file diff --git a/spec/unit/rest/constraints_spec.rb b/spec/unit/rest/constraints_spec.rb new file mode 100644 index 0000000..2724ca8 --- /dev/null +++ b/spec/unit/rest/constraints_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +module Neography + class Rest + describe Constraints do + + let(:connection) { double } + subject { Constraints.new(connection) } + + it "list constraints" do + connection.should_receive(:get).with("/schema/constraint/") + subject.list + end + + it "get constraints for a label" do + connection.should_receive(:get).with("/schema/constraint/label") + subject.get("label") + end + + it "create a unique constraint for a label" do + options = { + :body => '{"property_keys":["property"]}', + :headers => json_content_type + } + connection.should_receive(:post).with("/schema/constraint/label/uniqueness/", options) + subject.create_unique("label", "property") + end + + it "get unique constraints for a label" do + connection.should_receive(:get).with("/schema/constraint/label/uniqueness/") + subject.get_uniqueness("label") + end + + it "get a specific unique constraint for a label" do + connection.should_receive(:get).with("/schema/constraint/label/uniqueness/property") + subject.get_unique("label", "property") + end + + it "can delete a constraint for a label" do + connection.should_receive(:delete).with("/schema/constraint/label/uniqueness/property") + subject.drop("label","property") + end + + end + end +end \ No newline at end of file