Skip to content
Browse files

Sorted Hierarchy mid level tests added, piggy a bit

  • Loading branch information...
1 parent 0406243 commit 5d3c8aeeea828b7e78c5a46304f0f3b35cb68479 @khustochka committed Jan 22, 2010
View
10 test/fixtures/cities_districts_streets.rb
@@ -2,4 +2,12 @@
require 'test/unit/sorted_hierarchy/model'
require 'test/unit/sorted_hierarchy/factories'
-5.times { Factory.build(:city).save! }
+city = Array.new(5) do |n|
+ Factory.create(:city)
+end
+
+city[1].children.create(Factory.attributes_for(:district))
+
+5.times do
+ city[2].children.create(Factory.attributes_for(:district))
+end
View
7 test/unit/sorted_hierarchy/factories.rb
@@ -2,3 +2,10 @@
city.name 'Brovary'
city.sort nil
end
+
+Factory.define :district do |district|
+ district.name 'Massive'
+ district.sort_num nil
+ district.city nil
+end
+
View
52 test/unit/sorted_hierarchy/mid_level_test.rb
@@ -0,0 +1,52 @@
+require 'test/unit/sorted_hierarchy/test_helper'
+
+class SortedHierarchyMidLevelTest < ActiveSupport::TestCase
+ context "Mid level object" do
+
+ setup do
+ District.count.should == 6
+ District.should be_sorted
+ end
+
+ should "be created successfully with sort = 6" do
+ @new_district = Factory.build(:district, :city => City.find_by_sort(3), :sort_num => 6)
+ lambda {
+ @new_district.save!
+ }.should change(District, :count).by(1)
+ @new_district.sort_num.should == 6
+ District.should be_sorted
+ end
+
+ should "be created successfully with sort = nil" do
+ @new_district = Factory.build(:district, :city => City.find_by_sort(3))
+ lambda {
+ @new_district.save!
+ }.should change(District, :count).by(1)
+ @new_district.sort_num.should == 6
+ District.should be_sorted
+ end
+
+ should "not be created with invalid sort" do
+ lambda {
+ Factory.build(:district, :city => City.find_by_sort(3), :sort_num => "first").save!
+ }.should raise_exception(ActiveRecord::RecordInvalid)
+ District.should be_sorted
+ end
+
+ should "not be created with sort too large" do
+ lambda {
+ Factory.build(:district, :city => City.find_by_sort(3), :sort_num => 56).save!
+ }.should raise_exception(ActiveRecord::RecordInvalid)
+ District.should be_sorted
+ end
+
+ should "preserve sorting when destroyed" do
+ @old_district = District.find_by_sort_num(5)
+ lambda {
+ @old_district.destroy
+ }.should change(District, :count).by(-1)
+ District.should be_sorted
+ end
+ end
+
+end

0 comments on commit 5d3c8ae

Please sign in to comment.
Something went wrong with that request. Please try again.