-
Notifications
You must be signed in to change notification settings - Fork 8
/
hets_helper.rb
137 lines (109 loc) · 4.31 KB
/
hets_helper.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
# In this file, 'ontology_fixture' is always the path to the ontology file
# relative to Rails.root.join('spec/fixtures/ontologies/')
def hets_prove_matcher(request1, request2)
uri1 = URI(request1.uri)
uri2 = URI(request2.uri)
regexp_prove_path = %r{^/prove/(?<escaped_iri>[^/]*)(?<end>.*)$}
regexp_ontology_path = %r{^/ref/\d+/(?<repo>[^/]+)/(?<ontology>.*)$}
match1 = uri1.path.match(regexp_prove_path)
match2 = uri2.path.match(regexp_prove_path)
return request1.uri == request2.uri unless match1 && match2
inner_iri1 = URI.unescape(match1[:escaped_iri])
inner_iri2 = URI.unescape(match2[:escaped_iri])
inner_match1 = URI(inner_iri1).path.match(regexp_ontology_path)
inner_match2 = URI(inner_iri2).path.match(regexp_ontology_path)
return request1.uri == request2.uri unless inner_match1 && inner_match2
match1[:end] == match2[:end] &&
inner_match1[:ontology] == inner_match2[:ontology] &&
# We don't check for the port because the HetsInstances factory varies it.
%i(scheme host query fragment).all? { |m| uri1.send(m) == uri2.send(m)}
end
def hets_vcr_file(subdir, ontology_fixture)
# Replace last occurence of '.' by '_' in the filepath (VCR convention).
portions = ontology_fixture.split('.')
path = portions[0..-2].join('.')
extension = portions[-1]
fixture_file(File.join('vcr', 'hets-out', subdir, "#{path}_#{extension}.yml"))
end
def hets_ontology_vcr_file(ontology_fixture)
hets_vcr_file('dg', ontology_fixture)
end
def hets_proof_vcr_file(ontology_fixture)
hets_vcr_file('proof', ontology_fixture)
end
def hets_provers_vcr_file(ontology_fixture)
hets_vcr_file('provers', ontology_fixture)
end
def hets_out_body(subdir, ontology_fixture)
yaml = YAML.load(File.open(hets_vcr_file(subdir, ontology_fixture)))
yaml['http_interactions'].first['response']['body']['string']
end
def hets_out_body_ontology(ontology_fixture)
hets_out_body('dg', ontology_fixture)
end
def hets_out_body_proof(ontology_fixture)
hets_out_body('proof', ontology_fixture)
end
def hets_out_body_provers(ontology_fixture)
hets_out_body('provers', ontology_fixture)
end
def parse_ontology_hets_out(user, ontology, io, provers_io)
current_version = ontology.current_version
allow(Hets).to receive(:parse_via_api).and_return(io)
allow(Hets).to receive(:provers_via_api) do
provers_io.rewind
provers_io
end
allow(current_version).to receive(:ontology).and_return(ontology)
allow(ontology).to receive(:import_version) do
evaluator = Hets::DG::Importer.new(user, ontology, io: io,
version: ontology.current_version)
evaluator.import
end
current_version.parse
io.close unless io.closed?
provers_io.close unless provers_io.closed?
allow(Hets).to receive(:parse_via_api).and_call_original
end
def parse_ontology(user, ontology, ontology_fixture, provers_io = nil)
io = StringIO.new(hets_out_body_ontology(ontology_fixture))
provers_io = StringIO.new(hets_out_body_provers(ontology_fixture))
parse_ontology_hets_out(user, ontology, io, provers_io)
end
def setup_hets
let(:hets_instance) { create_local_hets_instance }
before do
stub_request(:get, %r{http://localhost:8\d{3}/version}).
to_return(body: Hets.minimal_version_string)
hets_instance
end
after { hets_instance.finish_work! }
end
def stub_hets_for(ontology_fixture,
command: 'dg', with: nil, with_version: nil, method: :get)
stub_request(:get, %r{http://localhost:8\d{3}/version}).
to_return(body: Hets.minimal_version_string)
stub_request(method, hets_uri(command, with, with_version)).
to_return(body: hets_out_body(command, ontology_fixture))
if command == 'dg'
stub_request(method, hets_uri('provers', with, with_version)).
to_return(body: hets_out_body('provers', ontology_fixture))
end
end
def create_local_hets_instance
Sidekiq::Testing.fake! { FactoryGirl.create(:local_hets_instance) }
end
def hets_uri(command = 'dg', portion = nil, version = nil)
hets_instance =
begin
HetsInstance.choose!
rescue HetsInstance::NoRegisteredHetsInstanceError
create_local_hets_instance
HetsInstance.choose!
end
specific = ''
# %2F is percent-encoding for forward slash /
specific << "ref%2F#{version}.*" if version
specific << "#{portion}.*" if portion
return %r{#{hets_instance.uri}/#{command}/.*#{specific}}
end