Class: DefaultValuePluginTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/vex/active_record/plugins/default_value_for/test.rb

Instance Method Summary collapse

Instance Method Details

#define_model_class(name = "TestClass", parent_class_name = "ActiveRecord::Base", &block) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 68

def define_model_class(name = "TestClass", parent_class_name = "ActiveRecord::Base", &block)
	Object.send(:remove_const, name) rescue nil
	eval("class #{name} < #{parent_class_name}; end", TOPLEVEL_BINDING)
	klass = eval(name, TOPLEVEL_BINDING)
	klass.class_eval do
		set_table_name 'numbers'
	end
	klass.class_eval(&block) if block_given?
end

#setupObject



60
61
62
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 60

def setup
	Number.create(:number => 9876)
end

#teardownObject



64
65
66
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 64

def teardown
	Number.delete_all
end

#test_also_works_on_attributes_that_arent_database_columnsObject



162
163
164
165
166
167
168
169
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 162

def test_also_works_on_attributes_that_arent_database_columns
	define_model_class do
		default_value_for :hello, "hi"
		attr_accessor :hello
	end
	object = TestClass.new
	assert_equal 'hi', object.hello
end

#test_attributes_with_default_values_are_not_marked_as_changedObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 243

def test_attributes_with_default_values_are_not_marked_as_changed
	define_model_class do
		default_value_for :count, 5
		default_value_for :number, 2
	end
	
	object = TestClass.new
	assert(!object.changed?)
	assert_equal([], object.changed)
	
	object.type = "foo"
	assert(object.changed?)
	assert_equal(["type"], object.changed)
end

#test_can_specify_default_value_via_associationObject



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 207

def test_can_specify_default_value_via_association
	user = User.create(:username => 'Kanako', :default_number => 123)
	define_model_class do
		belongs_to :user
		
		default_value_for :number do |n|
			n.user.default_number
		end
	end
	object = user.numbers.create
	assert_equal 123, object.number
end

#test_constructor_does_not_affect_the_hash_passed_to_itObject



269
270
271
272
273
274
275
276
277
278
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 269

def test_constructor_does_not_affect_the_hash_passed_to_it
	define_model_class do
		default_value_for :count, 5
	end
	
	options = { :count => 5, :user_id => 1 }
	options_dup = options.dup
	object = TestClass.new(options)
	assert_equal(options_dup, options)
end

#test_constructor_ignores_forbidden_mass_assignment_attributesObject



171
172
173
174
175
176
177
178
179
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 171

def test_constructor_ignores_forbidden_mass_assignment_attributes
	define_model_class do
		default_value_for :number, 1234
		attr_protected :number
	end
	object = TestClass.new(:number => 5678, :count => 987)
	assert_equal 1234, object.number
	assert_equal 987, object.count
end

#test_default_value_can_be_passed_as_argumentObject



78
79
80
81
82
83
84
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 78

def test_default_value_can_be_passed_as_argument
	define_model_class do
		default_value_for(:number, 1234)
	end
	object = TestClass.new
	assert_equal 1234, object.number
end

#test_default_value_can_be_passed_as_blockObject



86
87
88
89
90
91
92
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 86

def test_default_value_can_be_passed_as_block
	define_model_class do
		default_value_for(:number) { 1234 }
	end
	object = TestClass.new
	assert_equal 1234, object.number
end

#test_default_value_orderObject



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 231

def test_default_value_order
	define_model_class do
		default_value_for :count, 5
		default_value_for :number do |this|
			this.count * 2
		end
	end
	object = TestClass.new
	assert_equal(5, object.count)
	assert_equal(10, object.number)
end

#test_default_valuesObject



220
221
222
223
224
225
226
227
228
229
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 220

def test_default_values
	define_model_class do
		default_values :type => "normal",
		               :number => lambda { 10 + 5 }
	end
	
	object = TestClass.new
	assert_equal("normal", object.type)
	assert_equal(15, object.number)
