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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl 1.t'
#########################
# change 'tests => 1' to 'tests => last_test_to_print';
use warnings;
use strict;
use Test::More tests => 33;
BEGIN { use_ok('Net::Amazon') };
#use Log::Log4perl qw(:easy);
#Log::Log4perl->easy_init($INFO);
use Net::Amazon::Request::ASIN;
use Net::Amazon::Response::ASIN;
use File::Spec;
my $CANNED = "canned";
$CANNED = File::Spec->catfile("t", "canned") unless -d $CANNED;
if(! exists $ENV{NET_AMAZON_LIVE_TESTS}) {
for(map { File::Spec->catfile($CANNED, $_) }
qw(asin_pp.xml asin_err.xml
asin_mua.xml asin_cd.xml asin_cdm.xml dvd.xml)) {
open FILE, "<$_" or die "Cannot open $_";
my $data = join '', <FILE>;
close FILE;
push @Net::Amazon::CANNED_RESPONSES, $data;
}
}
######################################################################
# Successful ASIN fetch
######################################################################
my $ua = Net::Amazon->new(
token => 'YOUR_AMZN_TOKEN',
);
my $req = Net::Amazon::Request::ASIN->new(
asin => '0201360683'
);
# Response is of type Net::Amazon::ASIN::Response
my $resp = $ua->request($req);
ok($resp->is_success(), "Successful fetch");
like($resp->as_string(), qr/Schilli/, "Found Perl Power");
######################################################################
# Error fetching ASIN
######################################################################
$req = Net::Amazon::Request::ASIN->new(
asin => '123'
);
# Response is of type Net::Amazon::ASIN::Response
$resp = $ua->request($req);
ok($resp->is_error(), "Error reported correctly");
like($resp->message(), qr/Invalid/, "Invalid ASIN reported correctly");
######################################################################
# Multiple Authors
######################################################################
$req = Net::Amazon::Request::ASIN->new(
asin => '0201633612'
);
# Response is of type Net::Amazon::ASIN::Response
$resp = $ua->request($req);
ok($resp->is_success(), "Found Gamma");
my($book) = $resp->properties();
like(join('&', $book->authors()),
qr#Erich Gamma&Richard Helm&Ralph Johnson&John Vlissides#,
"Found multiple authors");
my @similar = $book->similar_asins();
is(scalar @similar, 0, "No similar items on this item");
######################################################################
# properties() in scalar context
######################################################################
$book = $resp->properties();
like(join('&', $book->authors()),
qr#Erich Gamma&Richard Helm&Ralph Johnson&John Vlissides#,
"Found multiple authors");
######################################################################
# Net::Amazon::Property::Book accessors
######################################################################
is($book->title, "Design Patterns", "Title");
is($book->year, "1995", "Year");
like($book->OurPrice, qr/\$/, "Amazon Price");
like($book->ListPrice, qr/\$/, "List Price");
is($book->binding, "Hardcover", "Binding");
######################################################################
# Successful ASIN fetch of a music CD
######################################################################
$req = Net::Amazon::Request::ASIN->new(
asin => 'B00007M84Q',
mode => 'music',
);
# Response is of type Net::Amazon::ASIN::Response
$resp = $ua->request($req);
ok($resp->is_success(), "Successful fetch");
like($resp->as_string(), qr/Zwan/, "Found Zwan");
######################################################################
# Net::Amazon::Property::Music accessors
######################################################################
my($cd) = $resp->properties();
is($cd->album, "Mary Star of the Sea", "Album");
is($cd->artist, "Zwan", "Artist");
is($cd->year, "2003", "Year");
like($cd->OurPrice, qr/\$/, "Amazon Price");
like($cd->ListPrice, qr/\$/, "List Price");
######################################################################
# Net::Amazon::Property::Music item with two artists
######################################################################
$req = Net::Amazon::Request::ASIN->new(
asin => 'B00005A46I',
mode => 'music',
);
# Response is of type Net::Amazon::ASIN::Response
$resp = $ua->request($req);
ok($resp->is_success(), "Successful fetch");
like($resp->as_string(), qr(Anne Sofie von Otter/Elvis Costello),
"Found Otter/Costello");
($cd) = $resp->properties();
is($cd->artist(), "Anne Sofie von Otter", "artist() on mult artists");
is(join('#', $cd->artists()), "Anne Sofie von Otter#Elvis Costello",
"artists() with mult artists");
######################################################################
# Net::Amazon::Property::DVD item with two artists
######################################################################
$req = Net::Amazon::Request::ASIN->new(
asin => '6305181772',
);
# Response is of type Net::Amazon::ASIN::Response
$resp = $ua->request($req);
ok($resp->is_success(), "Successful fetch");
like($resp->as_string(), qr(Mission Impossible),
"Found Mission Impossible");
my ($dvd) = $resp->properties();
is($dvd->director(), "Brian De Palma", "director() finds first director");
like($dvd->SalesRank(), qr/^[\d,]+$/, "Checking SalesRank");
is(join('#', $dvd->directors()), "Brian De Palma",
"directors() finds first director");
like(join('#', $dvd->starring()), qr/Tom Cruise#Jon Voight#Emmanuelle B/,
"starring() finds actors");
is($resp->total_results, 1, "Total of 1");
######################################################################
# Illegal request type
######################################################################
eval {$req = Net::Amazon::Request::ASIN->new(asin => "6305181772",
type => "whackamole");};
like($@, qr/Unknown type/, "Check illegal request type");
|