honkster-acts_as_solr 0.1.0

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