diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/public_body_category/category_collection_spec.rb | 81 | ||||
-rw-r--r-- | spec/models/public_body_category_link_spec.rb | 53 | ||||
-rw-r--r-- | spec/models/public_body_category_spec.rb | 65 | ||||
-rw-r--r-- | spec/models/public_body_heading_spec.rb | 68 |
4 files changed, 267 insertions, 0 deletions
diff --git a/spec/models/public_body_category/category_collection_spec.rb b/spec/models/public_body_category/category_collection_spec.rb new file mode 100644 index 000000000..1fbcbe739 --- /dev/null +++ b/spec/models/public_body_category/category_collection_spec.rb @@ -0,0 +1,81 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe PublicBodyCategory::CategoryCollection do + context "requesting data" do + + before do + data = [ "Local and regional", + [ "local_council", "Local councils", "a local council" ], + "Miscellaneous", + [ "other", "Miscellaneous", "miscellaneous" ] ] + @categories = PublicBodyCategory::CategoryCollection.new + data.each { |item| @categories << item } + end + + describe 'when asked for headings' do + + it 'should return a list of headings' do + @categories.headings().should == ['Local and regional', 'Miscellaneous'] + end + + end + + describe 'when asked for categories with headings' do + it 'should return a list of headings as plain strings, each followed by n tag specifications as + lists in the form: + ["tag_to_use_as_category", "Sub category title", "Instance description"]' do + expected_categories = ["Local and regional", ["local_council", + "Local councils", + "a local council"], + "Miscellaneous", ["other", + "Miscellaneous", + "miscellaneous"]] + @categories.with_headings().should == expected_categories + end + end + + + + describe 'when asked for tags by headings' do + it 'should return a hash of tags keyed by heading' do + @categories.by_heading().should == {'Local and regional' => ['local_council'], + 'Miscellaneous' => ['other']} + end + end + + describe 'when asked for categories with description' do + it 'should return a list of tag specifications as lists in the form: + ["tag_to_use_as_category", "Sub category title", "Instance description"]' do + expected_categories = [ + ["local_council", "Local councils", "a local council"], + ["other", "Miscellaneous", "miscellaneous"] + ] + @categories.with_description().should == expected_categories + end + end + + describe 'when asked for tags' do + it 'should return a list of tags' do + @categories.tags().should == ["local_council", "other"] + end + end + + describe 'when asked for categories by tag' do + it 'should return a hash of categories keyed by tag' do + @categories.by_tag().should == { + "local_council" => "Local councils", + "other" => "Miscellaneous" + } + end + end + + describe 'when asked for singular_by_tag' do + it 'should return a hash of category descriptions keyed by tag' do + @categories.singular_by_tag().should == { + "local_council" => "a local council", + "other" => "miscellaneous" + } + end + end + end +end diff --git a/spec/models/public_body_category_link_spec.rb b/spec/models/public_body_category_link_spec.rb new file mode 100644 index 000000000..8d91f02d5 --- /dev/null +++ b/spec/models/public_body_category_link_spec.rb @@ -0,0 +1,53 @@ +# == Schema Information +# +# Table name: public_body_category_link +# +# public_body_category_id :integer not null +# public_body_heading_id :integer not null +# category_display_order :integer +# + +require 'spec_helper' + +describe PublicBodyHeading, 'when validating' do + + it 'should set a default display order based on the next available display order' do + heading = FactoryGirl.create(:public_body_heading) + category = FactoryGirl.create(:public_body_category) + category_link = PublicBodyCategoryLink.new(:public_body_heading => heading, + :public_body_category => category) + category_link.valid? + category_link.category_display_order.should == PublicBodyCategoryLink.next_display_order(heading) + end + + it 'should be invalid without a category' do + category_link = PublicBodyCategoryLink.new + category_link.should_not be_valid + category_link.errors[:public_body_category].should == ["can't be blank"] + end + + it 'should be invalid without a heading' do + category_link = PublicBodyCategoryLink.new + category_link.should_not be_valid + category_link.errors[:public_body_heading].should == ["can't be blank"] + end + +end + +describe PublicBodyCategoryLink, 'when setting a category display order' do + + it 'should return 0 if there are no public body headings' do + heading = FactoryGirl.create(:public_body_heading) + PublicBodyCategoryLink.next_display_order(heading).should == 0 + end + + it 'should return one more than the highest display order if there are public body headings' do + heading = FactoryGirl.create(:public_body_heading) + category = FactoryGirl.create(:public_body_category) + category_link = PublicBodyCategoryLink.create(:public_body_heading_id => heading.id, + :public_body_category_id => category.id) + + PublicBodyCategoryLink.next_display_order(heading).should == 1 + end + +end diff --git a/spec/models/public_body_category_spec.rb b/spec/models/public_body_category_spec.rb new file mode 100644 index 000000000..2d39a7376 --- /dev/null +++ b/spec/models/public_body_category_spec.rb @@ -0,0 +1,65 @@ +# == Schema Information +# +# Table name: public_body_categories +# +# id :integer not null, primary key +# locale :string +# title :text not null +# category_tag :text not null +# description :text not null +# display_order :integer +# + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe PublicBodyCategory do + describe 'when loading the data' do + it 'should use the display_order field to preserve the original data order' do + PublicBodyCategory.add(:en, [ + "Local and regional", + [ "local_council", "Local councils", "a local council" ], + "Miscellaneous", + [ "other", "Miscellaneous", "miscellaneous" ], + [ "aardvark", "Aardvark", "daft test"],]) + + headings = PublicBodyHeading.all + cat_group1 = headings[0].public_body_categories + cat_group1.count.should eq 1 + cat_group1[0].title.should eq "Local councils" + + cat_group2 = headings[1].public_body_categories + cat_group2.count.should eq 2 + cat_group2[0].title.should eq "Miscellaneous" + cat_group2[0].public_body_category_links.where( + :public_body_heading_id => headings[1].id). + first. + category_display_order.should eq 0 + + cat_group2[1].title.should eq "Aardvark" + cat_group2[1].public_body_category_links.where( + :public_body_heading_id => headings[1].id). + first. + category_display_order.should eq 1 + end + end + + context 'when validating' do + + it 'should require a title' do + category = PublicBodyCategory.new + category.should_not be_valid + category.errors[:title].should == ["Title can't be blank"] + end + + it 'should require a category tag' do + category = PublicBodyCategory.new + category.should_not be_valid + category.errors[:category_tag].should == ["Tag can't be blank"] + end + + it 'should require a unique tag' do + existing = FactoryGirl.create(:public_body_category) + PublicBodyCategory.new(:email => existing.category_tag).should_not be_valid + end + end +end diff --git a/spec/models/public_body_heading_spec.rb b/spec/models/public_body_heading_spec.rb new file mode 100644 index 000000000..add2cac60 --- /dev/null +++ b/spec/models/public_body_heading_spec.rb @@ -0,0 +1,68 @@ +# == Schema Information +# +# Table name: public_body_headings +# +# id :integer not null, primary key +# locale :string +# name :text not null +# display_order :integer +# + +require 'spec_helper' + +describe PublicBodyHeading do + + context 'when loading the data' do + + before do + PublicBodyCategory.add(:en, [ + "Local and regional", + [ "local_council", "Local councils", "a local council" ], + "Miscellaneous", + [ "other", "Miscellaneous", "miscellaneous" ],]) + end + + it 'should use the display_order field to preserve the original data order' do + headings = PublicBodyHeading.all + headings[0].name.should eq 'Local and regional' + headings[0].display_order.should eq 0 + headings[1].name.should eq 'Miscellaneous' + headings[1].display_order.should eq 1 + end + + end + + context 'when validating' do + + it 'should require a name' do + heading = PublicBodyHeading.new + heading.should_not be_valid + heading.errors[:name].should == ["Name can't be blank"] + end + + it 'should require a unique name' do + heading = FactoryGirl.create(:public_body_heading) + new_heading = PublicBodyHeading.new(:name => heading.name) + new_heading.should_not be_valid + new_heading.errors[:name].should == ["Name is already taken"] + end + + it 'should set a default display order based on the next available display order' do + heading = PublicBodyHeading.new + heading.valid? + heading.display_order.should == PublicBodyHeading.next_display_order + end + end + + context 'when setting a display order' do + + it 'should return 0 if there are no public body headings' do + PublicBodyHeading.next_display_order.should == 0 + end + + it 'should return one more than the highest display order if there are public body headings' do + heading = FactoryGirl.create(:public_body_heading) + PublicBodyHeading.next_display_order.should == 1 + end + end +end |