Improve tests and test coverage
This commit is contained in:
parent
0b1c79c6f7
commit
3836226155
@ -13,10 +13,13 @@ class ActiveRecord::Base
|
||||
validate = options.delete(:perform_validations){|k| true}
|
||||
record = send("find_by_#{primary_key}", id) || new
|
||||
record.id = id
|
||||
record.attributes = options
|
||||
if validate
|
||||
record.attributes = options
|
||||
record.save!
|
||||
else
|
||||
options.each do |key, value|
|
||||
record.send("#{key}=", value)
|
||||
end
|
||||
record.save(false)
|
||||
end
|
||||
record
|
||||
|
@ -4,6 +4,7 @@ require 'rubygems'
|
||||
require 'mocha'
|
||||
|
||||
class User < ActiveRecord::Base
|
||||
attr_accessible :name
|
||||
end
|
||||
|
||||
class Customer < ActiveRecord::Base
|
||||
@ -16,43 +17,70 @@ class DbPopulateTest < Test::Unit::TestCase
|
||||
def test_creates_new_record
|
||||
User.delete_all
|
||||
User.create_or_update(:id => 1, :name => "Fred")
|
||||
assert_equal User.count, 1
|
||||
assert_equal 1, User.count
|
||||
u = User.find(:first)
|
||||
assert_equal u.name, "Fred"
|
||||
assert_equal "Fred", u.name
|
||||
end
|
||||
|
||||
def test_updates_existing_record
|
||||
User.delete_all
|
||||
User.create_or_update(:id => 1, :name => "Fred")
|
||||
User.create_or_update(:id => 1, :name => "George")
|
||||
assert_equal User.count, 1
|
||||
assert_equal 1, User.count
|
||||
u = User.find(:first)
|
||||
assert_equal u.name, "George"
|
||||
assert_equal "George", u.name
|
||||
end
|
||||
|
||||
def test_creates_new_record_with_nonstandard_pk
|
||||
Customer.delete_all
|
||||
Customer.create_or_update(:cust_id => 1, :name => "Fred")
|
||||
assert_equal Customer.count, 1
|
||||
assert_equal 1, Customer.count
|
||||
c = Customer.find(:first)
|
||||
assert_equal c.name, "Fred"
|
||||
assert_equal "Fred", c.name
|
||||
end
|
||||
|
||||
def test_creates_new_record_respects_attr_accessible
|
||||
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(STDERR)
|
||||
User.delete_all
|
||||
ActiveRecord::Base.expects(:log_protected_attribute_removal)
|
||||
User.create_or_update(:id => 1, :name => "Fred", :admin => true)
|
||||
u = User.find(:first)
|
||||
assert_equal false, u.admin
|
||||
end
|
||||
|
||||
def test_creates_new_record_skipping_attr_accessible
|
||||
User.delete_all
|
||||
ActiveRecord::Base.expects(:log_protected_attribute_removal).never
|
||||
User.create_or_update(:id => 1, :name => "Fred", :admin => true, :perform_validations => false)
|
||||
assert_equal 1, User.count
|
||||
u = User.find(:first)
|
||||
assert_equal "Fred", u.name
|
||||
assert_equal true, u.admin
|
||||
end
|
||||
|
||||
def test_updates_existing_record
|
||||
Customer.delete_all
|
||||
Customer.create_or_update(:cust_id => 1, :name => "Fred")
|
||||
Customer.create_or_update(:cust_id => 1, :name => "George")
|
||||
assert_equal Customer.count, 1
|
||||
assert_equal 1, Customer.count
|
||||
c = Customer.find(:first)
|
||||
assert_equal c.name, "George"
|
||||
assert_equal "George", c.name
|
||||
end
|
||||
|
||||
def test_creates_new_record_respects_validation
|
||||
Customer.delete_all
|
||||
assert_raise ActiveRecord::RecordInvalid do
|
||||
Customer.create_or_update(:cust_id => 1, :name => "Me")
|
||||
end
|
||||
assert_equal 0, Customer.count
|
||||
end
|
||||
|
||||
def test_creates_new_record_without_validation
|
||||
Customer.delete_all
|
||||
Customer.create_or_update(:cust_id => 1, :name => "Me", :perform_validations => false)
|
||||
assert_equal Customer.count, 1
|
||||
assert_equal 1, Customer.count
|
||||
c = Customer.find(:first)
|
||||
assert_equal c.name, "Me"
|
||||
assert_equal "Me", c.name
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -2,6 +2,7 @@ ActiveRecord::Schema.define(:version => 0) do
|
||||
|
||||
create_table "users", :force => true do |t|
|
||||
t.string "name"
|
||||
t.boolean "admin", :default => false
|
||||
end
|
||||
|
||||
create_table "customers", :primary_key => 'cust_id', :force => true do |t|
|
||||
|
@ -9,6 +9,7 @@ end
|
||||
require 'action_controller'
|
||||
require 'active_record'
|
||||
require 'action_view'
|
||||
require 'mocha'
|
||||
|
||||
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user