Skip to content

Commit fb1eadb

Browse files
author
Tom Johnson
committed
Extract Transactable behavior to module
1 parent cc0c7da commit fb1eadb

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

lib/rdf/spec/repository.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
require 'rdf/spec/dataset'
2424
it_behaves_like 'an RDF::Dataset'
2525
end
26+
27+
context 'as transactable' do
28+
require 'rdf/spec/transactable'
29+
let(:transactable) { repository }
30+
it_behaves_like 'an RDF::Transactable'
31+
end
2632

2733
context "when updating" do
2834
require 'rdf/spec/mutable'

lib/rdf/spec/transactable.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'rdf/spec'
2+
3+
RSpec.shared_examples 'an RDF::Transactable' do
4+
include RDF::Spec::Matchers
5+
6+
let(:statements) { RDF::Spec.quads }
7+
8+
before do
9+
raise '`transactable` must be set with `let(:transactable)`' unless
10+
defined? transactable
11+
end
12+
13+
subject { transactable }
14+
15+
describe "#transaction" do
16+
it 'gives an immutable transaction' do
17+
expect { subject.transaction { insert([]) } }.to raise_error TypeError
18+
end
19+
20+
it 'commits a successful transaction' do
21+
statement = RDF::Statement(:s, RDF.type, :o)
22+
expect(subject).to receive(:commit_transaction).and_call_original
23+
24+
expect do
25+
subject.transaction(mutable: true) { insert(statement) }
26+
end.to change { subject.statements }.to include(statement)
27+
end
28+
29+
it 'rolls back a failed transaction' do
30+
original_contents = subject.statements
31+
expect(subject).to receive(:rollback_transaction).and_call_original
32+
33+
expect do
34+
subject.transaction(mutable: true) do
35+
delete(*@statements)
36+
raise 'my error'
37+
end
38+
end.to raise_error RuntimeError
39+
40+
expect(subject.statements).to contain_exactly(*original_contents)
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)