tire-erez 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. data/.gitignore +14 -0
  2. data/.travis.yml +32 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +10 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.markdown +775 -0
  7. data/Rakefile +51 -0
  8. data/examples/rails-application-template.rb +263 -0
  9. data/examples/tire-dsl.rb +932 -0
  10. data/lib/tire.rb +59 -0
  11. data/lib/tire/alias.rb +296 -0
  12. data/lib/tire/configuration.rb +38 -0
  13. data/lib/tire/count.rb +85 -0
  14. data/lib/tire/dsl.rb +114 -0
  15. data/lib/tire/http/client.rb +62 -0
  16. data/lib/tire/http/clients/curb.rb +61 -0
  17. data/lib/tire/http/clients/faraday.rb +71 -0
  18. data/lib/tire/http/response.rb +27 -0
  19. data/lib/tire/index.rb +443 -0
  20. data/lib/tire/logger.rb +60 -0
  21. data/lib/tire/model/callbacks.rb +40 -0
  22. data/lib/tire/model/import.rb +27 -0
  23. data/lib/tire/model/indexing.rb +134 -0
  24. data/lib/tire/model/naming.rb +100 -0
  25. data/lib/tire/model/percolate.rb +99 -0
  26. data/lib/tire/model/persistence.rb +72 -0
  27. data/lib/tire/model/persistence/attributes.rb +148 -0
  28. data/lib/tire/model/persistence/finders.rb +54 -0
  29. data/lib/tire/model/persistence/storage.rb +77 -0
  30. data/lib/tire/model/search.rb +322 -0
  31. data/lib/tire/multi_search.rb +263 -0
  32. data/lib/tire/results/collection.rb +156 -0
  33. data/lib/tire/results/item.rb +94 -0
  34. data/lib/tire/results/pagination.rb +68 -0
  35. data/lib/tire/rubyext/hash.rb +8 -0
  36. data/lib/tire/rubyext/ruby_1_8.rb +1 -0
  37. data/lib/tire/rubyext/symbol.rb +11 -0
  38. data/lib/tire/rubyext/uri_escape.rb +74 -0
  39. data/lib/tire/search.rb +211 -0
  40. data/lib/tire/search/facet.rb +81 -0
  41. data/lib/tire/search/filter.rb +28 -0
  42. data/lib/tire/search/highlight.rb +37 -0
  43. data/lib/tire/search/queries/match.rb +40 -0
  44. data/lib/tire/search/query.rb +250 -0
  45. data/lib/tire/search/scan.rb +114 -0
  46. data/lib/tire/search/script_field.rb +23 -0
  47. data/lib/tire/search/sort.rb +25 -0
  48. data/lib/tire/tasks.rb +138 -0
  49. data/lib/tire/utils.rb +17 -0
  50. data/lib/tire/version.rb +18 -0
  51. data/test/fixtures/articles/1.json +1 -0
  52. data/test/fixtures/articles/2.json +1 -0
  53. data/test/fixtures/articles/3.json +1 -0
  54. data/test/fixtures/articles/4.json +1 -0
  55. data/test/fixtures/articles/5.json +1 -0
  56. data/test/integration/active_model_indexing_test.rb +51 -0
  57. data/test/integration/active_model_searchable_test.rb +114 -0
  58. data/test/integration/active_record_searchable_test.rb +620 -0
  59. data/test/integration/boolean_queries_test.rb +43 -0
  60. data/test/integration/boosting_queries_test.rb +32 -0
  61. data/test/integration/bulk_test.rb +86 -0
  62. data/test/integration/count_test.rb +64 -0
  63. data/test/integration/custom_score_queries_test.rb +89 -0
  64. data/test/integration/dis_max_queries_test.rb +68 -0
  65. data/test/integration/dsl_search_test.rb +30 -0
  66. data/test/integration/explanation_test.rb +44 -0
  67. data/test/integration/facets_test.rb +311 -0
  68. data/test/integration/filtered_queries_test.rb +66 -0
  69. data/test/integration/filters_test.rb +75 -0
  70. data/test/integration/fuzzy_queries_test.rb +20 -0
  71. data/test/integration/highlight_test.rb +64 -0
  72. data/test/integration/index_aliases_test.rb +122 -0
  73. data/test/integration/index_mapping_test.rb +43 -0
  74. data/test/integration/index_store_test.rb +112 -0
  75. data/test/integration/index_update_document_test.rb +121 -0
  76. data/test/integration/match_query_test.rb +79 -0
  77. data/test/integration/mongoid_searchable_test.rb +309 -0
  78. data/test/integration/multi_search_test.rb +114 -0
  79. data/test/integration/nested_query_test.rb +135 -0
  80. data/test/integration/percolator_test.rb +111 -0
  81. data/test/integration/persistent_model_test.rb +205 -0
  82. data/test/integration/prefix_query_test.rb +43 -0
  83. data/test/integration/query_return_version_test.rb +70 -0
  84. data/test/integration/query_string_test.rb +52 -0
  85. data/test/integration/range_queries_test.rb +36 -0
  86. data/test/integration/reindex_test.rb +56 -0
  87. data/test/integration/results_test.rb +58 -0
  88. data/test/integration/scan_test.rb +56 -0
  89. data/test/integration/script_fields_test.rb +38 -0
  90. data/test/integration/sort_test.rb +52 -0
  91. data/test/integration/text_query_test.rb +39 -0
  92. data/test/models/active_model_article.rb +31 -0
  93. data/test/models/active_model_article_with_callbacks.rb +49 -0
  94. data/test/models/active_model_article_with_custom_document_type.rb +7 -0
  95. data/test/models/active_model_article_with_custom_index_name.rb +7 -0
  96. data/test/models/active_record_models.rb +131 -0
  97. data/test/models/article.rb +15 -0
  98. data/test/models/mongoid_models.rb +85 -0
  99. data/test/models/persistent_article.rb +11 -0
  100. data/test/models/persistent_article_in_index.rb +16 -0
  101. data/test/models/persistent_article_in_namespace.rb +12 -0
  102. data/test/models/persistent_article_with_casting.rb +28 -0
  103. data/test/models/persistent_article_with_defaults.rb +12 -0
  104. data/test/models/persistent_article_with_percolation.rb +5 -0
  105. data/test/models/persistent_articles_with_custom_index_name.rb +10 -0
  106. data/test/models/supermodel_article.rb +17 -0
  107. data/test/models/validated_model.rb +11 -0
  108. data/test/test_helper.rb +118 -0
  109. data/test/unit/active_model_lint_test.rb +17 -0
  110. data/test/unit/configuration_test.rb +84 -0
  111. data/test/unit/count_test.rb +67 -0
  112. data/test/unit/http_client_test.rb +79 -0
  113. data/test/unit/http_response_test.rb +49 -0
  114. data/test/unit/index_alias_test.rb +335 -0
  115. data/test/unit/index_test.rb +1098 -0
  116. data/test/unit/logger_test.rb +125 -0
  117. data/test/unit/model_callbacks_test.rb +116 -0
  118. data/test/unit/model_import_test.rb +75 -0
  119. data/test/unit/model_initialization_test.rb +31 -0
  120. data/test/unit/model_persistence_test.rb +548 -0
  121. data/test/unit/model_search_test.rb +964 -0
  122. data/test/unit/multi_search_test.rb +304 -0
  123. data/test/unit/results_collection_test.rb +372 -0
  124. data/test/unit/results_item_test.rb +173 -0
  125. data/test/unit/rubyext_test.rb +66 -0
  126. data/test/unit/search_facet_test.rb +186 -0
  127. data/test/unit/search_filter_test.rb +42 -0
  128. data/test/unit/search_highlight_test.rb +46 -0
  129. data/test/unit/search_query_test.rb +419 -0
  130. data/test/unit/search_scan_test.rb +113 -0
  131. data/test/unit/search_script_field_test.rb +26 -0
  132. data/test/unit/search_sort_test.rb +50 -0
  133. data/test/unit/search_test.rb +556 -0
  134. data/test/unit/tire_test.rb +144 -0
  135. data/tire.gemspec +83 -0
  136. metadata +586 -0
