require 'spec_helper' describe ContentFolder do context 'fields' do subject { FactoryGirl.build :content_folder } let(:defined_fields) { [:folder_id, :label, :document_ids, :child_folders] } it 'has all defined fields' do defined_fields.each { |f| should respond_to(f) } end end context 'validation' do subject { FactoryGirl.build :content_folder_with_document_ids } let(:basic_subject) { FactoryGirl.build :content_folder } let(:subject_with_folders) { FactoryGirl.build :content_folder_with_child_folders } required_fields = [:folder_id, :label] required_fields.each do |f| it "validates #{f} is required" do should be_valid subject.send("#{f}=", nil) should be_invalid "should be invalid if #{f} is missing" end end it 'cannot have empty child_folders AND document_ids' do basic_subject.should be_invalid end it 'is valid when document_ids are present' do subject.should be_valid end it 'is valid when child_folders are present' do subject_with_folders.should be_valid end end context 'selectors' do subject { FactoryGirl.build :content_folder_with_document_ids } # TODO: add tests for known ids end context 'db operations' do let(:subject_with_folders) { FactoryGirl.build :content_folder_with_child_folders } it 'saves and can be retrieved' do id = subject_with_folders.folder_id subject_with_folders.save found = ContentFolder.find(id) found.should_not be_nil end end context 'folder retrieval' do before do ContentFolder.delete_all root = FactoryGirl.build :content_folder_with_document_ids lit_center = FactoryGirl.build :content_folder_with_document_ids, :folder_id => ContentFolder::FolderIds::LIT_CENTER merrill_lynch = FactoryGirl.build :content_folder_with_document_ids, :folder_id => ContentFolder::FolderIds::MERRILL_LYNCH plan_529 = FactoryGirl.build :content_folder_with_document_ids, :folder_id => ContentFolder::FolderIds::PLANS_529 other = FactoryGirl.build :content_folder_with_document_ids, :folder_id => 'abcdefg' root.child_folders = [lit_center, merrill_lynch, plan_529, other] root.save end it 'should return the lit center folder' do result = ContentFolder.lit_center_folder result.should_not be_nil result.folder_id.should == ContentFolder::FolderIds::LIT_CENTER end it 'should return the merrill lynch folder' do result = ContentFolder.merrill_lynch_folder result.should_not be_nil result.folder_id.should == ContentFolder::FolderIds::MERRILL_LYNCH end it 'should return the 529 folder' do result = ContentFolder.plan_529_folder result.should_not be_nil result.folder_id.should == ContentFolder::FolderIds::PLANS_529 end it 'should return nil for an unknown folder' do result = ContentFolder.find_folder('zyxw') result.should be_nil end end end