require 'spec_helper' describe ContentFolder do context 'fields' do subject { FactoryGirl.build :content_folder } let(:defined_fields) { [:folder_id, :label, :document_ids, :child_folders, :documents] } it 'has all defined fields' do defined_fields.each { |f| should respond_to(f) } end end context "folder with assets" do subject do FactoryGirl.build :content_folder end it 'has assets' do #puts "#{subject.inspect}" asset_one = FactoryGirl.create :digital_asset asset_two = FactoryGirl.create :digital_asset, :digital_asset_id => "guid_1234" subject.documents << [asset_one, asset_two] subject.save #puts "#{subject.inspect}" #puts "#{subject.documents.first.id}" #puts "#{asset_one.id}" subject.document_ids.should_not be_empty end end context 'validation' do subject { FactoryGirl.build :content_folder_with_document_ids, :folder_id => "folder_id_1" } let(:basic_subject) { FactoryGirl.build :content_folder, :folder_id => "folder_id_2" } let(:subject_with_folders) { FactoryGirl.build :content_folder_with_child_folders, :folder_id => "folder_id_3" } let(:a_folder) { FactoryGirl.build :content_folder, :folder_id => "foo" } let(:dupe_folder) { FactoryGirl.build :content_folder, :folder_id => "foo" } 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 'can have empty child_folders AND document_ids' do #puts basic_subject.inspect basic_subject.should be_valid end it 'is valid when document_ids are present' do #puts subject.inspect subject.should be_valid end it 'is valid when child_folders are present' do #puts subject_with_folders.inspect #puts subject_with_folders.child_folders.inspect subject_with_folders.should be_valid end it 'is valid to have duplicate folder_ids' do basic_subject.child_folders.push [a_folder, dupe_folder] basic_subject.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 'saved content folders can be retrieved' do ContentFolder.delete_all id = subject_with_folders.child_folders.first.folder_id subject_with_folders.save found = ContentFolder.find_folder(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 ttpf = FactoryGirl.build :content_folder_with_document_ids, :folder_id => "ttpf" plan_529.child_folders = [ttpf] 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 it 'should return folder by relative path' do path = ContentFolder::FolderIds::PLANS_529 + "/ttpf" result = ContentFolder.find_folder_by_relative_path(path) result.should_not be_nil end it 'should return child folder' do root = ContentFolder.last result = ContentFolder.find_child_folder(root, [ContentFolder::FolderIds::PLANS_529, "ttpf"]) result.should_not be_nil end end context 'content folder and asset association' do before do ContentFolder.delete_all root = FactoryGirl.build :content_folder_with_document_ids lit_center = FactoryGirl.build :content_folder, :folder_id => ContentFolder::FolderIds::LIT_CENTER merrill_lynch = FactoryGirl.build :content_folder, :folder_id => ContentFolder::FolderIds::MERRILL_LYNCH plan_529 = FactoryGirl.build :content_folder, :folder_id => ContentFolder::FolderIds::PLANS_529 asset_one = FactoryGirl.create :digital_asset, :digital_asset_id => "guid_1111" asset_two = FactoryGirl.create :digital_asset, :digital_asset_id => "guid_2222" asset_three = FactoryGirl.create :digital_asset, :digital_asset_id => "guid_3333" lit_center.documents << [asset_one, asset_two, asset_three] merrill_lynch.documents << [asset_two, asset_three] plan_529.documents << [asset_three] root.child_folders = [lit_center, merrill_lynch, plan_529] root.save end it 'should return the lit center folder with document count of 3' do result = ContentFolder.lit_center_folder result.should_not be_nil result.document_ids.count.should == 3 result.folder_id.should == ContentFolder::FolderIds::LIT_CENTER end it 'should return the merrill_lynch folder with document count of 2' do result = ContentFolder.merrill_lynch_folder result.should_not be_nil result.document_ids.count.should == 2 result.folder_id.should == ContentFolder::FolderIds::MERRILL_LYNCH end it 'delete an asset record from database, associated with a folder' do result = ContentFolder.plan_529_folder #puts "==== #{result.inspect}" #puts "Documents: #{result.documents.inspect}" result.documents.count.should == 1 asset3 = DigitalAsset.where(digital_asset_id: "guid_3333").exists? DigitalAsset.where(digital_asset_id: "guid_3333").delete_all #puts "Documents: #{result.documents.inspect}" result.documents.count.should == 0 result.folder_id.should == ContentFolder::FolderIds::PLANS_529 end end end