iq_triplestorage 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/iq_triplestorage.rb +1 -1
- data/lib/iq_triplestorage/sesame_adaptor.rb +43 -0
- data/test/core_test.rb +11 -0
- data/test/sesame_test.rb +66 -0
- data/test/test_helper.rb +25 -0
- data/test/virtuoso_test.rb +8 -23
- metadata +13 -2
data/lib/iq_triplestorage.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "cgi"
|
3
|
+
|
4
|
+
require "iq_triplestorage"
|
5
|
+
|
6
|
+
class IqTriplestorage::SesameAdaptor
|
7
|
+
attr_reader :host
|
8
|
+
|
9
|
+
def initialize(host, repository, options={})
|
10
|
+
@host = URI.parse(host).normalize
|
11
|
+
@repo = repository
|
12
|
+
@username = options[:username]
|
13
|
+
@password = options[:username]
|
14
|
+
end
|
15
|
+
|
16
|
+
# expects a hash of N-Triples by graph URI
|
17
|
+
def batch_update(triples_by_graph)
|
18
|
+
path = "/repositories/#{CGI.escape(@repo)}/statements"
|
19
|
+
path = URI.join("#{@host}/", path[1..-1]).path
|
20
|
+
|
21
|
+
data = triples_by_graph.map do |graph_uri, ntriples|
|
22
|
+
"<#{graph_uri}> {\n#{ntriples}\n}\n"
|
23
|
+
end.join("\n\n")
|
24
|
+
|
25
|
+
http_request("POST", path, data, { "Content-Type" => "application/x-trig" })
|
26
|
+
end
|
27
|
+
|
28
|
+
# converts N-Triples to N-Quads
|
29
|
+
def triples_to_quads(triples, context)
|
30
|
+
end
|
31
|
+
|
32
|
+
def http_request(method, path, body, headers={}) # XXX: largely duplicates VirtuosoAdaptor's
|
33
|
+
uri = URI.join("#{@host.to_s}/", path)
|
34
|
+
|
35
|
+
req = Net::HTTP.const_get(method.to_s.downcase.capitalize).new(uri.to_s)
|
36
|
+
req.basic_auth(@username, @password) if (@username && @password)
|
37
|
+
headers.each { |key, value| req[key] = value }
|
38
|
+
req.body = body
|
39
|
+
|
40
|
+
return Net::HTTP.new(uri.host, uri.port).request(req)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/test/core_test.rb
ADDED
data/test/sesame_test.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")
|
2
|
+
|
3
|
+
require "base64"
|
4
|
+
|
5
|
+
require "iq_triplestorage/sesame_adaptor"
|
6
|
+
|
7
|
+
class SesameTest < WebTestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
super
|
11
|
+
WebMock.stub_request(:any, /.*example.org.*/).with(&@request_handler).
|
12
|
+
to_return do |req|
|
13
|
+
{ :status => req.uri.to_s.end_with?("/rdf_sink") ? 200 : 201 }
|
14
|
+
end
|
15
|
+
|
16
|
+
@username = "foo"
|
17
|
+
@password = "bar"
|
18
|
+
@host = "http://example.org/sesame"
|
19
|
+
@repo = "test"
|
20
|
+
|
21
|
+
@adaptor = IqTriplestorage::SesameAdaptor.new(@host, @repo,
|
22
|
+
:username => @username, :password => @password)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_host_normalization
|
26
|
+
host = "http://example.org"
|
27
|
+
adaptor = IqTriplestorage::SesameAdaptor.new(host, "dummy")
|
28
|
+
assert_equal "/", adaptor.host.path
|
29
|
+
|
30
|
+
host = "http://example.org/"
|
31
|
+
adaptor = IqTriplestorage::SesameAdaptor.new(host, "dummy")
|
32
|
+
assert_equal "/", adaptor.host.path
|
33
|
+
|
34
|
+
host = "http://example.org/foo"
|
35
|
+
adaptor = IqTriplestorage::SesameAdaptor.new(host, "dummy")
|
36
|
+
assert_equal "/foo", adaptor.host.path
|
37
|
+
|
38
|
+
host = "http://example.org/foo/"
|
39
|
+
adaptor = IqTriplestorage::SesameAdaptor.new(host, "dummy")
|
40
|
+
assert_equal "/foo/", adaptor.host.path
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_batch
|
44
|
+
data = {
|
45
|
+
"http://example.com/foo" => "<aaa> <bbb> <ccc> .\n<ddd> <eee> <fff> .",
|
46
|
+
"http://example.com/bar" => "<ggg> <hhh> <iii> .\n<jjj> <kkk> <lll> ."
|
47
|
+
}
|
48
|
+
|
49
|
+
@observers << lambda do |req|
|
50
|
+
assert_equal :post, req.method
|
51
|
+
path = req.uri.path
|
52
|
+
assert path.
|
53
|
+
start_with?("/sesame/repositories/#{CGI.escape(@repo)}/statements")
|
54
|
+
assert_equal "application/x-trig", req.headers["Content-Type"]
|
55
|
+
data.each do |graph_uri, ntriples|
|
56
|
+
assert req.body.include?(<<-EOS)
|
57
|
+
<#{graph_uri}> {
|
58
|
+
#{ntriples}
|
59
|
+
}
|
60
|
+
EOS
|
61
|
+
end
|
62
|
+
end
|
63
|
+
assert @adaptor.batch_update(data)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require "simplecov"
|
2
|
+
require "minitest/autorun"
|
3
|
+
require "webmock/test_unit"
|
2
4
|
|
3
5
|
# limit coverage to /lib
|
4
6
|
cwd = File.expand_path(File.join(File.dirname(__FILE__), "../"))
|
@@ -6,3 +8,26 @@ SimpleCov.add_filter do |src_file|
|
|
6
8
|
!src_file.filename.start_with?(File.join(cwd, "lib").to_s)
|
7
9
|
end
|
8
10
|
SimpleCov.start
|
11
|
+
|
12
|
+
class WebTestCase < MiniTest::Unit::TestCase
|
13
|
+
|
14
|
+
def setup
|
15
|
+
# HTTP request mocking
|
16
|
+
@observers = [] # one per request
|
17
|
+
@request_handler = lambda do |req|
|
18
|
+
# not using webmock's custom assertions as those didn't seem to provide
|
19
|
+
# sufficient flexibility
|
20
|
+
fn = @observers.shift
|
21
|
+
raise(TypeError, "missing request observer: #{req.inspect}") unless fn
|
22
|
+
fn.call(req)
|
23
|
+
true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def teardown
|
28
|
+
WebMock.reset!
|
29
|
+
raise(TypeError, "unhandled request observer") unless @observers.length == 0
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
data/test/virtuoso_test.rb
CHANGED
@@ -1,26 +1,17 @@
|
|
1
1
|
require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")
|
2
2
|
|
3
3
|
require "base64"
|
4
|
-
require "minitest/autorun"
|
5
|
-
require "webmock/test_unit"
|
6
4
|
|
7
5
|
require "iq_triplestorage/virtuoso_adaptor"
|
8
6
|
|
9
|
-
class VirtuosoTest <
|
7
|
+
class VirtuosoTest < WebTestCase
|
10
8
|
|
11
9
|
def setup
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
fn = @observers.shift
|
18
|
-
raise(TypeError, "missing request observer: #{req.inspect}") unless fn
|
19
|
-
fn.call(req)
|
20
|
-
true
|
21
|
-
end.to_return do |req|
|
22
|
-
{ :status => req.uri.to_s.end_with?("/rdf_sink") ? 200 : 201 }
|
23
|
-
end
|
10
|
+
super
|
11
|
+
WebMock.stub_request(:any, /.*example.org.*/).with(&@request_handler).
|
12
|
+
to_return do |req|
|
13
|
+
{ :status => req.uri.to_s.end_with?("/rdf_sink") ? 200 : 201 }
|
14
|
+
end
|
24
15
|
|
25
16
|
@username = "foo"
|
26
17
|
@password = "bar"
|
@@ -30,11 +21,6 @@ class VirtuosoTest < MiniTest::Unit::TestCase
|
|
30
21
|
@username, @password)
|
31
22
|
end
|
32
23
|
|
33
|
-
def teardown
|
34
|
-
WebMock.reset!
|
35
|
-
raise(TypeError, "unhandled request observer") unless @observers.length == 0
|
36
|
-
end
|
37
|
-
|
38
24
|
def test_reset
|
39
25
|
uri = "http://example.com/foo"
|
40
26
|
|
@@ -124,12 +110,11 @@ class VirtuosoTest < MiniTest::Unit::TestCase
|
|
124
110
|
assert_equal 4, path.count("/")
|
125
111
|
assert_equal "application/sparql-query", req.headers["Content-Type"]
|
126
112
|
data.each do |graph_uri, ntriples|
|
127
|
-
assert req.body.
|
128
|
-
include?(<<-EOS)
|
113
|
+
assert req.body.include?(<<-EOS)
|
129
114
|
INSERT IN GRAPH <#{graph_uri}> {
|
130
115
|
#{ntriples}
|
131
116
|
}
|
132
|
-
|
117
|
+
EOS
|
133
118
|
end
|
134
119
|
end
|
135
120
|
assert @adaptor.batch_update(data)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iq_triplestorage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -24,7 +24,10 @@ files:
|
|
24
24
|
- Rakefile
|
25
25
|
- iq_triplestorage.gemspec
|
26
26
|
- lib/iq_triplestorage.rb
|
27
|
+
- lib/iq_triplestorage/sesame_adaptor.rb
|
27
28
|
- lib/iq_triplestorage/virtuoso_adaptor.rb
|
29
|
+
- test/core_test.rb
|
30
|
+
- test/sesame_test.rb
|
28
31
|
- test/test_helper.rb
|
29
32
|
- test/virtuoso_test.rb
|
30
33
|
homepage: http://github.com/innoq/iq_triplestorage
|
@@ -39,12 +42,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
42
|
- - ! '>='
|
40
43
|
- !ruby/object:Gem::Version
|
41
44
|
version: '0'
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
hash: -492828564790801431
|
42
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
49
|
none: false
|
44
50
|
requirements:
|
45
51
|
- - ! '>='
|
46
52
|
- !ruby/object:Gem::Version
|
47
53
|
version: '0'
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
hash: -492828564790801431
|
48
57
|
requirements: []
|
49
58
|
rubyforge_project: iq_triplestorage
|
50
59
|
rubygems_version: 1.8.23
|
@@ -52,5 +61,7 @@ signing_key:
|
|
52
61
|
specification_version: 3
|
53
62
|
summary: IqTriplestorage - library for interacting with RDF triplestores / quadstores
|
54
63
|
test_files:
|
64
|
+
- test/core_test.rb
|
65
|
+
- test/sesame_test.rb
|
55
66
|
- test/test_helper.rb
|
56
67
|
- test/virtuoso_test.rb
|