From 95314be65be197b6c38c8c93e3f8d1e8b5b0b674 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 15 Dec 2004 00:46:26 +0000 Subject: Added tree mixin and unit tests for all the mixins git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@156 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/mixin_test.rb | 101 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 activerecord/test/mixin_test.rb (limited to 'activerecord/test/mixin_test.rb') diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb new file mode 100644 index 0000000000..cc56bc7cec --- /dev/null +++ b/activerecord/test/mixin_test.rb @@ -0,0 +1,101 @@ +require 'abstract_unit' +require 'active_record/mixins/tree' +require 'active_record/mixins/list' +require 'active_record/mixins/touch' +require 'fixtures/mixin' + +class TreeTest < Test::Unit::TestCase + fixtures :mixins + + def test_has_child + assert_equal true, @first.has_children? + assert_equal false, @second.has_children? + assert_equal false, @third.has_children? + end + + def test_children + assert_equal @first.children, [@second, @third] + assert_equal @second.children, [] + end + + def test_parent + assert_equal @second.parent, @first + assert_equal @second.parent, @third.parent + assert_nil @first.parent + end + + def test_insert + @extra = @first.children.create + + assert @extra + + assert_equal @extra.parent, @first + assert_equal [@second, @third, @extra], @first.children + end + + def test_delete + assert_equal 3, Mixin.count + @first.destroy + assert_equal 0, Mixin.count + end +end + +class TouchTest < Test::Unit::TestCase + fixtures :mixins + + def test_update + assert_nil @first.updated_at + @first.save + assert_not_nil @first.updated_at + end + + def test_create + @obj = Mixin.create({"parent" => @third}) + assert_not_nil @obj.updated_at + assert_not_nil @obj.created_at + end + + +end + + +class ListTest < Test::Unit::TestCase + fixtures :mixins + + def test_reordering + + assert_equal [ListMixin.find(2), ListMixin.find(3)], ListMixin.find_all("parent_id=1", "pos") + + ListMixin.find(2).move_lower + + assert_equal [ListMixin.find(3), ListMixin.find(2)], ListMixin.find_all("parent_id=1", "pos") + + ListMixin.find(2).move_higher + + assert_equal [ListMixin.find(2), ListMixin.find(3)], ListMixin.find_all("parent_id=1", "pos") + + end + + def test_insert + new = ListMixin.create("parent_id"=>3) + assert_equal 1, new.pos + assert new.first? + assert new.last? + + new = ListMixin.create("parent_id"=>3) + assert_equal 2, new.pos + assert !new.first? + assert new.last? + + new = ListMixin.create("parent_id"=>3) + assert_equal 3, new.pos + assert !new.first? + assert new.last? + + new = ListMixin.create("parent_id"=>2) + assert_equal 1, new.pos + assert new.first? + assert new.last? + + end +end \ No newline at end of file -- cgit v1.2.3