Skip to content

Commit

Permalink
sparql tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saumier committed May 30, 2024
1 parent ba504c4 commit 06b0bcc
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 98 deletions.
24 changes: 13 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.5)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
bcp47_spec (0.2.1)
builder (3.2.4)
concurrent-ruby (1.2.2)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
ebnf (2.4.0)
htmlentities (~> 4.3)
rdf (~> 3.3)
scanf (~> 1.0)
sxp (~> 1.3)
unicode-types (~> 1.8)
haml (6.2.3)
haml (6.3.0)
temple (>= 0.8.2)
thor
tilt
Expand Down Expand Up @@ -71,15 +71,15 @@ GEM
multi_json (1.15.0)
net-http-persistent (4.0.2)
connection_pool (~> 2.2)
nokogiri (1.15.5-x86_64-darwin)
nokogiri (1.16.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.5-x86_64-linux)
nokogiri (1.16.5-x86_64-linux)
racc (~> 1.4)
psych (5.1.1.1)
psych (5.1.2)
stringio
public_suffix (5.0.4)
racc (1.7.3)
rack (3.0.8)
public_suffix (5.0.5)
racc (1.8.0)
rack (3.0.11)
rdf (3.3.1)
bcp47_spec (~> 0.2)
link_header (~> 0.0, >= 0.0.8)
Expand Down Expand Up @@ -144,7 +144,8 @@ GEM
rdf-xsd (3.3.0)
rdf (~> 3.3)
rexml (~> 3.2)
rexml (3.2.6)
rexml (3.2.8)
strscan (>= 3.0.9)
scanf (1.0.0)
shacl (0.4.1)
json-ld (~> 3.3)
Expand Down Expand Up @@ -173,11 +174,12 @@ GEM
net-http-persistent (~> 4.0, >= 4.0.2)
rdf (~> 3.3)
stringio (3.1.0)
strscan (3.1.0)
sxp (1.3.0)
matrix (~> 0.4)
rdf (~> 3.3)
temple (0.10.3)
thor (1.3.0)
thor (1.3.1)
tilt (2.3.0)
unicode-types (1.9.0)
yaml-ld (0.0.3)
Expand Down
29 changes: 19 additions & 10 deletions shacl/has_artsdata_ids_shacl.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@


ads:PlaceCheck
a sh:PropertyShape ;
sh:targetClass schema:Place ;
a sh:NodeShape ;
sh:targetClass ( schema:Place ) ;
sh:sparql [
a sh:SPARQLConstraint ; # This triple is optional
sh:message "Missing a sameAs adid." ;
sh:prefixes schema: ;
sh:select """
PREFIX schema: <http://schema.org/>
select distinct $this where {
$this a schema:Place .
OPTIONAL {
$this schema:sameAs ?adid .
filter(strstarts(str(?adid),"http://kg.artsdata.ca/resource/"))
}
filter(!BOUND(?adid))
}
""" ;
]


sh:path schema:sameAs ;
sh:nodeKind sh:IRI ;
sh:message "Should have alteast one Artsdata URI" ;
sh:severity sh:Violation ;
sh:qualifiedValueShape [
sh:pattern "http://kg.artsdata.ca/resource/K.*" ;
] ;
sh:qualifiedMinCount 1 ;
.
20 changes: 20 additions & 0 deletions src/tests/shacl_artsdata_ids_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'minitest/autorun'
require 'rdf'
require 'linkeddata'
require 'shacl'

class ShaclTest < Minitest::Test
def setup
@shacl = SHACL.open('../../shacl/has_artsdata_ids_shacl.ttl')
end

def test_full_event_should_pass
graph = RDF::Graph.load("./fixtures/places_to_test_artsdata_ids.jsonld")
puts graph.dump(:jsonld)
report = @shacl.execute(graph)

puts report

assert report.conform?
end
end
154 changes: 77 additions & 77 deletions src/tests/sparql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,75 @@ def setup
end

# Removes type: EventForIndex
# def test_that_eventforindex_is_removed
# sparql = "../sparql/remove_eventforindex.sparql"
# graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# assert_equal 254, graph.count
# end

# def test_transform_single_events
# sparql = "../sparql/transform_single_events.sparql"
# graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# assert_equal 264, graph.count
# end

# def test_convert_image_object_to_url
# sparql = "../sparql/convert_image_object_to_url.sparql"
# graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# # puts graph.dump(:turtle)
# assert_equal 240, graph.count
# end

# def test_convert_image_literal_to_uri
# sparql = "../sparql/convert_image_literal_to_uri.sparql"
# graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# # puts graph.dump(:turtle)
# assert_equal 256, graph.count
# end

# def test_convert_url_literal_to_uri
# sparql = "../sparql/convert_url_literal_to_uri.sparql"
# graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# # puts graph.dump(:turtle)
# assert_equal 256, graph.count
# end

# def test_convert_offers_to_aggregate_offer
# sparql = "../sparql/convert_offers_to_aggregate_offer.sparql"
# @graph = RDF::Graph.load("./fixtures/two_offers.jsonld")
# # puts @graph.dump(:turtle)
# @graph.query(SPARQL.parse(File.read(sparql), update: true))
# # puts @graph.dump(:jsonld)
# assert_equal "8.0", @graph.query([nil, RDF::Vocab::SCHEMA.lowPrice, nil]).first.object.to_s
# assert_equal "20.0", @graph.query([nil, RDF::Vocab::SCHEMA.highPrice, nil]).first.object.to_s
# assert_equal 1, @graph.query([RDF::URI("https://www.grandtheatre.qc.ca/programmation/midis-musique-25-octobre-2023-2023-10-25-16-10#SingleEvent_WrappedOffer"), RDF.type, RDF::Vocab::SCHEMA.AggregateOffer]).count
# assert_equal 5, @graph.query([RDF::URI("https://www.grandtheatre.qc.ca/programmation/midis-musique-25-octobre-2023-2023-10-25-16-10#SingleEvent_WrappedOffer"), RDF::Vocab::SCHEMA.offers, nil]).count
# end

