require "test_helper" class CrudTest < ActiveSupport::TestCase def setup CurlyMustache::Base.connection.flush_db end def attributes_hash { :id => 123, :name => "chris", :phone_number => 5128258325, :balance => 1.01, :is_admin => true, :created_at => Time.parse("2009-04-23 22:09:50 -05:00"), :updated_at => Time.parse("2009-04-23 22:09:50 -05:00") }.dup end def assert_attributes(record, attributes = nil) (attributes or attributes_hash).each do |k, v| assert_equal v, record.send(k), "for attribute '#{k}'" end end def test_create user = User.create(attributes_hash) assert(user) assert(!user.new_record?) user = User.find(user.id) assert_attributes(user) end def test_create_with_autogenerated_id user = User.create(attributes_hash.reject{ |k, v| k == :id }) assert user.id != attributes_hash[:id] assert_attributes(user, attributes_hash.merge(:id => user.id)) end def test_create_autogenerated_id_by_expectation id = "1341adb122d8190872c2928dbcc08b9d" User.any_instance.expects(:generate_id).returns(id) user = User.create :name => "christopher" assert_equal id, user.id end def test_find attributes1 = attributes_hash attributes1[:id] = 1 User.create(attributes1) attributes2 = attributes_hash attributes2[:id] = 2 User.create(attributes2) assert_equal User.find(1), User.find(1) user1 = User.find(1) assert_attributes(user1, attributes1) user2 = User.find(2) assert_attributes(user2, attributes2) assert_equal [user1, user2], User.find(1, 2) assert_raise(CurlyMustache::RecordNotFound){ User.find(3) } assert_raise(CurlyMustache::RecordNotFound){ User.find(1, 2, 3) } end def test_find_by_id assert_nil User.find_by_id(attributes_hash[:id]) User.create(attributes_hash) assert_attributes(User.find_by_id(attributes_hash[:id])) end def test_find_all_by_id 1.upto(3) do |i| attributes = attributes_hash attributes[:id] = i User.create(attributes) end user1, user2, user3 = User.find_all_by_id(1, 2, 3) assert_attributes(user1, attributes_hash.merge(:id => 1)) assert_attributes(user2, attributes_hash.merge(:id => 2)) assert_attributes(user3, attributes_hash.merge(:id => 3)) end def test_delete_all 1.upto(3) do |i| attributes = attributes_hash attributes[:id] = i User.create(attributes) end User.delete_all(1, 2) assert_raise(CurlyMustache::RecordNotFound){ User.find(1) } assert_raise(CurlyMustache::RecordNotFound){ User.find(2) } assert(User.find(3)) end def test_destroy_all 1.upto(3) do |i| attributes = attributes_hash attributes[:id] = i User.create(attributes) end User.destroy_all(1, 2) assert_raise(CurlyMustache::RecordNotFound){ User.find(1) } assert_raise(CurlyMustache::RecordNotFound){ User.find(2) } assert(User.find(3)) end def test_new user = User.new(attributes_hash) assert_attributes(user) assert(user.new_record?) end def test_reload user = User.new(attributes_hash) assert_raise(CurlyMustache::RecordNotFound){ user.reload } assert(user = User.create(attributes_hash)) User.delete_all(user.id) assert_raise(CurlyMustache::RecordNotFound){ user.reload } assert(user = User.create(attributes_hash)) user.name = "callie" assert_equal(user.read_attribute(:name), "callie") user.reload assert_equal(user.read_attribute(:name), attributes_hash[:name]) # Strange reload bug where it sets the id to 0. # Ahh, this was happening because attributes_manager wasn't inheriting properly. account = Account.create(:name => "blah") assert_equal 32, (account_id = account.id).length account.reload assert_equal account_id, account.id end def test_save_by_expectations user = User.new(attributes_hash) user.expects(:create).once user.save user = User.create(attributes_hash) user.expects(:update).once user.save end def test_save user = User.new(attributes_hash) user.save assert_attributes(user.reload) user = User.create(attributes_hash) user.name = "callie" user.save user.reload assert_equal("callie", user.name) end def test_destroy user = User.create(attributes_hash) user.destroy assert(user.frozen?) assert_raise(CurlyMustache::RecordNotFound){ user.reload } end end