<commit_before>require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))

require 'reservations/party_lodging'
require 'reservations/lodging'
require 'reservations/party'
require 'reservations/reservation'

describe "GraphMediator locking scenarios" do

  before(:all) do
    load 'reservations/schema.rb'
  end

  before(:each) do
    @today = Date.today
    @handle1_r1 = Reservation.create!(:starts => @today, :ends => @today + 1, :name => 'foo')
    @handle2_r1 = Reservation.find(@handle1_r1.id)
  end

  it "should be possible to create an unattached dependent object" do
    Lodging.create!.should_not be_nil
  end

  context "with optimistic locking for the graph" do

    it "should raise Stale for conflicts updating root" do
      @handle2_r1.update_attributes(:name => 'bar')
      lambda { @handle1_r1.update_attributes(:name => 'baz') }.should raise_error(ActiveRecord::StaleObjectError)
      @handle1_r1.reload
      @handle1_r1.name.should == 'bar' 
    end

    # Creating root cannot conflict
    # because the rows do not exist until the mediation transaction completes
    # so no one else is in a position to write first

    # not possible?  ActiveRecord::Locking::Optimistic does not decorate :destroy
    # it "should raise stale for conflicts deleting root" do

    it "should raise Stale for conflicts creating children" do
      @handle2_r1.parties.create(:name => 'Bob')
      lambda { @handle1_r1.update_attributes(:name => 'baz') }.should raise_error(ActiveRecord::StaleObjectError)
    end

    context "with children" do
      
      before(:each) do
        @handle1_r1.mediated_transaction do
          @party1 = @handle1_r1.parties.create(:name => 'Bob')<commit_msg>test that save_without_mediation still increments lock_version<commit_after>require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))

require 'reservations/party_lodging'
require 'reservations/lodging'
require 'reservations/party'
require 'reservations/reservation'

describe "GraphMediator locking scenarios" do

  before(:all) do
    load 'reservations/schema.rb'
  end

  before(:each) do
    @today = Date.today
    @handle1_r1 = Reservation.create!(:starts => @today, :ends => @today + 1, :name => 'foo')
    @handle2_r1 = Reservation.find(@handle1_r1.id)
  end

  it "should be possible to create an unattached dependent object" do
    Lodging.create!.should_not be_nil
  end

  it "should increment lock_version for save_without_mediation" do
    lambda {
      @handle1_r1.starts = @today - 1
      @handle1_r1.save_without_mediation
    }.should change(@handle1_r1, :lock_version).by(1)
  end

  context "with optimistic locking for the graph" do

    it "should raise Stale for conflicts updating root" do
      @handle2_r1.update_attributes(:name => 'bar')
      lambda { @handle1_r1.update_attributes(:name => 'baz') }.should raise_error(ActiveRecord::StaleObjectError)
      @handle1_r1.reload
      @handle1_r1.name.should == 'bar' 
    end

    # Creating root cannot conflict
    # because the rows do not exist until the mediation transaction completes
    # so no one else is in a position to write first

    # not possible?  ActiveRecord::Locking::Optimistic does not decorate :destroy
    # it "should raise stale for conflicts deleting root" do

    it "should raise Stale for conflicts creating children" do
      @handle2_r1.parties.create(:name => 'Bob')
      lambda { @handle1_r1.update_attributes(:name => 'baz') }.should raise_error(ActiveRecord::StaleObjectError)
    end

    context "with children" do
      
      before(:each) do
        @handle1_r1.mediated_transaction do
          @party1 = @handle1_r1.parties.create(:name => 'Bob')
{'fraction_non_alphanumeric': 0.10164022586716859, 'fraction_numerical': 0.015057811239580532, 'mean_word_length': 3.888304862023653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3938515', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1129, 'n_words': 318}