Skip to content

Commit

Permalink
refactoring Constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent d206a11 commit f59df45
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 63 deletions.
26 changes: 1 addition & 25 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Rest
include RelationshipTypes
include NodeLabels
include SchemaIndexes
include Constraints
extend Forwardable

attr_reader :connection
Expand Down Expand Up @@ -72,32 +73,7 @@ def initialize(options = ENV['NEO4J_URL'] || {})
@clean ||= Clean.new(@connection)
@transactions ||= Transactions.new(@connection)
@spatial ||= Spatial.new(@connection)
@constraints ||= Constraints.new(@connection)
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

Expand Down
40 changes: 15 additions & 25 deletions lib/neography/rest/constraints.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
module Neography
class Rest
class Constraints
extend Neography::Rest::Paths
module Constraints
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)

def drop_constraint(label, property)
@connection.delete("/schema/constraint/%{label}/uniqueness/%{property}" % {:label => label, :property => property})
end

def get(label)
@connection.get(label_path(:label => label))
def get_constraints(label=nil)
#if label.nil?
# @connection.get(base_path)
#else
@connection.get("/schema/constraint/%{label}" % {:label => label})
#end
end

def get_uniqueness(label)
@connection.get(uniqueness_path(:label => label))
@connection.get("/schema/constraint/%{label}/uniqueness/" % {:label => label})
end

def get_unique(label, property)
@connection.get(unique_path(:label => label, :property => property))
def get_unique_constraint(label, property)
@connection.get("/schema/constraint/%{label}/uniqueness/%{property}" % {:label => label, :property => property})
end

def create_unique(label, property)
def create_unique_constraint(label, property)
options = {
:body => {
:property_keys => [property]
}.to_json,
:headers => json_content_type
}
@connection.post(uniqueness_path(:label => label), options)
@connection.post("/schema/constraint/%{label}/uniqueness/" % {:label => label}, options)
end

end
Expand Down
25 changes: 12 additions & 13 deletions spec/unit/rest/constraints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,40 @@ module Neography
class Rest
describe Constraints do

let(:connection) { double }
subject { Constraints.new(connection) }
subject { Neography::Rest.new }

it "list constraints" do
connection.should_receive(:get).with("/schema/constraint/")
subject.list
subject.connection.should_receive(:get).with("/schema/constraint/")
subject.get_constraints
end

it "get constraints for a label" do
connection.should_receive(:get).with("/schema/constraint/label")
subject.get("label")
subject.connection.should_receive(:get).with("/schema/constraint/label")
subject.get_constraints("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")
subject.connection.should_receive(:post).with("/schema/constraint/label/uniqueness/", options)
subject.create_unique_constraint("label", "property")
end

it "get unique constraints for a label" do
connection.should_receive(:get).with("/schema/constraint/label/uniqueness/")
subject.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")
subject.connection.should_receive(:get).with("/schema/constraint/label/uniqueness/property")
subject.get_unique_constraint("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")
subject.connection.should_receive(:delete).with("/schema/constraint/label/uniqueness/property")
subject.drop_constraint("label","property")
end

end
Expand Down

0 comments on commit f59df45

Please sign in to comment.