symbolize 3.3.0pre → 4.0.1

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.
@@ -0,0 +1,440 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/../spec_helper_ar'
3
+
4
+ #
5
+ # Test model
6
+ class User < ActiveRecord::Base
7
+ symbolize :other
8
+ symbolize :language, :in => [:pt, :en]
9
+ symbolize :sex, :in => [true, false], :scopes => true
10
+ symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => true
11
+ symbolize :so, :allow_blank => true, :in => {
12
+ :linux => 'Linux',
13
+ :mac => 'Mac OS X',
14
+ :win => 'Videogame'
15
+ }, :scopes => true
16
+ symbolize :gui, :allow_blank => true, :in => [:cocoa, :qt, :gtk], :i18n => false
17
+ symbolize :karma, :in => %w{ good bad ugly}, :methods => true, :i18n => false, :allow_nil => true
18
+ symbolize :cool, :in => [true, false], :scopes => true
19
+
20
+ has_many :extras, :dependent => :destroy, :class_name => "UserExtra"
21
+ has_many :access, :dependent => :destroy, :class_name => "UserAccess"
22
+ end
23
+
24
+ class UserSkill < ActiveRecord::Base
25
+ symbolize :kind, :in => [:agility, :magic]
26
+ end
27
+
28
+ class UserExtra < ActiveRecord::Base
29
+ symbolize :key, :in => [:one, :another]
30
+ end
31
+
32
+ class Permission < ActiveRecord::Base
33
+ validates_presence_of :name
34
+ symbolize :kind, :in => [:temp, :perm], :default => :perm
35
+ symbolize :lvl, :in => (1..9).to_a, :i18n => false#, :default => 1
36
+ end
37
+
38
+ # Make with_scope public-usable for testing
39
+ #if ActiveRecord::VERSION::MAJOR < 3
40
+ class << ActiveRecord::Base
41
+ public :with_scope
42
+ end
43
+ #end
44
+
45
+ # Test records
46
+ User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
47
+ User.create!(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
48
+
49
+
50
+ describe "Symbolize" do
51
+
52
+ it "should respond to symbolize" do
53
+ ActiveRecord::Base.should respond_to :symbolize
54
+ end
55
+
56
+ describe "User Instantiated" do
57
+ before(:each) do
58
+ @user = User.first
59
+ end
60
+
61
+ it "test_symbolize_string" do
62
+ @user.status = 'inactive'
63
+ @user.status.should eql(:inactive)
64
+ # @user.status_before_type_cast.should eql(:inactive)
65
+ # @user.read_attribute(:status).should eql('inactive')
66
+ end
67
+
68
+ it "test_symbolize_symbol" do
69
+ @user.status = :active
70
+ @user.status.should eql(:active)
71
+ @user.status_before_type_cast.should eql(:active)
72
+ # @user.read_attribute(:status).should eql('active')
73
+ end
74
+
75
+ it "should work nice with numbers" do
76
+ @user.status = 43
77
+ @user.status.should_not be_nil
78
+ # @user.status_before_type_cast.should be_nil
79
+ # @user.read_attribute(:status).should be_nil
80
+ end
81
+
82
+ it "should acts nice with nil" do
83
+ @user.status = nil
84
+ @user.status.should be_nil
85
+ @user.status_before_type_cast.should be_nil
86
+ @user.read_attribute(:status).should be_nil
87
+ end
88
+
89
+ it "should acts nice with blank" do
90
+ @user.status = ""
91
+ @user.status.should be_nil
92
+ @user.status_before_type_cast.should be_nil
93
+ @user.read_attribute(:status).should be_nil
94
+ end
95
+
96
+ it "test_symbols_quoted_id" do
97
+ pending
98
+ @user.status = :active
99
+ @user.status.quoted_id.should eql("'active'")
100
+ end
101
+
102
+ it "should not validates other" do
103
+ @user.other = nil
104
+ @user.should be_valid
105
+ @user.other = ""
106
+ @user.should be_valid
107
+ end
108
+
109
+ it "should get the correct values" do
110
+ User.get_status_values.should eql([["Active", :active],["Inactive", :inactive]])
111
+ User::STATUS_VALUES.should eql({:inactive=>"Inactive", :active=>"Active"})
112
+ end
113
+
114
+ it "test_symbolize_humanize" do
115
+ @user.status_text.should eql("Active")
116
+ end
117
+
118
+ it "should get the correct values" do
119
+ User.get_gui_values.should =~ [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
120
+ User::GUI_VALUES.should eql({:cocoa=>"cocoa", :qt=>"qt", :gtk=>"gtk"})
121
+ end
122
+
123
+ it "test_symbolize_humanize" do
124
+ @user.gui_text.should eql("qt")
125
+ end
126
+
127
+ it "should get the correct values" do
128
+ User.get_so_values.should =~ [["Linux", :linux], ["Mac OS X", :mac], ["Videogame", :win]]
129
+ User::SO_VALUES.should eql({:linux => "Linux", :mac => "Mac OS X", :win => "Videogame"})
130
+ end
131
+
132
+ it "test_symbolize_humanize" do
133
+ @user.so_text.should eql("Linux")
134
+ end
135
+
136
+ it "test_symbolize_humanize" do
137
+ @user.so = :mac
138
+ @user.so_text.should eql("Mac OS X")
139
+ end
140
+
141
+ it "should stringify" do
142
+ @user.other_text.should eql("fo")
143
+ @user.other = :foo
144
+ @user.other_text.should eql("foo")
145
+ end
146
+
147
+ it "should validate status" do
148
+ @user.status = nil
149
+ @user.should_not be_valid
150
+ @user.should have(1).errors
151
+ end
152
+
153
+ it "should not validate so" do
154
+ @user.so = nil
155
+ @user.should be_valid
156
+ end
157
+
158
+ it "test_symbols_with_weird_chars_quoted_id" do
159
+ @user.status = :"weird'; chars"
160
+ @user.status_before_type_cast.should eql(:"weird'; chars")
161
+ # assert_equal "weird'; chars", @user.read_attribute(:status)
162
+ # assert_equal "'weird''; chars'", @user.status.quoted_id
163
+ end
164
+
165
+ it "should work fine through relations" do
166
+ @user.extras.create(:key => :one)
167
+ UserExtra.first.key.should eql(:one)
168
+ end
169
+
170
+ it "should play fine with null db columns" do
171
+ new_extra = @user.extras.build
172
+ new_extra.should_not be_valid
173
+ end
174
+
175
+ it "should play fine with null db columns" do
176
+ new_extra = @user.extras.build
177
+ new_extra.should_not be_valid
178
+ end
179
+
180
+
181
+
182
+ describe "View helpers" do
183
+ include ActionView::Helpers::FormHelper
184
+ include ActionView::Helpers::FormOptionsHelper
185
+
186
+ before(:each) do
187
+ @options_status = [['Active', :active], ['Inactive', :inactive]]
188
+ @options_gui = [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
189
+ @options_so = [["Linux", :linux] , ["Mac OS X", :mac], ["Videogame", :win]]
190
+ end
191
+
192
+ it "test_helper_select_sym" do
193
+ @user.status = :inactive
194
+ output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
195
+ output.should eql(select_sym("user", "status", nil))
196
+
197
+
198
+ output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
199
+ output.should eql(select_sym("user", "status", nil))
200
+ end
201
+
202
+ def test_helper_select_sym_order
203
+ output_so = "<select id=\"user_so\" name=\"user[so]\">#{options_for_select(@options_so, @user.so)}</select>"
204
+ output_office = "<select id=\"user_office\" name=\"user[office]\">#{options_for_select(@options_office, @user.office)}</select>"
205
+
206
+ assert_equal output_so, select_sym("user", "so", nil)
207
+ assert_equal output_office, select_sym("user", "office", nil)
208
+ end
209
+
210
+ def test_helper_radio_sym
211
+ output = radio_sym("user", "status", nil)
212
+ assert_equal("<label>Active: <input checked=\"checked\" id=\"user_status_active\" name=\"user[status]\" type=\"radio\" value=\"active\" /></label><label>Inactive: <input id=\"user_status_inactive\" name=\"user[status]\" type=\"radio\" value=\"inactive\" /></label>", output)
213
+ end
214
+
215
+ end
216
+
217
+ describe "i18n" do
218
+
219
+ it "should test i18n ones" do
220
+ @user.language_text.should eql("Português")
221
+ end
222
+
223
+ it "should get the correct values" do
224
+ User.get_language_values.should =~ [["Português", :pt], ["Inglês", :en]]
225
+ end
226
+
227
+ it "should get the correct values" do
228
+ User::LANGUAGE_VALUES.should eql({:pt=>"pt", :en=>"en"})
229
+ end
230
+
231
+ it "should test boolean" do
232
+ @user.sex_text.should eql("Feminino")
233
+ @user.sex = false
234
+ @user.sex_text.should eql('Masculino')
235
+ end
236
+
237
+ it "should get the correct values" do
238
+ User.get_sex_values.should eql([["Feminino", true],["Masculino", false]])
239
+ end
240
+
241
+ it "should get the correct values" do
242
+ User::SEX_VALUES.should eql({true=>"true", false=>"false"})
243
+ end
244
+
245
+ it "should translate a multiword class" do
246
+ @skill = UserSkill.create(:kind => :magic)
247
+ @skill.kind_text.should eql("Mágica")
248
+ end
249
+
250
+ it "should return nil if there's no value" do
251
+ @skill = UserSkill.create(:kind => nil)
252
+ @skill.kind_text.should be_nil
253
+ end
254
+
255
+ end
256
+
257
+ describe "Methods" do
258
+
259
+ it "should play nice with other stuff" do
260
+ @user.karma.should be_nil
261
+ User::KARMA_VALUES.should eql({:bad => "bad", :ugly => "ugly", :good => "good"})
262
+ end
263
+
264
+ it "should provide a boolean method" do
265
+ @user.should_not be_good
266
+ @user.karma = :ugly
267
+ @user.should be_ugly
268
+ end
269
+
270
+ it "should work" do
271
+ @user.karma = "good"
272
+ @user.should be_good
273
+ @user.should_not be_bad
274
+ end
275
+
276
+ end
277
+
278
+ end
279
+
280
+ describe "more tests on Permission" do
281
+
282
+ it "should use default value on object build" do
283
+ Permission.new.kind.should eql(:perm)
284
+ end
285
+
286
+ it "should not interfer on create" do
287
+ Permission.create!(:name => "p7", :kind =>:temp, :lvl => 7)
288
+ Permission.find_by_name("p7").kind.should eql(:temp)
289
+ end
290
+
291
+ it "should work on create" do
292
+ pm = Permission.new(:name => "p7", :lvl => 7)
293
+ pm.should be_valid
294
+ pm.save.should be_true
295
+ end
296
+
297
+ it "should work on create" do
298
+ Permission.create!(:name => "p8", :lvl => 9)
299
+ Permission.find_by_name("p8").kind.should eql(:perm)
300
+ end
301
+
302
+ it "should work on edit" do
303
+ pm = Permission.find_by_name("p8")
304
+ pm.kind = :temp
305
+ pm.save
306
+ Permission.find_by_name("p8").kind.should eql(:temp)
307
+ end
308
+
309
+ it "should work with default values" do
310
+ pm = Permission.new(:name => "p9")
311
+ pm.lvl = 9
312
+ pm.save
313
+ Permission.find_by_name("p9").lvl.to_i.should eql(9)
314
+ end
315
+
316
+ end
317
+
318
+ describe "ActiveRecord stuff" do
319
+
320
+ #
321
+ # ActiveRecord <= 2
322
+ #
323
+ if ActiveRecord::VERSION::MAJOR <= 2
324
+
325
+ it "test_symbolized_finder" do
326
+ User.find(:all, :conditions => { :status => :inactive }).map(&:name).should eql(['Bob'])
327
+ User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
328
+ end
329
+
330
+ it "test_symbolized_with_scope" do
331
+ User.with_scope(:find => { :conditions => { :status => :inactive }}) do
332
+ User.find(:all).map(&:name).should eql(['Bob'])
333
+ end
334
+ end
335
+
336
+ describe "dirty tracking / changed flag" do
337
+ before do
338
+ @anna = User.find_by_name!('Anna')
339
+ end
340
+
341
+ it "is dirty if you change the attribute value" do
342
+ @anna.language.should == :pt
343
+ @anna.language_changed?.should be_false
344
+
345
+ return_value = @anna.language = :en
346
+ return_value.should == :en
347
+ @anna.language_changed?.should be_true
348
+ end
349
+
350
+ it "is not dirty if you set the attribute value to the same value it was originally" do
351
+ @anna.language.should == :pt
352
+ @anna.language_changed?.should be_false
353
+
354
+ return_value = @anna.language = :pt
355
+ return_value.should == :pt
356
+ @anna.language_changed?.should be_false
357
+ end
358
+ end
359
+
360
+ #
361
+ # ActiveRecord >= 3
362
+ #
363
+ else
364
+
365
+ it "test_symbolized_finder" do
366
+ User.where({ :status => :inactive }).all.map(&:name).should eql(['Bob'])
367
+ User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
368
+ end
369
+
370
+ it "test_symbolized_with_scope" do
371
+ User.with_scope(:find => { :conditions => { :status => :inactive }}) do
372
+ User.find(:all).map(&:name).should eql(['Bob'])
373
+ end
374
+ end
375
+
376
+ describe "dirty tracking / changed flag" do
377
+ before do
378
+ @anna = User.find_by_name!('Anna')
379
+ end
380
+
381
+ it "is dirty if you change the attribute value" do
382
+ @anna.language.should == :pt
383
+ @anna.language_changed?.should be_false
384
+
385
+ return_value = @anna.language = :en
386
+ return_value.should == :en
387
+ @anna.language_changed?.should be_true
388
+ end
389
+
390
+ it "is not dirty if you set the attribute value to the same value it was originally" do
391
+ @anna.language.should == :pt
392
+ @anna.language_changed?.should be_false
393
+
394
+ return_value = @anna.language = :pt
395
+ return_value.should == :pt
396
+ p @anna.changes
397
+ @anna.language_changed?.should be_false
398
+ end
399
+ end
400
+
401
+ if ActiveRecord::VERSION::STRING <= "3.0"
402
+ describe "Named Scopes" do
403
+
404
+ before do
405
+ @anna = User.find_by_name!('Anna')
406
+ @bob = User.find_by_name!('Bob')
407
+ end
408
+
409
+ it "should have main named scope" do
410
+ User.inactive.should == [@bob]
411
+ end
412
+
413
+ it "should have other to test better" do
414
+ User.linux.should == [@anna]
415
+ end
416
+
417
+ it "should have 'with' helper" do
418
+ User.with_sex.should == [@anna]
419
+ end
420
+
421
+ it "should have 'without' helper" do
422
+ User.without_sex.should == [@bob]
423
+ end
424
+
425
+ it "should have 'attr_name' helper" do
426
+ User.cool.should == [@anna]
427
+ end
428
+
429
+ it "should have 'not_attr_name' helper" do
430
+ User.not_cool.should == [@bob]
431
+ end
432
+
433
+ end
434
+ end
435
+
436
+ end
437
+
438
+ end
439
+
440
+ end