PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/solr-ruby-0.0.6/test/unit/standard_request_test.rb

https://bitbucket.org/mediashelf/fedora-migrator
Ruby | 153 lines | 123 code | 18 blank | 12 comment | 0 complexity | f508424cc8c5bd1a2c35fbdf606f1b53 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, IPL-1.0, AGPL-1.0, LGPL-3.0
  1. # The ASF licenses this file to You under the Apache License, Version 2.0
  2. # (the "License"); you may not use this file except in compliance with
  3. # the License. You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. require 'test/unit'
  13. require 'solr'
  14. class StandardRequestTest < Test::Unit::TestCase
  15. def test_basic_query
  16. request = Solr::Request::Standard.new(:query => 'query')
  17. assert_equal :ruby, request.response_format
  18. assert_equal 'select', request.handler
  19. assert_equal 'query', request.to_hash[:q]
  20. assert_match /q=query/, request.to_s
  21. end
  22. def test_bad_params
  23. assert_raise(RuntimeError) do
  24. Solr::Request::Standard.new(:foo => "invalid")
  25. end
  26. assert_raise(RuntimeError) do
  27. Solr::Request::Standard.new(:query => "valid", :foo => "invalid")
  28. end
  29. assert_raise(RuntimeError) do
  30. Solr::Request::Standard.new(:query => "valid", :operator => :bogus)
  31. end
  32. end
  33. def test_common_params
  34. request = Solr::Request::Standard.new(:query => 'query', :start => 10, :rows => 50,
  35. :filter_queries => ['fq1', 'fq2'], :field_list => ['id','title','score'], :operator => :and)
  36. assert_equal 10, request.to_hash[:start]
  37. assert_equal 50, request.to_hash[:rows]
  38. assert_equal ['fq1','fq2'], request.to_hash[:fq]
  39. assert_equal "id,title,score", request.to_hash[:fl]
  40. assert_equal "AND", request.to_hash["q.op"]
  41. end
  42. def test_missing_params
  43. request = Solr::Request::Standard.new(:query => 'query', :debug_query => false, :facets => {:fields =>[:category_facet]})
  44. assert_nil request.to_hash[:rows]
  45. assert_no_match /rows/, request.to_s
  46. assert_no_match /facet\.sort/, request.to_s
  47. assert_match /debugQuery/, request.to_s
  48. end
  49. def test_only_facet_query
  50. request = Solr::Request::Standard.new(:query => 'query',
  51. :facets => {
  52. :queries => ["q1", "q2"],
  53. }
  54. )
  55. hash = request.to_hash
  56. assert_equal ["q1", "q2"], hash["facet.query"]
  57. end
  58. def test_facet_params_all
  59. request = Solr::Request::Standard.new(:query => 'query',
  60. :facets => {
  61. :fields => [:genre,
  62. # field that overrides the global facet parameters
  63. {:year => {:limit => 50, :mincount => 0, :missing => false, :sort => :term, :prefix=>"199", :offset => 7}}],
  64. :queries => ["q1", "q2"],
  65. :prefix => "cat",
  66. :offset => 3, :limit => 5, :zeros => true, :mincount => 20, :sort => :count # global facet parameters
  67. }
  68. )
  69. hash = request.to_hash
  70. assert_equal true, hash[:facet]
  71. assert_equal [:genre, :year], hash["facet.field"]
  72. assert_equal ["q1", "q2"], hash["facet.query"]
  73. assert_equal 5, hash["facet.limit"]
  74. assert_equal 20, hash["facet.mincount"]
  75. assert_equal true, hash["facet.sort"]
  76. assert_equal "cat", hash["facet.prefix"]
  77. assert_equal 50, hash["f.year.facet.limit"]
  78. assert_equal 0, hash["f.year.facet.mincount"]
  79. assert_equal false, hash["f.year.facet.sort"]
  80. assert_equal "199", hash["f.year.facet.prefix"]
  81. assert_equal 3, hash["facet.offset"]
  82. assert_equal 7, hash["f.year.facet.offset"]
  83. end
  84. def test_basic_sort
  85. request = Solr::Request::Standard.new(:query => 'query', :sort => [{:title => :descending}])
  86. assert_equal 'query;title desc', request.to_hash[:q]
  87. end
  88. def test_highlighting
  89. request = Solr::Request::Standard.new(:query => 'query',
  90. :highlighting => {
  91. :field_list => ['title', 'author'],
  92. :alternate_fields => {'title'=>'title', 'author'=>'author'},
  93. :max_alternate_field_length => {'title'=>30, 'author'=>20},
  94. :max_snippets => 3,
  95. :require_field_match => true,
  96. :prefix => "<blink>",
  97. :suffix => "</blink>",
  98. :fragment_size => 300
  99. }
  100. )
  101. hash = request.to_hash
  102. assert_equal true, hash[:hl]
  103. assert_equal "title,author", hash["hl.fl"]
  104. assert_equal "title", hash["f.title.hl.alternateField"]
  105. assert_equal "author", hash["f.author.hl.alternateField"]
  106. assert_equal 30, hash["f.title.hl.maxAlternateFieldLength"]
  107. assert_equal 20, hash["f.author.hl.maxAlternateFieldLength"]
  108. assert_equal true, hash["hl.requireFieldMatch"]
  109. assert_equal "<blink>", hash["hl.simple.pre"]
  110. assert_equal "</blink>", hash["hl.simple.post"]
  111. assert_equal 300, hash["hl.fragsize"]
  112. end
  113. def test_mlt
  114. request = Solr::Request::Standard.new(:query => 'query',
  115. :mlt => {
  116. :count => 5, :field_list => ['field1', 'field2'],
  117. :min_term_freq => 3, :min_doc_freq => 10,
  118. :min_word_length => 4, :max_word_length => 17,
  119. :max_query_terms => 20, :max_tokens_parsed => 100,
  120. :boost => true
  121. }
  122. )
  123. hash = request.to_hash
  124. assert_equal true, hash[:mlt]
  125. assert_equal 5, hash["mlt.count"]
  126. assert_equal 'field1,field2', hash["mlt.fl"]
  127. assert_equal 3, hash["mlt.mintf"]
  128. assert_equal 10, hash["mlt.mindf"]
  129. assert_equal 4, hash["mlt.minwl"]
  130. assert_equal 17, hash["mlt.maxwl"]
  131. assert_equal 20, hash["mlt.maxqt"]
  132. assert_equal 100, hash["mlt.maxntp"]
  133. assert_equal true, hash["mlt.boost"]
  134. end
  135. end