From 06b0bcc5343a6830d96253fe1ac89780bd95b033 Mon Sep 17 00:00:00 2001 From: Gregory Saumier-Finch Date: Wed, 29 May 2024 22:41:33 -0400 Subject: [PATCH] sparql tests --- Gemfile.lock | 24 +++-- shacl/has_artsdata_ids_shacl.ttl | 29 +++-- src/tests/shacl_artsdata_ids_test.rb | 20 ++++ src/tests/sparql_test.rb | 154 +++++++++++++-------------- 4 files changed, 129 insertions(+), 98 deletions(-) create mode 100644 src/tests/shacl_artsdata_ids_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index ec9abfb..118c9cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,11 @@ 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) @@ -13,7 +13,7 @@ GEM scanf (~> 1.0) sxp (~> 1.3) unicode-types (~> 1.8) - haml (6.2.3) + haml (6.3.0) temple (>= 0.8.2) thor tilt @@ -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) @@ -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) @@ -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) diff --git a/shacl/has_artsdata_ids_shacl.ttl b/shacl/has_artsdata_ids_shacl.ttl index a1dcb74..07d4bd1 100644 --- a/shacl/has_artsdata_ids_shacl.ttl +++ b/shacl/has_artsdata_ids_shacl.ttl @@ -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: + 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 ; . \ No newline at end of file diff --git a/src/tests/shacl_artsdata_ids_test.rb b/src/tests/shacl_artsdata_ids_test.rb new file mode 100644 index 0000000..c430886 --- /dev/null +++ b/src/tests/shacl_artsdata_ids_test.rb @@ -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 \ No newline at end of file diff --git a/src/tests/sparql_test.rb b/src/tests/sparql_test.rb index 2d610f5..2beb34d 100644 --- a/src/tests/sparql_test.rb +++ b/src/tests/sparql_test.rb @@ -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: - # 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: + 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 @@ -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