OOify case statements in Request::Base. Added more tests to increase code coverage

git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@495830 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2007-01-13 03:36:10 +00:00
parent ebab2b4cad
commit 753ae838e8
8 changed files with 51 additions and 32 deletions

View File

@ -12,11 +12,12 @@
require 'solr/request/base'
require 'solr/document'
require 'solr/request/update'
require 'rexml/document'
module Solr
module Request
class AddDocument < Solr::Request::Base
class AddDocument < Solr::Request::Update
# create the request, optionally passing in a Solr::Document
#

View File

@ -18,40 +18,14 @@ module Solr
# response type is for a given request
def response_format
case self
when Solr::Request::Commit
return :xml
when Solr::Request::Update
return :xml
when Solr::Request::Select
return :ruby
when Solr::Request::AddDocument
return :xml
when Solr::Request::Ping
return :xml
else
raise "unkown request type: #{self.class}"
end
raise "unknown request type: #{self.class}"
end
# returns the solr handler or url fragment that can
# respond to this type of request
def handler
case self
when Solr::Request::Commit
return 'update'
when Solr::Request::Update
return 'update'
when Solr::Request::Select
return 'select'
when Solr::Request::AddDocument
return 'update'
when Solr::Request::Ping
return 'admin/ping'
else
raise "unkown request type: #{self.class}"
end
raise "unkown request type: #{self.class}"
end
end

View File

@ -14,7 +14,7 @@ require 'rexml/document'
module Solr
module Request
class Commit < Solr::Request::Base
class Commit < Solr::Request::Update
def to_s
return REXML::Element.new('commit').to_s

View File

@ -13,6 +13,13 @@
module Solr
module Request
class Ping < Solr::Request::Base
def response_format
:xml
end
def handler
'admin/ping'
end
end
end
end

View File

@ -20,6 +20,14 @@ module Solr
def initialize(query)
@query = query
end
def response_format
:ruby
end
def handler
'select'
end
def to_hash
return {:q => query, :wt => 'ruby', :fl => '*,score'}
@ -35,7 +43,7 @@ module Solr
http_params.join("&")
end
end
end
end

View File

@ -13,7 +13,12 @@
module Solr
module Request
class Update < Solr::Request::Base
def initialize
def response_format
:xml
end
def handler
'update'
end
end
end

View File

@ -20,6 +20,12 @@ class ConnectionTest < SolrMockBaseTestCase
set_post_return("foo")
assert_equal "foo", connection.post(Solr::Request::Update.new)
end
def test_bad_url
assert_raise(RuntimeError) do
Connection.new("ftp://localhost:9999")
end
end
def test_connection_initialize
connection = Solr::Connection.new("http://localhost:8983/solr")

View File

@ -13,6 +13,9 @@
require 'test/unit'
require 'solr'
class BadRequest < Solr::Request::Base
end
class RequestTest < Test::Unit::TestCase
def test_commit_request
@ -36,5 +39,20 @@ class RequestTest < Test::Unit::TestCase
assert 'belkin', request.to_hash['q']
assert_match /q=belkin/, request.to_s
end
def test_ping_request
request = Solr::Request::Ping.new
assert_equal :xml, request.response_format
end
def test_bad_request_class
assert_raise(RuntimeError) do
BadRequest.new.response_format
end
assert_raise(RuntimeError) do
BadRequest.new.handler
end
end
end