#!/usr/bin/ruby -w
#
# $Id: tc_yahoo,v 1.7 2005/03/24 22:09:44 ianmacd Exp $

$: << File.dirname(__FILE__) + "/../lib"

require 'test/unit'
require 'finance/quote'

include Finance

class TC_YahooTest < Test::Unit::TestCase

  # Test base method.
  #
  def test_yahoo
    q = Quote.new

    # Check for correct method mapping of virtual methods.
    assert_nothing_thrown do
      q.usa( 'CRM', 'AMZN' )
    end

    # Check for exception on bogus virtual method.
    # assert_throws( NoMethodError, q.bogus_method( 'CRM', 'AMZN' ) )

    assert_instance_of( Finance::Quote::QuoteData,
		        qd = q.yahoo( 'CRM', 'AMZN', 'BOGUS' ) )

    [ 'CRM', 'AMZN' ].each do |stock|

      # Check the most reliable indicators of success.
      assert( qd[stock][:last].to_f > 0 )
      assert( qd[stock][:success] )
      assert_equal( 'USD', qd[stock][:currency] )

      # Check that '%' signs have been deleted.
      assert_no_match( /%/, qd[stock][:p_change] )
    end

    # Check that a bogus stock returns failure.
    assert( ! qd['BOGUS'][:success] )

    # Check that a symbol that begins with ^ is properly encoded.
    assert_instance_of( Finance::Quote::QuoteData,
		        qd = q.yahoo( '^DJI' ) )
    assert( qd['^DJI'][:success] )
  end

  # Test European market retrieval.
  #
  def test_yahoo_europe
    q = Quote.new

    assert_instance_of( Finance::Quote::QuoteData,
			qd = q.yahoo_europe( '12150.PA', 'BOGUS' ) )

    # Check the most reliable indicators of success.
    assert( qd['12150.PA'][:last].to_f > 0 )
    assert( qd['12150.PA'][:name].length > 0 )
    assert( qd['12150.PA'][:success] )
    assert_equal( 'EUR', qd['12150.PA'][:currency] )

    # Check that '%' signs have been deleted.
    assert_no_match( /%/, qd['12150.PA'][:p_change] )

    # Check that a bogus stock returns failure.
    assert( ! qd['BOGUS'][:success], 'Bogus stock look-up returned success.' )

    # Check that London stocks are retrieved in GBP. Test the block version
    # of fetch, while we're at it.
    q.fetch( 'yahoo_europe', 'BAY.L' ) do |qd|
      assert( qd['BAY.L'][:success] )
      assert_equal( 'GBP', qd['BAY.L'][:currency] )
    end
  end

end
