picky 3.6.16 → 4.0.0pre1
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.
- data/lib/picky/application.rb +1 -1
- data/lib/picky/backends/backend.rb +2 -0
- data/lib/picky/backends/memory.rb +14 -7
- data/lib/picky/backends/{memory → prepared}/text.rb +10 -4
- data/lib/picky/backends/redis/directly_manipulable.rb +3 -5
- data/lib/picky/backends/redis/list.rb +5 -1
- data/lib/picky/backends/sqlite/basic.rb +4 -2
- data/lib/picky/bundle.rb +6 -7
- data/lib/picky/bundle_indexed.rb +2 -2
- data/lib/picky/bundle_realtime.rb +8 -7
- data/lib/picky/categories.rb +0 -1
- data/lib/picky/categories_indexing.rb +14 -0
- data/lib/picky/category.rb +3 -5
- data/lib/picky/category_indexed.rb +2 -5
- data/lib/picky/category_indexing.rb +28 -16
- data/lib/picky/constants.rb +3 -1
- data/lib/picky/frontend_adapters/rack.rb +2 -2
- data/lib/picky/generators/similarity/phonetic.rb +6 -14
- data/lib/picky/generators/strategy.rb +1 -1
- data/lib/picky/generators/weights/runtime.rb +2 -2
- data/lib/picky/helpers/indexing.rb +20 -0
- data/lib/picky/index.rb +7 -10
- data/lib/picky/index_indexed.rb +1 -8
- data/lib/picky/index_indexing.rb +44 -42
- data/lib/picky/indexers/base.rb +5 -6
- data/lib/picky/indexers/parallel.rb +35 -32
- data/lib/picky/indexers/serial.rb +38 -15
- data/lib/picky/indexes_indexed.rb +0 -7
- data/lib/picky/indexes_indexing.rb +16 -19
- data/lib/picky/loader.rb +6 -4
- data/lib/picky/query/allocation.rb +7 -2
- data/lib/picky/query/combination.rb +1 -1
- data/lib/picky/query/indexes.rb +1 -1
- data/lib/picky/query/indexes_check.rb +12 -14
- data/lib/picky/query/token.rb +33 -15
- data/lib/picky/results/exact_first.rb +53 -0
- data/lib/picky/scheduler.rb +43 -0
- data/lib/picky/search.rb +0 -2
- data/lib/picky/sources/csv.rb +2 -3
- data/lib/picky/sources/db.rb +4 -3
- data/lib/picky/sources/mongo.rb +1 -1
- data/lib/picky/tokenizer.rb +0 -4
- data/lib/picky/wrappers/bundle/location.rb +1 -1
- data/lib/picky.rb +2 -2
- data/lib/tasks/index.rake +13 -14
- data/spec/functional/backends/file_spec.rb +2 -4
- data/spec/functional/backends/memory_spec.rb +2 -2
- data/spec/functional/backends/redis_spec.rb +1 -1
- data/spec/functional/exact_first_spec.rb +24 -4
- data/spec/functional/realtime_spec.rb +7 -3
- data/spec/lib/application_spec.rb +30 -30
- data/spec/lib/backends/backend_spec.rb +25 -27
- data/spec/lib/backends/{memory → prepared}/text_spec.rb +1 -1
- data/spec/lib/category_indexing_spec.rb +1 -1
- data/spec/lib/extensions/symbol_spec.rb +1 -1
- data/spec/lib/generators/similarity/phonetic_spec.rb +46 -0
- data/spec/lib/index_indexed_spec.rb +5 -5
- data/spec/lib/index_indexing_spec.rb +13 -12
- data/spec/lib/index_spec.rb +8 -8
- data/spec/lib/indexers/base_spec.rb +5 -6
- data/spec/lib/indexers/parallel_spec.rb +10 -10
- data/spec/lib/indexes_indexed_spec.rb +1 -7
- data/spec/lib/indexes_indexing_spec.rb +10 -5
- data/spec/lib/query/indexes_check_spec.rb +44 -15
- data/spec/lib/query/indexes_spec.rb +11 -11
- data/spec/lib/query/token_spec.rb +10 -0
- data/spec/lib/{indexed/wrappers → results}/exact_first_spec.rb +18 -21
- data/spec/lib/scheduler_spec.rb +92 -0
- metadata +45 -34
- data/lib/picky/cores.rb +0 -127
- data/lib/picky/tokenizers/location.rb +0 -53
- data/lib/picky/wrappers/category/exact_first.rb +0 -94
- data/spec/lib/cores_spec.rb +0 -185
@@ -10,4 +10,50 @@ describe Picky::Generators::Similarity::Phonetic do
|
|
10
10
|
}.to raise_error("In Picky 2.0+, the Similarity::Phonetic has been renamed to Similarity::DoubleMetaphone. Please use that one. Thanks!")
|
11
11
|
end
|
12
12
|
|
13
|
+
describe 'sort!' do
|
14
|
+
let(:phonetic) { described_class.allocate }
|
15
|
+
it 'sorts correctly' do
|
16
|
+
phonetic.instance_variable_set :@amount, 2
|
17
|
+
|
18
|
+
ary = [:a, :b, :c]
|
19
|
+
phonetic.sort ary, :b
|
20
|
+
ary.should == [:b, :a]
|
21
|
+
end
|
22
|
+
it 'sorts correctly' do
|
23
|
+
phonetic.instance_variable_set :@amount, 2
|
24
|
+
|
25
|
+
ary = [:aaa, :aa, :aaaa]
|
26
|
+
phonetic.sort ary, :aaa
|
27
|
+
ary.should == [:aaa, :aa]
|
28
|
+
end
|
29
|
+
it 'sorts correctly' do
|
30
|
+
phonetic.instance_variable_set :@amount, 3
|
31
|
+
|
32
|
+
ary = [:aaa, :aa, :aaaa]
|
33
|
+
phonetic.sort ary, :aaa
|
34
|
+
ary.should == [:aaa, :aa, :aaaa]
|
35
|
+
end
|
36
|
+
it 'sorts correctly' do
|
37
|
+
phonetic.instance_variable_set :@amount, 3
|
38
|
+
|
39
|
+
ary = [:aaaaa, :aa, :aaaa]
|
40
|
+
phonetic.sort ary, :aaa
|
41
|
+
ary.should == [:aaaa, :aa, :aaaaa]
|
42
|
+
end
|
43
|
+
it 'sorts correctly' do
|
44
|
+
phonetic.instance_variable_set :@amount, 3
|
45
|
+
|
46
|
+
ary = [:aaaaa, :aa]
|
47
|
+
phonetic.sort ary, :aaa
|
48
|
+
ary.should == [:aa, :aaaaa]
|
49
|
+
end
|
50
|
+
it 'sorts correctly' do
|
51
|
+
phonetic.instance_variable_set :@amount, 3
|
52
|
+
|
53
|
+
ary = [:aaa]
|
54
|
+
phonetic.sort ary, :aaa
|
55
|
+
ary.should == [:aaa]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
13
59
|
end
|
@@ -7,14 +7,14 @@ describe Picky::Index do
|
|
7
7
|
@index = described_class.new :some_index_name
|
8
8
|
end
|
9
9
|
|
10
|
-
describe '
|
10
|
+
describe 'category' do
|
11
11
|
it 'adds a new category to the categories' do
|
12
|
-
@index.
|
12
|
+
@index.category :some_category_name
|
13
13
|
|
14
14
|
@index.categories.categories.size.should == 1
|
15
15
|
end
|
16
16
|
it 'returns the new category' do
|
17
|
-
@index.
|
17
|
+
@index.category(:some_category_name).should be_kind_of(Picky::Category)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -24,8 +24,8 @@ describe Picky::Index do
|
|
24
24
|
@categories = stub :categories
|
25
25
|
|
26
26
|
@index = described_class.new :some_name
|
27
|
-
@index.
|
28
|
-
@index.
|
27
|
+
@index.category :some_category_name1
|
28
|
+
@index.category :some_category_name2
|
29
29
|
|
30
30
|
@index.stub! :categories => @categories
|
31
31
|
end
|
@@ -55,16 +55,16 @@ describe Picky::Index do
|
|
55
55
|
context 'in general' do
|
56
56
|
context 'with #each source' do
|
57
57
|
let(:index) do
|
58
|
-
described_class.new :some_name
|
59
|
-
source []
|
60
|
-
end
|
58
|
+
described_class.new :some_name
|
61
59
|
end
|
62
60
|
|
63
61
|
it 'does things in order' do
|
64
|
-
|
65
|
-
index.should_receive(:index_in_parallel).once.with.ordered
|
62
|
+
scheduler = stub :scheduler, :fork? => false, :finish => nil
|
66
63
|
|
67
|
-
index.
|
64
|
+
index.should_receive(:prepare).once.with(scheduler).ordered
|
65
|
+
index.should_receive(:cache).once.with(scheduler).ordered
|
66
|
+
|
67
|
+
index.index scheduler
|
68
68
|
end
|
69
69
|
end
|
70
70
|
context 'with non#each source' do
|
@@ -80,7 +80,8 @@ describe Picky::Index do
|
|
80
80
|
categories = stub :categories
|
81
81
|
index.stub! :categories => categories
|
82
82
|
|
83
|
-
categories.should_receive(:
|
83
|
+
categories.should_receive(:prepare).once
|
84
|
+
categories.should_receive(:cache).once
|
84
85
|
|
85
86
|
index.index
|
86
87
|
end
|
@@ -94,14 +95,14 @@ describe Picky::Index do
|
|
94
95
|
@index = described_class.new :some_name do
|
95
96
|
source the_source
|
96
97
|
end
|
97
|
-
@index.
|
98
|
-
@index.
|
98
|
+
@index.category :some_category_name1
|
99
|
+
@index.category :some_category_name2
|
99
100
|
end
|
100
|
-
describe '
|
101
|
+
describe 'source' do
|
101
102
|
it 'can be set with this method' do
|
102
103
|
source = stub :source, :each => [].each
|
103
104
|
|
104
|
-
@index.
|
105
|
+
@index.source source
|
105
106
|
|
106
107
|
@index.source.should == source
|
107
108
|
end
|
@@ -116,7 +117,7 @@ describe Picky::Index do
|
|
116
117
|
end
|
117
118
|
context 'with categories' do
|
118
119
|
before(:each) do
|
119
|
-
@index.
|
120
|
+
@index.category :some_name, :source => stub(:source)
|
120
121
|
end
|
121
122
|
it 'returns it if found' do
|
122
123
|
@index[:some_name].should_not == nil
|
data/spec/lib/index_spec.rb
CHANGED
@@ -49,35 +49,35 @@ describe Picky::Index do
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
describe '
|
52
|
+
describe 'category' do
|
53
53
|
context 'with block' do
|
54
54
|
it 'returns the category' do
|
55
55
|
expected = nil
|
56
|
-
api.
|
56
|
+
api.category(:some_name){ |category| expected = category }.should == expected
|
57
57
|
end
|
58
58
|
it 'takes a string' do
|
59
|
-
lambda { api.
|
59
|
+
lambda { api.category('some_name'){ |indexing, indexed| } }.should_not raise_error
|
60
60
|
end
|
61
61
|
it 'yields both the indexing category and the indexed category' do
|
62
|
-
api.
|
62
|
+
api.category(:some_name) do |category|
|
63
63
|
category.should be_kind_of(Picky::Category)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
it 'yields the category which has the given name' do
|
67
|
-
api.
|
67
|
+
api.category(:some_name) do |category|
|
68
68
|
category.name.should == :some_name
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
72
72
|
context 'without block' do
|
73
73
|
it 'works' do
|
74
|
-
lambda { api.
|
74
|
+
lambda { api.category(:some_name) }.should_not raise_error
|
75
75
|
end
|
76
76
|
it 'takes a string' do
|
77
|
-
lambda { api.
|
77
|
+
lambda { api.category('some_name') }.should_not raise_error
|
78
78
|
end
|
79
79
|
it 'returns itself' do
|
80
|
-
api.
|
80
|
+
api.category(:some_name).should be_kind_of(Picky::Category)
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
@@ -23,21 +23,21 @@ describe Picky::Indexers::Base do
|
|
23
23
|
indexer.stub! :process
|
24
24
|
|
25
25
|
expect {
|
26
|
-
indexer.
|
26
|
+
indexer.prepare []
|
27
27
|
}.to raise_error("Trying to index without a source for some_index_or_category.")
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
describe '
|
31
|
+
describe 'prepare' do
|
32
32
|
before(:each) do
|
33
33
|
some_index_or_category.should_receive(:source).any_number_of_times.and_return :some_source
|
34
34
|
end
|
35
35
|
it 'processes' do
|
36
36
|
categories = stub :categories, :empty => nil, :cache => nil
|
37
37
|
|
38
|
-
indexer.should_receive(:process).once.with categories
|
38
|
+
indexer.should_receive(:process).once.with categories, anything
|
39
39
|
|
40
|
-
indexer.
|
40
|
+
indexer.prepare categories
|
41
41
|
end
|
42
42
|
it 'calls the right methods on the categories' do
|
43
43
|
indexer.stub! :process
|
@@ -45,9 +45,8 @@ describe Picky::Indexers::Base do
|
|
45
45
|
categories = stub :categories
|
46
46
|
|
47
47
|
categories.should_receive(:empty).once.ordered
|
48
|
-
categories.should_receive(:cache).once.ordered
|
49
48
|
|
50
|
-
indexer.
|
49
|
+
indexer.prepare categories
|
51
50
|
end
|
52
51
|
end
|
53
52
|
|
@@ -5,24 +5,24 @@ describe Picky::Indexers::Parallel do
|
|
5
5
|
before(:each) do
|
6
6
|
@source = stub :source
|
7
7
|
@index = stub :index, :name => :some_index, :source => @source
|
8
|
-
|
8
|
+
|
9
9
|
@categories = stub :categories
|
10
|
-
|
10
|
+
|
11
11
|
@indexer = described_class.new @index
|
12
12
|
@indexer.stub! :timed_exclaim
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
describe 'flush' do
|
16
16
|
it 'flushes to joined cache to the file and clears it' do
|
17
17
|
cache = stub :cache
|
18
18
|
file = stub :file
|
19
|
-
|
20
|
-
cache.should_receive(:join).
|
21
|
-
file.should_receive(:write).
|
22
|
-
cache.should_receive(:clear).
|
23
|
-
|
24
|
-
@indexer.flush
|
19
|
+
|
20
|
+
cache.should_receive(:join).once.and_return :joined
|
21
|
+
file.should_receive(:write).once.with(:joined).and_return :joined
|
22
|
+
cache.should_receive(:clear).once
|
23
|
+
|
24
|
+
@indexer.flush file, cache
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
end
|
@@ -48,14 +48,8 @@ describe Picky::Indexes do
|
|
48
48
|
indexes.index_mapping.should == {}
|
49
49
|
end
|
50
50
|
end
|
51
|
-
describe '
|
51
|
+
describe 'load' do
|
52
52
|
it 'calls load on each in order' do
|
53
|
-
@index1.should_receive(:reload).once.with.ordered
|
54
|
-
@index2.should_receive(:reload).once.with.ordered
|
55
|
-
|
56
|
-
indexes.reload
|
57
|
-
end
|
58
|
-
it 'is aliased' do
|
59
53
|
@index1.should_receive(:load).once.with.ordered
|
60
54
|
@index2.should_receive(:load).once.with.ordered
|
61
55
|
|
@@ -19,12 +19,17 @@ describe Picky::Indexes do
|
|
19
19
|
indexes.register @index1
|
20
20
|
indexes.register @index2
|
21
21
|
end
|
22
|
-
describe '
|
23
|
-
it '
|
24
|
-
|
25
|
-
@index2.should_receive(:index).once.with.ordered
|
22
|
+
describe 'index' do
|
23
|
+
it 'prepares and caches each' do
|
24
|
+
scheduler = stub :scheduler, :fork? => false, :finish => nil
|
26
25
|
|
27
|
-
|
26
|
+
@index1.should_receive(:prepare).once.with(scheduler).ordered
|
27
|
+
@index2.should_receive(:prepare).once.with(scheduler).ordered
|
28
|
+
|
29
|
+
@index1.should_receive(:cache).once.with(scheduler).ordered
|
30
|
+
@index2.should_receive(:cache).once.with(scheduler).ordered
|
31
|
+
|
32
|
+
indexes.index scheduler
|
28
33
|
end
|
29
34
|
end
|
30
35
|
describe 'register' do
|
@@ -3,23 +3,52 @@ require 'spec_helper'
|
|
3
3
|
describe Picky::Query::IndexesCheck do
|
4
4
|
|
5
5
|
describe 'check_backend_types' do
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
backends = [
|
7
|
+
Picky::Backends::Memory.new,
|
8
|
+
Picky::Backends::File.new,
|
9
|
+
Picky::Backends::SQLite.new,
|
10
|
+
Picky::Backends::Redis.new
|
11
|
+
]
|
12
|
+
backends.each do |backend|
|
13
|
+
it 'does not raise on the same type' do
|
14
|
+
index = stub :index, backend: backend
|
15
|
+
described_class.check_backends [index, index]
|
16
|
+
end
|
9
17
|
end
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
18
|
+
|
19
|
+
# Test all combinations
|
20
|
+
#
|
21
|
+
combinations = backends.combination 2
|
22
|
+
combinations.each do |backend1, backend2|
|
23
|
+
let(:index1) { stub :index1, backend: backend1 }
|
24
|
+
let(:index2) { stub :index2, backend: backend2 }
|
25
|
+
it 'raises on multiple types' do
|
26
|
+
expect do
|
27
|
+
described_class.check_backends [index1, index2]
|
28
|
+
end.to raise_error(Picky::Query::DifferentBackendsError)
|
29
|
+
end
|
30
|
+
it 'raises with the right message on multiple types' do
|
31
|
+
expect do
|
32
|
+
described_class.check_backends [index1, index2]
|
33
|
+
end.to raise_error("Currently it isn't possible to mix Indexes with backends #{index1.backend.class} and #{index2.backend.class} in the same Search instance.")
|
34
|
+
end
|
17
35
|
end
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
36
|
+
combinations = backends.combination 3
|
37
|
+
combinations.each do |backend1, backend2, backend3|
|
38
|
+
let(:index1) { stub :index1, backend: backend1 }
|
39
|
+
let(:index2) { stub :index2, backend: backend2 }
|
40
|
+
let(:index3) { stub :index2, backend: backend3 }
|
41
|
+
it 'raises on multiple types' do
|
42
|
+
expect do
|
43
|
+
described_class.check_backends [index1, index2, index3]
|
44
|
+
end.to raise_error(Picky::Query::DifferentBackendsError)
|
45
|
+
end
|
46
|
+
it 'raises with the right message on multiple types' do
|
47
|
+
expect do
|
48
|
+
described_class.check_backends [index1, index2, index3]
|
49
|
+
end.to raise_error("Currently it isn't possible to mix Indexes with backends #{index1.backend.class} and #{index2.backend.class} and #{index3.backend.class} in the same Search instance.")
|
50
|
+
end
|
22
51
|
end
|
23
52
|
end
|
24
|
-
|
53
|
+
|
25
54
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Picky::Query::Indexes do
|
4
|
-
|
4
|
+
|
5
5
|
before(:each) do
|
6
|
-
Picky::Query::IndexesCheck.stub! :
|
6
|
+
Picky::Query::IndexesCheck.stub! :check_backends
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
3.times do |i|
|
10
10
|
n = i + 1
|
11
11
|
name = :"index#{n}"
|
@@ -19,7 +19,7 @@ describe Picky::Query::Indexes do
|
|
19
19
|
indexes.send :initialize, index1, index2, index3
|
20
20
|
indexes
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
describe 'expand_combinations_from' do
|
24
24
|
it 'generates all possible combinations from the given ones' do
|
25
25
|
combinations = [[1,2,3], [:a, :b, :c], [:k, :l]]
|
@@ -47,22 +47,22 @@ describe Picky::Query::Indexes do
|
|
47
47
|
end
|
48
48
|
it 'can handle small combinations' do
|
49
49
|
combinations = [[1], [2], [3]]
|
50
|
-
|
50
|
+
|
51
51
|
indexes.expand_combinations_from(combinations).should == [[1, 2, 3]]
|
52
52
|
end
|
53
53
|
it 'can handle empty combinations' do
|
54
54
|
combinations = [[1,2,3], [:a, :b, :c], []]
|
55
|
-
|
55
|
+
|
56
56
|
indexes.expand_combinations_from(combinations).should == []
|
57
57
|
end
|
58
58
|
it 'can handle empty combinations' do
|
59
59
|
combinations = [[], [:a, :b, :c], []]
|
60
|
-
|
60
|
+
|
61
61
|
indexes.expand_combinations_from(combinations).should == []
|
62
62
|
end
|
63
63
|
it 'can handle totally empty combinations' do
|
64
64
|
combinations = [[], [], []]
|
65
|
-
|
65
|
+
|
66
66
|
indexes.expand_combinations_from(combinations).should == []
|
67
67
|
end
|
68
68
|
it 'is fast in a complicated case' do
|
@@ -86,7 +86,7 @@ describe Picky::Query::Indexes do
|
|
86
86
|
performance_of { indexes.expand_combinations_from(combinations) }.should < 0.00045
|
87
87
|
end
|
88
88
|
end
|
89
|
-
|
89
|
+
|
90
90
|
describe 'prepared_allocations_for' do
|
91
91
|
before(:each) do
|
92
92
|
@allocations = stub :allocations
|
@@ -96,14 +96,14 @@ describe Picky::Query::Indexes do
|
|
96
96
|
@allocations.should_receive(:uniq).once.ordered.with()
|
97
97
|
@allocations.should_receive(:calculate_score).once.ordered.with(:some_weights)
|
98
98
|
@allocations.should_receive(:sort!).once.ordered.with()
|
99
|
-
|
99
|
+
|
100
100
|
indexes.prepared_allocations_for :some_tokens, :some_weights
|
101
101
|
end
|
102
102
|
it 'calls the right method in order' do
|
103
103
|
@allocations.should_receive(:uniq).once.ordered.with()
|
104
104
|
@allocations.should_receive(:calculate_score).once.ordered.with({})
|
105
105
|
@allocations.should_receive(:sort!).once.ordered.with()
|
106
|
-
|
106
|
+
|
107
107
|
indexes.prepared_allocations_for :some_tokens
|
108
108
|
end
|
109
109
|
end
|
@@ -407,6 +407,16 @@ describe Picky::Query::Token do
|
|
407
407
|
|
408
408
|
token.instance_variable_get(:@partial).should be_nil
|
409
409
|
end
|
410
|
+
it 'lets the last one win' do
|
411
|
+
token = described_class.processed 'text"*'
|
412
|
+
|
413
|
+
token.partial?.should == true
|
414
|
+
end
|
415
|
+
it 'lets the last one win' do
|
416
|
+
token = described_class.processed 'text*"'
|
417
|
+
|
418
|
+
token.partial?.should == false
|
419
|
+
end
|
410
420
|
end
|
411
421
|
|
412
422
|
describe "processed" do
|
@@ -1,22 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Picky::
|
3
|
+
describe Picky::Results::ExactFirst do
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
@exact = stub :exact
|
7
7
|
@partial = stub :partial
|
8
8
|
@category = stub :category, :exact => @exact, :partial => @partial
|
9
|
-
|
10
|
-
@wrapper = described_class.new @category
|
9
|
+
@category.extend described_class
|
11
10
|
end
|
12
11
|
|
13
12
|
describe "self.wrap" do
|
14
13
|
context "index" do
|
15
14
|
it "wraps each category" do
|
16
15
|
index = Picky::Index.new :some_index
|
17
|
-
index.
|
16
|
+
index.category :some_category
|
18
17
|
|
19
|
-
|
18
|
+
index.extend described_class
|
20
19
|
|
21
20
|
index.categories.categories.each do |category|
|
22
21
|
category.should be_kind_of(described_class)
|
@@ -24,16 +23,14 @@ describe Picky::Wrappers::Category::ExactFirst do
|
|
24
23
|
end
|
25
24
|
it "returns the index" do
|
26
25
|
index = Picky::Index.new :some_index
|
27
|
-
index.
|
26
|
+
index.category :some_category
|
28
27
|
|
29
|
-
|
28
|
+
index.extend(described_class).should == index
|
30
29
|
end
|
31
30
|
end
|
32
31
|
context "category" do
|
33
|
-
it "wraps
|
34
|
-
category
|
35
|
-
|
36
|
-
described_class.wrap(category).should be_kind_of(described_class)
|
32
|
+
it "wraps the category" do
|
33
|
+
@category.should be_kind_of(described_class)
|
37
34
|
end
|
38
35
|
end
|
39
36
|
end
|
@@ -43,13 +40,13 @@ describe Picky::Wrappers::Category::ExactFirst do
|
|
43
40
|
@exact.stub! :ids => [1,4,5,6]
|
44
41
|
@partial.stub! :ids => [2,3,7]
|
45
42
|
|
46
|
-
@
|
43
|
+
@category.ids(stub(:token, :text => :anything, :partial? => true)).should == [1,4,5,6,2,3,7]
|
47
44
|
end
|
48
45
|
it "uses only the exact ids" do
|
49
46
|
@exact.stub! :ids => [1,4,5,6]
|
50
47
|
@partial.stub! :ids => [2,3,7]
|
51
48
|
|
52
|
-
@
|
49
|
+
@category.ids(stub(:token, :text => :anything, :partial? => false)).should == [1,4,5,6]
|
53
50
|
end
|
54
51
|
end
|
55
52
|
|
@@ -63,10 +60,10 @@ describe Picky::Wrappers::Category::ExactFirst do
|
|
63
60
|
@partial.stub! :weight => 1.23
|
64
61
|
end
|
65
62
|
it "uses the higher weight" do
|
66
|
-
@
|
63
|
+
@category.weight(stub(:token, :text => :anything, :partial? => true)).should == 1.23
|
67
64
|
end
|
68
65
|
it "uses the exact weight" do
|
69
|
-
@
|
66
|
+
@category.weight(stub(:token, :text => :anything, :partial? => false)).should == 0.12
|
70
67
|
end
|
71
68
|
end
|
72
69
|
context "partial without weight" do
|
@@ -74,10 +71,10 @@ describe Picky::Wrappers::Category::ExactFirst do
|
|
74
71
|
@partial.stub! :weight => nil
|
75
72
|
end
|
76
73
|
it "uses the exact weight" do
|
77
|
-
@
|
74
|
+
@category.weight(stub(:token, :text => :anything, :partial? => true)).should == 0.12
|
78
75
|
end
|
79
76
|
it "uses the exact weight" do
|
80
|
-
@
|
77
|
+
@category.weight(stub(:token, :text => :anything, :partial? => false)).should == 0.12
|
81
78
|
end
|
82
79
|
end
|
83
80
|
end
|
@@ -90,10 +87,10 @@ describe Picky::Wrappers::Category::ExactFirst do
|
|
90
87
|
@partial.stub! :weight => 0.12
|
91
88
|
end
|
92
89
|
it "uses the partial weight" do
|
93
|
-
@
|
90
|
+
@category.weight(stub(:token, :text => :anything, :partial? => true)).should == 0.12
|
94
91
|
end
|
95
92
|
it "uses the exact weight" do
|
96
|
-
@
|
93
|
+
@category.weight(stub(:token, :text => :anything, :partial? => false)).should == nil
|
97
94
|
end
|
98
95
|
end
|
99
96
|
context "partial without weight" do
|
@@ -101,10 +98,10 @@ describe Picky::Wrappers::Category::ExactFirst do
|
|
101
98
|
@partial.stub! :weight => nil
|
102
99
|
end
|
103
100
|
it "is nil" do
|
104
|
-
@
|
101
|
+
@category.weight(stub(:token, :text => :anything, :partial? => true)).should == nil
|
105
102
|
end
|
106
103
|
it "is nil" do
|
107
|
-
@
|
104
|
+
@category.weight(stub(:token, :text => :anything, :partial? => false)).should == nil
|
108
105
|
end
|
109
106
|
end
|
110
107
|
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Picky::Scheduler do
|
5
|
+
|
6
|
+
context 'default params' do
|
7
|
+
let(:scheduler) { described_class.new }
|
8
|
+
|
9
|
+
context 'without forking' do
|
10
|
+
before(:each) do
|
11
|
+
scheduler.stub! :fork? => false
|
12
|
+
end
|
13
|
+
context 'non-stubbed forking' do
|
14
|
+
it 'works correctly' do
|
15
|
+
scheduler.schedule { sleep 0.01 }
|
16
|
+
scheduler.schedule { sleep 0.01 }
|
17
|
+
scheduler.schedule { sleep 0.01 }
|
18
|
+
scheduler.schedule { sleep 0.01 }
|
19
|
+
end
|
20
|
+
it 'works correctly' do
|
21
|
+
called = 0
|
22
|
+
|
23
|
+
scheduler.schedule { called += 1 }
|
24
|
+
scheduler.schedule { called += 1 }
|
25
|
+
scheduler.schedule { called += 1 }
|
26
|
+
scheduler.schedule { called += 1 }
|
27
|
+
|
28
|
+
called.should == 4
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'fork?' do
|
34
|
+
context 'OS can fork' do
|
35
|
+
it 'returns false' do
|
36
|
+
scheduler.fork?.should be_false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
context 'OS cannot fork' do
|
40
|
+
before(:each) do
|
41
|
+
Process.stub! :fork => nil
|
42
|
+
end
|
43
|
+
it 'returns false' do
|
44
|
+
scheduler.fork?.should be_false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
context 'default params' do
|
50
|
+
let(:scheduler) { described_class.new parallel: true }
|
51
|
+
|
52
|
+
context 'stubbed forking' do
|
53
|
+
it 'works correctly' do
|
54
|
+
scheduler.scheduler.should_receive(:schedule).exactly(4).times.and_yield
|
55
|
+
|
56
|
+
scheduler.schedule { sleep 0.01 }
|
57
|
+
scheduler.schedule { sleep 0.01 }
|
58
|
+
scheduler.schedule { sleep 0.01 }
|
59
|
+
scheduler.schedule { sleep 0.01 }
|
60
|
+
end
|
61
|
+
it 'works correctly' do
|
62
|
+
scheduler.scheduler.should_receive(:schedule).any_number_of_times.and_yield
|
63
|
+
|
64
|
+
called = 0
|
65
|
+
|
66
|
+
scheduler.schedule { called += 1 }
|
67
|
+
scheduler.schedule { called += 1 }
|
68
|
+
scheduler.schedule { called += 1 }
|
69
|
+
scheduler.schedule { called += 1 }
|
70
|
+
|
71
|
+
called.should == 4
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'fork?' do
|
76
|
+
context 'OS can fork' do
|
77
|
+
it 'returns true' do
|
78
|
+
scheduler.fork?.should == true
|
79
|
+
end
|
80
|
+
end
|
81
|
+
# context 'OS cannot fork' do
|
82
|
+
# before(:each) do
|
83
|
+
# Process.send :undef, :fork
|
84
|
+
# end
|
85
|
+
# it 'returns false' do
|
86
|
+
# scheduler.fork?.should == false
|
87
|
+
# end
|
88
|
+
# end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|