Sha256: a246ec982772c79a4eeb35a0c00ef79708f70fc07fa18efd1445e4905b1677cf

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

require File.join(File.dirname(__FILE__),'..','helper')
require File.join(File.dirname(__FILE__),'..','model','user')

class Icon < ActiveObject::Base
	attribute :path
end

class Friend < ActiveObject::Base
	attribute :name
end


User.instance_eval{
	has_one :icon
	has_many :friends
}


class CreateObjectTest < Test::Unit::TestCase
	def setup
		@attributes = {:name=>'aaron',:email=>'aaron@nonobo.com',:password=>'123456'}
		@path = '/home/Aaron'
		@friends = ['kame','jim']
	end

	def test_has_one
		@icon = Icon.create(:path=> @path)
		@u = User.new(@attributes)
		@u.icon = @icon
		@u.save

		@read_user = User.find(@u.id)

		assert_equal @read_user.icon.id, @icon.id
		assert_equal @read_user.icon.path,@path
	end

	def test_has_one_change
		@icon = Icon.create(:path=> @path)
		@u = User.new(@attributes)
		@u.icon = @icon
		@u.save
		@u = User.find(@u.id)
		old_icon = @u.icon

		@icon = Icon.create(:path=> 'valid path')
		@u.icon = @icon
		@u.save
		@u = User.find(@u.id)

		assert_not_equal old_icon.id, @u.icon.id
		assert_equal @u.icon.path,'valid path'

	end

	def test_has_many
		@friend = Friend.create(:name=>@friends[0])

		@u = User.new(@attributes)
		@u.friends.append @friend

		assert_equal @u.friends.size,1

		@friend = Friend.create(:name=>@friends[1])

		@u.friends.append @friend

		@u.save

		@u = User.find(@u.id)

		@u.friends.each_with_index do |friend,index|
			assert_equal friend.name,@friends[index]
		end

		@friend = Friend.create(:name=>'nancy')
		@friends.insert(1,'nancy')

		@u.friends.insert(1,@friend)

		@u.friends.each_with_index do |friend,index|
			assert_equal friend.name,@friends[index]
		end

		assert_equal @u.friends.size,@friends.size

		@u.friends.clear

		@u.save

		@u = User.find(@u.id)

		assert_equal @u.friends.object_ids,[]

	end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activeobject-0.0.3 ./spec/case/association_test.rb
activeobject-0.0.4 ./spec/case/association_test.rb