| 1 | package Quant::Framework::Utils::Test; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Quant::Framework::Utils::Test | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | This module is used when testing Quant::Framework modules to create new documents. | ||
| 10 | A set of pre-defined templates are provided which can be used to create test documents. | ||
| 11 | Also it is possible to change some/all of the test document by passing corresponding | ||
| 12 | key/values to create_doc. | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Quant::Framework::Utils::Test; | ||
| 17 | |||
| 18 | #using default values from test data file | ||
| 19 | Quant::Framework::Utils::Test::create_doc("corporate_actions"); | ||
| 20 | |||
| 21 | #changing some of values in the test data file | ||
| 22 | Quant::Framework::Utils::Test::create_doc("corporate_actions", | ||
| 23 | { symbol => 'ABCD', | ||
| 24 | chronicle_reader => $reader, | ||
| 25 | chronicle_writer => $writer, | ||
| 26 | actions => { | ||
| 27 | "62799500" => { | ||
| 28 | "monitor_date" => "2014-02-07T06:00:07Z", | ||
| 29 | "type" => "ACQUIS", | ||
| 30 | "monitor" => 1, | ||
| 31 | "description" => "Acquisition", | ||
| 32 | "effective_date" => "15-Jul-14", | ||
| 33 | "flag" => "N" | ||
| 34 | }, | ||
| 35 | } | ||
| 36 | }); | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | 2 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | |||
| 44 | 2 | ▶ | use File::ShareDir (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | 2 | ▶ | use YAML::XS qw(LoadFile); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | 2 | ▶ | use Quant::Framework::CorporateAction; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 47 | 2 | ▶ | use Data::Chronicle::Writer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 48 | 2 | ▶ | use Data::Chronicle::Reader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 49 | 2 | ▶ | use Data::Chronicle::Mock; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 50 | 2 | ▶ | use Quant::Framework::InterestRate; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 51 | use Quant::Framework::Currency; | ||
| 52 | use Quant::Framework::Asset; | ||
| 53 | use Quant::Framework::Dividend; | ||
| 54 | use Quant::Framework::VolSurface::Delta; | ||
| 55 | use Quant::Framework::VolSurface::Moneyness; | ||
| 56 | use Quant::Framework::Utils::UnderlyingConfig; | ||
| 57 | |||
| 58 | =head2 create_doc | ||
| 59 | |||
| 60 | Create a new document in the test database | ||
| 61 | |||
| 62 | params: | ||
| 63 | $yaml_db => The name of the entity in the YAML file (eg. promo_code) | ||
| 64 | $data_mod => hasref of modifictions required to the data (optional) | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub create_doc { | ||
| 69 | my ($yaml_db, $data_mod) = @_; | ||
| 70 | |||
| 71 | my $save = 1; | ||
| 72 | if (exists $data_mod->{save}) { | ||
| 73 | $save = delete $data_mod->{save}; | ||
| 74 | } | ||
| 75 | |||
| 76 | # get data to insert | ||
| 77 | my $fixture = LoadFile(File::ShareDir::dist_file('Quant-Framework', 'test_data.yml')); | ||
| 78 | |||
| 79 | my $data = $fixture->{$yaml_db}{data}; | ||
| 80 | |||
| 81 | die "Invalid yaml db name: $yaml_db" if not defined $data; | ||
| 82 | |||
| 83 | # modify data? | ||
| 84 | for (keys %$data_mod) { | ||
| 85 | $data->{$_} = $data_mod->{$_}; | ||
| 86 | } | ||
| 87 | |||
| 88 | # use class to create the document | ||
| 89 | my $class_name = $fixture->{$yaml_db}{class_name}; | ||
| 90 | my $obj = $class_name->new($data); | ||
| 91 | |||
| 92 | if ($save) { | ||
| 93 | $obj->save; | ||
| 94 | } | ||
| 95 | |||
| 96 | return $obj; | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 create_underlying_config | ||
| 100 | |||
| 101 | Creates an instance of UnderlyingConfig (for EURUSD or GDAXI) for tesing purposes. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub create_underlying_config { | ||
| 106 | my $symbol = shift; | ||
| 107 | my $custom_attributes = shift; | ||
| 108 | |||
| 109 | my $fixture = LoadFile(File::ShareDir::dist_file('Quant-Framework', 'test_underlying_config.yml')); | ||
| 110 | my $data = $fixture->{$symbol}; | ||
| 111 | |||
| 112 | if (defined $custom_attributes) { | ||
| 113 | while (my ($key, $value) = each %$custom_attributes) { | ||
| 114 | $data->{$key} = $value; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | return Quant::Framework::Utils::UnderlyingConfig->new($data); | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; | ||