Sha256: ba16c7234b42281cf9ca8b2fbc3c63c3dc51856ec481fa25aaac19f69f55ed92

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

require 'rdf/spec'

RSpec.shared_examples 'an RDF::Transactable' do
  include RDF::Spec::Matchers

  let(:statements) { @rdf_transactable_iv_statements = RDF::Spec.quads }

  before do
    raise '`transactable` must be set with `let(:transactable)`' unless
      defined? transactable
  end

  subject { transactable }

  describe "#transaction" do
    it 'gives an immutable transaction' do
      expect { subject.transaction { insert([]) } }.to raise_error TypeError
    end

    it 'commits a successful transaction' do
      statement = RDF::Statement(RDF::URI('http://example.com/s'), 
                                 RDF.type, 
                                 RDF::URI('http://example.com/o'))
      expect(subject).to receive(:commit_transaction).and_call_original
      
      expect do
        subject.transaction(mutable: true) { insert(statement) }
      end.to change { subject.statements }.to include(statement)
    end

    it 'rolls back a failed transaction' do
      original_contents = subject.statements
      expect(subject).to receive(:rollback_transaction).and_call_original

      expect do
        subject.transaction(mutable: true) do
          delete(*statements)
          raise 'my error'
        end
      end.to raise_error RuntimeError

      expect(subject.statements).to contain_exactly(*original_contents)
    end

    context 'without block given' do
      it 'returns a transaction' do
        expect(subject.transaction).to be_a RDF::Transaction
      end

      it 'the returned transaction is live' do
        tx = subject.transaction(mutable: true)
        tx.insert(RDF::Statement(:s, RDF.type, :o))
        expect { tx.execute }.not_to raise_error
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rdf-spec-3.3.0 lib/rdf/spec/transactable.rb
rdf-spec-3.2.0 lib/rdf/spec/transactable.rb
rdf-spec-3.1.2 lib/rdf/spec/transactable.rb
rdf-spec-3.1.1 lib/rdf/spec/transactable.rb
rdf-spec-3.1.0 lib/rdf/spec/transactable.rb