Skip to content

Commit

Permalink
add ConstraintError class + small rubocop refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed Jul 17, 2015
1 parent 6b5102b commit 4e6be72
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/neo4j-server/cypher_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(msg, status, code)
end
end

class ConstraintError < ResponseError; end

class HashEnumeration
include Enumerable
Expand Down Expand Up @@ -201,9 +202,15 @@ def set_error(error)
self
end

CONSTRAINT_ERROR = 'Neo.ClientError.Schema.ConstraintViolation'
def raise_error
fail 'Tried to raise error without an error' unless @error
fail ResponseError.new(@error_msg, @error_status, @error_code)
error_class = constraint_error? ? ConstraintError : ResponseError
fail error_class.new(@error_msg, @error_status, @error_code)
end

def constraint_error?
@error_code == CONSTRAINT_ERROR || @error_msg.include?('already exists with')
end

def raise_cypher_error
Expand All @@ -228,11 +235,7 @@ def self.create_with_tx(response)

new(response, true).tap do |cr|
body = response.body
if body[:errors].empty?
cr.set_data(body[:results].first)
else
cr.set_error(body[:errors].first)
end
body[:errors].empty? ? cr.set_data(body[:results].first) : cr.set_error(body[:errors].first)
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/neo4j-server/unit/cypher_response_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ def successful_response(response)
it { is_expected.to be_truthy }
end
end

describe 'CypherResponse::ConstraintError' do
it 'inherits from ResponseError' do
expect(CypherResponse::ConstraintError.new(nil, nil, nil)).to be_a(CypherResponse::ResponseError)
end
end
end
end
end
22 changes: 22 additions & 0 deletions spec/shared_examples/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@
end
end

describe 'unsatisfied constraint' do
let(:label) { Neo4j::Label.create(:MyFoo) }
before do
if Neo4j::Transaction.current
Neo4j::Transaction.current.failure
Neo4j::Transaction.current.close
end
Neo4j::Session.current.query.match('(n:MyFoo)').delete(:n).exec
label.create_constraint(:bar, type: :unique)
end

after do
label.drop_constraint(:bar, type: :unique)
Neo4j::Session.current.query.match('(n:MyFoo)').delete(:n).exec
end

it 'raises a ConstraintError' do
expect { Neo4j::Node.create({bar: 'dawn'}, :MyFoo) }.not_to raise_error
expect { Neo4j::Node.create({bar: 'dawn'}, :MyFoo) }.to raise_error { Neo4j::Server::CypherResponse::ConstraintError }
end
end

describe 'instance methods' do
describe 'create_index' do
it 'creates an index on given properties' do
Expand Down

0 comments on commit 4e6be72

Please sign in to comment.