require 'spec_helper' describe DigitalAsset do subject { FactoryGirl.build :digital_asset } context '#fields' do let(:defined_fields) { [:title, :changed_at, :sami_code, :summary, :published_at, :expires_at, :digital_asset_id, :audiences, :path, :doc_changed_at,:content_type,:pages, :size, :mime_type,:subject, :keywords, :author, :finra_path, :fund_codes] } it 'has all defined fields' do defined_fields.each {|f| should respond_to(f)} end end context '#validation' do required_fields = [:digital_asset_id, :title, :changed_at, :published_at, :expires_at, :path] 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 a past expiration date' do subject.expires_at = 2.hours.ago subject.should be_invalid end it "cannot have duplicate path" do FactoryGirl.create :digital_asset, :path => "/some/path.pdf" digital_asset = FactoryGirl.build :digital_asset, :path => "/some/path.pdf" # p "errors = #{digital_asset.errors.inspect}" digital_asset.should be_invalid end it "cannot have duplicate digital_asset_id" do FactoryGirl.create :digital_asset, :digital_asset_id => "aaaa_kkkk_1234" digital_asset = FactoryGirl.build :digital_asset, :digital_asset_id => "aaaa_kkkk_1234" # p "errors = #{digital_asset.errors.inspect}" digital_asset.should be_invalid end end context "purge!" do before do 50.times { FactoryGirl.create :digital_asset } FactoryGirl.create :digital_asset, :updated_at => 10.minutes.ago end it 'removes all assets more than 5 minutes stale if bulk file has been processed' do expect { DigitalAsset.purge! }.to change(DigitalAsset, :count).by(-1) end end context "#latest_doc_changed_at" do before do @one_day_ago = 1.day.ago @asset = FactoryGirl.create :digital_asset, :audiences => ['690'], :sami_code => 'F000000.BAR' @asset.doc_changed_at = @one_day_ago end it 'returns latest doc changed at date which is not finra' do result = @asset.latest_doc_changed_at expect(result.to_i).to eq @one_day_ago.to_i end end context '#finders' do before do @asset = FactoryGirl.create :digital_asset, :audiences => ['690'], :sami_code => 'F000000.BAR' FactoryGirl.create :digital_asset, :sami_code => 'MEH12345.000', :updated_at => 1.hour.ago end it 'has a finder by fund code' do DigitalAsset.should respond_to(:fund_in) DigitalAsset.fund_in(['00190']).should have(2).digital_asset end it 'has a finder by sami_code' do DigitalAsset.should respond_to(:sami_is) DigitalAsset.sami_is('F000000.BAR').should have(1).digital_asset end it 'has a finder by the embedded doc path' do DigitalAsset.should respond_to(:path_is) DigitalAsset.path_is(@asset.path).should have(1).digital_asset end it 'has a type finder for the documents' do DigitalAsset.should respond_to(:doctype_in) DigitalAsset.doctype_in(['fact_sheet']).should have(2).digital_asset end it 'has a finder for stale documents' do DigitalAsset.should respond_to(:stale) DigitalAsset.stale.should have(1).item end it 'has a method that tells you if the bulk-file processing is working' do DigitalAsset.should respond_to(:bulk_processed?) 2.times { FactoryGirl.create :digital_asset } 2.times { FactoryGirl.create :digital_asset, :updated_at => 10.minutes.ago } DigitalAsset.count.should == 6 DigitalAsset.stale.count.should == 3 DigitalAsset.should_not be_bulk_processed 54.times { FactoryGirl.create :digital_asset } DigitalAsset.should be_bulk_processed end it "does not return expired docs" do pending "this does not work due to mongoid not allowing dynamic default scoes" FactoryGirl.create :digital_asset, fund_codes: ['69'] @expired = FactoryGirl.create :digital_asset, fund_codes: ['69'] @expired.update_attribute(:expires_at, 2.hours.ago) DigitalAsset.fund_in(['69']).should have(1).item end end end