Sha256: f227fcfcf441abfb09501be4e278e1ecac243c1d6d32f33161ce9eb28e56c4ba

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require  File.dirname(__FILE__) + '/joinfix_test_helper'

class HabtmMigration < ActiveRecord::Migration
  def self.up
	create_table :habtm_parents do |t|
		t.column :field, :string
	end	
	create_table :habtm_children do |t|
		t.column :field, :string
	end	
	create_table :habtm_children_habtm_parents do |t|
		t.column :habtm_parent_id, :integer
		t.column :habtm_child_id, :integer
	end	
	create_table :habtm_joins do |t|
		t.column :join_parent_id, :integer
		t.column :join_child_id, :integer
	end	
  end

  def self.down
	drop_table :habtm_parents
	drop_table :habtm_children
	drop_table :habtm_children_habtm_parents
	drop_table :habtm_joins
  end
end

class HabtmParent < ActiveRecord::Base
	has_and_belongs_to_many :habtm_children
	has_and_belongs_to_many :children,
		:foreign_key => "join_parent_id",
		:association_foreign_key => "join_child_id",
		:class_name => "HabtmChild",
		:join_table => :habtm_joins
end

class HabtmChild < ActiveRecord::Base
end

class HabtmChildrenHabtmParent < ActiveRecord::Base
end

class HabtmJoin < ActiveRecord::Base
end

class HasAndBelongsToManyTest < Test::Unit::TestCase
	fixtures :habtm_parents, :habtm_children, :habtm_children_habtm_parents, :habtm_joins
	
	def setup
	end
	
	def teardown
	end
	
	def test_has_and_belongs_to_many
		database_test(HabtmMigration) do 
			entry = habtm_parents(:without_children)
			assert_equal "without_children", entry.field
			assert entry.habtm_children.empty?
			assert entry.children.empty?
			
			entry = habtm_parents(:with_habtm_children)
			assert_equal "with_habtm_children", entry.field
			assert_equal ["1","2"], entry.habtm_children.collect {|e| e.field}.sort

			entry = habtm_parents(:with_children)
			assert_equal "with_children", entry.field
			assert_equal ["3","4"], entry.children.collect {|e| e.field}.sort
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
joinfix-0.1.0 test/has_and_belongs_to_many_test.rb