Business-Shipping

 view release on metacpan or  search on metacpan

t/410-UPS_Offline-Rate.t  view on Meta::CPAN

plan skip_all => 'DataFiles version 1.02+ is required.'
    unless $Business::Shipping::DataFiles::VERSION >= 1.02;

plan 'no_plan';

$::UPS_Online = 1 if Business::Shipping::Config::calc_req_mod('UPS_Online');

unless ($ENV{UPS_USER_ID} and $ENV{UPS_PASSWORD} and $ENV{UPS_ACCESS_KEY}) {
    $::UPS_Online     = 0;
    $::UPS_Online_msg = 'No credentials. Set three UPS_* variables to run.';
}

if ($ENV{DISABLE_UPS_ONLINE}) {
    $::UPS_Online     = 0;
    $::UPS_Online_msg = 'Comparison to UPS_Online is disabled. '
        . 'Unset DISABLE_UPS_ONLINE to run.';
}

if (not $ENV{TEST_SLOW}) {
    $::UPS_Online = 0;
    $::UPS_Online_msg
        = 'Comparison to UPS_Online is too slow. Set TEST_SLOW to run.';
}

# How to test for performance: time DIABLE_UPS_ONLINE=1 /usr/local/perl/bin/perl t/45_UPS_Offline.t
# Currently 1.8 - 2.2 seconds.

use constant CLOSE_ENOUGH_PERCENT => 10;

my %user = (
    user_id    => $ENV{UPS_USER_ID},
    password   => $ENV{UPS_PASSWORD},
    access_key => $ENV{UPS_ACCESS_KEY},
);

sub test {
    my (%args) = @_;
    my $shipment = Business::Shipping->rate_request(
        from_state => 'Washington',
        shipper    => 'UPS_Offline',
        cache      => 0,
    );

    $shipment->submit(%args) or print STDERR $shipment->user_error();
    return $shipment;
}

sub test_online {
    my (%args) = @_;
    my $shipment = Business::Shipping->rate_request(
        shipper => 'UPS_Online',
        cache   => 0,
        %user
    );

    $shipment->submit(%args) or print STDERR $shipment->user_error();
    return $shipment;
}

sub close_enough {
    my ($n1, $n2, $percent) = @_;

    $percent ||= CLOSE_ENOUGH_PERCENT;

    my ($greater, $lesser) = $n1 > $n2 ? ($n1, $n2) : ($n2, $n1);
    my $percentage_of_difference = 1 - ($lesser / $greater);
    my $required_percentage = $percent * .01;

#print "percentage_of_difference = $percentage_of_difference, required = $required_percentage\n";

    return 1 if ($percentage_of_difference <= $required_percentage);
    return 0;
}

{
###########################################################################
##  Domestic Single-package API
###########################################################################

    my %one_da_light_us = (
        service        => '1DA',
        weight         => '3.45',
        from_zip       => '98682',
        to_residential => '0',
        to_zip         => '98270',
    );

    my $shipment = test(%one_da_light_us);
    ok($shipment->total_charges(),
        'UPS domestic single-package API total_charges > 0');
    print "offline 1DA light close: " . $shipment->total_charges() . "\n";

SKIP: {
        skip($::UPS_Online_msg, 1) unless $::UPS_Online;

        my $shipment_online = test_online(%one_da_light_us);
        ok($shipment_online->total_charges(),
            'UPS domestic single-package API total_charges > 0');
        print "online 1DA light close: "
            . $shipment_online->total_charges() . "\n";
    }

}

{
    my %ground_res_heavy_far_us = (
        service        => 'GNDRES',
        weight         => '45.00',
        from_zip       => '98682',
        to_residential => '',
        to_zip         => '22182',
    );

    my $shipment = test(%ground_res_heavy_far_us);
    ok($shipment->total_charges(),
        'UPS domestic single-package API total_charges > 0');
    print "Offline: GNDRES, heavy, far: " . $shipment->total_charges() . "\n";

SKIP: {
        skip($::UPS_Online_msg, 1) unless $::UPS_Online;

        my $shipment_online = test_online(%ground_res_heavy_far_us);
        ok($shipment_online->total_charges(),
            'UPS domestic single-package API total_charges > 0');
        print "Online: GNDRES, heavy, far: "
            . $shipment_online->total_charges() . "\n";
    }

}

{



( run in 0.870 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )