Skip to content

Instantly share code, notes, and snippets.

@bhuga
Created October 18, 2010 10:58
Show Gist options
  • Save bhuga/632039 to your computer and use it in GitHub Desktop.
Save bhuga/632039 to your computer and use it in GitHub Desktop.
first draft. Needs implementation of @setup, @teardown, @query.
#!/usr/bin/env ruby1.9
# local spira bug fix for bnode objects with Types::Any
$:.unshift '~/repos/spira/lib'
require 'spira'
require 'rdf/n3'
require 'rdf/isomorphic'
DAWG = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#')
MF = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#')
QT = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/test-query#')
RS = RDF::Vocabulary.new('http://www.w3.org/2001/sw/DataAccess/tests/result-set#')
test_repo = RDF::Repository.new
Spira.add_repository(:default, test_repo)
test_repo.load('tests/data/manifest-evaluation.ttl', :base_uri => 'tests/data/')
test_repo.load('tests/data-r2/manifest-evaluation.ttl', :base_uri => 'tests/data-r2/')
test_repo.load('tests/data-r2/extended-manifest-evaluation.ttl', :base_uri => 'tests/data-r2/')
class Manifest < Spira::Base
type MF.Manifest
has_many :manifests, :predicate => MF.include
property :entry_list, :predicate => MF.entries
property :comment, :predicate => RDFS.comment
def entries
RDF::List.new(entry_list, self.class.repository).map { |entry| entry.as(SPARQLTest) }
end
def include_files!
manifests.each do |manifest|
RDF::List.new(manifest, self.class.repository).each do |file|
self.class.repository.load(file.path)
end
end
end
end
class SPARQLTest < Spira::Base
property :name, :predicate => MF.name
property :comment, :predicate => RDFS.comment
property :action, :predicate => MF.action, :type => 'SPARQLAction'
property :result, :predicate => MF.result
property :approval, :predicate => DAWG.approval
def approved?
approval == DAWG.Approved
end
end
class SPARQLAction < Spira::Base
property :query_file, :predicate => QT.query
property :data, :predicate => QT.data
has_many :graphData, :predicate => QT.graphData
def query
IO.read(query_file.path)
end
end
class Binding < Spira::Base
default_vocabulary RS
property :value
property :variable
default_repository :results
end
class ResultBindings < Spira::Base
type RS.ResultSet
has_many :variables, :predicate => RS.ResultSet
has_many :solution_lists, :predicate => RS.solution
default_repository :results
def bindings
@bindings ||= solution_lists.map { |solution| RDF::List.new(solution, self.class.repository).map { |binding| binding.as(Binding) } }
end
def solutions
@solutions ||= bindings.map { |binding| RDF::Query::Solution.new(binding.variable => binding.value) }
end
end
Manifest.each do |manifest| manifest.include_files! end
tests = Manifest.each.map { |m| m.entries }.flatten.find_all { |t| t.approved? }
tests.each do |test|
if test.name.nil? || test.action.data.nil? || test.action.query.nil? || test.result.nil?
puts "#{test.name}: action data: #{test.action.data} action query: #{test.action.query} result: #{test.result}"
end
end
describe 'A SPARQL engine' do
before :all do
raise "@setup must be a proc to load repository data" unless @setup.respond_to(:call)
end
after :each do
@teardown.call unless @teardown.nil?
end
tests.each do |test|
it "should pass #{test.name}" do
# this should be in a before :each, but this is rough to do when creating tests automagically
@test_repository = RDF::Repository.new
@test_repository.load(test.data.path) unless test.data.nil?
test.graphData.each do |graph|
@test_repository.load(graph.path, :context => RDF::URI(graph.basename))
end
@setup.call(test, @test_repository)
Spira.add_repository(:bindings, RDF::Repository.load(test.result.path))
result = @query.call(test.query)
result.should be_a? SPARQL::Client::Query
results = result.solutions.to_a.map { |result| result.to_hash }
ResultBindings.each.first.solutions.to_a.map { |result| result.to_hash }
solutions.each do |solution|
results.should include solution
results.delete_at(results.index(solution))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment