2007-01-02 23:57:00 -05:00
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2007-01-08 15:23:28 -05:00
|
|
|
require 'test/unit'
|
2007-01-17 02:57:48 -05:00
|
|
|
require 'solr'
|
2007-01-08 15:23:28 -05:00
|
|
|
|
2007-01-14 20:41:18 -05:00
|
|
|
class BadRequest < Solr::Request::Standard
|
2007-01-12 23:31:09 -05:00
|
|
|
def response_format
|
|
|
|
:invalid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-12 21:53:39 -05:00
|
|
|
class ServerTest < Test::Unit::TestCase
|
2007-01-08 15:23:28 -05:00
|
|
|
include Solr
|
2007-01-08 16:50:30 -05:00
|
|
|
|
|
|
|
def setup
|
2007-01-16 17:02:13 -05:00
|
|
|
@connection = Connection.new("http://localhost:8888/solr", :autocommit => :on)
|
|
|
|
clean
|
2007-01-08 16:50:30 -05:00
|
|
|
end
|
2007-01-16 17:02:13 -05:00
|
|
|
|
|
|
|
def test_full_lifecycle
|
|
|
|
# make sure autocommit is on
|
|
|
|
assert @connection.autocommit
|
|
|
|
|
|
|
|
# make sure this doc isn't there to begin with
|
|
|
|
@connection.delete(123456)
|
|
|
|
|
|
|
|
# add it
|
|
|
|
@connection.add(:id => 123456, :text => 'Borges')
|
|
|
|
|
|
|
|
# look for it
|
|
|
|
response = @connection.query('Borges')
|
|
|
|
assert_equal 1, response.total_hits
|
|
|
|
assert_equal '123456', response.hits[0]['id']
|
|
|
|
|
|
|
|
# delete it
|
|
|
|
@connection.delete(123456)
|
|
|
|
|
|
|
|
# make sure it's gone
|
|
|
|
response = @connection.query('Borges')
|
|
|
|
assert_equal 0, response.total_hits
|
|
|
|
end
|
|
|
|
|
2007-01-12 23:31:09 -05:00
|
|
|
def test_bad_connection
|
2007-01-25 09:31:40 -05:00
|
|
|
conn = Solr::Connection.new 'http://127.0.0.1:9999/invalid'
|
2007-01-12 23:31:09 -05:00
|
|
|
assert_raise(Errno::ECONNREFUSED) do
|
|
|
|
conn.send(Solr::Request::Ping.new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_url
|
|
|
|
conn = Solr::Connection.new 'http://localhost:8888/invalid'
|
|
|
|
assert_raise(Net::HTTPServerException) do
|
|
|
|
conn.send(Solr::Request::Ping.new)
|
2007-01-11 10:42:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-08 15:23:28 -05:00
|
|
|
def test_commit
|
2007-01-12 21:53:39 -05:00
|
|
|
response = @connection.send(Solr::Request::Commit.new)
|
2007-01-08 15:23:28 -05:00
|
|
|
assert_equal "<result status=\"0\"></result>", response.raw_response
|
2007-01-02 23:57:00 -05:00
|
|
|
end
|
2007-01-08 16:50:30 -05:00
|
|
|
|
2007-01-12 23:31:09 -05:00
|
|
|
def test_ping
|
2007-01-16 17:02:13 -05:00
|
|
|
assert_equal true, @connection.ping
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_with_query
|
|
|
|
assert_equal true, @connection.delete_by_query('[* TO *]')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ping_with_bad_server
|
|
|
|
conn = Solr::Connection.new 'http://localhost:8888/invalid'
|
|
|
|
assert_equal false, conn.ping
|
2007-01-12 23:31:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_response_format
|
2007-01-14 20:41:18 -05:00
|
|
|
request = BadRequest.new(:query => "solr")
|
2007-01-16 17:02:13 -05:00
|
|
|
assert_raise(Solr::Exception) do
|
2007-01-12 23:31:09 -05:00
|
|
|
@connection.send(request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-08 16:50:30 -05:00
|
|
|
def test_escaping
|
2007-01-14 20:49:54 -05:00
|
|
|
doc = Solr::Document.new :id => 47, :ruby_text => 'puts "ouch!"'
|
2007-01-21 15:00:18 -05:00
|
|
|
@connection.add(doc)
|
2007-01-12 21:53:39 -05:00
|
|
|
@connection.commit
|
2007-01-08 16:50:30 -05:00
|
|
|
|
2007-01-14 20:49:54 -05:00
|
|
|
request = Solr::Request::Standard.new :query => 'ouch'
|
2007-01-08 16:50:30 -05:00
|
|
|
result = @connection.send(request)
|
|
|
|
|
2007-01-12 21:53:39 -05:00
|
|
|
assert_match /puts/, result.raw_response
|
2007-01-08 16:50:30 -05:00
|
|
|
end
|
2007-01-12 21:53:39 -05:00
|
|
|
|
2007-01-16 17:02:13 -05:00
|
|
|
def test_add_document
|
|
|
|
doc = {:id => 999, :text => 'hi there!'}
|
|
|
|
request = Solr::Request::AddDocument.new(doc)
|
|
|
|
response = @connection.send(request)
|
|
|
|
assert response.status_code == '0'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_update
|
|
|
|
@connection.update(:id => 999, :text => 'update test')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_such_field
|
2007-01-21 11:08:31 -05:00
|
|
|
doc = {:id => 999, :bogus => 'foo'}
|
2007-01-16 17:02:13 -05:00
|
|
|
request = Solr::Request::AddDocument.new(doc)
|
|
|
|
response = @connection.send(request)
|
|
|
|
assert_equal false, response.ok?
|
2007-01-21 11:08:31 -05:00
|
|
|
assert_equal "ERROR:unknown field 'bogus'", response.status_message
|
2007-01-16 17:02:13 -05:00
|
|
|
end
|
2007-01-21 11:08:31 -05:00
|
|
|
|
2007-01-21 15:00:18 -05:00
|
|
|
def test_index_info
|
|
|
|
doc = {:id => 999, :test_index_facet => 'value'}
|
|
|
|
@connection.add(doc)
|
|
|
|
info = @connection.send(Solr::Request::IndexInfo.new)
|
|
|
|
assert info.field_names.include?("id") && info.field_names.include?("test_index_facet")
|
|
|
|
assert_equal 1, info.num_docs
|
|
|
|
end
|
|
|
|
|
2007-01-16 17:02:13 -05:00
|
|
|
# wipe the index clean
|
|
|
|
def clean
|
|
|
|
@connection.delete_by_query('[* TO *]')
|
|
|
|
end
|
|
|
|
|
2007-01-02 23:57:00 -05:00
|
|
|
end
|