public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
added methods should_have_db_column and should_have_db_columns for 
activerecord tests

git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@251 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
tsaleh (author)
Fri Nov 23 07:18:28 -0800 2007
commit  88ce3dab1299061ba1fc1e6774110c359844d0bf
tree    9f7d6451d707ba2b67d5d7d1d28594eaa1aa1f74
parent  ce44c7340ca04f6b202997479388a756c7ecb4e4
...
29
30
31
 
32
33
34
...
53
54
55
 
56
57
58
...
92
93
94
 
95
96
97
...
118
119
120
 
121
122
123
...
142
143
144
 
145
146
147
...
167
168
169
 
170
171
172
...
208
209
210
 
211
212
213
...
245
246
247
 
248
249
250
...
268
269
270
 
271
272
273
...
288
289
290
 
291
292
293
...
302
303
304
 
305
306
307
...
316
317
318
 
319
320
321
...
333
334
335
 
336
337
338
...
346
347
348
 
349
350
351
...
355
356
357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358
359
360
...
29
30
31
32
33
34
35
...
54
55
56
57
58
59
60
...
94
95
96
97
98
99
100
...
121
122
123
124
125
126
127
...
146
147
148
149
150
151
152
...
172
173
174
175
176
177
178
...
214
215
216
217
218
219
220
...
252
253
254
255
256
257
258
...
276
277
278
279
280
281
282
...
297
298
299
300
301
302
303
...
312
313
314
315
316
317
318
...
327
328
329
330
331
332
333
...
345
346
347
348
349
350
351
...
359
360
361
362
363
364
365
...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
0
@@ -29,6 +29,7 @@ module ThoughtBot # :nodoc:
0
       #
0
       # Example:
0
       # should_require_attributes :name, :phone_number
0
+ #
0
       def should_require_attributes(*attributes)
0
         message = get_options!(attributes, :message)
0
         message ||= /blank/
0
@@ -53,6 +54,7 @@ module ThoughtBot # :nodoc:
0
       #
0
       # Example:
0
       # should_require_unique_attributes :keyword, :username
0
+ #
0
       def should_require_unique_attributes(*attributes)
0
         message, scope = get_options!(attributes, :message, :scoped_to)
0
         message ||= /taken/
0
@@ -92,6 +94,7 @@ module ThoughtBot # :nodoc:
0
       # Requires an existing record
0
       #
0
       # should_protect_attributes :password, :admin_flag
0
+ #
0
       def should_protect_attributes(*attributes)
0
         get_options!(attributes)
0
         klass = model_class
0
@@ -118,6 +121,7 @@ module ThoughtBot # :nodoc:
0
       #
0
       # Example:
0
       # should_not_allow_values_for :isbn, "bad 1", "bad 2"
0
+ #
0
       def should_not_allow_values_for(attribute, *bad_values)
0
         message = get_options!(bad_values, :message)
0
         message ||= /invalid/
0
@@ -142,6 +146,7 @@ module ThoughtBot # :nodoc:
0
       #
0
       # Example:
0
       # should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
0
+ #
0
       def should_allow_values_for(attribute, *good_values)
0
         message = get_options!(good_values, :message)
0
         message ||= /invalid/
0
@@ -167,6 +172,7 @@ module ThoughtBot # :nodoc:
0
       #
0
       # Example:
0
       # should_ensure_length_in_range :password, (6..20)
0
+ #
0
       def should_ensure_length_in_range(attribute, range, opts = {})
0
         short_message, long_message = get_options!([opts], :short_message, :long_message)
0
         short_message ||= /short/
0
@@ -208,6 +214,7 @@ module ThoughtBot # :nodoc:
0
       #
0
       # Example:
0
       # should_ensure_value_in_range :age, (0..100)
0
+ #
0
       def should_ensure_value_in_range(attribute, range, opts = {})
0
         low_message, high_message = get_options!([opts], :low_message, :high_message)
0
         low_message ||= /included/
0
@@ -245,6 +252,7 @@ module ThoughtBot # :nodoc:
0
       #
0
       # Example:
0
       # should_only_allow_numeric_values_for :age
0
+ #
0
       def should_only_allow_numeric_values_for(*attributes)
0
         message = get_options!(attributes, :message)
