public
Description: Provides a way to specify default values for ActiveRecord models
Homepage:
Clone URL: git://github.com/FooBarWidget/default_value_for.git
htanata (author)
Wed Apr 22 02:19:04 -0700 2009
Hongli Lai (Phusion) (committer)
Sun May 17 05:20:07 -0700 2009
default_value_for / test.rb
ddd84f35 » Hongli Lai (Phusion) 2009-03-08 After setting default value... 1 # Copyright (c) 2008, 2009 Phusion
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 # THE SOFTWARE.
20
21 require 'rubygems'
22 require 'active_record'
23 require 'test/unit'
24 require File.dirname(__FILE__) + '/init'
25 Dir.chdir(File.dirname(__FILE__))
26
27 if RUBY_PLATFORM == "java"
28 database_adapter = "jdbcsqlite3"
29 else
30 database_adapter = "sqlite3"
31 end
32
33 File.unlink('test.sqlite3') rescue nil
d5cd9809 » Hongli Lai (Phusion) 2008-09-22 Various improvements. 34 ActiveRecord::Base.logger = Logger.new(STDERR)
35 ActiveRecord::Base.logger.level = Logger::WARN
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 36 ActiveRecord::Base.establish_connection(
37 :adapter => database_adapter,
38 :database => 'test.sqlite3'
39 )
daa83f08 » Hongli Lai (Phusion) 2008-10-03 Fix some bugs, add more doc... 40 ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
41 t.string :username
42 t.integer :default_number
43 end
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 44 ActiveRecord::Base.connection.create_table(:numbers, :force => true) do |t|
45 t.string :type
46 t.integer :number
47 t.integer :count, :null => false, :default => 1
daa83f08 » Hongli Lai (Phusion) 2008-10-03 Fix some bugs, add more doc... 48 t.integer :user_id
1ae4bf83 » htanata 2009-04-22 Should not overwrite multip... 49 t.timestamp :timestamp
daa83f08 » Hongli Lai (Phusion) 2008-10-03 Fix some bugs, add more doc... 50 end
51
52 class User < ActiveRecord::Base
53 has_many :numbers, :class_name => 'TestClass'
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 54 end
4a114c13 » Hongli Lai (Phusion) 2008-09-22 Add test that checks that d... 55
56 class Number < ActiveRecord::Base
57 end
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 58
59 class DefaultValuePluginTest < Test::Unit::TestCase
4a114c13 » Hongli Lai (Phusion) 2008-09-22 Add test that checks that d... 60 def setup
61 Number.create(:number => 9876)
62 end
63
64 def teardown
65 Number.delete_all
66 end
67
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 68 def define_model_class(name = "TestClass", parent_class_name = "ActiveRecord::Base", &block)
69 Object.send(:remove_const, name) rescue nil
70 eval("class #{name} < #{parent_class_name}; end", TOPLEVEL_BINDING)
71 klass = eval(name, TOPLEVEL_BINDING)
d5cd9809 » Hongli Lai (Phusion) 2008-09-22 Various improvements. 72 klass.class_eval do
73 set_table_name 'numbers'
74 end
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 75 klass.class_eval(&block) if block_given?
76 end
77
78 def test_default_value_can_be_passed_as_argument
79 define_model_class do
80 default_value_for(:number, 1234)
81 end
82 object = TestClass.new
83 assert_equal 1234, object.number
84 end
85
86 def test_default_value_can_be_passed_as_block
87 define_model_class do
88 default_value_for(:number) { 1234 }
89 end
90 object = TestClass.new
91 assert_equal 1234, object.number
92 end
93
4a114c13 » Hongli Lai (Phusion) 2008-09-22 Add test that checks that d... 94 def test_works_with_create
95 define_model_class do
96 default_value_for :number, 1234
97 end
98 TestClass.create
99 assert_not_nil TestClass.find_by_number(1234)
100 end
101
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 102 def test_overwrites_db_default
103 define_model_class do
104 default_value_for :count, 1234
105 end
106 object = TestClass.new
107 assert_equal 1234, object.count
108 end
109
110 def test_doesnt_overwrite_values_provided_by_mass_assignment
111 define_model_class do
112 default_value_for :number, 1234
113 end
114 object = TestClass.new(:number => 1, :count => 2)
115 assert_equal 1, object.number
116 end
1ae4bf83 » htanata 2009-04-22 Should not overwrite multip... 117
118 def test_doesnt_overwrite_values_provided_by_multiparameter_assignment
119 define_model_class do
120 default_value_for :timestamp, Time.mktime(2000, 1, 1)
121 end
122 timestamp = Time.mktime(2009, 1, 1)
123 object = TestClass.new('timestamp(1i)' => '2009', 'timestamp(2i)' => '1', 'timestamp(3i)' => '1')
124 assert_equal timestamp, object.timestamp
125 end
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 126
127 def test_doesnt_overwrite_values_provided_by_constructor_block
128 define_model_class do
129 default_value_for :number, 1234
130 end
131 object = TestClass.new do |x|
132 x.number = 1
133 x.count = 2
134 end
135 assert_equal 1, object.number
136 end
137
138 def test_doesnt_overwrite_explicitly_provided_nil_values_in_mass_assignment
139 define_model_class do
140 default_value_for :number, 1234
141 end
142 object = TestClass.new(:number => nil)
143 assert_nil object.number
144 end
145
146 def test_default_values_are_inherited
147 define_model_class("TestSuperClass") do
148 default_value_for :number, 1234
149 end
150 define_model_class("TestClass", "TestSuperClass")
151 object = TestClass.new
152 assert_equal 1234, object.number
153 end
154
155 def test_doesnt_set_default_on_saved_records
156 define_model_class do
157 default_value_for :number, 1234
158 end
159 assert_equal 9876, TestClass.find(:first).number
160 end
d5cd9809 » Hongli Lai (Phusion) 2008-09-22 Various improvements. 161
4a114c13 » Hongli Lai (Phusion) 2008-09-22 Add test that checks that d... 162 def test_also_works_on_attributes_that_arent_database_columns
d5cd9809 » Hongli Lai (Phusion) 2008-09-22 Various improvements. 163 define_model_class do
164 default_value_for :hello, "hi"
165 attr_accessor :hello
166 end
167 object = TestClass.new
168 assert_equal 'hi', object.hello
169 end
170
171 def test_constructor_ignores_forbidden_mass_assignment_attributes
172 define_model_class do
173 default_value_for :number, 1234
174 attr_protected :number
175 end
176 object = TestClass.new(:number => 5678, :count => 987)
177 assert_equal 1234, object.number
178 assert_equal 987, object.count
179 end
4a114c13 » Hongli Lai (Phusion) 2008-09-22 Add test that checks that d... 180
181 def test_doesnt_conflict_with_overrided_initialize_method_in_model_class
182 define_model_class do
183 def initialize(attrs = {})
184 @initialized = true
185 super(:count => 5678)
186 end
187
188 default_value_for :number, 1234
189 end
190 object = TestClass.new
191 assert_equal 1234, object.number
192 assert_equal 5678, object.count
193 assert object.instance_variable_get('@initialized')
194 end
daa83f08 » Hongli Lai (Phusion) 2008-10-03 Fix some bugs, add more doc... 195
196 def test_model_instance_is_passed_to_the_given_block
197 $instance = nil
198 define_model_class do
199 default_value_for :number do |n|
200 $instance = n
201 end
202 end
203 object = TestClass.new
204 assert_same object, $instance
205 end
206
207 def test_can_specify_default_value_via_association
208 user = User.create(:username => 'Kanako', :default_number => 123)
209 define_model_class do
210 belongs_to :user
211
212 default_value_for :number do |n|
213 n.user.default_number
214 end
215 end
216 object = user.numbers.create
217 assert_equal 123, object.number
218 end
a501a444 » Peeja 2008-11-17 Add test for default_values. 219
220 def test_default_values
221 define_model_class do
222 default_values :type => "normal",
223 :number => lambda { 10 + 5 }
224 end
225
226 object = TestClass.new
227 assert_equal("normal", object.type)
228 assert_equal(15, object.number)
229 end
a995a8c0 » Bertg 2009-01-29 The order in which default_... 230
231 def test_default_value_order
13505df3 » Hongli Lai (Phusion) 2009-02-15 Fix indentation. 232 define_model_class do
233 default_value_for :count, 5
234 default_value_for :number do |this|
235 this.count * 2
236 end
237 end
238 object = TestClass.new
a995a8c0 » Bertg 2009-01-29 The order in which default_... 239 assert_equal(5, object.count)
240 assert_equal(10, object.number)
13505df3 » Hongli Lai (Phusion) 2009-02-15 Fix indentation. 241 end
ddd84f35 » Hongli Lai (Phusion) 2009-03-08 After setting default value... 242
243 def test_attributes_with_default_values_are_not_marked_as_changed
244 define_model_class do
245 default_value_for :count, 5
246 default_value_for :number, 2
247 end
248
249 object = TestClass.new
250 assert(!object.changed?)
251 assert_equal([], object.changed)
252
253 object.type = "foo"
254 assert(object.changed?)
255 assert_equal(["type"], object.changed)
256 end
ef531805 » Hongli Lai (Phusion) 2009-03-08 Document the fact that defa... 257
258 def test_default_values_are_not_duplicated
259 define_model_class do
260 set_table_name "users"
261 default_value_for :username, "hello"
262 end
263 user1 = TestClass.new
264 user1.username << " world"
265 user2 = TestClass.new
266 assert_equal("hello world", user2.username)
267 end
16ae7caf » Hongli Lai (Phusion) 2009-04-08 The constructor must not ch... 268
269 def test_constructor_does_not_affect_the_hash_passed_to_it
270 define_model_class do
271 default_value_for :count, 5
272 end
273
274 options = { :count => 5, :user_id => 1 }
275 options_dup = options.dup
276 object = TestClass.new(options)
277 assert_equal(options_dup, options)
278 end
ed7d6bad » Hongli Lai (Phusion) 2008-09-22 Initial commit. Comment 279 end