-
Notifications
You must be signed in to change notification settings - Fork 10
/
transaction.rb
162 lines (136 loc) · 4.56 KB
/
transaction.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 'rdf/spec'
# Pass in an instance of RDF::Transaction as follows:
#
# it_behaves_like "RDF::Transaction", RDF::Transaction
shared_examples "an RDF::Transaction" do |klass|
include RDF::Spec::Matchers
subject { klass.new(graph_name: RDF::URI("name")) }
describe "#initialize" do
subject {klass}
it "accepts a graph" do
g = double("graph")
this = subject.new(graph: g)
expect(this.graph).to eq g
end
it "accepts a graph_name" do
c = double("graph_name")
this = subject.new(graph: c)
expect(this.graph).to eq c
expect(this.graph_name).to eq c
this = subject.new(graph_name: c)
expect(this.graph).to eq c
expect(this.graph_name).to eq c
end
it "accepts inserts" do
g = double("inserts")
this = subject.new(insert: g)
expect(this.inserts).to eq g
end
it "accepts deletes" do
g = double("deletes")
this = subject.new(delete: g)
expect(this.deletes).to eq g
end
end
its(:deletes) {is_expected.to be_a(RDF::Enumerable)}
its(:inserts) {is_expected.to be_a(RDF::Enumerable)}
it {is_expected.to be_mutable}
it {is_expected.to_not be_readable}
it "does not respond to #load" do
expect {subject.load("http://example/")}.to raise_error(NoMethodError)
end
it "does not respond to #update" do
expect {subject.update(RDF::Statement.new)}.to raise_error(NoMethodError)
end
it "does not respond to #clear" do
expect {subject.clear}.to raise_error(NoMethodError)
end
describe "#execute" do
let(:s) {RDF::Statement.new(RDF::URI("s"), RDF::URI("p"), RDF::URI("o"))}
let(:r) {double("repository")}
it "deletes statements" do
statement = s.dup
statement.graph_name = (subject.graph_name rescue nil)
expect(r).to receive(:delete).with(statement)
expect(r).not_to receive(:insert)
subject.delete(s)
subject.execute(r)
end
it "inserts statements" do
statement = s.dup
statement.graph_name = (subject.graph_name rescue nil)
expect(r).not_to receive(:delete)
expect(r).to receive(:insert).with(statement)
subject.insert(s)
subject.execute(r)
end
context 'with graph names' do
let(:s) {RDF::Statement.new(RDF::URI("s"), RDF::URI("p"), RDF::URI("o"))}
let(:s_with_c) {RDF::Statement.new(RDF::URI("s"), RDF::URI("p"), RDF::URI("o"), graph_name: RDF::URI('c_st'))}
it "deletes statements" do
statement = s.dup
statement.graph_name = subject.graph_name
expect(r).to receive(:delete).with(statement, s_with_c)
expect(r).not_to receive(:insert)
subject.delete(s)
subject.delete(s_with_c)
subject.execute(r)
end
it "inserts statements" do
statement = s.dup
statement.graph_name = subject.graph_name
expect(r).not_to receive(:delete)
expect(r).to receive(:insert).with(statement, s_with_c)
subject.insert(s)
subject.insert(s_with_c)
subject.execute(r)
end
end
it "calls before_execute" do
is_expected.to receive(:before_execute).with(r, {})
subject.execute(r)
end
it "calls after_execute" do
is_expected.to receive(:after_execute).with(r, {})
subject.execute(r)
end
it "returns self" do
expect(subject.execute(r)).to eq subject
end
end
describe "#delete_statement" do
let(:s) {RDF::Statement.new(RDF::URI("s"), RDF::URI("p"), RDF::URI("o"))}
it "adds statement to #deletes" do
subject.delete(s)
expect(subject.deletes.to_a).to eq [s]
end
end
describe "#insert_statement" do
let(:s) {RDF::Statement.new(RDF::URI("s"), RDF::URI("p"), RDF::URI("o"))}
it "adds statement to #inserts" do
subject.insert(s)
expect(subject.inserts.to_a).to eq [s]
end
end
context 'with graph names' do
let(:s) {RDF::Statement.new(RDF::URI("s"), RDF::URI("p"), RDF::URI("o"))}
let(:s_with_c) {RDF::Statement.new(RDF::URI("s"), RDF::URI("p"), RDF::URI("o"), graph_name: RDF::URI('c_st'))}
describe "#delete_statement" do
it "adds statement to #deletes" do
subject.delete(s)
subject.delete(s_with_c)
expect(subject.deletes.to_a).to contain_exactly(s, s_with_c)
end
end
describe "#insert_statement" do
it "adds statement to #inserts" do
subject.insert(s)
subject.insert(s_with_c)
expect(subject.inserts.to_a).to contain_exactly(s, s_with_c)
end
end
end
end
shared_examples "RDF_Transaction" do |klass|
it_behaves_like 'an RDF::Transaction', klass
end