websolr-rails 0.0.0 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. data/.document +5 -0
  2. data/.gitignore +15 -0
  3. data/CHANGELOG +8 -0
  4. data/LICENSE +19 -0
  5. data/README.rdoc +69 -0
  6. data/Rakefile +112 -0
  7. data/TESTING_THE_PLUGIN +25 -0
  8. data/VERSION +1 -0
  9. data/bin/websolr +10 -0
  10. data/config/solr.yml +15 -0
  11. data/config/solr_environment.rb +22 -0
  12. data/generate_rdoc.sh +13 -0
  13. data/lib/acts_as_solr.rb +65 -0
  14. data/lib/acts_methods.rb +281 -0
  15. data/lib/class_methods.rb +243 -0
  16. data/lib/common_methods.rb +89 -0
  17. data/lib/deprecation.rb +61 -0
  18. data/lib/instance_methods.rb +181 -0
  19. data/lib/lazy_document.rb +18 -0
  20. data/lib/parser_methods.rb +230 -0
  21. data/lib/search_results.rb +72 -0
  22. data/lib/solr.rb +21 -0
  23. data/lib/solr/connection.rb +190 -0
  24. data/lib/solr/document.rb +78 -0
  25. data/lib/solr/exception.rb +13 -0
  26. data/lib/solr/field.rb +39 -0
  27. data/lib/solr/importer.rb +19 -0
  28. data/lib/solr/importer/array_mapper.rb +26 -0
  29. data/lib/solr/importer/delimited_file_source.rb +38 -0
  30. data/lib/solr/importer/hpricot_mapper.rb +27 -0
  31. data/lib/solr/importer/mapper.rb +51 -0
  32. data/lib/solr/importer/solr_source.rb +43 -0
  33. data/lib/solr/importer/xpath_mapper.rb +35 -0
  34. data/lib/solr/indexer.rb +52 -0
  35. data/lib/solr/request.rb +26 -0
  36. data/lib/solr/request/add_document.rb +63 -0
  37. data/lib/solr/request/base.rb +36 -0
  38. data/lib/solr/request/commit.rb +31 -0
  39. data/lib/solr/request/delete.rb +50 -0
  40. data/lib/solr/request/dismax.rb +46 -0
  41. data/lib/solr/request/index_info.rb +22 -0
  42. data/lib/solr/request/modify_document.rb +51 -0
  43. data/lib/solr/request/optimize.rb +21 -0
  44. data/lib/solr/request/ping.rb +36 -0
  45. data/lib/solr/request/select.rb +56 -0
  46. data/lib/solr/request/spellcheck.rb +30 -0
  47. data/lib/solr/request/standard.rb +402 -0
  48. data/lib/solr/request/update.rb +23 -0
  49. data/lib/solr/response.rb +27 -0
  50. data/lib/solr/response/add_document.rb +17 -0
  51. data/lib/solr/response/base.rb +42 -0
  52. data/lib/solr/response/commit.rb +17 -0
  53. data/lib/solr/response/delete.rb +13 -0
  54. data/lib/solr/response/dismax.rb +8 -0
  55. data/lib/solr/response/index_info.rb +26 -0
  56. data/lib/solr/response/modify_document.rb +17 -0
  57. data/lib/solr/response/optimize.rb +14 -0
  58. data/lib/solr/response/ping.rb +28 -0
  59. data/lib/solr/response/ruby.rb +42 -0
  60. data/lib/solr/response/select.rb +17 -0
  61. data/lib/solr/response/spellcheck.rb +20 -0
  62. data/lib/solr/response/standard.rb +64 -0
  63. data/lib/solr/response/xml.rb +42 -0
  64. data/lib/solr/solrtasks.rb +27 -0
  65. data/lib/solr/util.rb +32 -0
  66. data/lib/solr/xml.rb +44 -0
  67. data/lib/solr_fixtures.rb +13 -0
  68. data/lib/tasks/database.rake +18 -0
  69. data/lib/tasks/solr.rake +94 -0
  70. data/lib/tasks/test.rake +7 -0
  71. data/lib/websolr-rails.rb +1 -0
  72. data/lib/websolr.rb +1 -0
  73. data/lib/websolr_controller.rb +233 -0
  74. data/lib/websolr_option_parser.rb +61 -0
  75. data/lib/websolr_rails.rb +1 -0
  76. data/lib/websolr_rails/tasks.rb +4 -0
  77. data/lib/will_paginate_support.rb +12 -0
  78. data/solr/CHANGES.txt +1207 -0
  79. data/solr/LICENSE.txt +712 -0
  80. data/solr/NOTICE.txt +90 -0
  81. data/solr/etc/jetty.xml +205 -0
  82. data/solr/etc/webdefault.xml +379 -0
  83. data/solr/lib/easymock.jar +0 -0
  84. data/solr/lib/jetty-6.1.3.jar +0 -0
  85. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  86. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  87. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  88. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  89. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  90. data/solr/lib/servlet-api-2.4.jar +0 -0
  91. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  92. data/solr/lib/xpp3-1.1.3.4.O.jar +0 -0
  93. data/solr/logs/.empty-dir-for-git +0 -0
  94. data/solr/solr/README.txt +52 -0
  95. data/solr/solr/bin/abc +176 -0
  96. data/solr/solr/bin/abo +176 -0
  97. data/solr/solr/bin/backup +108 -0
  98. data/solr/solr/bin/backupcleaner +142 -0
  99. data/solr/solr/bin/commit +128 -0
  100. data/solr/solr/bin/optimize +129 -0
  101. data/solr/solr/bin/readercycle +129 -0
  102. data/solr/solr/bin/rsyncd-disable +77 -0
  103. data/solr/solr/bin/rsyncd-enable +76 -0
  104. data/solr/solr/bin/rsyncd-start +145 -0
  105. data/solr/solr/bin/rsyncd-stop +105 -0
  106. data/solr/solr/bin/scripts-util +83 -0
  107. data/solr/solr/bin/snapcleaner +148 -0
  108. data/solr/solr/bin/snapinstaller +168 -0
  109. data/solr/solr/bin/snappuller +248 -0
  110. data/solr/solr/bin/snappuller-disable +77 -0
  111. data/solr/solr/bin/snappuller-enable +77 -0
  112. data/solr/solr/bin/snapshooter +109 -0
  113. data/solr/solr/conf/admin-extra.html +31 -0
  114. data/solr/solr/conf/protwords.txt +21 -0
  115. data/solr/solr/conf/schema.xml +126 -0
  116. data/solr/solr/conf/scripts.conf +24 -0
  117. data/solr/solr/conf/solrconfig.xml +458 -0
  118. data/solr/solr/conf/stopwords.txt +57 -0
  119. data/solr/solr/conf/synonyms.txt +31 -0
  120. data/solr/solr/conf/xslt/example.xsl +132 -0
  121. data/solr/solr/conf/xslt/example_atom.xsl +63 -0
  122. data/solr/solr/conf/xslt/example_rss.xsl +62 -0
  123. data/solr/start.jar +0 -0
  124. data/solr/tmp/.empty-dir-for-git +0 -0
  125. data/solr/webapps/solr.war +0 -0
  126. data/test/config/solr.yml +2 -0
  127. data/test/db/connections/mysql/connection.rb +10 -0
  128. data/test/db/connections/sqlite/connection.rb +8 -0
  129. data/test/db/migrate/001_create_books.rb +15 -0
  130. data/test/db/migrate/002_create_movies.rb +12 -0
  131. data/test/db/migrate/003_create_categories.rb +11 -0
  132. data/test/db/migrate/004_create_electronics.rb +16 -0
  133. data/test/db/migrate/005_create_authors.rb +12 -0
  134. data/test/db/migrate/006_create_postings.rb +9 -0
  135. data/test/db/migrate/007_create_posts.rb +13 -0
  136. data/test/db/migrate/008_create_gadgets.rb +11 -0
  137. data/test/fixtures/authors.yml +9 -0
  138. data/test/fixtures/books.yml +13 -0
  139. data/test/fixtures/categories.yml +7 -0
  140. data/test/fixtures/db_definitions/mysql.sql +41 -0
  141. data/test/fixtures/electronics.yml +49 -0
  142. data/test/fixtures/movies.yml +9 -0
  143. data/test/fixtures/postings.yml +10 -0
  144. data/test/functional/acts_as_solr_test.rb +413 -0
  145. data/test/functional/association_indexing_test.rb +37 -0
  146. data/test/functional/faceted_search_test.rb +163 -0
  147. data/test/functional/multi_solr_search_test.rb +51 -0
  148. data/test/models/author.rb +10 -0
  149. data/test/models/book.rb +10 -0
  150. data/test/models/category.rb +8 -0
  151. data/test/models/electronic.rb +21 -0
  152. data/test/models/gadget.rb +9 -0
  153. data/test/models/movie.rb +17 -0
  154. data/test/models/novel.rb +2 -0
  155. data/test/models/post.rb +3 -0
  156. data/test/models/posting.rb +11 -0
  157. data/test/test_helper.rb +51 -0
  158. data/test/unit/acts_methods_shoulda.rb +70 -0
  159. data/test/unit/class_methods_shoulda.rb +88 -0
  160. data/test/unit/common_methods_shoulda.rb +112 -0
  161. data/test/unit/instance_methods_shoulda.rb +326 -0
  162. data/test/unit/lazy_document_shoulda.rb +35 -0
  163. data/test/unit/parser_instance.rb +19 -0
  164. data/test/unit/parser_methods_shoulda.rb +278 -0
  165. data/test/unit/solr_instance.rb +46 -0
  166. data/test/unit/test_helper.rb +14 -0
  167. data/websolr-rails.gemspec +243 -0
  168. metadata +229 -18
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'class_methods'
3
+ require 'search_results'
4
+ require 'active_support'
5
+
6
+ class User
7
+ attr_accessor :name, :id
8
+ def self.find(*args)
9
+ @paul ||= User.new
10
+ @paul.name = "Paul"
11
+ @paul.id = 1
12
+ @paul
13
+ end
14
+
15
+ def self.find_by_id(id)
16
+ find
17
+ end
18
+
19
+ def self.primary_key
20
+ "id"
21
+ end
22
+ end
23
+
24
+ class ClassMethodsTest < Test::Unit::TestCase
25
+ include ActsAsSolr::ClassMethods
26
+
27
+ def solr_configuration
28
+ @solr_configuration ||= {:type_field => "type_t", :primary_key_field => "id"}
29
+ end
30
+
31
+ context "when multi-searching" do
32
+ setup do
33
+ stubs(:name).returns("User")
34
+ end
35
+
36
+ should "include the type field in the query" do
37
+ expects(:parse_query).with("name:paul", {:results_format => :objects}, "AND (type_t:User)")
38
+ multi_solr_search("name:paul")
39
+ end
40
+
41
+ should "add all models in the query" do
42
+ expects(:parse_query).with("name:paul", {:results_format => :objects, :models => ["Movie", "DVD"]}, "AND (type_t:User OR type_t:Movie OR type_t:DVD)")
43
+ multi_solr_search("name:paul", :models => ["Movie", "DVD"])
44
+ end
45
+
46
+ should "return an empty result set if no data was returned" do
47
+ stubs(:parse_query).returns(nil)
48
+ result = multi_solr_search("name:paul")
49
+ assert_equal 0, result.docs.size
50
+ end
51
+
52
+ should "return an empty result set if no results were found" do
53
+ stubs(:parse_query).returns(stub(:total_hits => 0, :hits => []))
54
+ result = multi_solr_search("name:paul")
55
+ assert_equal 0, result.docs.size
56
+ end
57
+
58
+ context "with results" do
59
+ should "find the objects in the database" do
60
+ stubs(:parse_query).returns(stub(:total_hits => 1, :hits => ["score" => 0.12956427, "id" => ["User:1"]]))
61
+ result = multi_solr_search("name:paul")
62
+
63
+ assert_equal(User.find, result.docs.first)
64
+ assert_equal 1, result.docs.size
65
+ end
66
+
67
+ context "when requesting ids" do
68
+ should "return only ids" do
69
+ stubs(:parse_query).returns(stub(:total_hits => 1, :hits => ["score" => 0.12956427, "id" => ["User:1"]]))
70
+ result = multi_solr_search("name:paul", :results_format => :ids)
71
+ assert_equal "User:1", result.docs.first["id"]
72
+ end
73
+ end
74
+
75
+ context "with scores" do
76
+ setup do
77
+ solr_configuration[:primary_key_field] = nil
78
+ end
79
+
80
+ should "add an accessor with the solr score" do
81
+ stubs(:parse_query).returns(stub(:total_hits => 1, :hits => ["score" => 0.12956427, "id" => ["User:1"]]))
82
+ result = multi_solr_search("name:paul", :scores => true)
83
+ assert_equal 0.12956427, result.docs.first.solr_score
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,112 @@
1
+ require 'test_helper'
2
+ require 'common_methods'
3
+
4
+ class CommonMethodsTest < Test::Unit::TestCase
5
+ include ActsAsSolr::CommonMethods
6
+
7
+ class << self
8
+ def primary_key
9
+ "id"
10
+ end
11
+ end
12
+
13
+ def id
14
+ 10
15
+ end
16
+
17
+ context "when determining the field type" do
18
+ setup do
19
+ end
20
+
21
+ should "return i for an integer" do
22
+ assert_equal "i", get_solr_field_type(:integer)
23
+ end
24
+
25
+ should "return f for a float" do
26
+ assert_equal "f", get_solr_field_type(:float)
27
+ end
28
+
29
+ should "return b for a boolean" do
30
+ assert_equal "b", get_solr_field_type(:boolean)
31
+ end
32
+
33
+ should "return s for a string" do
34
+ assert_equal "s", get_solr_field_type(:string)
35
+ end
36
+
37
+ should "return t for a text" do
38
+ assert_equal "t", get_solr_field_type(:text)
39
+ end
40
+
41
+ should "return d for a date" do
42
+ assert_equal "d", get_solr_field_type(:date)
43
+ end
44
+
45
+ should "return ri for a range_integer" do
46
+ assert_equal "ri", get_solr_field_type(:range_integer)
47
+ end
48
+
49
+ should "return rf for a range_float" do
50
+ assert_equal "rf", get_solr_field_type(:range_float)
51
+ end
52
+
53
+ should "return facet for a facet field" do
54
+ assert_equal "facet", get_solr_field_type(:facet)
55
+ end
56
+
57
+ should "return the string if one was given as an argument" do
58
+ assert_equal "string", get_solr_field_type("string")
59
+ end
60
+
61
+ should "raise an error if invalid field type was specified" do
62
+ assert_raise(RuntimeError) {get_solr_field_type(:something)}
63
+ end
64
+
65
+ should "raise an error if argument is not symbol or string" do
66
+ assert_raise(RuntimeError) {get_solr_field_type(123)}
67
+ end
68
+ end
69
+
70
+ context "when determining a default value for a field when it's nil" do
71
+ should "return 0.00 for a float" do
72
+ assert_equal 0.00, set_value_if_nil("f")
73
+ assert_equal 0.00, set_value_if_nil(:float)
74
+ assert_equal 0.00, set_value_if_nil("rf")
75
+ assert_equal 0.00, set_value_if_nil(:range_float)
76
+ end
77
+
78
+ should "return 0 for an integer" do
79
+ assert_equal 0, set_value_if_nil(:integer)
80
+ assert_equal 0, set_value_if_nil(:range_integer)
81
+ assert_equal 0, set_value_if_nil("i")
82
+ assert_equal 0, set_value_if_nil("ri")
83
+ end
84
+
85
+ should "return false for a boolean" do
86
+ assert_equal "false", set_value_if_nil(:boolean)
87
+ assert_equal "false", set_value_if_nil("b")
88
+ end
89
+
90
+ should "return empty string for strings and text" do
91
+ assert_equal "", set_value_if_nil(:string)
92
+ assert_equal "", set_value_if_nil(:text)
93
+ assert_equal "", set_value_if_nil("t")
94
+ assert_equal "", set_value_if_nil("s")
95
+ end
96
+
97
+ should "return an empty string for a date" do
98
+ assert_equal "", set_value_if_nil(:date)
99
+ assert_equal "", set_value_if_nil("d")
100
+ end
101
+
102
+ should "return an empty string for everything else" do
103
+ assert_equal "", set_value_if_nil("something")
104
+ end
105
+ end
106
+
107
+ context "when determining the record id" do
108
+ should "return the primary key value" do
109
+ assert_equal 10, record_id(self)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,326 @@
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
+
12
+ class InstanceMethodsTest < Test::Unit::TestCase
13
+
14
+ context "With a Solr record instance" do
15
+ setup do
16
+ @instance = SolrInstance.new
17
+ end
18
+
19
+ context "when checking whether indexing is disabled" do
20
+
21
+ setup do
22
+ @instance.configuration = {:if => true}
23
+ end
24
+
25
+ should "return true if the specified proc returns true " do
26
+ @instance.configuration[:offline] = proc {|record| true}
27
+ assert @instance.indexing_disabled?
28
+ end
29
+
30
+ should "return false if the specified proc returns false" do
31
+ @instance.configuration[:offline] = proc {|record| false}
32
+ assert !@instance.indexing_disabled?
33
+ end
34
+
35
+ should "return true if no valid offline option was specified" do
36
+ @instance.configuration[:offline] = nil
37
+ @instance.configuration[:if] = proc {true}
38
+ assert !@instance.indexing_disabled?
39
+ end
40
+ end
41
+
42
+ context "when validating the boost" do
43
+ setup do
44
+ @instance.solr_configuration = {:default_boost => 10.0}
45
+ @instance.configuration = {:if => true}
46
+ end
47
+
48
+ should "accept and evaluate a block" do
49
+ @instance.configuration[:boost] = proc {|record| record.boost_rate}
50
+ assert_equal 10.0, @instance.send(:validate_boost, @instance.configuration[:boost])
51
+ end
52
+
53
+ should "accept and return a float" do
54
+ @instance.configuration[:boost] = 9.0
55
+ assert_equal 9.0, @instance.send(:validate_boost, @instance.configuration[:boost])
56
+ end
57
+
58
+ should "return the default float when the specified is negative" do
59
+ @instance.configuration[:boost] = -1.0
60
+ assert_equal 10.0, @instance.send(:validate_boost, @instance.configuration[:boost])
61
+ end
62
+
63
+ should "execute the according method when value is a symbol" do
64
+ @instance.configuration[:boost] = :irate
65
+ assert_equal 8.0, @instance.send(:validate_boost, @instance.configuration[:boost])
66
+ end
67
+
68
+ should "return the default boost when there is no valid boost" do
69
+ @instance.configuration[:boost] = "boost!"
70
+ assert_equal 10.0, @instance.send(:validate_boost, @instance.configuration[:boost])
71
+ end
72
+ end
73
+
74
+ context "when determining the solr document id" do
75
+ should "combine class name and id" do
76
+ assert_equal "SolrInstance:10", @instance.solr_id
77
+ end
78
+ end
79
+
80
+ context "when saving the instance to solr" do
81
+ context "with indexing disabled" do
82
+ setup do
83
+ @instance.configuration = {:fields => [:name], :if => nil}
84
+ end
85
+
86
+ should "just return and do nothing" do
87
+ @instance.expects(:solr_add).never
88
+ @instance.expects(:solr_destroy).never
89
+ assert @instance.solr_save
90
+ end
91
+ end
92
+
93
+ context "with indexing enabled" do
94
+ setup do
95
+ @instance.configuration = {:fields => [:name], :if => "true", :auto_commit => true}
96
+ @instance.stubs(:solr_commit)
97
+ @instance.stubs(:solr_add)
98
+ @instance.stubs(:to_solr_doc).returns("My test document")
99
+ end
100
+
101
+ should "add the solr document" do
102
+ @instance.expects(:solr_add).with("My test document").once
103
+ @instance.solr_save
104
+ end
105
+
106
+ should "commit to solr" do
107
+ @instance.expects(:solr_commit).once
108
+ @instance.solr_save
109
+ end
110
+
111
+ should "not commit if auto_commit is disabled" do
112
+ @instance.configuration.merge!(:auto_commit => false)
113
+ @instance.expects(:solr_commit).never
114
+ @instance.solr_save
115
+ end
116
+
117
+ should "destroy the document if :if clause is false" do
118
+ @instance.configuration.merge!(:if => "false")
119
+ @instance.expects(:solr_destroy).once
120
+ @instance.solr_save
121
+ end
122
+ end
123
+ end
124
+
125
+ context "when destroying an instance in solr" do
126
+ setup do
127
+ @instance.configuration = {:if => true, :auto_commit => true}
128
+ @instance.stubs(:solr_commit)
129
+ @instance.stubs(:solr_delete)
130
+ end
131
+
132
+ should "delete the instance" do
133
+ @instance.expects(:solr_delete).with("SolrInstance:10")
134
+ @instance.solr_destroy
135
+ end
136
+
137
+ should "commit to solr" do
138
+ @instance.expects(:solr_commit)
139
+ @instance.solr_destroy
140
+ end
141
+
142
+ should "not commit if auto_commit is disabled" do
143
+ @instance.configuration.merge!(:auto_commit => false)
144
+ @instance.expects(:solr_commit).never
145
+ @instance.solr_destroy
146
+ end
147
+
148
+ context "with indexing disabled" do
149
+ should "not contact solr" do
150
+ @instance.configuration.merge!(:offline => true, :if => nil)
151
+ @instance.expects(:solr_delete).never
152
+ @instance.solr_destroy
153
+ end
154
+ end
155
+ end
156
+
157
+ context "when converting an instance to a solr document" do
158
+ setup do
159
+ @instance.configuration = {:if => true, :auto_commit => true, :solr_fields => {:name => {:boost => 9.0}}, :boost => 10.0}
160
+ @instance.solr_configuration = {:type_field => "type", :primary_key_field => "pk_id", :default_boost => 25.0}
161
+ end
162
+
163
+ should "add a document boost" do
164
+ assert_equal 10, @instance.to_solr_doc.boost
165
+ end
166
+
167
+ should "set the solr id" do
168
+ assert_equal "SolrInstance:10", @instance.to_solr_doc[:id]
169
+ end
170
+
171
+ should "set the type field" do
172
+ assert_equal "SolrInstance", @instance.to_solr_doc[:type]
173
+ end
174
+
175
+ should "set the primary key fields" do
176
+ assert_equal("10", @instance.to_solr_doc[:pk_id])
177
+ end
178
+
179
+ should "add the includes if they were configured" do
180
+ @instance.configuration.merge! :include => [:author]
181
+ @instance.expects(:add_includes)
182
+ @instance.to_solr_doc
183
+ end
184
+
185
+ context "with indexed fields" do
186
+ should "add fields with type" do
187
+ assert_equal "Chunky bacon!", @instance.to_solr_doc[:name_s]
188
+ end
189
+
190
+ should "add the field boost" do
191
+ field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "name_s"}
192
+ assert_equal 9.0, field.boost
193
+ end
194
+
195
+ should "set the default boost for the field, if none is configured" do
196
+ @instance.configuration[:solr_fields][:name][:boost] = nil
197
+ field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "name_s"}
198
+ assert_equal 25.0, field.boost
199
+ end
200
+
201
+ should "not overwrite the type or id field" do
202
+ @instance.configuration[:solr_fields] = {:type => {}, :id => {}}
203
+ doc = @instance.to_solr_doc
204
+ assert_not_equal "humbug", doc[:type]
205
+ assert_not_equal "bogus", doc[:id]
206
+ end
207
+
208
+ should "set the default value if field value is nil" do
209
+ @instance.name = nil
210
+ @instance.expects(:set_value_if_nil).with('s')
211
+ @instance.to_solr_doc
212
+ end
213
+
214
+ should "not include nil values" do
215
+ @instance.name = ""
216
+ @instance.stubs(:set_value_if_nil).returns ""
217
+ assert_nil @instance.to_solr_doc[:name_s]
218
+ end
219
+
220
+ should "escape the contents" do
221
+ @instance.name = "<script>malicious()</script>"
222
+ assert_equal "&lt;script&gt;malicious()&lt;/script&gt;", @instance.to_solr_doc[:name_s]
223
+ end
224
+
225
+ should "use an alternate field name if specified in options" do
226
+ @instance.stubs(:nickname_for_solr).returns('Nick')
227
+ @instance.configuration[:solr_fields].merge! :nickname => {:as => :alias}
228
+ doc = @instance.to_solr_doc
229
+ assert_not_nil @instance.to_solr_doc.fields.find {|f| f.name.to_s == "alias_s"}
230
+ assert_nil @instance.to_solr_doc.fields.find {|f| f.name.to_s == "nickname_s"}
231
+ end
232
+
233
+ context "when associations are included" do
234
+ setup do
235
+ class AssocLabel < String
236
+ @@singular = {'people' => 'person'}
237
+ def to_s
238
+ self
239
+ end
240
+ def singularize
241
+ @@singular[self]
242
+ end
243
+ end
244
+ @assoc = AssocLabel.new('people')
245
+ person = {:name => 'Hank Venture', :address => 'Venture Compound'}
246
+ @people = [OpenStruct.new(person.merge(:attributes => person))]
247
+ @instance.stubs(:people).returns(@people)
248
+ @reflection = OpenStruct.new(:macro => :has_many)
249
+ @instance.class.stubs(:reflect_on_association).returns(@reflection)
250
+ @instance.configuration[:solr_includes] = {@assoc => {}}
251
+ @instance.solr_configuration.merge! :default_boost => 35.0
252
+ end
253
+
254
+ should "set the default name for the include, if none is configured" do
255
+ @instance.configuration[:solr_includes] = {@assoc => {:type => :text}}
256
+ doc = @instance.to_solr_doc
257
+ assert_not_nil doc.fields.find {|f| f.name.to_s == "person_s"}
258
+ end
259
+
260
+ should "add the include alias" do
261
+ @instance.configuration[:solr_includes] = {@assoc => {:as => :human, :type => :text}}
262
+ doc = @instance.to_solr_doc
263
+ assert_not_nil doc.fields.find {|f| f.name.to_s == "human_s"}
264
+ assert_nil doc.fields.find {|f| f.name.to_s == "person_s"}
265
+ end
266
+
267
+ should "add the include type" do
268
+ @instance.configuration[:solr_includes] = {@assoc => {:type => :date}}
269
+ @instance.expects(:get_solr_field_type).with(){|v| true}.at_least_once.returns('s')
270
+ @instance.expects(:get_solr_field_type).with(:date).once.returns('d')
271
+ doc = @instance.to_solr_doc
272
+ end
273
+
274
+ should "set the default boost for the include, if none is configured" do
275
+ # @instance.configuration[:solr_includes] = {@assoc => {}}
276
+ field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
277
+ assert_equal 35.0, field.boost
278
+ end
279
+
280
+ should "add the include boost" do
281
+ @instance.configuration[:solr_includes] = {@assoc => {:boost => 10.0}}
282
+ field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
283
+ assert_equal 10.0, field.boost
284
+ end
285
+
286
+ should "default to a field value with all association attributes" do
287
+ # @instance.configuration[:solr_includes] = {@assoc => {}}
288
+ field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
289
+ @people.first.attributes.each do |attr, value|
290
+ assert_match /#{attr}=#{value}/, field.value
291
+ end
292
+ end
293
+
294
+ should "use a field value from an association method, if one is configured" do
295
+ @instance.configuration[:solr_includes] = {@assoc => {:using => :name}}
296
+ field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
297
+ assert_equal @people.first.name, field.value
298
+ end
299
+
300
+ should "use a field value from a proc, if one is configured" do
301
+ @instance.configuration[:solr_includes] = {@assoc => {:using => lambda{|r| r.name.reverse}}}
302
+ field = @instance.to_solr_doc.fields.find {|f| f.name.to_s == "person_s"}
303
+ assert_equal @people.first.name.reverse, field.value
304
+ end
305
+
306
+ should "join multiple values into a single field unless the :multivalued options is specified" do
307
+ @instance.configuration[:solr_includes] = {@assoc => {:multivalued => :true}}
308
+ second_person = {:name => 'Dean Venture', :address => 'Venture Compound'}
309
+ @people << OpenStruct.new(second_person.merge(:attributes => second_person))
310
+ fields = @instance.to_solr_doc.fields.select {|f| f.name.to_s == "person_s"}
311
+ assert_equal @people.size, fields.size
312
+ end
313
+
314
+ should "include multiple values separately if the :multivalued options is specified" do
315
+ # @instance.configuration[:solr_includes] = {@assoc => {}}
316
+ second_person = {:name => 'Dean Venture', :address => 'Venture Compound'}
317
+ @people << OpenStruct.new(second_person.merge(:attributes => second_person))
318
+ fields = @instance.to_solr_doc.fields.select {|f| f.name.to_s == "person_s"}
319
+ assert_not_equal @people.size, fields.size
320
+ assert_equal 1, fields.size
321
+ end
322
+ end
323
+ end
324
+ end
325
+ end
326
+ end