#!/usr/bin/ruby -w
#
# $Id: tc_yahoo,v 1.2 2004/07/10 09:54:30 ianmacd Exp $

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

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

include Finance

class TC_YahooTest < Test::Unit::TestCase

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

    assert_instance_of( Hash, 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] )
  end

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

    assert_instance_of( Hash, 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.
    qd = q.fetch( 'yahoo_europe', 'BAY.L' )
    assert( qd['BAY.L'][:success] )
    assert_equal( 'GBP', qd['BAY.L'][:currency] )
  end

end
