File: testload.pm

package info (click to toggle)
libfinance-quotehist-perl 1.19-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 428 kB
  • sloc: perl: 1,940; makefile: 4
file content (147 lines) | stat: -rw-r--r-- 3,304 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package testload;

use strict;
use Test::More;
use File::Spec;
use LWP::UserAgent;
use HTTP::Request;

use Finance::QuoteHist;

use constant DEV_TESTS => $ENV{FQH_DEV_TESTS};

use constant GOLDEN_CHILD => 'yahoo';

use vars qw( @ISA @EXPORT );

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
  network_ok
  new_quotehist
  modules
  sources
  modes
  granularities
  basis
  csv_content
  GOLDEN_CHILD
  DEV_TESTS
);

my $Dat_Dir;
BEGIN {
  my($vol, $dir, $file) = File::Spec->splitpath(__FILE__);
  my @parts = File::Spec->splitdir($dir);
  pop @parts while @parts && $parts[-1] ne 't';
  $dir = File::Spec->catdir(@parts, 'dat');
  $Dat_Dir = File::Spec->catpath($vol, $dir, '');
}

my $csv_txt;
my $csv_file = "$Dat_Dir/csv.dat";
open(F, '<', $csv_file) or die "problem reading $csv_file : $!";
$csv_txt = join('', <F>);
close(F);

sub csv_content { $csv_txt }

my(%Modules, %Files);

for my $f (glob("$Dat_Dir/*.dat")) {
  my($vol, $dir, $label) = File::Spec->splitpath($f);
  $label =~ s/\.dat$//;
  next unless $label =~ /^(quote|dividend|split)_/;
  open(F, '<', $f) or die "problem reading $f : $!";
  my @lines = <F>;
  chomp @lines;
  close(F);
  my $class = shift @lines;
  ++$Modules{$class};
  my($sym, $start, $end) = split(/,/, shift @lines);
  if ($1 eq 'quote') {
    my($mode, $gran, $source) = split(/_/, $label);
    if ($lines[0] =~ tr/:/:/ > 6) {
      # drop adjusted, they've proven to be too variable for testing
      for my $i (0 .. $#lines) {
        my @line = split(/:/, $lines[$i]);
        pop @line while @line > 7;
        $lines[$i] = join(':', @line);
      }
    }
    $Files{$source}{$mode}{$gran} = [$class, $sym, $start, $end, \@lines];
  }
  else {
    my($mode, $source) = split(/_/, $label);
    $Files{$source}{$mode} = [$class, $sym, $start, $end, \@lines];
  }
}

my $Network_Up;

sub network_ok {
  if (! defined $Network_Up) {
    my %ua_parms;
    if ($ENV{disable_network_tests}) {
      $Network_Up = 0;
      return $Network_Up;
    }
    if ($ENV{http_proxy}) {
      $ua_parms{env_proxy} = 1;
    }
    my $ua = LWP::UserAgent->new(%ua_parms)
      or die "Problem creating user agent\n";
    my $request = HTTP::Request->new('HEAD', 'http://finance.yahoo.com')
      or die "Problem creating http request object\n";
    my $response = $ua->request($request, @_);
    $Network_Up = $response->is_redirect || $response->is_success;
    if (!$Network_Up) {
      print STDERR "Problem with net fetch: ", $response->status_line, "\n";
    }
  }
  $Network_Up;
}

sub new_quotehist {
  my($symbols, $start_date, $end_date, %parms) = @_;
  my $class = $parms{class} || 'Finance::QuoteHist';
  delete $parms{class};
  $class->new(
    symbols    => $symbols,
    start_date => $start_date,
    end_date   => $end_date,
    auto_proxy => 1,
    %parms,
  );
}

sub modules { sort keys %Modules }

sub sources { sort keys %Files }

sub modes {
  my $src = shift || return;
  my $h = $Files{$src} || return;
  sort keys %$h;
}

sub granularities {
  my $src = shift || return;
  my $h = $Files{$src}{quote} || return;
  sort keys %$h;
}

sub basis {
  my($src, $mode, $gran) = @_;
  my $basis;
  if ($mode eq 'quote') {
    $basis = $Files{$src}{$mode}{$gran};
  }
  else {
    $basis = $Files{$src}{$mode};
  }
  return unless $basis;
  @$basis;
}

1;