2008-09-16 01:08:50 +00:00
|
|
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
|
|
|
require 'test/unit'
|
|
|
|
require 'rubygems'
|
|
|
|
require 'mocha'
|
|
|
|
|
2008-10-12 00:05:39 +00:00
|
|
|
class User < ActiveRecord::Base
|
2009-11-26 17:45:28 +00:00
|
|
|
attr_accessible :name
|
2008-10-12 00:05:39 +00:00
|
|
|
end
|
|
|
|
|
2009-03-21 11:28:27 +00:00
|
|
|
class Customer < ActiveRecord::Base
|
|
|
|
set_primary_key "cust_id"
|
2009-11-26 16:54:30 +00:00
|
|
|
validates_length_of :name, :minimum => 4
|
2009-03-21 11:28:27 +00:00
|
|
|
end
|
|
|
|
|
2008-10-12 00:05:39 +00:00
|
|
|
class DbPopulateTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_creates_new_record
|
|
|
|
User.delete_all
|
|
|
|
User.create_or_update(:id => 1, :name => "Fred")
|
2009-11-26 17:45:28 +00:00
|
|
|
assert_equal 1, User.count
|
2008-10-12 00:05:39 +00:00
|
|
|
u = User.find(:first)
|
2009-11-26 17:45:28 +00:00
|
|
|
assert_equal "Fred", u.name
|
2008-10-12 00:05:39 +00:00
|
|
|
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")
|
2009-11-26 17:45:28 +00:00
|
|
|
assert_equal 1, User.count
|
2008-10-12 00:05:39 +00:00
|
|
|
u = User.find(:first)
|
2009-11-26 17:45:28 +00:00
|
|
|
assert_equal "George", u.name
|
2008-10-12 00:05:39 +00:00
|
|
|
end
|
|
|
|
|
2009-03-21 11:28:27 +00:00
|
|
|
def test_creates_new_record_with_nonstandard_pk
|
|
|
|
Customer.delete_all
|
|
|
|
Customer.create_or_update(:cust_id => 1, :name => "Fred")
|
2009-11-26 17:45:28 +00:00
|
|
|
assert_equal 1, Customer.count
|
2009-03-21 11:28:27 +00:00
|
|
|
c = Customer.find(:first)
|
2009-11-26 17:45:28 +00:00
|
|
|
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
|
2009-03-21 11:28:27 +00:00
|
|
|
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")
|
2009-11-26 17:45:28 +00:00
|
|
|
assert_equal 1, Customer.count
|
2009-03-21 11:28:27 +00:00
|
|
|
c = Customer.find(:first)
|
2009-11-26 17:45:28 +00:00
|
|
|
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
|
2009-03-21 11:28:27 +00:00
|
|
|
end
|
|
|
|
|
2009-11-26 16:54:30 +00:00
|
|
|
def test_creates_new_record_without_validation
|
|
|
|
Customer.delete_all
|
2009-11-26 17:09:31 +00:00
|
|
|
Customer.create_or_update(:cust_id => 1, :name => "Me", :perform_validations => false)
|
2009-11-26 17:45:28 +00:00
|
|
|
assert_equal 1, Customer.count
|
2009-11-26 16:54:30 +00:00
|
|
|
c = Customer.find(:first)
|
2009-11-26 17:45:28 +00:00
|
|
|
assert_equal "Me", c.name
|
2009-11-26 16:54:30 +00:00
|
|
|
end
|
|
|
|
|
2008-09-16 01:08:50 +00:00
|
|
|
end
|
|
|
|
|