Skip to content

Commit

Permalink
adding constraints to fix #143
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Feb 13, 2014
1 parent 9d2fee7 commit 7a77fc4
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require 'neography/rest/clean'
require 'neography/rest/transactions'
require 'neography/rest/spatial'
require 'neography/rest/constraints'

require 'neography/errors'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions lib/neography/rest/constraints.rb
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions spec/integration/rest_constraints_spec.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions spec/unit/rest/constraints_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7a77fc4

Please sign in to comment.