Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Fixes dirtification and undirtification of objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
joegatt committed May 25, 2013
1 parent f08fab6 commit e30a25e
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 77 deletions.
18 changes: 10 additions & 8 deletions app/helpers/sync_helper.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions app/models/evernote_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
110 changes: 71 additions & 39 deletions spec/models/cloud_note_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }

Expand All @@ -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) }

Expand All @@ -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
117 changes: 90 additions & 27 deletions spec/models/resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

describe Resource do
let(:note) { FactoryGirl.create(:note) }
before { @resource = FactoryGirl.create(:resource, note: note) }
Expand Down Expand Up @@ -31,75 +33,136 @@
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' }
end

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
before { @resource.update_attributes(dirty: true, attempts: 0) }
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

0 comments on commit e30a25e

Please sign in to comment.