Sha256: 9f06ad2c5b78d81bbc5e0b84f49dc5bd2db82bd652a2b43ee14984860da12c02

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

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

class NestedMigration < ActiveRecord::Migration
  def self.up
	create_table :nested_parents do |t|
		t.column :field, :string
		t.column :inner_child_id, :integer
	end	
	create_table :nested_children do |t|
		t.column :field, :string
		t.column :nested_parent_id, :integer
	end	
	create_table :inner_children do |t|
		t.column :field, :string
		t.column :nested_child_id, :integer
	end	
  end

  def self.down
	drop_table :nested_parents
	drop_table :nested_children
	drop_table :inner_children
  end
end

class NestedParent < ActiveRecord::Base
	belongs_to :inner_child
	has_many :nested_children
end

class NestedChild < ActiveRecord::Base
	belongs_to :nested_parent
	has_many :inner_children
end

class InnerChild < ActiveRecord::Base
	belongs_to :nested_child
	has_many :nested_parents
end

class NestedTest < Test::Unit::TestCase
	fixtures :nested_parents, :nested_children, :inner_children

	def setup
		if connected?
			migration.up
			# setup fixtures from the start
			setup_with_fixtures 
		end
	end
	
	def teardown
		migration.down if connected?
		# do not delete files!
	end
	
	def test_nested_fixtures
		database_test do
			parent_one= nested_parents(:parent_one)
			parent_two = nested_parents(:parent_two)
			parent_three = nested_parents(:parent_three)
			parent_four = nested_parents(:parent_four)
				
			query_result = NestedChild.find_by_field(1).nested_parent
			assert_equal parent_one, query_result
				
			query_result = NestedParent.find_by_field(1).nested_children.first.inner_children.first.nested_parents
			assert_equal [parent_two, parent_three], query_result
				
			query_result = NestedParent.find_by_field(1).inner_child.nested_child.nested_parent
			assert_equal parent_two, query_result
				
			query_result = InnerChild.find_by_field(3).nested_parents.first
			assert_equal parent_four, query_result
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
joinfix-0.1.1 test/nested_test.rb
joinfix-1.0.0 test/nested_test.rb
joinfix-1.0.1 test/nested_test.rb