0
         message ||= /number/
0
@@ -268,6 +276,7 @@ module ThoughtBot # :nodoc:
0
       # Example:
0
       # should_have_many :friends
0
       # should_have_many :enemies, :through => :friends
0
+ #
0
       def should_have_many(*associations)
0
         through = get_options!(associations, :through)
0
         klass = model_class
0
@@ -288,6 +297,7 @@ module ThoughtBot # :nodoc:
0
       # Ensures that the has_and_belongs_to_many relationship exists.
0
       #
0
       # should_have_and_belong_to_many :posts, :cars
0
+ #
0
       def should_have_and_belong_to_many(*associations)
0
         get_options!(associations)
0
         klass = model_class
0
@@ -302,6 +312,7 @@ module ThoughtBot # :nodoc:
0
       # Ensure that the has_one relationship exists.
0
       #
0
       # should_have_one :god # unless hindu
0
+ #
0
       def should_have_one(*associations)
0
         get_options!(associations)
0
         klass = model_class
0
@@ -316,6 +327,7 @@ module ThoughtBot # :nodoc:
0
       # Ensure that the belongs_to relationship exists.
0
       #
0
       # should_belong_to :parent
0
+ #
0
       def should_belong_to(*associations)
0
         get_options!(associations)
0
         klass = model_class
0
@@ -333,6 +345,7 @@ module ThoughtBot # :nodoc:
0
       # Ensure that the given class methods are defined on the model.
0
       #
0
       # should_have_class_methods :find, :destroy
0
+ #
0
       def should_have_class_methods(*methods)
0
         get_options!(methods)
0
         klass = model_class
0
@@ -346,6 +359,7 @@ module ThoughtBot # :nodoc:
0
       # Ensure that the given instance methods are defined on the model.
0
       #
0
       # should_have_instance_methods :email, :name, :name=
0
+ #
0
       def should_have_instance_methods(*methods)
0
         get_options!(methods)
0
         klass = model_class
0
@@ -355,6 +369,43 @@ module ThoughtBot # :nodoc:
0
           end
0
         end
0
       end
0
+
0
+ # Ensure that the given columns are defined on the models backing SQL table.
0
+ #
0
+ # should_have_db_columns :id, :email, :name, :created_at
0
+ #
0
+ def should_have_db_columns(*columns)
0
+ column_type = get_options!(columns, :exclusive, :type)
0
+ klass = model_class
0
+ columns.each do |name|
0
+ test_name = "have column #{name}"
0
+ test_name += " of type #{column_type}" if column_type
0
+ should test_name do
0
+ column = klass.columns.detect {|c| c.name == name.to_s }
0
+ assert column, "#{klass.name} does not have column #{name}"
0
+ end
0
+ end
0
+ end
0
+
0
+ # Ensure that the given column is defined on the models backing SQL table. The options are the same as
0
+ # the instance variables defined on the column definition: :precision, :limit, :default, :null,
0
+ # :primary, :type, :scale, and :sql_type.
0
+ #
0
+ # should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
0
+ # :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
0
+ #
0
+ def should_have_db_column(name, opts = {})
0
+ klass = model_class
0
+ test_name = "have column named :#{name}"
0
+ test_name += " with options " + opts.inspect unless opts.empty?
0
+ should test_name do
0
+ column = klass.columns.detect {|c| c.name == name.to_s }
0
+ assert column, "#{klass.name} does not have column #{name}"
0
+ opts.each do |k, v|
0
+ assert_equal column.instance_variable_get("@#{k}").to_s, v.to_s, ":#{name} column on table for #{klass} does not match option :#{k}"
0
+ end
0
+ end
0
+ end
0
       
0
       private
0
       
...
12
13
14
 
 
 
 
15
...
12
13
14
15
16
17
18
19
0
@@ -12,4 +12,8 @@ class UserTest < Test::Unit::TestCase
0
   should_protect_attributes :password
0
   should_have_class_methods :find, :destroy
0
   should_have_instance_methods :email, :age, :email=, :valid?
0
+ should_have_db_columns :name, :email, :age
0
+ should_have_db_column :id, :type => "integer", :primary => true
0
+ should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
0
+ :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
0
 end

Comments

    No one has commented yet.