picky 0.0.0 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. data/bin/picky +14 -0
  2. data/lib/bundling.rb +10 -0
  3. data/lib/constants.rb +9 -0
  4. data/lib/deployment.rb +212 -0
  5. data/lib/picky/application.rb +40 -0
  6. data/lib/picky/cacher/convenience.rb +3 -0
  7. data/lib/picky/cacher/generator.rb +17 -0
  8. data/lib/picky/cacher/partial/default.rb +7 -0
  9. data/lib/picky/cacher/partial/none.rb +19 -0
  10. data/lib/picky/cacher/partial/strategy.rb +7 -0
  11. data/lib/picky/cacher/partial/subtoken.rb +91 -0
  12. data/lib/picky/cacher/partial_generator.rb +15 -0
  13. data/lib/picky/cacher/similarity/default.rb +7 -0
  14. data/lib/picky/cacher/similarity/double_levenshtone.rb +73 -0
  15. data/lib/picky/cacher/similarity/none.rb +25 -0
  16. data/lib/picky/cacher/similarity/strategy.rb +7 -0
  17. data/lib/picky/cacher/similarity_generator.rb +15 -0
  18. data/lib/picky/cacher/weights/default.rb +7 -0
  19. data/lib/picky/cacher/weights/logarithmic.rb +39 -0
  20. data/lib/picky/cacher/weights/strategy.rb +7 -0
  21. data/lib/picky/cacher/weights_generator.rb +15 -0
  22. data/lib/picky/configuration/configuration.rb +13 -0
  23. data/lib/picky/configuration/field.rb +68 -0
  24. data/lib/picky/configuration/indexes.rb +60 -0
  25. data/lib/picky/configuration/queries.rb +32 -0
  26. data/lib/picky/configuration/type.rb +52 -0
  27. data/lib/picky/cores.rb +101 -0
  28. data/lib/picky/db/configuration.rb +23 -0
  29. data/lib/picky/ext/ruby19/extconf.rb +7 -0
  30. data/lib/picky/ext/ruby19/performant.c +339 -0
  31. data/lib/picky/extensions/array.rb +45 -0
  32. data/lib/picky/extensions/hash.rb +11 -0
  33. data/lib/picky/extensions/module.rb +15 -0
  34. data/lib/picky/extensions/symbol.rb +18 -0
  35. data/lib/picky/generator.rb +156 -0
  36. data/lib/picky/helpers/cache.rb +23 -0
  37. data/lib/picky/helpers/gc.rb +11 -0
  38. data/lib/picky/helpers/measuring.rb +45 -0
  39. data/lib/picky/helpers/search.rb +27 -0
  40. data/lib/picky/index/bundle.rb +328 -0
  41. data/lib/picky/index/category.rb +109 -0
  42. data/lib/picky/index/combined.rb +38 -0
  43. data/lib/picky/index/type.rb +30 -0
  44. data/lib/picky/indexers/base.rb +77 -0
  45. data/lib/picky/indexers/default.rb +3 -0
  46. data/lib/picky/indexers/field.rb +13 -0
  47. data/lib/picky/indexers/no_source_specified_error.rb +5 -0
  48. data/lib/picky/indexers/solr.rb +60 -0
  49. data/lib/picky/indexes.rb +180 -0
  50. data/lib/picky/initializers/ext.rb +6 -0
  51. data/lib/picky/initializers/mysql.rb +22 -0
  52. data/lib/picky/loader.rb +287 -0
  53. data/lib/picky/loggers/search.rb +19 -0
  54. data/lib/picky/performant/array.rb +23 -0
  55. data/lib/picky/query/allocation.rb +82 -0
  56. data/lib/picky/query/allocations.rb +131 -0
  57. data/lib/picky/query/base.rb +124 -0
  58. data/lib/picky/query/combination.rb +69 -0
  59. data/lib/picky/query/combinations.rb +106 -0
  60. data/lib/picky/query/combinator.rb +92 -0
  61. data/lib/picky/query/full.rb +15 -0
  62. data/lib/picky/query/live.rb +22 -0
  63. data/lib/picky/query/qualifiers.rb +73 -0
  64. data/lib/picky/query/solr.rb +77 -0
  65. data/lib/picky/query/token.rb +215 -0
  66. data/lib/picky/query/tokens.rb +102 -0
  67. data/lib/picky/query/weigher.rb +159 -0
  68. data/lib/picky/query/weights.rb +55 -0
  69. data/lib/picky/rack/harakiri.rb +37 -0
  70. data/lib/picky/results/base.rb +103 -0
  71. data/lib/picky/results/full.rb +19 -0
  72. data/lib/picky/results/live.rb +19 -0
  73. data/lib/picky/routing.rb +165 -0
  74. data/lib/picky/signals.rb +11 -0
  75. data/lib/picky/solr/schema_generator.rb +73 -0
  76. data/lib/picky/sources/base.rb +19 -0
  77. data/lib/picky/sources/csv.rb +30 -0
  78. data/lib/picky/sources/db.rb +77 -0
  79. data/lib/picky/tokenizers/base.rb +130 -0
  80. data/lib/picky/tokenizers/default.rb +3 -0
  81. data/lib/picky/tokenizers/index.rb +73 -0
  82. data/lib/picky/tokenizers/query.rb +70 -0
  83. data/lib/picky/umlaut_substituter.rb +21 -0
  84. data/lib/picky-tasks.rb +6 -0
  85. data/lib/picky.rb +18 -0
  86. data/lib/tasks/application.rake +5 -0
  87. data/lib/tasks/cache.rake +53 -0
  88. data/lib/tasks/framework.rake +4 -0
  89. data/lib/tasks/index.rake +29 -0
  90. data/lib/tasks/server.rake +48 -0
  91. data/lib/tasks/shortcuts.rake +13 -0
  92. data/lib/tasks/solr.rake +36 -0
  93. data/lib/tasks/spec.rake +11 -0
  94. data/lib/tasks/statistics.rake +13 -0
  95. data/lib/tasks/try.rake +29 -0
  96. data/prototype_project/Gemfile +23 -0
  97. data/prototype_project/Rakefile +1 -0
  98. data/prototype_project/app/README +6 -0
  99. data/prototype_project/app/application.rb +50 -0
  100. data/prototype_project/app/application.ru +29 -0
  101. data/prototype_project/app/db.yml +10 -0
  102. data/prototype_project/app/logging.rb +20 -0
  103. data/prototype_project/app/unicorn.ru +10 -0
  104. data/prototype_project/log/README +1 -0
  105. data/prototype_project/script/console +34 -0
  106. data/prototype_project/tmp/README +0 -0
  107. data/prototype_project/tmp/pids/README +0 -0
  108. data/spec/ext/performant_spec.rb +64 -0
  109. data/spec/lib/application_spec.rb +61 -0
  110. data/spec/lib/cacher/partial/subtoken_spec.rb +89 -0
  111. data/spec/lib/cacher/partial_generator_spec.rb +35 -0
  112. data/spec/lib/cacher/similarity/double_levenshtone_spec.rb +60 -0
  113. data/spec/lib/cacher/similarity/none_spec.rb +23 -0
  114. data/spec/lib/cacher/similarity_generator_spec.rb +22 -0
  115. data/spec/lib/cacher/weights/logarithmic_spec.rb +30 -0
  116. data/spec/lib/cacher/weights_generator_spec.rb +21 -0
  117. data/spec/lib/configuration/configuration_spec.rb +38 -0
  118. data/spec/lib/configuration/type_spec.rb +49 -0
  119. data/spec/lib/configuration_spec.rb +8 -0
  120. data/spec/lib/cores_spec.rb +65 -0
  121. data/spec/lib/extensions/array_spec.rb +37 -0
  122. data/spec/lib/extensions/hash_spec.rb +11 -0
  123. data/spec/lib/extensions/module_spec.rb +27 -0
  124. data/spec/lib/extensions/symbol_spec.rb +85 -0
  125. data/spec/lib/generator_spec.rb +135 -0
  126. data/spec/lib/helpers/cache_spec.rb +35 -0
  127. data/spec/lib/helpers/gc_spec.rb +71 -0
  128. data/spec/lib/helpers/measuring_spec.rb +18 -0
  129. data/spec/lib/helpers/search_spec.rb +50 -0
  130. data/spec/lib/index/bundle_partial_generation_speed_spec.rb +47 -0
  131. data/spec/lib/index/bundle_spec.rb +260 -0
  132. data/spec/lib/index/category_spec.rb +203 -0
  133. data/spec/lib/indexers/base_spec.rb +73 -0
  134. data/spec/lib/indexers/field_spec.rb +20 -0
  135. data/spec/lib/loader_spec.rb +48 -0
  136. data/spec/lib/loggers/search_spec.rb +19 -0
  137. data/spec/lib/performant/array_spec.rb +13 -0
  138. data/spec/lib/query/allocation_spec.rb +194 -0
  139. data/spec/lib/query/allocations_spec.rb +336 -0
  140. data/spec/lib/query/base_spec.rb +104 -0
  141. data/spec/lib/query/combination_spec.rb +90 -0
  142. data/spec/lib/query/combinations_spec.rb +83 -0
  143. data/spec/lib/query/combinator_spec.rb +112 -0
  144. data/spec/lib/query/full_spec.rb +22 -0
  145. data/spec/lib/query/live_spec.rb +61 -0
  146. data/spec/lib/query/qualifiers_spec.rb +31 -0
  147. data/spec/lib/query/solr_spec.rb +51 -0
  148. data/spec/lib/query/token_spec.rb +297 -0
  149. data/spec/lib/query/tokens_spec.rb +189 -0
  150. data/spec/lib/query/weights_spec.rb +47 -0
  151. data/spec/lib/results/base_spec.rb +233 -0
  152. data/spec/lib/routing_spec.rb +318 -0
  153. data/spec/lib/solr/schema_generator_spec.rb +42 -0
  154. data/spec/lib/sources/db_spec.rb +91 -0
  155. data/spec/lib/tokenizers/base_spec.rb +61 -0
  156. data/spec/lib/tokenizers/index_spec.rb +51 -0
  157. data/spec/lib/tokenizers/query_spec.rb +105 -0
  158. data/spec/lib/umlaut_substituter_spec.rb +84 -0
  159. data/spec/specific/speed_spec.rb +55 -0
  160. metadata +371 -15
  161. data/README.textile +0 -9
