Skip to content

Instantly share code, notes, and snippets.

@igravious
Created December 15, 2017 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igravious/1d5316eca6471af84b9e7216fc665bb5 to your computer and use it in GitHub Desktop.
Save igravious/1d5316eca6471af84b9e7216fc665bb5 to your computer and use it in GitHub Desktop.
# test/lib/triples_create_test.rb
require 'test_helper'
# assert_nil
# assert_equal
#
# http://guides.rubyonrails.org/testing.html
class CreateTriple < ActiveSupport::TestCase
setup do
# require 'rake'
@repo = Knowledge.new_repo
ReFrame::Application.load_tasks
end
test "RDF Sanity Check" do
begin
@repo.clear!
assert_equal @repo.count, 0
# sql_repo = RDF::DataObjects::Repository.new uri: 'postgres://postgres@server/database'
# heroku_repo = RDF::DataObjects::Repository.new uri: ENV['DATABASE_URL']
# mongo_repo = …
@repo.load('http://datagraph.org/jhacker/foaf.nt')
# How many statements did we have?
assert_equal @repo.count, 10
#=> 10
# Get the URI of the first subject
# binding.pry
jhacker = @repo.first.subject
# p jhacker
assert_equal jhacker, RDF::URI.new('http://datagraph.org/jhacker/foaf')
# Delete everything to do with it
jhacker_statements = @repo.query(subject: jhacker).entries
# p jhacker_statements
# [#<RDF::Statement:0x2af20a6a6504(<http://datagraph.org/jhacker/foaf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/PersonalProfileDocument> .)>, #<RDF::Statement:0x2af20bf25f6c(<http://datagraph.org/jhacker/foaf> <http://xmlns.com/foaf/0.1/maker> <http://datagraph.org/jhacker/#self> .)>, #<RDF::Statement:0x2af20a680fe8(<http://datagraph.org/jhacker/foaf> <http://xmlns.com/foaf/0.1/primaryTopic> <http://datagraph.org/jhacker/#self> .)>]
@repo.delete *jhacker_statements
assert_equal @repo.count, 7
#=> 7
# with Postgres, we could have done this, but SQLite gives us a locking error:
# repo.delete(*repo.query(subject: jhacker))
# Changed our mind--bring it back
@repo.insert *jhacker_statements
assert_equal @repo.count, 10
@repo.clear!
assert_equal @repo.count, 0
rescue SocketError
# https://stackoverflow.com/questions/2191632/begin-rescue-and-ensure-in-ruby
rescue # $!
puts $!.class
puts $!
puts $!.backtrace[0]
end
end
# scenario (1)
#
# remote – explicit (good)
#
test "remote – explicit (good)" do
# https://stackoverflow.com/questions/31858964/invoke-rake-task-with-arguments-from-another-task
Rake::Task['triples:create'].invoke('_', '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>', '_')
Rake::Task['triples:create'].reenable
end
# scenario (2)
#
# remote – prefix
#
test "remote – prefix" do
# https://stackoverflow.com/questions/31858964/invoke-rake-task-with-arguments-from-another-task
Rake::Task['triples:create'].invoke('_', '<rdf:type>', '_')
Rake::Task['triples:create'].reenable
end
# scenario (3)
#
# remote – explicit (bad)
#
test "remote – explicit (bad)" do
# https://stackoverflow.com/questions/31858964/invoke-rake-task-with-arguments-from-another-task
assert_raises(RDF::ReaderError) do
Rake::Task['triples:create'].invoke('_', '<http://www.w3.org#meh>', '_')
end
Rake::Task['triples:create'].reenable
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment