Sha256: 0fc39330a090c3cd487746ccab4a660e27934f9e5dbcef6849dfdd065972f2b9

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

require 'spec_helper'

describe 'conflict handling' do
  let(:db) { CouchPotato::Database.new(CouchPotato.couchrest_database) }
  class Measurement
    include CouchPotato::Persistence

    property :value
  end

  it 're-runs the block with a reloaded instance and saves again when there is a conflict' do
    measurement = Measurement.new value: 1
    db.save! measurement

    db.couchrest_database.save_doc measurement.reload._document.merge('value' => 2)

    measurement.is_dirty
    db.save measurement do |m|
      m.value += 1
    end

    expect(measurement.reload.value).to eql(3)
  end

  it 'raises an error after 5 tries' do
    couchrest_database = stub(:couchrest_database, info: stub.as_null_object)
    couchrest_database.stub(:save_doc).and_raise(RestClient::Conflict)
    db = CouchPotato::Database.new(couchrest_database)
    measurement = stub(:measurement).as_null_object
    measurement.stub(:run_callbacks).and_yield

    expect {
      db.save(measurement, false) {|m| }
    }.to raise_error(CouchPotato::Conflict)
  end

  it 'runs the block before saving' do
    measurement = Measurement.new value: 1
    db.save! measurement

    db.save measurement do |m|
      m.value += 1
    end

    expect(measurement.reload.value).to eql(2)
  end

  it 'retries destroying a document' do
    measurement = Measurement.new value: 1
    db.save! measurement

    db.couchrest_database.save_doc measurement.reload._document.merge('value' => 2)
    db.destroy measurement

    expect(measurement.reload).to be_nil
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
couch_potato-1.4.0 spec/conflict_handling_spec.rb
couch_potato-1.3.0 spec/conflict_handling_spec.rb
couch_potato-1.2.0 spec/conflict_handling_spec.rb
couch_potato-1.1.4 spec/conflict_handling_spec.rb
couch_potato-1.1.2 spec/conflict_handling_spec.rb
couch_potato-1.1.1 spec/conflict_handling_spec.rb
couch_potato-1.1.0 spec/conflict_handling_spec.rb