end

#test_default_values_are_inheritedObject



146
147
148
149
150
151
152
153
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 146

def test_default_values_are_inherited
	define_model_class("TestSuperClass") do
		default_value_for :number, 1234
	end
	define_model_class("TestClass", "TestSuperClass")
	object = TestClass.new
	assert_equal 1234, object.number
end

#test_default_values_are_not_duplicatedObject



258
259
260
261
262
263
264
265
266
267
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 258

def test_default_values_are_not_duplicated
	define_model_class do
		set_table_name "users"
		default_value_for :username, "hello"
	end
	user1 = TestClass.new
	user1.username << " world"
	user2 = TestClass.new
	assert_equal("hello world", user2.username)
end

#test_doesnt_conflict_with_overrided_initialize_method_in_model_classObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 181

def test_doesnt_conflict_with_overrided_initialize_method_in_model_class
	define_model_class do
		def initialize(attrs = {})
			@initialized = true
			super(:count => 5678)
		end
		
		default_value_for :number, 1234
	end
	object = TestClass.new
	assert_equal 1234, object.number
	assert_equal 5678, object.count
	assert object.instance_variable_get('@initialized')
end

#test_doesnt_overwrite_explicitly_provided_nil_values_in_mass_assignmentObject



138
139
140
141
142
143
144
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 138

def test_doesnt_overwrite_explicitly_provided_nil_values_in_mass_assignment
	define_model_class do
		default_value_for :number, 1234
	end
	object = TestClass.new(:number => nil)
	assert_nil object.number
end

#test_doesnt_overwrite_values_provided_by_constructor_blockObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 127

def test_doesnt_overwrite_values_provided_by_constructor_block
	define_model_class do
		default_value_for :number, 1234
	end
	object = TestClass.new do |x|
		x.number = 1
		x.count = 2
	end
	assert_equal 1, object.number
end

#test_doesnt_overwrite_values_provided_by_mass_assignmentObject



110
111
112
113
114
115
116
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 110

def test_doesnt_overwrite_values_provided_by_mass_assignment
	define_model_class do
		default_value_for :number, 1234
	end
	object = TestClass.new(:number => 1, :count => 2)
	assert_equal 1, object.number
end

#test_doesnt_overwrite_values_provided_by_multiparameter_assignmentObject



118
119
120
121
122
123
124
125
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 118

def test_doesnt_overwrite_values_provided_by_multiparameter_assignment
	define_model_class do
		default_value_for :timestamp, Time.mktime(2000, 1, 1)
	end
	timestamp = Time.mktime(2009, 1, 1)
	object = TestClass.new('timestamp(1i)' => '2009', 'timestamp(2i)' => '1', 'timestamp(3i)' => '1')
	assert_equal timestamp, object.timestamp
end

#test_doesnt_set_default_on_saved_recordsObject



155
156
157
158
159
160
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 155

def test_doesnt_set_default_on_saved_records
	define_model_class do
		default_value_for :number, 1234
	end
	assert_equal 9876, TestClass.find(:first).number
end

#test_model_instance_is_passed_to_the_given_blockObject



196
197
198
199
200
201
202
203
204
205
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 196

def test_model_instance_is_passed_to_the_given_block
	$instance = nil
	define_model_class do
		default_value_for :number do |n|
			$instance = n
		end
	end
	object = TestClass.new
	assert_same object, $instance
end

#test_overwrites_db_defaultObject



102
103
104
105
106
107
108
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 102

def test_overwrites_db_default
	define_model_class do
		default_value_for :count, 1234
	end
	object = TestClass.new
	assert_equal 1234, object.count
end

#test_works_with_createObject



94
95
96
97
98
99
100
# File 'lib/vex/active_record/plugins/default_value_for/test.rb', line 94

def test_works_with_create
	define_model_class do
		default_value_for :number, 1234
	end
	TestClass.create
	assert_not_nil TestClass.find_by_number(1234)
end