diff --git a/app/helpers/sync_helper.rb b/app/helpers/sync_helper.rb index b5350105..dbe9bb42 100644 --- a/app/helpers/sync_helper.rb +++ b/app/helpers/sync_helper.rb @@ -1,21 +1,23 @@ +# encoding: utf-8 + module SyncHelper def dirtify(save_it = true) - dirty = true - attempts = 0 - save! if save_it + self.dirty = true + self.attempts = 0 + self.save! if save_it end def undirtify(save_it = true) - dirty = false - attempts = 0 - save! if save_it + self.dirty = false + self.attempts = 0 + self.save! if save_it end def increment_attempts - increment!(:attempts) + self.increment!(:attempts) end def max_out_attempts - update_attributes!(:attempts => Settings.notes.attempts + 1) + self.update_attributes!(attempts: Settings.notes.attempts + 1) end end diff --git a/app/models/evernote_note.rb b/app/models/evernote_note.rb index d6b7dc53..7c4d2fe3 100644 --- a/app/models/evernote_note.rb +++ b/app/models/evernote_note.rb @@ -56,14 +56,14 @@ def syncdown_one logger.info I18n.t('notes.sync.rejected.tag_missing', error_details) undirtify end - + elsif cloud_note_ignorable?(cloud_note_tags) logger.info I18n.t('notes.sync.rejected.ignore', error_details) - max_out_attempts + undirtify elsif cloud_note_updated?(cloud_note_metadata.updated) logger.info I18n.t('notes.sync.rejected.not_latest', error_details) - max_out_attempts + undirtify else # If this is a new EvernoteNote, we only create a note here since many CloudNotes will be created diff --git a/spec/models/cloud_note_spec.rb b/spec/models/cloud_note_spec.rb index 94721293..9fdebd86 100644 --- a/spec/models/cloud_note_spec.rb +++ b/spec/models/cloud_note_spec.rb @@ -1,10 +1,14 @@ -describe CloudNote do +# encoding: utf-8 +describe CloudNote do let(:note) { FactoryGirl.build_stubbed(:note) } let(:cloud_service) { FactoryGirl.build_stubbed(:cloud_service) } - before { - @cloud_note = FactoryGirl.build_stubbed(:cloud_note, :note => note, :cloud_service => cloud_service) - } + before do + @cloud_note = FactoryGirl.build_stubbed( + :cloud_note, + note: note, + cloud_service: cloud_service) + end subject { @cloud_note } @@ -17,6 +21,11 @@ it { should respond_to(:content_hash) } it { should respond_to(:update_sequence_number) } + it { should respond_to(:dirtify) } + it { should respond_to(:undirtify) } + it { should respond_to(:max_out_attempts) } + it { should respond_to(:increment_attempts) } + it { should belong_to(:note) } it { should belong_to(:cloud_service) } @@ -27,62 +36,85 @@ # it { should validate_presence_of(:note) } it { should validate_presence_of(:cloud_service) } - it { should validate_uniqueness_of(:cloud_note_identifier).scoped_to(:cloud_service_id) } + it do + should validate_uniqueness_of(:cloud_note_identifier) + .scoped_to(:cloud_service_id) + end - describe "need_syncdown scope should contain all dirty notes" do - before { - @cloud_note = FactoryGirl.create(:cloud_note, :dirty => true, :attempts => 0) - } - CloudNote.need_syncdown.last.should == @cloud_note + describe 'dirtify should mark it dirty' do + before { @cloud_note.dirtify } + its(:dirty) { should == true } + its(:attempts) { should == 0 } + end + + describe 'undirtify should mark it not dirty' do + before do + @cloud_note = FactoryGirl.create(:cloud_note, dirty: true, attempts: 1) + @cloud_note.undirtify + end + its(:dirty) { should == false } + its(:attempts) { should == 0 } end - describe "need_syncdown scope should increment attempts when requested to" do - before { - @cloud_note = FactoryGirl.create(:cloud_note, :attempts => 0) + describe 'increment_attempts should increment attempts' do + before do + @cloud_note = FactoryGirl.create(:cloud_note, attempts: 0) @cloud_note.increment_attempts - } + end its(:attempts) { should == 1 } end - describe "need_syncdown scope should not contain dirty notes that have been retried too often" do - before { - @cloud_note = FactoryGirl.create(:cloud_note, :dirty => true, :attempts => Settings.notes.attempts + 1) - } + describe 'max_out_attempts should increment attempts' do + before { @cloud_note.max_out_attempts } + its(:attempts) { should >= Settings.notes.attempts } + end + + describe 'need_syncdown should contain all dirty notes' do + before do + @cloud_note = FactoryGirl.create(:cloud_note, dirty: true, attempts: 0) + end + CloudNote.need_syncdown.last.should == @cloud_note + end + + describe 'need_syncdown should not contain dirty notes retried too often' do + before do + @cloud_note = FactoryGirl.create(:cloud_note, + dirty: true, + attempts: Settings.notes.attempts + 1) + end CloudNote.need_syncdown.last.should == nil end - describe "a cloud_note is disincluded from need_syncdown when max_out method is applied to it" do - before { - @cloud_note = FactoryGirl.create(:cloud_note, :dirty => true) + describe 'is disincluded from need_syncdown after max_out_attempts' do + before do + @cloud_note = FactoryGirl.create(:cloud_note, dirty: true) @cloud_note.increment_attempts @cloud_note.max_out_attempts - } + end CloudNote.need_syncdown.last.should == nil end - describe "run_evernote_tasks should syncdown pending notes" do - before { - } - it "should de-activate a note that is not in a required notebook" do - # xxx + describe 'run_evernote_tasks should syncdown pending notes' do + it 'should de-activate a note that is not in a required notebook' do + pending 'Need to add this test.' end - it "should de-activate a note that has been deleted in the cloud notebook" do - # xxx + it 'should de-activate a note that has been deleted in the cloud' do + pending 'Need to add this test.' end - it "should de-activate a note not tagged with __PUBLISH (or synonyms)" do - # xxx + it 'should de-activate a note not tagged with __PUBLISH (or synonyms)' do + pending 'Need to add this test.' end - it "should not update a note tagged with __IGNORE (or synonyms)" do - # xxx + it 'should not update a note tagged with __IGNORE (or synonyms)' do + pending 'Need to add this test.' end - it "should not request full data if content hash has not changed" do - # xxx + it 'should not request full data if content hash has not changed' do + pending 'Need to add this test.' end - it "should request full data if content hash has changed" do - # xxx + it 'should request full data if content hash has changed' do + pending 'Need to add this test.' end - it "should syncdown resource if it has changed" do - # xxx + it 'should syncdown resource if it has changed' do + pending 'Need to add this test.' end end end diff --git a/spec/models/resource_spec.rb b/spec/models/resource_spec.rb index 827c6295..06b43aaf 100644 --- a/spec/models/resource_spec.rb +++ b/spec/models/resource_spec.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + describe Resource do let(:note) { FactoryGirl.create(:note) } before { @resource = FactoryGirl.create(:resource, note: note) } @@ -31,10 +33,43 @@ it { should respond_to(:dirty) } it { should respond_to(:attempts) } + it { should respond_to(:dirtify) } + it { should respond_to(:undirtify) } + it { should respond_to(:max_out_attempts) } + it { should respond_to(:increment_attempts) } + it { should validate_presence_of(:note) } it { should validate_presence_of(:cloud_resource_identifier) } it { should validate_uniqueness_of(:cloud_resource_identifier) } + describe 'dirtify should mark it dirty' do + before { @resource.dirtify } + its(:dirty) { should == true } + its(:attempts) { should == 0 } + end + + describe 'undirtify should mark it not dirty' do + before do + @resource = FactoryGirl.create(:resource, dirty: true, attempts: 1) + @resource.undirtify + end + its(:dirty) { should == false } + its(:attempts) { should == 0 } + end + + describe 'increment_attempts should increment attempts' do + before do + @resource = FactoryGirl.create(:resource, attempts: 0) + @resource.increment_attempts + end + its(:attempts) { should == 1 } + end + + describe 'max_out_attempts should increment attempts' do + before { @resource.max_out_attempts } + its(:attempts) { should >= Settings.notes.attempts } + end + describe 'file_ext should return correct file extension' do before { @resource.update_attributes(mime: 'image/png') } its(:file_ext) { should == 'png' } @@ -42,22 +77,20 @@ describe 'blank_location should return path for blank file of same format' do before { @resource.update_attributes(mime: 'image/png') } - its(:blank_location) { should == File.join(Rails.root, 'public', 'resources', 'cut', 'blank.png') } + its(:blank_location) do + should == File.join(Rails.root, 'public', 'resources', 'cut', + 'blank.png') + end end describe 'cut_location should return path to the cut image' do - pending 'Need to add this test' - before { @resource.update_attributes(caption: 'IMAGE CAPTION') } - # its.cut_location(160, 90, 500, 0, 0, 0).should =~ /\/public\/resources\/cut\/image-caption-160-90-500-0-0-0.png/ + pending 'Need to add this test.' + # its.cut_location(160, 90, 500, 0, 0, 0).should + # =~ /\/public\/resources\/cut\/image-caption-160-90-500-0-0-0.png/ end describe 'template_location should return path to the cut image' do - pending 'Need to add this test' - # before { - # @resource = FactoryGirl.create(:resource, :note => note, :mime => 'image/png', :caption => 'IMAGE CAPTION') - # } - # @resource.cut_location(160, 90, 500, 0, 0, 0).should =~ /\/public\/resources\/cut\/image-caption-160-90-500-0-0-0.png/ - # @resource.template_location(16, 9).should =~ /\/public\/resources\/templates\/#{ @resource.cloud_resource_identifier }-16-9.png/ + pending 'Need to add this test.' end describe 'need_syncdown scope should contain all dirty resources' do @@ -65,41 +98,71 @@ Resource.need_syncdown.last.should == @resource end - describe 'needs_syncdown scope should not contain dirty resources that have been retried too often' do - before { @resource.update_attributes(dirty: true, attempts: Settings.notes.attempts + 1) } + describe 'needs_syncdown should not contain maxed_out, dirty resources' do + before do + @resource.update_attributes(dirty: true, + attempts: Settings.notes.attempts + 1) + end Resource.need_syncdown.last.should == nil end - describe 'local_file_name is set to file_name if mime type is not image and file_name is available' do - before { @resource.update_attributes(mime: 'application/pdf', file_name: 'ORIGINAL FILE NAME') } + describe 'local_file_name is set to file_name if mime type is not image and + file_name is available' do + before do + @resource.update_attributes(mime: 'application/pdf', + file_name: 'ORIGINAL FILE NAME') + end its(:local_file_name) { should == 'original-file-name' } end - describe 'local_file_name is set to caption if mime type is image and caption is available' do + describe 'local_file_name is set to caption if mime type is image and caption + is available' do before { @resource.update_attributes(caption: 'IMAGE CAPTION') } its(:local_file_name) { should == 'image-caption' } end - describe 'local_file_name is set to description if mime type is image, caption is nil and description is available' do - before { @resource.update_attributes(caption: nil, description: nil, file_name: 'IMAGE DESCRIPTION') } + describe 'local_file_name is set to description if mime type is image, + caption is nil and description is available' do + before do + @resource.update_attributes(caption: nil, + description: nil, + file_name: 'IMAGE DESCRIPTION') + end its(:local_file_name) { should == 'image-description' } end - describe 'local_file_name is set to file_name if mime type is image, caption is nil, description is nil, and file_name is available' do - before { @resource.update_attributes(caption: nil, description: nil, file_name: 'ORIGINAL FILE NAME') } + describe 'local_file_name is set to file_name if mime type is image, caption + is nil, description is nil, and file_name is available' do + before do + @resource.update_attributes(caption: nil, + description: nil, + file_name: 'ORIGINAL FILE NAME') + end its(:local_file_name) { should == 'original-file-name' } end - describe 'local_file_name is set to cloud_resource_identifier if mime type is image, file_name is empty and all else is nil' do - before { @resource.update_attributes(caption: nil, description: nil, file_name: '') } - its(:local_file_name) { should == @resource.cloud_resource_identifier.parameterize } + describe 'local_file_name is set to cloud_resource_identifier if mime type is + image, file_name is empty and all else is nil' do + before do + @resource.update_attributes(caption: nil, + description: nil, + file_name: '') + end + its(:local_file_name) do + should == @resource.cloud_resource_identifier.parameterize + end end - describe 'local_file_name is set to cloud_resource_identifier if mime type is image and all else is nil' do - before { - @resource.update_attributes(caption: nil, description: nil, file_name: '') + describe 'local_file_name is set to cloud_resource_identifier if mime type is + image and all else is nil' do + before do + @resource.update_attributes(caption: nil, + description: nil, + file_name: '') @resource.valid? - } - its(:local_file_name) { should == @resource.cloud_resource_identifier.parameterize } + end + its(:local_file_name) do + should == @resource.cloud_resource_identifier.parameterize + end end end