Files
simply_based/spec/conflict_handling_spec.rb
GregandClaude Opus 4.8 86299b8827 SimplyBased 0.1.0 — Couchbase-native ActiveModel ORM
A from-scratch fork of simply_couch's model layer that runs on Couchbase
natively: `include SimplyBased::Model` instead of `include SimplyCouch::Model`.

- SimplyBased::Store replaces the CouchDB layer: KV documents, N1QL/SQL++
  finders + lazily-created GSIs, CAS mapped onto _rev. No design documents or
  JavaScript map/reduce — the hand-written association views become N1QL
  (belongs_to/has_many/has_one scalar FK; habtm via ANY ... SATISFIES + array
  index, or KV multi-get on the storing side).
- Redis replacements on the same cluster: Counter (atomic increment/decrement),
  Cache (ActiveSupport::Cache::Store over KV+TTL, drop-in Rails.cache/ActiveJob),
  Lock (insert-if-absent + TTL + CAS-checked release).
- Runs the simply_couch test suite against live Couchbase: 378/432 applicable
  examples green (CouchDB-mechanic specs tagged :couchdb_only and excluded).
- Remaining: ancestry (tree) + embedded_in reverse-lookups (recursive/embedded
  N1QL) and a few pagination/lifecycle edges.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-20 13:58:47 -05:00

119 lines
3.5 KiB
Ruby

require 'spec_helper'
describe "ConflictHandling" do
let(:original) { User.create name: 'Mickey Mouse', title: "Dr.", homepage: 'www.gmx.de'}
let!(:copy) { User.find(original.id) }
context "when handling conflicts" do
before do
User.auto_conflict_resolution_on_save = true
end
it "should be able to save without modifications" do
copy.save.should be true
end
it "be able to save when modification happen on different attributes" do
original.name = "Pluto"
original.save.should be true
copy.title = 'Prof.'
expect { copy.save }.not_to raise_error
copy.reload.name.should eq "Pluto"
copy.reload.title.should eq "Prof."
copy.reload.homepage.should eq "www.gmx.de"
end
it "be able to save when modification happen on different, multiple attributes - remote" do
original.name = "Pluto"
original.homepage = 'www.google.com'
original.save.should be true
copy.title = 'Prof.'
expect { copy.save }.not_to raise_error
copy.reload.name.should eq "Pluto"
copy.reload.title.should eq "Prof."
copy.reload.homepage.should eq "www.google.com"
end
it "be able to save when modification happen on different, multiple attributes locally" do
original.name = "Pluto"
original.save.should be true
copy.title = 'Prof.'
copy.homepage = 'www.google.com'
expect { copy.save }.not_to raise_error
copy.reload.name.should eq "Pluto"
copy.reload.title.should eq "Prof."
copy.reload.homepage.should eq "www.google.com"
end
it "re-raise the conflict if there is no merge possible" do
original.name = "Pluto"
original.save.should be true
copy.name = 'Prof.'
expect { copy.save }.to raise_error SimplyBased::Conflict
copy.name.should eq "Prof."
copy.reload.name.should eq "Pluto"
end
it "re-raise the conflict if retried several times" do
allow( User.database ).to receive(:save_document).and_raise(SimplyBased::Conflict.new)
copy.name = 'Prof.'
expect { copy.save }.to raise_error SimplyBased::Conflict
end
it "not try to merge and re-save if auto_conflict_resolution_on_save is disabled" do
User.auto_conflict_resolution_on_save = false
allow( User.database ).to receive(:save_document).and_raise(SimplyBased::Conflict.new)
copy.name = 'Prof.'
expect { copy.save }.to raise_error SimplyBased::Conflict
end
context "with conflict information" do
it "add information about the conflict to the exception" do
original.name = "Pluto"
original.save.should be true
copy.name = 'Prof.'
begin
copy.save
rescue SimplyBased::Conflict => e
e.message.should eq '409 Conflict - conflict on attributes: ["name"]'
end
end
it "only add the conflict information to one exception" do
other_copy = User.find(original.id)
original.name = "Pluto"
original.title = "Frau"
original.save.should be true
copy.name = 'Prof.'
begin
copy.save.should be true
rescue SimplyBased::Conflict => e
e.message.should eq '409 Conflict - conflict on attributes: ["name"]'
end
other_copy.title = 'Prof.'
begin
other_copy.save
rescue SimplyBased::Conflict => e
e.message.should eq '409 Conflict - conflict on attributes: ["title"]'
end
end
end
end
end