@@ -0,0 +1,194 @@
1
+ require 'spec_helper'
2
+
3
+ describe Query::Allocation do
4
+
5
+ before(:each) do
6
+ @combinations = stub :combinations
7
+ @allocation = Query::Allocation.new @combinations
8
+ end
9
+
10
+ describe 'remove' do
11
+ it 'should delegate to the combinations' do
12
+ @combinations.should_receive(:remove).once.with [:some_identifiers]
13
+
14
+ @allocation.remove [:some_identifiers]
15
+ end
16
+ end
17
+
18
+ describe 'keep' do
19
+ it 'should delegate to the combinations' do
20
+ @combinations.should_receive(:keep).once.with [:some_identifiers]
21
+
22
+ @allocation.keep [:some_identifiers]
23
+ end
24
+ end
25
+
26
+ describe 'process!' do
27
+ context 'no ids' do
28
+ before(:each) do
29
+ @allocation.stub! :calculate_ids => []
30
+ end
31
+ it 'should process right' do
32
+ @allocation.process!(0, 0).should == []
33
+ end
34
+ it 'should process right' do
35
+ @allocation.process!(0, 10).should == []
36
+ end
37
+ it 'should process right' do
38
+ @allocation.process!(20, 0).should == []
39
+ end
40
+ it 'should process right' do
41
+ @allocation.process!(20, 10).should == []
42
+ end
43
+ end
44
+ context 'with ids' do
45
+ before(:each) do
46
+ @allocation.stub! :calculate_ids => [1,2,3,4,5,6,7,8,9,10]
47
+ end
48
+ it 'should process right' do
49
+ @allocation.process!(0, 0).should == []
50
+ end
51
+ it 'should process right' do
52
+ @allocation.process!(0, 10).should == []
53
+ end
54
+ it 'should process right' do
55
+ @allocation.process!(5, 0).should == [1,2,3,4,5]
56
+ end
57
+ it 'should process right' do
58
+ @allocation.process!(5, 5).should == [6,7,8,9,10]
59
+ end
60
+ it 'should process right' do
61
+ @allocation.process!(20, 0).should == [1,2,3,4,5,6,7,8,9,10]
62
+ end
63
+ it 'should process right' do
64
+ @allocation.process!(20, 5).should == [6,7,8,9,10]
65
+ end
66
+ it 'should process right' do
67
+ @allocation.process!(20, 10).should == []
68
+ end
69
+ end
70
+ end
71
+
72
+ describe 'to_result' do
73
+ context 'with few combinations' do
74
+ before(:each) do
75
+ @allocation = Query::Allocation.new stub(:combinations, :ids => [1,2,3], :to_result => [:some_result])
76
+ @allocation.instance_variable_set :@score, :some_score
77
+ @allocation.result_type = :some_result_type
78
+ end
79
+ context 'with ids' do
80
+ it 'should output an array of information' do
81
+ @allocation.process! 20, 0
82
+
83
+ @allocation.to_result.should == [:some_result_type, :some_score, 3, [:some_result], [1, 2, 3]]
84
+ end
85
+ end
86
+ end
87
+ context 'with results' do
88
+ before(:each) do
89
+ combinations = stub :combinations, :ids => [1,2,3], :to_result => [:some_result1, :some_result2]
90
+ @allocation = Query::Allocation.new combinations
91
+ @allocation.instance_variable_set :@score, :some_score
92
+ @allocation.result_type = :some_result_type
93
+ end
94
+ context 'with ids' do
95
+ it 'should output an array of information' do
96
+ @allocation.process! 20, 0
97
+
98
+ @allocation.to_result.should == [:some_result_type, :some_score, 3, [:some_result1, :some_result2], [1, 2, 3]]
99
+ end
100
+ end
101
+ end
102
+ context 'without results' do
103
+ before(:each) do
104
+ @allocation = Query::Allocation.new stub(:combinations, :ids => [], :to_result => [])
105
+ @allocation.instance_variable_set :@score, :some_score
106
+ end
107
+ it 'should return nil' do
108
+ @allocation.process! 20, 0
109
+
110
+ @allocation.to_result.should == nil
111
+ end
112
+ end
113
+ end
114
+
115
+ describe 'to_json' do
116
+ before(:each) do
117
+ @allocation = Query::Allocation.new stub(:combination, :ids => [1,2,3,4,5,6,7], :to_result => [:some_result1, :some_result2])
118
+ @allocation.instance_variable_set :@score, :some_score
119
+ @allocation.result_type = :some_result_type
120
+ end
121
+ it 'should output the correct json string' do
122
+ @allocation.process! 20, 0
123
+
124
+ @allocation.to_json.should == '["some_result_type","some_score",7,["some_result1","some_result2"],[1,2,3,4,5,6,7]]'
125
+ end
126
+ end
127
+
128
+ describe "calculate_score" do
129
+ it 'should delegate to the combinations' do
130
+ @combinations.should_receive(:calculate_score).once.with :some_weights
131
+
132
+ @allocation.calculate_score :some_weights
133
+ end
134
+ end
135
+
136
+ describe "<=>" do
137
+ it "should sort higher first" do
138
+ first = Query::Allocation.new []
139
+ first.instance_variable_set :@score, 20
140
+ second = Query::Allocation.new []
141
+ second.instance_variable_set :@score, 10
142
+
143
+ first.<=>(second).should == -1
144
+ end
145
+ end
146
+
147
+ describe "sort!" do
148
+ it "should sort correctly" do
149
+ first = Query::Allocation.new :whatever
150
+ first.instance_variable_set :@score, 20
151
+ second = Query::Allocation.new :whatever
152
+ second.instance_variable_set :@score, 10
153
+ third = Query::Allocation.new :whatever
154
+ third.instance_variable_set :@score, 5
155
+
156
+ allocations = [second, third, first]
157
+
158
+ allocations.sort!.should == [first, second, third]
159
+ end
160
+ end
161
+
162
+ describe "process!" do
163
+ before(:each) do
164
+ @amount = stub :amount
165
+ @offset = stub :offset
166
+ @ids = stub :ids, :size => :some_original_size, :slice! => :some_sliced_ids
167
+ @allocation.stub! :calculate_ids => @ids
168
+ end
169
+ it 'should calculate_ids' do
170
+ @allocation.should_receive(:calculate_ids).once.with.and_return @ids
171
+
172
+ @allocation.process! @amount, @offset
173
+ end
174
+ it 'should get the original ids count' do
175
+ @allocation.process! @amount, @offset
176
+
177
+ @allocation.count.should == :some_original_size
178
+ end
179
+ it 'should slice! the ids down' do
180
+ @ids.should_receive(:slice!).once.with @offset, @amount
181
+
182
+ @allocation.process! @amount, @offset
183
+ end
184
+ it 'should return the sliced ids' do
185
+ @allocation.process!(@amount, @offset).should == :some_sliced_ids
186
+ end
187
+ it 'should assign the sliced ids correctly' do
188
+ @allocation.process! @amount, @offset
189
+
190
+ @allocation.ids.should == :some_sliced_ids
191
+ end
192
+ end
193
+
194
+ end
@@ -0,0 +1,336 @@
1
+ require 'spec_helper'
2
+
3
+ describe Query::Allocations do
4
+
5
+ describe 'reduce_to' do
6
+ before(:each) do
7
+ @allocation1 = stub :allocation1
8
+ @allocation2 = stub :allocation2
9
+ @allocation3 = stub :allocation3
10
+ @allocations = Query::Allocations.new [@allocation1, @allocation2, @allocation3]
11
+ end
12
+ it 'should do nothing' do
13
+ @allocations.reduce_to 2
14
+
15
+ @allocations.to_a.should == [@allocation1, @allocation2]
16
+ end
17
+ end
18
+
19
+ describe 'remove' do
20
+ before(:each) do
21
+ @allocation1 = stub :allocation1
22
+ @allocation2 = stub :allocation2
23
+ @allocation3 = stub :allocation3
24
+ @allocations = Query::Allocations.new [@allocation1, @allocation2, @allocation3]
25
+ end
26
+ context 'identifiers empty' do
27
+ it 'should do nothing' do
28
+ @allocation1.should_receive(:remove).never
29
+ @allocation2.should_receive(:remove).never
30
+
31
+ @allocations.remove
32
+ end
33
+ end
34
+ context 'identifiers not empty' do
35
+ it 'should remove each' do
36
+ @allocation1.should_receive(:remove).once.with :some_identifier
37
+ @allocation2.should_receive(:remove).once.with :some_identifier
38
+ @allocation3.should_receive(:remove).once.with :some_identifier
39
+
40
+ @allocations.remove :some_identifier
41
+ end
42
+ end
43
+ end
44
+
45
+ describe 'keep' do
46
+ before(:each) do
47
+ @allocation1 = stub :allocation1
48
+ @allocation2 = stub :allocation2
49
+ @allocation3 = stub :allocation3
50
+ @allocations = Query::Allocations.new [@allocation1, @allocation2, @allocation3]
51
+ end
52
+ context 'identifiers empty' do
53
+ it 'should do nothing' do
54
+ @allocation1.should_receive(:keep).never
55
+ @allocation2.should_receive(:keep).never
56
+
57
+ @allocations.keep
58
+ end
59
+ end
60
+ context 'identifiers not empty' do
61
+ it 'should filter each' do
62
+ @allocation1.should_receive(:keep).once.with :some_identifier
63
+ @allocation2.should_receive(:keep).once.with :some_identifier
64
+ @allocation3.should_receive(:keep).once.with :some_identifier
65
+
66
+ @allocations.keep :some_identifier
67
+ end
68
+ end
69
+ end
70
+
71
+ describe 'ids' do
72
+ before(:each) do
73
+ @allocation1 = stub :allocation1, :ids => [1, 2, 3, 4]
74
+ @allocation2 = stub :allocation2, :ids => [5, 6, 7]
75
+ @allocation3 = stub :allocation3, :ids => [8, 9]
76
+ @allocations = Query::Allocations.new [@allocation1, @allocation2, @allocation3]
77
+ end
78
+ it 'should return the right amount of ids' do
79
+ @allocations.ids(0).should == []
80
+ end
81
+ it 'should return the right amount of ids' do
82
+ @allocations.ids(6).should == [1,2,3,4,5,6]
83
+ end
84
+ it 'should return the right amount of ids' do
85
+ @allocations.ids.should == [1,2,3,4,5,6,7,8,9]
86
+ end
87
+ end
88
+
89
+ describe 'process!' do
90
+ before(:each) do
91
+ @allocation1 = stub :allocation1, :process! => [], :count => 4 #, ids: [1, 2, 3, 4]
92
+ @allocation2 = stub :allocation2, :process! => [], :count => 3 #, ids: [5, 6, 7]
93
+ @allocation3 = stub :allocation3, :process! => [], :count => 2 #, ids: [8, 9]
94
+ @allocations = Query::Allocations.new [@allocation1, @allocation2, @allocation3]
95
+ end
96
+ describe 'amount spanning 3 allocations' do
97
+ before(:each) do
98
+ @amount = 6
99
+ @offset = 2
100
+ end
101
+ it 'should call the process! method right' do
102
+ @allocation1.should_receive(:process!).once.with(6,2).and_return [3,4]
103
+ @allocation2.should_receive(:process!).once.with(4,0).and_return [5,6,7]
104
+ @allocation3.should_receive(:process!).once.with(1,0).and_return [8]
105
+
106
+ @allocations.process! @amount, @offset
107
+ end
108
+ end
109
+ describe 'large offset' do
110
+ before(:each) do
111
+ @amount = 10
112
+ @offset = 8
113
+ end
114
+ it 'should call the process! method right' do
115
+ @allocation1.should_receive(:process!).once.with(10,8).and_return []
116
+ @allocation2.should_receive(:process!).once.with(10,4).and_return []
117
+ @allocation3.should_receive(:process!).once.with(10,1).and_return [9]
118
+
119
+ @allocations.process! @amount, @offset
120
+ end
121
+ end
122
+ context 'amount 0' do
123
+ before(:each) do
124
+ @amount = 0
125
+ end
126
+ it 'should return an empty array always' do
127
+ @allocation1.should_receive(:process!).once.with(0,0).and_return []
128
+ @allocation2.should_receive(:process!).once.with(0,0).and_return []
129
+ @allocation3.should_receive(:process!).once.with(0,0).and_return []
130
+
131
+ @allocations.process! @amount
132
+ end
133
+ end
134
+ context 'amount > 0' do
135
+ before(:each) do
136
+ @amount = 3
137
+ end
138
+ context 'offset 0' do
139
+ before(:each) do
140
+ @offset = 0
141
+ end
142
+ it 'should return certain ids' do
143
+ @allocation1.should_receive(:process!).once.with(3,0).and_return [1,2,3]
144
+ @allocation2.should_receive(:process!).once.with(0,0)
145
+
146
+ @allocations.process! @amount, @offset
147
+ end
148
+ end
149
+ context 'offset 3' do
150
+ before(:each) do
151
+ @offset = 3
152
+ end
153
+ it 'should return certain ids' do
154
+ @allocation1.should_receive(:process!).once.with(3,3).and_return [4]
155
+ @allocation2.should_receive(:process!).once.with(2,0).and_return [5,6]
156
+
157
+ @allocations.process! @amount, @offset
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ describe 'random_ids' do
164
+ context 'enough ids' do
165
+ before(:each) do
166
+ @allocation1 = stub :allocation1, :ids => [1, 2, 3]
167
+ @allocation2 = stub :allocation1, :ids => [4, 5, 6, 7]
168
+ @allocations = Query::Allocations.new [@allocation1, @allocation2]
169
+ end
170
+ it 'should return one random id from the first allocations by default' do
171
+ @allocations.random_ids.size.should == 1
172
+ end
173
+ it 'should return multiple random ids from the first allocation' do
174
+ @allocations.random_ids(5).size.should == 3
175
+ end
176
+ it "should return one random id from the first allocation's ids" do
177
+ (1..7).to_a.should include(@allocations.random_ids.first)
178
+ end
179
+ it 'should not contain the same id twice' do
180
+ 100.times do
181
+ @allocations.random_ids(2).uniq.size.should_not == 1
182
+ end
183
+ end
184
+ end
185
+ context 'just one id' do
186
+ before(:each) do
187
+ @allocation1 = stub :allocation1, :ids => [1]
188
+ @allocation2 = stub :allocation1, :ids => []
189
+ @allocations = Query::Allocations.new [@allocation1, @allocation2]
190
+ end
191
+ it 'should return one random id from its allocations by default' do
192
+ @allocations.random_ids.size.should == 1
193
+ end
194
+ it 'should return multiple random ids from its allocations' do
195
+ @allocations.random_ids(5).size.should == 1
196
+ end
197
+ it 'should return one random id from the allocations ids' do
198
+ @allocations.random_ids.should == [1]
199
+ end
200
+ end
201
+ context 'no id' do
202
+ before(:each) do
203
+ @allocation1 = stub :allocation1, :ids => []
204
+ @allocation2 = stub :allocation1, :ids => []
205
+ @allocations = Query::Allocations.new [@allocation1, @allocation2]
206
+ end
207
+ it 'should return one random id from its allocations by default' do
208
+ @allocations.random_ids.size.should == 0
209
+ end
210
+ it 'should return no random id from its allocations' do
211
+ @allocations.random_ids(5).size.should == 0
212
+ end
213
+ it 'should return no random id from the allocations ids' do
214
+ @allocations.random_ids.should == []
215
+ end
216
+ end
217
+ end
218
+
219
+ describe 'to_result' do
220
+ context 'all allocations have results' do
221
+ before(:each) do
222
+ @allocation = stub :allocation
223
+ @allocations = Query::Allocations.new [@allocation, @allocation, @allocation]
224
+ end
225
+ it 'should delegate to each allocation with no params' do
226
+ @allocation.should_receive(:to_result).exactly(3).times.with
227
+
228
+ @allocations.to_result
229
+ end
230
+ end
231
+ context 'one allocation has no results' do
232
+ before(:each) do
233
+ @allocation = stub :allocation, :to_result => :some_result
234
+ @no_result_allocation = stub :no_results, :to_result => nil
235
+ @allocations = Query::Allocations.new [@allocation, @no_result_allocation, @allocation]
236
+ end
237
+ it 'should delegate to each allocation with the same params' do
238
+ @allocations.to_result.should == [:some_result, :some_result]
239
+ end
240
+ end
241
+ end
242
+
243
+ describe 'total' do
244
+ context 'with allocations' do
245
+ before(:each) do
246
+ @allocations = Query::Allocations.new [
247
+ stub(:allocation, :process! => (1..10).to_a, :count => 10),
248
+ stub(:allocation, :process! => (1..80).to_a, :count => 80),
249
+ stub(:allocation, :process! => (1..20).to_a, :count => 20)
250
+ ]
251
+ end
252
+ it 'should traverse the allocations and sum the counts' do
253
+ @allocations.process! 20, 0
254
+
255
+ @allocations.total.should == 110
256
+ end
257
+ end
258
+ context 'without allocations' do
259
+ before(:each) do
260
+ @allocations = Query::Allocations.new []
261
+ end
262
+ it 'should be 0' do
263
+ @allocations.process! 20, 0
264
+
265
+ @allocations.total.should == 0
266
+ end
267
+ end
268
+ end
269
+
270
+ describe "each" do
271
+ before(:each) do
272
+ @internal_allocations = mock :internal_allocations
273
+ @allocations = Query::Allocations.new @internal_allocations
274
+ end
275
+ it "should delegate to the internal allocations" do
276
+ stub_proc = lambda {}
277
+ @internal_allocations.should_receive(:each).once.with &stub_proc
278
+
279
+ @allocations.each &stub_proc
280
+ end
281
+ end
282
+ describe "inject" do
283
+ before(:each) do
284
+ @internal_allocations = mock :internal_allocations
285
+ @allocations = Query::Allocations.new @internal_allocations
286
+ end
287
+ it "should delegate to the internal allocations" do
288
+ stub_proc = lambda {}
289
+ @internal_allocations.should_receive(:inject).once.with &stub_proc
290
+
291
+ @allocations.inject &stub_proc
292
+ end
293
+ end
294
+ describe "empty?" do
295
+ before(:each) do
296
+ @internal_allocations = mock :internal_allocations
297
+ @allocations = Query::Allocations.new @internal_allocations
298
+ end
299
+ it "should delegate to the internal allocations array" do
300
+ @internal_allocations.should_receive(:empty?).once
301
+ @allocations.empty?
302
+ end
303
+ end
304
+
305
+ describe "to_s" do
306
+ before(:each) do
307
+ @internal_allocations = mock :internal_allocations
308
+ @allocations = Query::Allocations.new @internal_allocations
309
+ end
310
+ it "should delegate to the internal allocations array" do
311
+ @internal_allocations.should_receive(:inspect).once
312
+ @allocations.to_s
313
+ end
314
+ end
315
+
316
+ describe "process!" do
317
+ context 'some allocations' do
318
+ before(:each) do
319
+ @allocations = Query::Allocations.new [
320
+ stub(:allocation, :process! => (1..10).to_a, :count => 10),
321
+ stub(:allocation, :process! => (1..80).to_a, :count => 80),
322
+ stub(:allocation, :process! => (1..20).to_a, :count => 20)
323
+ ]
324
+ end
325
+ it 'should calculate the right total' do
326
+ @allocations.process! 20, 0
327
+
328
+ @allocations.total.should == 110
329
+ end
330
+ it 'should be fast' do
331
+ Benchmark.realtime { @allocations.process!(20, 0) }.should <= 0.0001
332
+ end
333
+ end
334
+ end
335
+
336
+ end
@@ -0,0 +1,104 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'Query::Base' do
5
+
6
+ describe 'reduce' do
7
+ context 'real' do
8
+ before(:each) do
9
+ @allocations = stub :allocations
10
+ @query = Query::Full.new
11
+ end
12
+ context 'reduce_to_amount not set' do
13
+ it 'should not call anything on the allocations' do
14
+ @allocations.should_receive(:reduce_to).never
15
+
16
+ @query.reduce @allocations
17
+ end
18
+ end
19
+ context 'reduce_to_amount set' do
20
+ before(:each) do
21
+ @query.reduce_to_amount = :some_amount
22
+ end
23
+ it 'should call reduce_to on the allocations' do
24
+ @allocations.should_receive(:reduce_to).once.with :some_amount
25
+
26
+ @query.reduce @allocations
27
+ end
28
+ end
29
+ end
30
+ context 'stubbed' do
31
+ before(:each) do
32
+ @allocations = stub :allocations
33
+ @query = Query::Full.new
34
+ end
35
+ context 'reduce_to_amount not set' do
36
+ it 'should not call anything on the allocations' do
37
+ @allocations.should_receive(:reduce_to).never
38
+
39
+ @query.reduce @allocations
40
+ end
41
+ end
42
+ context 'reduce_to_amount set' do
43
+ before(:each) do
44
+ @query.stub! :reduce_to_amount => :some_amount
45
+ end
46
+ it 'should call reduce_to on the allocations' do
47
+ @allocations.should_receive(:reduce_to).once.with :some_amount
48
+
49
+ @query.reduce @allocations
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ describe 'initializer' do
56
+ context 'with tokenizer' do
57
+ before(:each) do
58
+ @tokenizer = stub :tokenizer, :tokenize => :some_tokenized_text
59
+ @query = Query::Full.new 'some query', :some_index, :tokenizer => @tokenizer
60
+ end
61
+ it 'should tokenize using the tokenizer' do
62
+ @query.tokenized('some text').should == :some_tokenized_text
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "results_from" do
68
+ describe 'Full' do
69
+ before(:each) do
70
+ @query = Query::Full.new 'some query', :some_index
71
+ end
72
+ it "should work" do
73
+ allocations = stub :allocations, :process! => true
74
+
75
+ @query.results_from allocations
76
+ end
77
+ end
78
+ describe 'Live' do
79
+ before(:each) do
80
+ @query = Query::Live.new 'some query', :some_index
81
+ end
82
+ it "should work" do
83
+ allocations = stub :allocations, :process! => true
84
+
85
+ @query.results_from allocations
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "sorted_allocations" do
91
+ before(:each) do
92
+ @index_class = stub :index_class
93
+ @query = Query::Base.new @index_class
94
+ end
95
+ it "should generate the right kind of allocations" do
96
+ tokens = @query.tokenized 'some query'
97
+
98
+ @index_class.stub! :possible_combinations => []
99
+
100
+ @query.sorted_allocations(tokens).should be_kind_of(Query::Allocations)
101
+ end
102
+ end
103
+
104
+ end