PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/instance_methods_shoulda.rb

https://bitbucket.org/daveed/acts_as_solr
Ruby | 326 lines | 272 code | 51 blank | 3 comment | 32 complexity | 0381c9798dda196176d5ae47003c067c MD5 | raw file
  1. require File.dirname(__FILE__) + '/test_helper'
  2. require 'instance_methods'
  3. require 'logger'
  4. module Solr; end
  5. require 'solr/xml'
  6. require 'solr/field'
  7. require 'solr/document'
  8. require 'solr_instance'
  9. require 'erb'
  10. require 'ostruct'
  11. class InstanceMethodsTest < Test::Unit::TestCase
  12. context "With a Solr record instance" do
  13. setup do
  14. @instance = SolrInstance.new
  15. end
  16. context "when checking whether indexing is disabled" do
  17. setup do
  18. @instance.configuration = {:if => true}
  19. end
  20. should "return true if the specified proc returns true " do
  21. @instance.configuration[:offline] = proc {|record| true}
  22. assert @instance.indexing_disabled?
  23. end
  24. should "return false if the specified proc returns false" do
  25. @instance.configuration[:offline] = proc {|record| false}
  26. assert !@instance.indexing_disabled?
  27. end
  28. should "return true if no valid offline option was specified" do
  29. @instance.configuration[:offline] = nil
  30. @instance.configuration[:if] = proc {true}
  31. assert !@instance.indexing_disabled?
  32. end
  33. end
  34. context "when validating the boost" do
  35. setup do
  36. @instance.solr_configuration = {:default_boost => 10.0}
  37. @instance.configuration = {:if => true}
  38. end
  39. should "accept and evaluate a block" do
  40. @instance.configuration[:boost] = proc {|record| record.boost_rate}
  41. assert_equal 10.0, @instance.send(:validate_boost, @instance.configuration[:boost])
  42. end
  43. should "accept and return a float" do
  44. @instance.configuration[:boost] = 9.0
  45. assert_equal 9.0, @instance.send(:validate_boost, @instance.configuration[:boost])
  46. end
  47. should "return the default float when the specified is negative" do
  48. @instance.configuration[:boost] = -1.0
  49. assert_equal 10.0, @instance.send(:validate_boost, @instance.configuration[:boost])
  50. end
  51. should "execute the according method when value is a symbol" do
  52. @instance.configuration[:boost] = :irate
  53. assert_equal 8.0, @instance.send(:validate_boost, @instance.configuration[:boost])
  54. end
  55. should "return the default boost when there is no valid boost" do
  56. @instance.configuration[:boost] = "boost!"
  57. assert_equal 10.0, @instance.send(:validate_boost, @instance.configuration[:boost])
  58. end
  59. end
  60. context "when determining the solr document id" do
  61. should "combine class name and id" do
  62. assert_equal "SolrInstance:10", @instance.solr_id
  63. end
  64. end
  65. context "when saving the instance to solr" do
  66. context "with indexing disabled" do
  67. setup do
  68. @instance.configuration = {:fields => [:name], :if => nil}
  69. end
  70. should "just return and do nothing" do
  71. @instance.expects(:solr_add).never
  72. @instance.expects(:solr_destroy).never
  73. assert @instance.solr_save
  74. end
  75. end
  76. context "with indexing enabled" do
  77. setup do
  78. @instance.configuration = {:fields => [:name], :if => "true", :auto_commit => true}
  79. @instance.stubs(:solr_commit)
  80. @instance.stubs(:solr_add)
  81. @instance.stubs(:to_solr_doc).returns("My test document")
  82. end
  83. should "add the solr document" do
  84. @instance.expects(:solr_add).with("My test document").once
  85. @instance.solr_save
  86. end
  87. should "commit to solr" do
  88. @instance.expects(:solr_commit).once
  89. @instance.solr_save
  90. end
  91. should "not commit if auto_commit is disabled" do
  92. @instance.configuration.merge!(:auto_commit => false)
  93. @instance.expects(:solr_commit).never
  94. @instance.solr_save
  95. end
  96. should "destroy the document if :if clause is false" do
  97. @instance.configuration.merge!(:if => "false")
  98. @instance.expects(:solr_destroy).once
  99. @instance.solr_save
  100. end
  101. end
  102. end
  103. context "when destroying an instance in solr" do
  104. setup do
  105. @instance.configuration = {:if => true, :auto_commit => true}
  106. @instance.stubs(:solr_commit)
  107. @instance.stubs(:solr_delete)
  108. end
  109. should "delete the instance" do
  110. @instance.expects(:solr_delete).with("SolrInstance:10")
  111. @instance.solr_destroy
  112. end
  113. should "commit to solr" do
  114. @instance.expects(:solr_commit)
  115. @instance.solr_destroy
  116. end
  117. should "not commit if auto_commit is disabled" do
  118. @instance.configuration.merge!(:auto_commit => false)
  119. @instance.expects(:solr_commit).never
  120. @instance.solr_destroy
  121. end
  122. context "with indexing disabled" do
  123. should "not contact solr" do
  124. @instance.configuration.merge!(:offline => true, :if => nil)
  125. @instance.expects(:solr_delete).never
  126. @instance.solr_destroy
  127. end
  128. end
  129. end
  130. context "when converting an instance to a solr document" do
  131. setup do
  132. @instance.configuration = {:if => true, :auto_commit => true, :solr_fields => {:name => {:boost => 9.0}}, :boost => 10.0}
  133. @instance.solr_configuration = {:type_field => "type", :primary_key_field => "pk_id", :default_boost => 25.0}
  134. end
  135. should "add a document boost" do
  136. assert_equal 10, @instance.to_solr_doc.boost
  137. end
  138. should "set the solr id" do
  139. assert_equal "SolrInstance:10", @instance.to_solr_doc[:id]
  140. end
  141. should "set the type field" do
  142. assert_equal "SolrInstance", @instance.to_solr_doc[:type]
  143. end
  144. should "set the primary key fields" do
  145. assert_equal("10", @instance.to_solr_doc[:pk_id])
  146. end
  147. should "add the includes if they were configured" do
  148. @instance.configuration.merge! :include => [:author]
  149. @instance.expects(:add_includes)
  150. @instance.to_solr_doc
  151. end
  152. context "with indexed fields" do
  153. should "add fields with type" do
  154. assert_equal "Chunky bacon!", @instance.to_solr_doc[:name_s]
  155. end
  156. should "add the field boost" do
  157. field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "name_s"}
  158. assert_equal 9.0, field.boost
  159. end
  160. should "set the default boost for the field, if none is configured" do
  161. @instance.configuration[:solr_fields][:name][:boost] = nil
  162. field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "name_s"}
  163. assert_equal 25.0, field.boost
  164. end
  165. should "not overwrite the type or id field" do
  166. @instance.configuration[:solr_fields] = {:type => {}, :id => {}}
  167. doc = @instance.to_solr_doc
  168. assert_not_equal "humbug", doc[:type]
  169. assert_not_equal "bogus", doc[:id]
  170. end
  171. should "set the default value if field value is nil" do
  172. @instance.name = nil
  173. @instance.expects(:set_value_if_nil).with('s')
  174. @instance.to_solr_doc
  175. end
  176. should "not include nil values" do
  177. @instance.name = ""
  178. @instance.stubs(:set_value_if_nil).returns ""
  179. assert_nil @instance.to_solr_doc[:name_s]
  180. end
  181. should "escape the contents" do
  182. @instance.name = "<script>malicious()</script>"
  183. assert_equal "&lt;script&gt;malicious()&lt;/script&gt;", @instance.to_solr_doc[:name_s]
  184. end
  185. should "use an alternate field name if specified in options" do
  186. @instance.stubs(:nickname_for_solr).returns('Nick')
  187. @instance.configuration[:solr_fields].merge! :nickname => {:as => :alias}
  188. doc = @instance.to_solr_doc
  189. assert_not_nil @instance.to_solr_doc.fields.find {|f| f.name.to_s == "alias_s"}
  190. assert_nil @instance.to_solr_doc.fields.find {|f| f.name.to_s == "nickname_s"}
  191. end
  192. context "when associations are included" do
  193. setup do
  194. class AssocLabel < String
  195. @@singular = {'people' => 'person'}
  196. def to_s
  197. self
  198. end
  199. def singularize
  200. @@singular[self]
  201. end
  202. end
  203. @assoc = AssocLabel.new('people')
  204. person = {:name => 'Hank Venture', :address => 'Venture Compound'}
  205. @people = [OpenStruct.new(person.merge(:attributes => person))]
  206. @instance.stubs(:people).returns(@people)
  207. @reflection = OpenStruct.new(:macro => :has_many)
  208. @instance.class.stubs(:reflect_on_association).returns(@reflection)
  209. @instance.configuration[:solr_includes] = {@assoc => {}}
  210. @instance.solr_configuration.merge! :default_boost => 35.0
  211. end
  212. should "set the default name for the include, if none is configured" do
  213. @instance.configuration[:solr_includes] = {@assoc => {:type => :text}}
  214. doc = @instance.to_solr_doc
  215. assert_not_nil doc.fields.find {|f| f.name.to_s == "person_s"}
  216. end
  217. should "add the include alias" do
  218. @instance.configuration[:solr_includes] = {@assoc => {:as => :human, :type => :text}}
  219. doc = @instance.to_solr_doc
  220. assert_not_nil doc.fields.find {|f| f.name.to_s == "human_s"}
  221. assert_nil doc.fields.find {|f| f.name.to_s == "person_s"}
  222. end
  223. should "add the include type" do
  224. @instance.configuration[:solr_includes] = {@assoc => {:type => :date}}
  225. @instance.expects(:get_solr_field_type).with(){|v| true}.at_least_once.returns('s')
  226. @instance.expects(:get_solr_field_type).with(:date).once.returns('d')
  227. doc = @instance.to_solr_doc
  228. end
  229. should "set the default boost for the include, if none is configured" do
  230. # @instance.configuration[:solr_includes] = {@assoc => {}}
  231. field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
  232. assert_equal 35.0, field.boost
  233. end
  234. should "add the include boost" do
  235. @instance.configuration[:solr_includes] = {@assoc => {:boost => 10.0}}
  236. field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
  237. assert_equal 10.0, field.boost
  238. end
  239. should "default to a field value with all association attributes" do
  240. # @instance.configuration[:solr_includes] = {@assoc => {}}
  241. field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
  242. @people.first.attributes.each do |attr, value|
  243. assert_match /#{attr}=#{value}/, field.value
  244. end
  245. end
  246. should "use a field value from an association method, if one is configured" do
  247. @instance.configuration[:solr_includes] = {@assoc => {:using => :name}}
  248. field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
  249. assert_equal @people.first.name, field.value
  250. end
  251. should "use a field value from a proc, if one is configured" do
  252. @instance.configuration[:solr_includes] = {@assoc => {:using => lambda{|r| r.name.reverse}}}
  253. field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
  254. assert_equal @people.first.name.reverse, field.value
  255. end
  256. should "join multiple values into a single field unless the :multivalued options is specified" do
  257. @instance.configuration[:solr_includes] = {@assoc => {:multivalued => :true}}
  258. second_person = {:name => 'Dean Venture', :address => 'Venture Compound'}
  259. @people << OpenStruct.new(second_person.merge(:attributes => second_person))
  260. fields = @instance.to_solr_doc.fields.select {|f| f.name.to_s == "person_s"}
  261. assert_equal @people.size, fields.size
  262. end
  263. should "include multiple values separately if the :multivalued options is specified" do
  264. # @instance.configuration[:solr_includes] = {@assoc => {}}
  265. second_person = {:name => 'Dean Venture', :address => 'Venture Compound'}
  266. @people << OpenStruct.new(second_person.merge(:attributes => second_person))
  267. fields = @instance.to_solr_doc.fields.select {|f| f.name.to_s == "person_s"}
  268. assert_not_equal @people.size, fields.size
  269. assert_equal 1, fields.size
  270. end
  271. end
  272. end
  273. end
  274. end
  275. end