re-work installation to use a bse.cfg style file
[bse.git] / t / 110-courier / 020-auspost.t
CommitLineData
a50980d7
TC
1#!perl -w
2use strict;
3use Test::More tests => 27;
4
5use Courier::AustraliaPost::Standard;
6use Courier::AustraliaPost::Air;
7use Courier::AustraliaPost::Sea;
8use BSE::Shipping;
9
10my %cfg_work =
11 (
12 shipping =>
13 {
14 sourcepostcode => "4350",
15 },
16 debug =>
17 {
18 auspost => 0
19 },
20 );
21
22my $cfg = bless \%cfg_work, "Test::Cfg";
23
24my $std = Courier::AustraliaPost::Standard->new(config => $cfg);
25my $air = Courier::AustraliaPost::Air->new(config => $cfg);
26my $sea = Courier::AustraliaPost::Sea->new(config => $cfg);
27
28ok($std, "make standard courier object");
a358f0ab 29ok($std->can_deliver(country => "AU",
a50980d7
TC
30 suburb => "Westmead",
31 postcode => "2145"), "can deliver to australia");
a358f0ab
TC
32ok(!$std->can_deliver(country => "NZ"),
33 "can't deliver to NZ");
a50980d7
TC
34
35my $tiny_parcel = BSE::Shipping::Parcel->new
36 (
37 length => 50,
38 width => 20,
39 height => 20,
40 weight => 200
41 );
42my $small_parcel = BSE::Shipping::Parcel->new
43 (
44 length => 100,
45 width => 200,
46 height => 200,
47 weight => 800
48 );
49
50my $medium_parcel = BSE::Shipping::Parcel->new
51 (
52 length => 500,
53 width => 400,
54 height => 300,
55 weight => 6000
56 );
57
58my $local_tiny_cost = $std->calculate_shipping
59 (
60 parcels => [ $tiny_parcel ],
61 postcode => 4405,
62 suburb => "Dalby",
a358f0ab 63 country => "AU"
a50980d7
TC
64 );
65ok($local_tiny_cost, "got a local tiny parcel cost")
66 or diag $std->error_message;
67like($local_tiny_cost, qr/^\d+$/, "it's an integer");
68print "# $local_tiny_cost\n";
69
70my $local_small_cost = $std->calculate_shipping
71 (
72 parcels => [ $small_parcel ],
73 postcode => 4405,
74 suburb => "Dalby",
a358f0ab 75 country => "AU"
a50980d7
TC
76 );
77ok($local_small_cost, "got a local small parcel cost")
78 or diag $std->error_message;
79like($local_small_cost, qr/^\d+$/, "it's an integer");
80print "# $local_small_cost\n";
81
82my $local_medium_cost = $std->calculate_shipping
83 (
84 parcels => [ $medium_parcel ],
85 postcode => 4405,
86 suburb => "Dalby",
a358f0ab 87 country => "AU"
a50980d7
TC
88 );
89ok($local_medium_cost, "got a local medium cost")
90 or diag $std->error_message;
91like($local_medium_cost, qr/^\d+$/, "it's an integer");
92print "# $local_medium_cost\n";
93cmp_ok($local_tiny_cost, "<=", $local_small_cost, "tiny < small");
94cmp_ok($local_small_cost, "<=", $local_medium_cost, "small < medium");
95
96# longer distance
97my $nsw_medium_cost = $std->calculate_shipping
98 (
99 parcels => [ $medium_parcel ],
100 postcode => 2145,
101 suburb => "Westmead",
a358f0ab 102 country => "AU"
a50980d7
TC
103 );
104
105ok($nsw_medium_cost, "got a nsw medium cost");
106like($nsw_medium_cost, qr/^\d+$/, "it's an integer");
107print "# $nsw_medium_cost\n";
108cmp_ok($local_medium_cost, "<=", $nsw_medium_cost, "local <= nsw");
109
110# longest
111my $perth_medium_cost = $std->calculate_shipping
112 (
113 parcels => [ $medium_parcel ],
114 postcode => 6000,
115 suburb => "Perth",
a358f0ab 116 country => "AU"
a50980d7
TC
117 );
118
119ok($perth_medium_cost, "got a perth medium cost");
120like($perth_medium_cost, qr/^\d+$/, "it's an integer");
121print "# $perth_medium_cost\n";
122cmp_ok($nsw_medium_cost, "<=", $perth_medium_cost, "nsw <= perth");
123
124# international
125my $us_medium_cost_air = $air->calculate_shipping
126 (
127 parcels => [ $medium_parcel ],
128 postcode => 6000,
129 suburb => "Perth",
a358f0ab 130 country => "US"
a50980d7
TC
131 );
132
133ok($us_medium_cost_air, "got a US medium cost");
134like($us_medium_cost_air, qr/^\d+$/, "it's an integer");
135print "# $us_medium_cost_air\n";
136cmp_ok($perth_medium_cost, "<=", $us_medium_cost_air, "perth <= us air");
137
138my $us_medium_cost_sea = $sea->calculate_shipping
139 (
140 parcels => [ $medium_parcel ],
141 postcode => 6000,
142 suburb => "Perth",
a358f0ab 143 country => "US"
a50980d7
TC
144 );
145
146ok($us_medium_cost_sea, "got a US medium cost");
147like($us_medium_cost_sea, qr/^\d+$/, "it's an integer");
148print "# $us_medium_cost_sea\n";
5bbf7309 149cmp_ok($perth_medium_cost, "<=", $us_medium_cost_air, "perth <= us air");
a50980d7
TC
150
151# too big
152my $too_long = BSE::Shipping::Parcel->new
153 (
154 length => 1060,
155 width => 100,
156 height => 100,
157 weight => 200
158 );
159my $too_long_cost = $std->calculate_shipping
160 (
161 parcels => [ $too_long ],
162 postcode => 2145,
163 suburb => "Westmead",
a358f0ab 164 country => "AU",
a50980d7
TC
165 );
166is($too_long_cost, undef, "too long returns undef");
167ok($std->error_message, "some error set");
168
169my $big_girth = BSE::Shipping::Parcel->new
170 (
171 length => 4000,
172 width => 300,
173 height => 500,
174 weight => 200
175 );
176
177my $big_girth_cost = $std->calculate_shipping
178 (
179 parcels => [ $big_girth ],
180 postcode => 2145,
181 suburb => "Westmead",
a358f0ab 182 country => "AU",
a50980d7
TC
183 );
184is($too_long_cost, undef, "big girth returns undef");
185ok($std->error_message, "some error set");
186
187package Test::Cfg;
188
189sub entry {
190 my ($self, $section, $key, $def) = @_;
191
192 exists $self->{$section} or return $def;
193 exists $self->{$section}{$key} or return $def;
194 return $self->{$section}{$key};
195}