forked from pat/thinking-sphinx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rails_additions_spec.rb
203 lines (172 loc) · 5.37 KB
/
rails_additions_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'spec/spec_helper'
describe ThinkingSphinx::HashExcept do
before(:each) do
@hash = { :number => 20, :letter => 'b', :shape => 'rectangle' }
end
describe "except method" do
it "returns a hash without the specified keys" do
new_hash = @hash.except(:number)
new_hash.should_not have_key(:number)
end
end
describe "except! method" do
it "modifies hash removing specified keys" do
@hash.except!(:number)
@hash.should_not have_key(:number)
end
end
describe "extends Hash" do
before :each do
@instance_methods = Hash.instance_methods.collect { |m| m.to_s }
end
it 'with except' do
@instance_methods.include?('except').should be_true
end
it 'with except!' do
@instance_methods.include?('except!').should be_true
end
end
end
describe ThinkingSphinx::ArrayExtractOptions do
describe 'extract_options! method' do
it 'returns a hash' do
array = []
array.extract_options!.should be_kind_of(Hash)
end
it 'returns the last option if it is a hash' do
array = ['a', 'b', {:c => 'd'}]
array.extract_options!.should == {:c => 'd'}
end
end
describe "extends Array" do
it 'with extract_options!' do
Array.instance_methods.collect { |m| m.to_s }.include?('extract_options!').should be_true
end
end
end
describe ThinkingSphinx::AbstractQuotedTableName do
describe 'quote_table_name method' do
it 'calls quote_column_name' do
adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql')
adapter.should_receive(:quote_column_name).with('messages')
adapter.quote_table_name('messages')
end
end
describe "extends ActiveRecord::ConnectionAdapters::AbstractAdapter" do
it 'with quote_table_name' do
ActiveRecord::ConnectionAdapters::AbstractAdapter.instance_methods.collect { |m|
m.to_s
}.include?('quote_table_name').should be_true
end
end
end
describe ThinkingSphinx::MysqlQuotedTableName do
describe "quote_table_name method" do
it 'correctly quotes' do
adapter = ActiveRecord::Base.connection
adapter.quote_table_name('thinking_sphinx.messages').should == "`thinking_sphinx`.`messages`"
end
end
describe "extends ActiveRecord::ConnectionAdapters::MysqlAdapter" do
it 'with quote_table_name' do
adapter = defined?(JRUBY_VERSION) ? :JdbcAdapter : :MysqlAdapter
ActiveRecord::ConnectionAdapters.const_get(adapter).instance_methods.collect { |m|
m.to_s
}.include?("quote_table_name").should be_true
end
end
end
describe ThinkingSphinx::ActiveRecordQuotedName do
describe "quoted_table_name method" do
it 'returns table name wrappd in quotes' do
Person.quoted_table_name.should == '`people`'
end
end
describe "extends ActiveRecord::Base" do
it 'with quoted_table_name' do
ActiveRecord::Base.respond_to?("quoted_table_name").should be_true
end
end
end
describe ThinkingSphinx::ActiveRecordStoreFullSTIClass do
describe "store_full_sti_class method" do
it 'returns false' do
Person.store_full_sti_class.should be_false
end
end
describe "extends ActiveRecord::Base" do
it 'with store_full_sti_class' do
ActiveRecord::Base.respond_to?(:store_full_sti_class).should be_true
end
end
end
describe ThinkingSphinx::MetaClass do
describe 'metaclass' do
it "should exist as an instance method in Object" do
Object.new.should respond_to('metaclass')
end
it "should return the meta/eigen/singleton class" do
Object.new.metaclass.should be_a(Class)
end
end
end
class TestModel
@@squares = 89
@@circles = 43
def number_of_polygons
@@polygons
end
end
describe ThinkingSphinx::ClassAttributeMethods do
describe "cattr_reader method" do
it 'creates getters' do
TestModel.cattr_reader :herbivores
test_model = TestModel.new
test_model.respond_to?(:herbivores).should be_true
end
it 'sets the initial value to nil' do
TestModel.cattr_reader :carnivores
test_model = TestModel.new
test_model.carnivores.should be_nil
end
it 'does not override an existing definition' do
TestModel.cattr_reader :squares
test_model = TestModel.new
test_model.squares.should == 89
end
end
describe "cattr_writer method" do
it 'creates setters' do
TestModel.cattr_writer :herbivores
test_model = TestModel.new
test_model.respond_to?(:herbivores=).should be_true
end
it 'does not override an existing definition' do
TestModel.cattr_writer :polygons
test_model = TestModel.new
test_model.polygons = 100
test_model.number_of_polygons.should == 100
end
end
describe "cattr_accessor method" do
it 'calls cattr_reader' do
Class.should_receive(:cattr_reader).with('polygons')
Class.cattr_accessor('polygons')
end
it 'calls cattr_writer' do
Class.should_receive(:cattr_writer).with('polygons')
Class.cattr_accessor('polygons')
end
end
describe "extends Class" do
it 'with cattr_reader' do
Class.respond_to?('cattr_reader').should be_true
end
it 'with cattr_writer' do
Class.respond_to?('cattr_writer').should be_true
end
it 'with cattr_accessor' do
Class.respond_to?('cattr_accessor').should be_true
end
end
end