# # Ensure only one AggregateOffer is created for an event
# def test_fix_aggreate_offer_url
# sparql = "../sparql/fix_aggregate_offer_url.sparql"
# @graph = RDF::Graph.load("./fixtures/two_offers_multiple_buy_urls.jsonld")
# graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))

# # puts graph.dump(:turtle)
# query = SPARQL.parse("
# PREFIX schema: <http://schema.org/>
# ASK { select ?event (count(?url) as ?count)
# where { ?AggregateOffer a schema:AggregateOffer ; schema:url ?url . }
# group by ?AggregateOffer having(count(?url) > 1) }
# ")
# multiple_urls = query.execute(graph)
# assert multiple_urls.false?
# end

# def test_remove_temporary_eventtype
# sparql = "../sparql/remove_temporary_eventtype.sparql"
# @graph = RDF::Graph.load("./fixtures/event_with_additional_types.jsonld")
# graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# # puts graph.dump(:turtle)
# assert_equal 5, graph.count
# end
def test_that_eventforindex_is_removed
sparql = "../sparql/remove_eventforindex.sparql"
graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
assert_equal 254, graph.count
end

def test_transform_single_events
sparql = "../sparql/transform_single_events.sparql"
graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
assert_equal 264, graph.count
end

def test_convert_image_object_to_url
sparql = "../sparql/convert_image_object_to_url.sparql"
graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# puts graph.dump(:turtle)
assert_equal 240, graph.count
end

def test_convert_image_literal_to_uri
sparql = "../sparql/convert_image_literal_to_uri.sparql"
graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# puts graph.dump(:turtle)
assert_equal 256, graph.count
end

def test_convert_url_literal_to_uri
sparql = "../sparql/convert_url_literal_to_uri.sparql"
graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# puts graph.dump(:turtle)
assert_equal 256, graph.count
end

def test_convert_offers_to_aggregate_offer
sparql = "../sparql/convert_offers_to_aggregate_offer.sparql"
@graph = RDF::Graph.load("./fixtures/two_offers.jsonld")
# puts @graph.dump(:turtle)
@graph.query(SPARQL.parse(File.read(sparql), update: true))
# puts @graph.dump(:jsonld)
assert_equal "8.0", @graph.query([nil, RDF::Vocab::SCHEMA.lowPrice, nil]).first.object.to_s
assert_equal "20.0", @graph.query([nil, RDF::Vocab::SCHEMA.highPrice, nil]).first.object.to_s
assert_equal 1, @graph.query([RDF::URI("https://www.grandtheatre.qc.ca/programmation/midis-musique-25-octobre-2023-2023-10-25-16-10#SingleEvent_WrappedOffer"), RDF.type, RDF::Vocab::SCHEMA.AggregateOffer]).count
assert_equal 5, @graph.query([RDF::URI("https://www.grandtheatre.qc.ca/programmation/midis-musique-25-octobre-2023-2023-10-25-16-10#SingleEvent_WrappedOffer"), RDF::Vocab::SCHEMA.offers, nil]).count
end

# Ensure only one AggregateOffer is created for an event
def test_fix_aggreate_offer_url
sparql = "../sparql/fix_aggregate_offer_url.sparql"
@graph = RDF::Graph.load("./fixtures/two_offers_multiple_buy_urls.jsonld")
graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))

# puts graph.dump(:turtle)
query = SPARQL.parse("
PREFIX schema: <http://schema.org/>
ASK { select ?event (count(?url) as ?count)
where { ?AggregateOffer a schema:AggregateOffer ; schema:url ?url . }
group by ?AggregateOffer having(count(?url) > 1) }
")
multiple_urls = query.execute(graph)
assert multiple_urls.false?
end

def test_remove_temporary_eventtype
sparql = "../sparql/remove_temporary_eventtype.sparql"
@graph = RDF::Graph.load("./fixtures/event_with_additional_types.jsonld")
graph = @graph.query(SPARQL.parse(File.read(sparql), update: true))
# puts graph.dump(:turtle)
assert_equal 5, graph.count
end


def test_offers_footlight_aggregate_offer_removed
Expand All @@ -99,14 +99,14 @@ def test_offers_footlight_aggregate_offer_kept
end


# def test_fix_aggregate_offers_missing_url
# sparql = "../sparql/fix_aggregate_offers_missing_url.sparql"
# @graph = RDF::Graph.load("./fixtures/aggregate_offers_missing_url.jsonld")
# # puts "BEFORE: #{@graph.dump(:turtle)}"
# @graph.query(SPARQL.parse(File.read(sparql), update: true))
# # puts "AFTER: #{@graph.dump(:turtle)}"
# assert_equal 22, @graph.count
# end
def test_fix_aggregate_offers_missing_url
sparql = "../sparql/fix_aggregate_offers_missing_url.sparql"
@graph = RDF::Graph.load("./fixtures/aggregate_offers_missing_url.jsonld")
# puts "BEFORE: #{@graph.dump(:turtle)}"
@graph.query(SPARQL.parse(File.read(sparql), update: true))
# puts "AFTER: #{@graph.dump(:turtle)}"
assert_equal 22, @graph.count
end

end

0 comments on commit 06b0bcc

Please sign in to comment.