#!/usr/bin/env ruby
# rubyunit tests for Ruby-RDF
#
# more docs:  http://www.eng.cse.dmu.ac.uk/~hgs/ruby/ruby-unit.html
# 	http://homepage1.nifty.com/markey/ruby/rubyunit/index_e.html

# todo: look at walkit, for better test sequencing
#
# http://homepage1.nifty.com/markey/ruby/rubyunit/index_e.html

=begin
=Testing Ruby-RDF
(1)load RubyUnit framework.
(2)Define class derived from RUNIT::TestCase class.
(3)If you want to prepare to run test, override setup method.
(4)Add testXXXX method for running test.
(5)Add teardown method if you want to do something after running test.
=end

require 'rubyunit'

require '../basicrdf'
srand() # TODO: ugh

FOAF = 'http://xmlns.com/foaf/0.1/'
DC = 'http://purl.org/dc/elements/1.1/'


class RDFTest < RUNIT::TestCase

  def setup
    print "\n============= SETUP =========== \n"
    @data = 'webwho.xrdf'
    @db = Graph.new ([])
    @db.reg_xmlns(FOAF, 'foaf')  

    @foaf_mbox = Node.getResource(FOAF+'mbox')
    @foaf_livesIn = Node.getResource(FOAF+'livesIn')
    @foaf_homepage = Node.getResource(FOAF+'homepage')

    @bristol = @db.getBlank()
    @libby = @db.getBlank()
    @damey = @db.getBlank()
    @danbri = @db.getBlank()
    @@dbhome = @db.getResource('http://rdfweb.org/people/danbri/')
    @dbmail = @db.getResource('mailto:danbri@rdfweb.org')
    @s1 = Statement.new(@danbri, @foaf_homepage, @dbhome)
    @s2 = Statement.new(@danbri, @foaf_mbox, @dbmail)
    @s3 = Statement.new(@danbri, @foaf_livesIn, @db.getLiteral("Bristol"))
    @s4 = Statement.new(@damey, @foaf_livesIn, @db.getLiteral("Bristol"))
    @lie = Statement.new(@danbri, @foaf_livesIn, @db.getLiteral("Hong Kong"))
    print "Setup says danbri is: #{@danbri} aka #{@danbri.inspect} \n\n"
  end 

  def test_tell_memgraph
print "\n===================================== testing tell api on:  '#{@db}' \n "
    @db.tell_all([@s1, @s2, @s3, @s4])
    @db.tell(Statement.new(@libby, @foaf_livesIn, Node.getLiteral("Bristol")))
    @db.tell(Statement.new(@libby, @foaf_mbox, Node.getResource('mailto:libby.miller@bristol.ac.uk')))
    @db.tell(Statement.new(@libby, @foaf_mbox, Node.getResource('mailto:libby@rdfweb.org')))
    @db.tell(Statement.new(@damey, @foaf_mbox, Node.getResource('mailto:d.m.steer@lse.ac.uk')))
    s3count = @db.ask(@s3).size
    print "S3 is: #{@s3.inspect} \n"
    assert( s3count > 0, "Statement S3 should be in the graph at least once. count = #{s3count}")
   end


  def test_blanknode_query
    # this test should ensure that genids are irrelevant in ask() queries
    # we have a problem with S3 above...    
    tmp = Graph.new([])
    d1 = tmp.getBlank()
    d2 = tmp.getBlank()
    mb = tmp.getResource('http://xmlns.com/foaf/0.1/mbox')
    v = tmp.getResource('mailto:danbri@w3.org')
    s1 = Statement.new(d1,mb,v)
    s2 = Statement.new(d1,mb,v)
    tmp.tell( s1 )
    ans = tmp.ask( s1 )
    
    print "Test query: result is: #{ans.inspect} \n"
    print "Blanknode test db: #{tmp.inspect} \n"
      
    # to do with identity of nodes, statements etc in queries
    # RubyUnit is re-running setup() each time, so we have various objects playing role of S3
    # todo? police this in constructors perhaps?
    # or...
    # maybe ask() should turn genid nodes into blanks?
  end



  def test_ask_memgraph
    print "\n===================================== testing query api on:  '#{@db}' \n "
    print ("Statement 's3' (ie. #{@s3.inspect}) is in the graph\n") if ((@db.ask(@s3)).size >0 ) 

    s3count = @db.ask(@s3).size
    assert( s3count > 0, "S3 should be in the graph; #{@s3.inspect}  ")

    print "Size s3: " , @db.ask(@s3).size  ,"\n"
    print ("Statement 'lie' (ie. #{@lie.inspect}) is (rightly) NOT in the graph\n") if((@db.ask(@lie)).size ==0)

    # query 'xxo'
    puts "Asking for mailboxes of libby:\n"
    t1 = @db.ask( Statement.new( @libby, @foaf_mbox, nil)).objects()
    # maybe: @db.ask(...).
    puts "Answer was: #{t1} \n"

    # query 'oxx'
    puts "Mailboxes of people who live in bristol: \n\n"

    who = @db.ask(Statement.new( nil, @foaf_livesIn, Node.getLiteral("Bristol")))

    puts "Got an answer graph: #{who.inspect} \n"
    puts "All the people in bristol: \n #{who.subjects()} \n"
 
    puts "Their mailboxes...\n"
    mbox=[]
    puts "All the people in bristol: \n #{who.subjects()} \n"

    who.subjects().each {
      |w| mbox.push( @db.ask( Statement.new(w, @foaf_mbox,nil)).objects() ) 
    } 

    puts "mbox is: #{mbox.inspect}\n"
    puts mbox.each { |mailto| puts "Email: #{mailto} \n" }
  end

  
  def test_data
    text = open(@data){ |f| f.read }
    assert(text != nil, "Test data required" )
#    assert_match(text,'web', "Test data should have substring 'foo'")
  end



end