@@ -0,0 +1,173 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+ class ResultsItemTest < Test::Unit::TestCase
5
+
6
+ # ActiveModel compatibility tests
7
+ #
8
+ def setup
9
+ super
10
+ begin; Object.send(:remove_const, :Rails); rescue; end
11
+ @model = Results::Item.new :title => 'Test'
12
+ end
13
+ include ActiveModel::Lint::Tests
14
+
15
+ context "Item" do
16
+
17
+ setup do
18
+ @document = Results::Item.new :title => 'Test',
19
+ :author => { :name => 'Kafka' },
20
+ :awards => { :best_fiction => { :year => '1925' } }
21
+ end
22
+
23
+ should "be initialized with a Hash or Hash like object" do
24
+ assert_raise(ArgumentError) { Results::Item.new('FUUUUUUU') }
25
+
26
+ assert_nothing_raised do
27
+ d = Results::Item.new(:id => 1)
28
+ assert_instance_of Results::Item, d
29
+ end
30
+
31
+ assert_nothing_raised do
32
+ class AlmostHash < Hash; end
33
+ d = Results::Item.new(AlmostHash.new(:id => 1))
34
+ end
35
+ end
36
+
37
+ should "have an 'id' method" do
38
+ a = Results::Item.new(:_id => 1)
39
+ b = Results::Item.new(:id => 1)
40
+ assert_equal 1, a.id
41
+ assert_equal 1, b.id
42
+ end
43
+
44
+ should "have a 'type' method" do
45
+ a = Results::Item.new(:_type => 'foo')
46
+ b = Results::Item.new(:type => 'foo')
47
+ assert_equal 'foo', a.type
48
+ assert_equal 'foo', b.type
49
+ end
50
+
51
+ should "respond to :to_indexed_json" do
52
+ assert_respond_to Results::Item.new, :to_indexed_json
53
+ end
54
+
55
+ should "retrieve simple values from underlying hash" do
56
+ assert_equal 'Test', @document[:title]
57
+ end
58
+
59
+ should "retrieve hash values from underlying hash" do
60
+ assert_equal 'Kafka', @document[:author][:name]
61
+ end
62
+
63
+ should "allow to retrieve value by methods" do
64
+ assert_not_nil @document.title
65
+ assert_equal 'Test', @document.title
66
+ end
67
+
68
+ should "implement respond_to? for proxied methods" do
69
+ assert @document.respond_to?(:title)
70
+ assert @document.respond_to?(:title, true)
71
+ end
72
+
73
+ should "return nil for non-existing keys/methods" do
74
+ assert_nothing_raised { @document.whatever }
75
+ assert_nil @document.whatever
76
+ end
77
+
78
+ should "not care about symbols or strings in keys" do
79
+ @document = Results::Item.new 'title' => 'Test'
80
+ assert_not_nil @document.title
81
+ assert_equal 'Test', @document.title
82
+ end
83
+
84
+ should "not care about symbols or strings in composite keys" do
85
+ @document = Results::Item.new :highlight => { 'name.ngrams' => 'abc' }
86
+
87
+ assert_not_nil @document.highlight['name.ngrams']
88
+ assert_equal 'abc', @document.highlight['name.ngrams']
89
+ assert_equal @document.highlight['name.ngrams'], @document.highlight['name.ngrams'.to_sym]
90
+ end
91
+
92
+ should "allow to retrieve values from nested hashes" do
93
+ assert_not_nil @document.author.name
94
+ assert_equal 'Kafka', @document.author.name
95
+ end
96
+
97
+ should "wrap arrays" do
98
+ @document = Results::Item.new :stats => [1, 2, 3]
99
+ assert_equal [1, 2, 3], @document.stats
100
+ end
101
+
102
+ should "wrap hashes in arrays" do
103
+ @document = Results::Item.new :comments => [{:title => 'one'}, {:title => 'two'}]
104
+ assert_equal 2, @document.comments.size
105
+ assert_instance_of Results::Item, @document.comments.first
106
+ assert_equal 'one', @document.comments.first.title
107
+ assert_equal 'two', @document.comments.last.title
108
+ end
109
+
110
+ should "be an Item instance" do
111
+ assert_instance_of Tire::Results::Item, @document
112
+ end
113
+
114
+ should "be convertible to hash" do
115
+ assert_instance_of Hash, @document.to_hash
116
+ assert_instance_of Hash, @document.to_hash[:author]
117
+ assert_instance_of Hash, @document.to_hash[:awards][:best_fiction]
118
+
119
+ assert_equal 'Kafka', @document.to_hash[:author][:name]
120
+ assert_equal '1925', @document.to_hash[:awards][:best_fiction][:year]
121
+ end
122
+
123
+ should "be convertible to JSON" do
124
+ assert_instance_of Hash, @document.as_json
125
+ assert_equal 'Test', @document.as_json(:only => 'title')['title']
126
+ assert_nil @document.as_json(:only => 'title')['author']
127
+ end
128
+
129
+ should "be inspectable" do
130
+ assert_match /<Item .* title|Item .* author/, @document.inspect
131
+ end
132
+
133
+ context "within Rails" do
134
+
135
+ setup do
136
+ module ::Rails
137
+ end
138
+
139
+ class ::FakeRailsModel
140
+ extend ActiveModel::Naming
141
+ include ActiveModel::Conversion
142
+ def self.find(id, options); new; end
143
+ end
144
+
145
+ @document = Results::Item.new :id => 1, :_type => 'fake_rails_model', :title => 'Test'
146
+ end
147
+
148
+ should "be an instance of model, based on _type" do
149
+ assert_equal FakeRailsModel, @document.class
150
+ end
151
+
152
+ should "be inspectable with masquerade" do
153
+ assert_match /<Item \(FakeRailsModel\)/, @document.inspect
154
+ end
155
+
156
+ should "return proper singular and plural forms" do
157
+ assert_equal 'fake_rails_model', ActiveModel::Naming.singular(@document)
158
+ assert_equal 'fake_rails_models', ActiveModel::Naming.plural(@document)
159
+ end
160
+
161
+ should "instantiate itself for deep hashes, not a Ruby class corresponding to type" do
162
+ document = Results::Item.new :_type => 'my_model', :title => 'Test', :author => { :name => 'John' }
163
+
164
+ assert_equal Tire::Results::Item, document.class
165
+ end
166
+
167
+ end
168
+
169
+ end
170
+
171
+ end
172
+
173
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ module Tire
4
+
5
+ class RubyCoreExtensionsTest < Test::Unit::TestCase
6
+
7
+ context "Hash" do
8
+
9
+ context "with no to_json method provided" do
10
+
11
+ setup do
12
+ @hash = { :one => 1}
13
+ # Undefine the `to_json` method...
14
+ ::Hash.class_eval { remove_method(:to_json) rescue nil }
15
+ # ... and reload the extension, so it's added
16
+ load 'tire/rubyext/hash.rb'
17
+ end
18
+
19
+ should "have its own to_json method" do
20
+ assert_respond_to( @hash, :to_json )
21
+ assert_equal '{"one":1}', @hash.to_json
22
+ end
23
+
24
+ should "allow to pass options to to_json for compatibility" do
25
+ assert_nothing_raised do
26
+ assert_equal '{"one":1}', @hash.to_json({})
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ should "have a to_json method from a JSON serialization library" do
33
+ assert_respond_to( {}, :to_json )
34
+ assert_equal '{"one":1}', { :one => 1}.to_json
35
+ end
36
+
37
+ should "have to_indexed_json method doing the same as to_json" do
38
+ [{}, { :foo => 2 }, { :foo => 4, :bar => 6 }, { :foo => [7,8,9] }].each do |h|
39
+ assert_equal MultiJson.decode(h.to_json), MultiJson.decode(h.to_indexed_json)
40
+ end
41
+ end
42
+
43
+ should "properly serialize Time into JSON" do
44
+ json = { :time => Time.mktime(2011, 01, 01, 11, 00).to_json }.to_json
45
+ assert_match /"2011-01-01T11:00:00.*"/, MultiJson.decode(json)['time']
46
+ end
47
+
48
+ end
49
+
50
+ context "Array" do
51
+
52
+ should "encode itself to JSON" do
53
+ assert_equal '["one","two"]', ['one','two'].to_json
54
+ end
55
+
56
+ end
57
+
58
+ context "Ruby Test::Unit" do
59
+ should "actually return true from assert..." do
60
+ assert_equal true, assert(true)
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,186 @@
1
+ require 'test_helper'
2
+
3
+ module Tire::Search
4
+
5
+ class FacetTest < Test::Unit::TestCase
6
+
7
+ context "Facet" do
8
+
9
+ should "be serialized to JSON" do
10
+ assert_respond_to Facet.new('foo'), :to_json
11
+ end
12
+
13
+ context "generally" do
14
+
15
+ should "encode facets with defaults for current query" do
16
+ assert_equal( MultiJson.load({ :foo => { :terms => {:field=>'bar',:size=>10,:all_terms=>false} } }.to_json),
17
+ MultiJson.load(Facet.new('foo').terms(:bar).to_json) )
18
+ end
19
+
20
+ should "encode facets as global" do
21
+ assert_equal( MultiJson.load({ :foo => { :terms => {:field=>'bar',:size=>10,:all_terms=>false}, :global => true } }.to_json),
22
+ MultiJson.load(Facet.new('foo', :global => true).terms(:bar).to_json) )
23
+ end
24
+
25
+ should "pass options to facets" do
26
+ payload = Facet.new('foo', :facet_filter => { :term => { :account_id => 'foo' } }).terms(:bar).to_hash
27
+
28
+ assert_not_nil payload['foo'][:facet_filter]
29
+ assert_equal( { :term => { :account_id => 'foo' } },
30
+ payload['foo'][:facet_filter] )
31
+ end
32
+
33
+ should "encode facet options" do
34
+ assert_equal( MultiJson.load( { :foo => { :terms => {:field=>'bar',:size=>5,:all_terms=>false} } }.to_json ),
35
+ MultiJson.load( Facet.new('foo').terms(:bar, :size => 5).to_json ) )
36
+ end
37
+
38
+ should "encode facets when passed as a block" do
39
+ f = Facet.new('foo') do
40
+ terms :bar
41
+ end
42
+ assert_equal( MultiJson.load({ :foo => { :terms => {:field=>'bar',:size=>10,:all_terms=>false} } }.to_json),
43
+ MultiJson.load(f.to_json) )
44
+ end
45
+
46
+ should "encode facets when passed as a block, using variables from outer scope" do
47
+ def foo; 'bar'; end
48
+
49
+ f = Facet.new('foo') do |facet|
50
+ facet.terms foo, :size => 20
51
+ end
52
+ assert_equal( MultiJson.load({ :foo => { :terms => {:field=>'bar',:size=>20,:all_terms=>false} } }.to_json),
53
+ MultiJson.load(f.to_json) )
54
+ end
55
+
56
+ should "encode facet_filter option with DSL" do
57
+ f = Facet.new('foo'){
58
+ terms :published_on
59
+ facet_filter :terms, :tags => ['ruby']
60
+ }.to_hash
61
+
62
+ assert_equal( { :terms => {:tags => ['ruby'] }}.to_json, f['foo'][:facet_filter].to_json)
63
+ end
64
+
65
+ should "encode multiple facet_filter options with DSL" do
66
+ f = Facet.new('foo'){
67
+ terms :published_on
68
+ facet_filter :and, { :tags => ['ruby'] },
69
+ { :words => 250 }
70
+ }.to_hash
71
+
72
+ assert_equal( { :and => [{:tags => ['ruby']}, {:words => 250 }] }.to_json,
73
+ f['foo'][:facet_filter].to_json )
74
+ end
75
+
76
+ should "allow arbitrary ordering of methods in the DSL block" do
77
+ f = Facet.new('foo') do
78
+ facet_filter :terms, :tags => ['ruby']
79
+ terms :published_on
80
+ end.to_hash
81
+
82
+ assert_equal( { :terms => {:tags => ['ruby'] }}.to_json, f['foo'][:facet_filter].to_json)
83
+ end
84
+
85
+ end
86
+
87
+ context "terms facet" do
88
+
89
+ should "encode the default all_terms option" do
90
+ assert_equal false, Facet.new('foo') { terms :foo }.to_hash['foo'][:terms][:all_terms]
91
+ end
92
+
93
+ should "encode the all_terms option" do
94
+ assert_equal true, Facet.new('foo') { terms :foo, :all_terms => true }.to_hash['foo'][:terms][:all_terms]
95
+ end
96
+
97
+ should "encode custom options" do
98
+ assert_equal( MultiJson.load({ :foo => { :terms => {:field=>'bar',:size=>5,:all_terms=>false,:exclude=>['moo']} } }.to_json),
99
+ MultiJson.load(Facet.new('foo').terms(:bar, :size => 5, :exclude => ['moo']).to_json) )
100
+ end
101
+
102
+ end
103
+
104
+ context "date histogram" do
105
+
106
+ should "encode the JSON with default values" do
107
+ f = Facet.new('date') { date :published_on }
108
+ assert_equal({ :date => { :date_histogram => { :field => 'published_on', :interval => 'day' } } }.to_json, f.to_json)
109
+ end
110
+
111
+ should "encode the JSON with custom interval" do
112
+ f = Facet.new('date') { date :published_on, :interval => 'month' }
113
+ assert_equal({ :date => { :date_histogram => { :field => 'published_on', :interval => 'month' } } }.to_json, f.to_json)
114
+ end
115
+
116
+ should "encode custom options" do
117
+ f = Facet.new('date') { date :published_on, :value_field => 'price' }
118
+ assert_equal( {:date=>{:date_histogram=>{:key_field=>'published_on',:interval=>'day',:value_field=>'price' } } }.to_json,
119
+ f.to_json )
120
+ end
121
+
122
+ end
123
+
124
+ context "range facet" do
125
+ should "encode facet options" do
126
+ f = Facet.new('range') { range :published_on, [{:to => '2010-12-31'}, {:from => '2011-01-01', :to => '2011-05-27'}, {:from => '2011-05-28'}]}
127
+ assert_equal({ :range => { :range => { :field => 'published_on', :ranges => [{:to => '2010-12-31'}, {:from => '2011-01-01', :to => '2011-05-27'}, {:from => '2011-05-28'}] } } }.to_json, f.to_json)
128
+ end
129
+ end
130
+
131
+ context "histogram facet" do
132
+ should "encode facet options with default key" do
133
+ f = Facet.new('histogram') { histogram :age, {:interval => 5} }
134
+ assert_equal({ :histogram => { :histogram => { :field => 'age', :interval => 5 } } }.to_json, f.to_json)
135
+ end
136
+
137
+ should "encode the JSON if define an histogram" do
138
+ f = Facet.new('histogram') { histogram :age, {:histogram => {:key_field => "age", :value_field => "age", :interval => 100}} }
139
+ assert_equal({ :histogram => { :histogram => {:key_field => "age", :value_field => "age", :interval => 100} } }.to_json, f.to_json)
140
+ end
141
+ end
142
+
143
+ context "statistical facet" do
144
+ should "encode facet options" do
145
+ f = Facet.new('statistical') { statistical :words }
146
+ assert_equal({:statistical => {:statistical => {:field => 'words'}}}.to_json, f.to_json)
147
+ end
148
+
149
+ should "encode the JSON if a 'statistical' custom option is defined" do
150
+ f = Facet.new('statistical') { statistical :words, :statistical => {:params => {:factor => 5}} }
151
+ assert_equal({:statistical => {:statistical => {:params => {:factor => 5 }}}}.to_json, f.to_json)
152
+ end
153
+ end
154
+
155
+ context "terms_stats facet" do
156
+ should "should encode facet options" do
157
+ f = Facet.new('terms_stats') { terms_stats :tags, :words }
158
+ assert_equal({:terms_stats => {:terms_stats => {:key_field => 'tags', :value_field => 'words'}}}.to_json, f.to_json)
159
+ end
160
+ end
161
+
162
+ context "query facet" do
163
+ should "encode facet options" do
164
+ f = Facet.new('q_facet') do
165
+ query { string '_exists_:foo' }
166
+ end
167
+ assert_equal({ :q_facet => { :query => { :query_string => { :query => '_exists_:foo' } } } }.to_json, f.to_json)
168
+ end
169
+ end
170
+
171
+ context "filter facet" do
172
+
173
+ should "encode facet options" do
174
+ f = Facet.new('filter_facet') do
175
+ filter :term, :tags => 'ruby'
176
+ end
177
+ assert_equal({ :filter_facet => { :filter => { :term => { :tags => 'ruby' } } } }.to_json, f.to_json)
178
+ end
179
+
180
+ end
181
+
182
+ end
183
+
184
+ end
185
+
186
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ module Tire::Search
4
+
5
+ class FilterTest < Test::Unit::TestCase
6
+
7
+ context "Filter" do
8
+
9
+ should "be serialized to JSON" do
10
+ assert_respond_to Filter.new(:terms, {}), :to_json
11
+ end
12
+
13
+ should "encode simple filter declarations as JSON" do
14
+ assert_equal( { :terms => {} }.to_json,
15
+ Filter.new('terms').to_json )
16
+
17
+ assert_equal( { :terms => { :tags => ['foo'] } }.to_json,
18
+ Filter.new('terms', :tags => ['foo']).to_json )
19
+
20
+ assert_equal( { :range => { :age => { :from => 10, :to => 20 } } }.to_json,
21
+ Filter.new('range', { :age => { :from => 10, :to => 20 } }).to_json )
22
+
23
+ assert_equal( { :geo_distance => { :distance => '12km', :location => [40, -70] } }.to_json,
24
+ Filter.new('geo_distance', { :distance => '12km', :location => [40, -70] }).to_json )
25
+ end
26
+
27
+ should "encode 'or' filter with multiple other filters" do
28
+ # See http://www.elasticsearch.org/guide/reference/query-dsl/or-filter.html
29
+ assert_equal( { :or => [ {:terms => {:tags => ['foo']}}, {:terms => {:tags => ['bar']}} ] }.to_json,
30
+ Filter.new('or', {:terms => {:tags => ['foo']}}, {:terms => {:tags => ['bar']}}).to_json )
31
+ end
32
+
33
+ should "encode 'bool' filter with multiple filters" do
34
+ # http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html
35
+ assert_equal( { :bool => [ {:must => {:terms => {:tags => ['foo']}}}, {:should => {:terms => {:tags => ['bar']}}} ] }.to_json,
36
+ Filter.new('bool', {:must => {:terms => {:tags => ['foo']}}}, { :should => {:terms => {:tags => ['bar']}}}).to_json )
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ end