Skip to content

Commit

Permalink
+ return more sensible error codes for the index actions
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Mar 24, 2012
1 parent bfeee5f commit c5100e9
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 81 deletions.
6 changes: 3 additions & 3 deletions server/lib/picky/category_realtime.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Picky

class Category

class Picky::IdNotGivenException < StandardError; end

# Removes an indexed object with the
# given id.
Expand Down Expand Up @@ -31,11 +33,9 @@ def replace object, where = :unshift
# Note: Takes a hash as opposed to the add/replace method.
#
def replace_from hash
# TODO Decide on a format.
#
return unless text = hash[from] || hash[from.to_s]

id = hash[:id] || hash['id']
raise IdNotGivenException.new unless id = hash[:id] || hash['id']
id = id.send key_format

remove id
Expand Down
42 changes: 33 additions & 9 deletions server/lib/picky/sinatra/index_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,45 @@ module Sinatra

module IndexActions

# TODO Add customizable path?
#
def self.extended base
# Updates the given item and returns HTTP codes:
# * 200 if the index has been updated or no error case has occurred.
# * 404 if the index cannot be found.
# * 400 if no data or item id has been provided in the data.
#
# Note: 200 returns no data yet.
#
base.post '/' do
index_name = params['index']
index = Picky::Indexes[index_name.to_sym]
data = params['data']
index.replace_from Yajl::Parser.parse(data) if data
begin
index = Picky::Indexes[index_name.to_sym]
data = params['data']
return 400 unless data
data && index.replace_from(Yajl::Parser.parse data) && 200
rescue IdNotGivenException
400
rescue StandardError
404
end
end

# Deletes the given item and returns:
# * 200 if the index has been updated or no error case has occurred.
# * 404 if the index cannot be found.
# * 400 if no data or item id has been provided in the data.
#
# Note: 200 returns no data yet.
#
base.delete '/' do
index_name = params['index']
index = Picky::Indexes[index_name.to_sym]
data = Yajl::Parser.parse params['data']
id = data['id']
index.remove id if id
begin
index = Picky::Indexes[index_name.to_sym]
data = Yajl::Parser.parse params['data']
id = data['id']
id ? index.remove(id) && 200 : 400
rescue StandardError
404
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions server/spec/category_realtime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,16 @@
it 'offers an unshift method' do
category.unshift Thing.new(1, 'text')
end
it 'offers a replace_from method' do
category.replace_from id: 1, text: "some text"
end
it 'raises on no id given' do
expect {
category.replace_from text: "some text"
}.to raise_error
end
it 'shrugs off no data given' do
category.replace_from id: 1
end

end
212 changes: 143 additions & 69 deletions server/spec/integration/sinatra_index_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,90 +33,164 @@ class MyIndexActionsPickyServer < Sinatra::Base
Picky::Indexes.clear
end
let(:request) { ::Rack::MockRequest.new MyIndexActionsPickyServer }
it 'updates the index correctly' do
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})
context 'return values' do
describe 'update' do
it 'returns a correct code after updating without problems' do
result = request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})
result.status.should == 200
end
it 'returns a correct code after updating with just the id' do
result = request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1" }}
})
result.status.should == 200
end
it 'returns a correct code after updating without id' do
result = request.post('/', params: {
index: 'some_index',
data: %Q{{ "name":"Florian", "surname":"Hanke" }}
})
result.status.should == 400
end
it 'returns a correct code after updating with the wrong index' do
result = request.post('/', params: {
index: 'some_wrong_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})
result.status.should == 404
end
end
describe 'delete' do
before(:each) do
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})
end
it 'returns a correct code after deleting without problems' do
result = request.delete('/', params: {
index: 'some_index',
data: %Q{{ "id":"1" }}
})
result.status.should == 200
end
it 'returns a correct code after deleting twice' do
result = request.delete('/', params: {
index: 'some_index',
data: %Q{{ "id":"1" }}
})
result = request.delete('/', params: {
index: 'some_index',
data: %Q{{ "id":"1" }}
})
result.status.should == 200
end
it 'returns a correct code after deleting without id' do
result = request.delete('/', params: {
index: 'some_index',
data: %Q{{}}
})
result.status.should == 400
end
it 'returns a correct code after deleting with the wrong index' do
result = request.delete('/', params: {
index: 'some_wrong_index',
data: %Q{{ "id":"1" }}
})
result.status.should == 404
end
end
end
context '' do
it 'updates the index correctly' do
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})

results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 1
results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 1

request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"2", "name":"Florian", "surname":"Meier" }}
})
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"2", "name":"Florian", "surname":"Meier" }}
})

results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 2
end
it 'updates the index correctly' do
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Flarian", "surname":"Hanke" }}
})
results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 2
end
it 'updates the index correctly' do
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Flarian", "surname":"Hanke" }}
})

results = Yajl::Parser.parse request.get('/people', params: { query: 'hanke' }).body
results['total'].should == 1
results = Yajl::Parser.parse request.get('/people', params: { query: 'hanke' }).body
results['total'].should == 1

results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 0
results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 0

# Whoops, typo. Let's fix it.
#
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})
# Whoops, typo. Let's fix it.
#
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})

results = Yajl::Parser.parse request.get('/people', params: { query: 'hanke' }).body
results['total'].should == 1
results = Yajl::Parser.parse request.get('/people', params: { query: 'hanke' }).body
results['total'].should == 1

results = Yajl::Parser.parse request.get('/people', params: { query: 'flarian' }).body
results['total'].should == 0
results = Yajl::Parser.parse request.get('/people', params: { query: 'flarian' }).body
results['total'].should == 0

results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 1
end
it 'deletes entries from the index correctly' do
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"2", "name":"Florian", "surname":"Meier" }}
})
results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 1
end
it 'deletes entries from the index correctly' do
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"2", "name":"Florian", "surname":"Meier" }}
})

results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 2
results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 2

request.delete('/', params: {
index: 'some_index',
data: %Q{{ "id":"1" }}
})
request.delete('/', params: {
index: 'some_index',
data: %Q{{ "id":"1" }}
})

results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 1
end
it 'has no problem with a superfluous delete' do
request.delete('/', params: {
index: 'some_index',
data: %Q{{ "id":"1" }}
})
results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 1
end
it 'has no problem with a superfluous delete' do
request.delete('/', params: {
index: 'some_index',
data: %Q{{ "id":"1" }}
})

results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 0
end
it 'works with the (test) client' do
client = Picky::TestClient.new MyIndexActionsPickyServer, :path => '/people'
results = Yajl::Parser.parse request.get('/people', params: { query: 'florian' }).body
results['total'].should == 0
end
it 'works with the (test) client' do
client = Picky::TestClient.new MyIndexActionsPickyServer, :path => '/people'

request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})
request.post('/', params: {
index: 'some_index',
data: %Q{{ "id":"1", "name":"Florian", "surname":"Hanke" }}
})

client.search('florian').total.should == 1
client.search('florian').total.should == 1
end
end
end

Expand Down

0 comments on commit c5100e9

Please sign in to comment.