symbolize 3.3.0pre → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,350 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/../spec_helper_mongoid'
3
+
4
+ #
5
+ # Test model
6
+ class User
7
+ include Mongoid::Document
8
+ include Mongoid::Symbolize
9
+
10
+ symbolize :other
11
+ symbolize :language, :in => [:pt, :en]
12
+ # symbolize :sex, :in => [true, false], :scopes => true
13
+ symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => true
14
+ symbolize :so, :allow_blank => true, :in => {
15
+ :linux => 'Linux',
16
+ :mac => 'Mac OS X',
17
+ :win => 'Videogame'
18
+ }, :scopes => true
19
+ symbolize :gui, :allow_blank => true, :in => [:cocoa, :qt, :gtk], :i18n => false
20
+ symbolize :karma, :in => %w{ good bad ugly}, :methods => true, :i18n => false, :allow_nil => true
21
+ # symbolize :cool, :in => [true, false], :scopes => true
22
+
23
+ has_many :extras, :dependent => :destroy, :class_name => "UserExtra"
24
+ embeds_many :skills, :class_name => "UserSkill"
25
+ end
26
+
27
+ class UserSkill
28
+ include Mongoid::Document
29
+ include Mongoid::Symbolize
30
+
31
+ symbolize :kind, :in => [:agility, :magic]
32
+ end
33
+
34
+ class UserExtra
35
+ include Mongoid::Document
36
+ include Mongoid::Symbolize
37
+
38
+ symbolize :key, :in => [:one, :another]
39
+ end
40
+
41
+ class Permission
42
+ include Mongoid::Document
43
+ include Mongoid::Symbolize
44
+
45
+ validates_presence_of :name
46
+ symbolize :kind, :in => [:temp, :perm], :default => :perm
47
+ end
48
+
49
+ [User, UserExtra, UserSkill, Permission].each { |k| k.destroy_all }
50
+
51
+ # Test records
52
+ User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
53
+ User.create!(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
54
+
55
+
56
+ describe "Symbolize" do
57
+
58
+ it "should be a module" do
59
+ Mongoid.const_defined?("Symbolize").should be_true
60
+ end
61
+
62
+ describe "User Instantiated" do
63
+ before(:each) do
64
+ @user = User.first
65
+ end
66
+
67
+ it "test_symbolize_string" do
68
+ @user.status = 'inactive'
69
+ @user.status.should eql(:inactive)
70
+ # @user.status_before_type_cast.should eql(:inactive)
71
+ # @user.read_attribute(:status).should eql('inactive')
72
+ end
73
+
74
+ it "test_symbolize_symbol" do
75
+ @user.status = :active
76
+ @user.status.should eql(:active)
77
+ @user.save
78
+ @user.status.should eql(:active)
79
+ @user[:status].should eql(:active)
80
+ end
81
+
82
+ # it "should work nice with numbers" do
83
+ # pending
84
+ # @user.status = 43
85
+ # @user.status.should_not be_nil
86
+ # # @user.status_before_type_cast.should be_nil
87
+ # # @user.read_attribute(:status).should be_nil
88
+ # end
89
+
90
+ # it "should acts nice with nil" do
91
+ # @user.status = nil
92
+ # @user.status.should be_nil
93
+ # @user.status_before_type_cast.should be_nil
94
+ # @user.read_attribute(:status).should be_nil
95
+ # end
96
+
97
+ # it "should acts nice with blank" do
98
+ # @user.status = ""
99
+ # @user.status.should be_nil
100
+ # @user.status_before_type_cast.should be_nil
101
+ # @user.read_attribute(:status).should be_nil
102
+ # end
103
+
104
+ it "should not validates other" do
105
+ @user.other = nil
106
+ @user.should be_valid
107
+ @user.other = ""
108
+ @user.should be_valid
109
+ end
110
+
111
+ it "should get the correct values" do
112
+ User.get_status_values.should eql([["Active", :active],["Inactive", :inactive]])
113
+ User::STATUS_VALUES.should eql({:inactive=>"Inactive", :active=>"Active"})
114
+ end
115
+
116
+ it "test_symbolize_humanize" do
117
+ @user.status_text.should eql("Active")
118
+ end
119
+
120
+ it "should get the correct values" do
121
+ User.get_gui_values.should =~ [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
122
+ User::GUI_VALUES.should eql({:cocoa=>"cocoa", :qt=>"qt", :gtk=>"gtk"})
123
+ end
124
+
125
+ it "test_symbolize_humanize" do
126
+ @user.gui_text.should eql("qt")
127
+ end
128
+
129
+ it "should get the correct values" do
130
+ User.get_so_values.should =~ [["Linux", :linux], ["Mac OS X", :mac], ["Videogame", :win]]
131
+ User::SO_VALUES.should eql({:linux => "Linux", :mac => "Mac OS X", :win => "Videogame"})
132
+ end
133
+
134
+ it "test_symbolize_humanize" do
135
+ @user.so_text.should eql("Linux")
136
+ end
137
+
138
+ it "test_symbolize_humanize" do
139
+ @user.so = :mac
140
+ @user.so_text.should eql("Mac OS X")
141
+ end
142
+
143
+ it "should stringify" do
144
+ @user.other_text.should eql("fo")
145
+ @user.other = :foo
146
+ @user.other_text.should eql("foo")
147
+ end
148
+
149
+ it "should validate status" do
150
+ @user.status = nil
151
+ @user.should_not be_valid
152
+ @user.should have(1).errors
153
+ end
154
+
155
+ it "should not validate so" do
156
+ @user.so = nil
157
+ @user.should be_valid
158
+ end
159
+
160
+ it "test_symbols_with_weird_chars_quoted_id" do
161
+ @user.status = :"weird'; chars"
162
+ @user.status.should eql(:"weird'; chars")
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.should, 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
+ # end
234
+
235
+ # it "should get the correct values" do
236
+ # User.get_sex_values.should eql([["Feminino", true],["Masculino", false]])
237
+ # end
238
+
239
+ # it "should get the correct values" do
240
+ # User::SEX_VALUES.should eql({true=>"true", false=>"false"})
241
+ # end
242
+
243
+ it "should translate a multiword class" do
244
+ @skill = UserSkill.create(:kind => :magic)
245
+ @skill.kind_text.should eql("Mágica")
246
+ end
247
+
248
+ it "should return nil if there's no value" do
249
+ @skill = UserSkill.create(:kind => nil)
250
+ @skill.kind_text.should be_nil
251
+ end
252
+
253
+ end
254
+
255
+ describe "Methods" do
256
+
257
+ it "should play nice with other stuff" do
258
+ @user.karma.should be_nil
259
+ User::KARMA_VALUES.should eql({:bad => "bad", :ugly => "ugly", :good => "good"})
260
+ end
261
+
262
+ it "should provide a boolean method" do
263
+ @user.should_not be_good
264
+ @user.karma = :ugly
265
+ @user.should be_ugly
266
+ end
267
+
268
+ it "should work" do
269
+ @user.karma = "good"
270
+ @user.should be_good
271
+ @user.should_not be_bad
272
+ end
273
+
274
+ end
275
+
276
+ end
277
+
278
+ describe "more tests on Permission" do
279
+
280
+ it "should use default value on object build" do
281
+ Permission.new.kind.should eql(:perm)
282
+ end
283
+
284
+ it "should not interfer on create" do
285
+ Permission.create!(:name => "p7", :kind => :temp)
286
+ Permission.where(name: "p7").first.kind.should eql(:temp)
287
+ end
288
+
289
+ it "should work on create" do
290
+ pm = Permission.new(:name => "p7")
291
+ pm.should be_valid
292
+ pm.save.should be_true
293
+ end
294
+
295
+ it "should work on create" do
296
+ Permission.create(:name => "p8")
297
+ Permission.first(conditions: { name: "p8" }).kind.should eql(:perm)
298
+ end
299
+
300
+ it "should work on edit" do
301
+ pm = Permission.where(name: "p8").first
302
+ pm.kind = :temp
303
+ pm.save
304
+ Permission.where(name: "p8").first.kind.should eql(:temp)
305
+ end
306
+
307
+ end
308
+
309
+ describe "Mongoid stuff" do
310
+
311
+ it "test_symbolized_finder" do
312
+ User.where({ :status => :inactive }).all.map(&:name).should eql(['Bob'])
313
+ User.where(status: :inactive).map(&:name).should eql(['Bob'])
314
+ end
315
+
316
+ # it "test_symabolized_with_scope" do
317
+ # User.with_scope({ :status => :inactive }) do
318
+ # User.all.map(&:name).should eql(['Bob'])
319
+ # end
320
+ # end
321
+
322
+ describe "dirty tracking / changed flag" do
323
+ before do
324
+ @anna = User.where(name: 'Anna').first
325
+ end
326
+
327
+ it "is dirty if you change the attribute value" do
328
+ @anna.language.should == :pt
329
+ @anna.language_changed?.should be_false
330
+
331
+ return_value = @anna.language = :en
332
+ return_value.should == :en
333
+ @anna.language_changed?.should be_true
334
+ end
335
+
336
+ it "is not dirty if you set the attribute value to the same value it was originally" do
337
+ @anna.language.should == :pt
338
+ @anna.language_changed?.should be_false
339
+
340
+ return_value = @anna.language = :pt
341
+ return_value.should == :pt
342
+ @anna.language_changed?.should be_false
343
+ end
344
+ end
345
+
346
+
347
+ end
348
+
349
+ end
350
+