/
context_spec.rb
126 lines (95 loc) · 3.56 KB
/
context_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'spec_helper'
describe ThinkingSphinx::Context do
let(:ts_context) { ThinkingSphinx::Context.new }
describe '#prepare' do
let(:config) { ThinkingSphinx::Configuration.instance }
let(:file_name) { 'a.rb' }
let(:model_name_lower) { 'a' }
let(:class_name) { 'A' }
before :each do
config.model_directories = ['']
file_name.stub!(:gsub).and_return(model_name_lower)
model_name_lower.stub!(:camelize).and_return(class_name)
Dir.stub(:[]).and_return([file_name])
end
it "should load the files by guessing the file name" do
class_name.should_receive(:constantize).and_return(true)
ts_context.prepare
end
it "should not raise errors if the model name is nil" do
file_name.stub!(:gsub).and_return(nil)
lambda {
ts_context.prepare
}.should_not raise_error
end
it "should report name errors but not raise them" do
class_name.stub(:constantize).and_raise(NameError)
STDERR.stub!(:puts => '')
STDERR.should_receive(:puts).with('Warning: Error loading a.rb:')
lambda {
ts_context.prepare
}.should_not raise_error
end
it "should retry if the first load fails and contains a directory" do
model_name_lower.should_receive(:gsub!).twice.and_return(true, nil)
class_name.stub(:constantize).and_raise(LoadError)
lambda {
ts_context.prepare
}.should_not raise_error
end
it "should catch database errors with a warning" do
class_name.should_receive(:constantize).and_raise(Mysql2::Error)
STDERR.stub!(:puts => '')
STDERR.should_receive(:puts).with('Warning: Error loading a.rb:')
lambda {
ts_context.prepare
}.should_not raise_error
end unless RUBY_PLATFORM == 'java'
it "should not load models if they're explicitly set in the configuration" do
config.indexed_models = ['Alpha', 'Beta']
ts_context.prepare
ts_context.indexed_models.should == ['Alpha', 'Beta']
end
end
describe '#define_indexes' do
it "should call define_indexes on all known indexed models" do
ts_context.stub!(:indexed_models => ['Alpha', 'Beta'])
Alpha.should_receive(:define_indexes)
Beta.should_receive(:define_indexes)
ts_context.define_indexes
end
end
describe '#add_indexed_model' do
before :each do
ts_context.indexed_models.clear
end
it "should add the model to the collection" do
ts_context.add_indexed_model 'Alpha'
ts_context.indexed_models.should == ['Alpha']
end
it "should not duplicate models in the collection" do
ts_context.add_indexed_model 'Alpha'
ts_context.add_indexed_model 'Alpha'
ts_context.indexed_models.should == ['Alpha']
end
it "should keep the collection in alphabetical order" do
ts_context.add_indexed_model 'Beta'
ts_context.add_indexed_model 'Alpha'
ts_context.indexed_models.should == ['Alpha', 'Beta']
end
it "should translate classes to their names" do
ts_context.add_indexed_model Alpha
ts_context.indexed_models.should == ['Alpha']
end
end
describe '#superclass_indexed_models' do
it "should return indexed model names" do
ts_context.stub!(:indexed_models => ['Alpha', 'Beta'])
ts_context.superclass_indexed_models.should == ['Alpha', 'Beta']
end
it "should not include classes which have indexed superclasses" do
ts_context.stub!(:indexed_models => ['Parent', 'Person'])
ts_context.superclass_indexed_models.should == ['Person']
end
end
end