Skip to content

Commit

Permalink
Use Test::MockModule for mocking request responses
Browse files Browse the repository at this point in the history
  • Loading branch information
taskula committed Jan 31, 2021
1 parent 2991aee commit 4c30856
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ requires "JSON" => "2.07";
requires "LWP::Protocol::https" => "6.04";
requires "LWP::UserAgent" => "5.837";

test_requires "Test::MockModule" => "0.171.0";
test_requires "Test::More" => "0.88";
test_requires "Test::Warn" => "0.36";
2 changes: 2 additions & 0 deletions lib/Binance/PerlDependencies.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ our $required = {
};

our $required_test = {
'Test::MockModule'
=> '0.171.0',
'Test::More'
=> '0.88',
'Test::Warn'
Expand Down
51 changes: 51 additions & 0 deletions t/Binance/API.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@
use strict;
use warnings;

use Test::MockModule;
use Test::More tests => 10;
use Test::Warn;

use Binance::API;

# Mock Binance::API::Request::_exec in order to avoid executing a real http
# request. Instead return whatever we pass to the api_result method.
my $mock = Test::MockModule->new('Binance::API::Request');
sub api_result {
my $expected_result = shift;
$mock->redefine('_exec', sub {
return $expected_result;
});
}

my $api = Binance::API->new();

subtest 'new() tests' => sub {
Expand All @@ -56,6 +67,10 @@ subtest 'aggregate_trades() tests' => sub {
ok(Binance::API->can('aggregate_trades'), 'method aggregate_trades() '.
'available');

api_result([
{ 'T' => 1 }
]);

eval { $api->aggregate_trades };
is(ref($@), 'Binance::Exception::Parameter::Required', 'Exception thrown '
.'when missing a parameter');
Expand All @@ -72,6 +87,10 @@ subtest 'all_book_tickers() tests' => sub {
ok(Binance::API->can('all_book_tickers'), 'method all_book_tickers() '
.'available');

api_result([
{ 'symbol' => 'ETHBTC' }
]);

my $all_book_tickers = $api->all_book_tickers;
ok(defined $all_book_tickers, 'Requested all_book_tickers');
ok(defined $all_book_tickers->[0]->{'symbol'}, 'Got a successful response');
Expand All @@ -83,11 +102,19 @@ subtest 'ticker_price() tests' => sub {
ok(Binance::API->can('ticker_price'), 'method ticker_price() '
.'available');

api_result([
{ 'symbol' => 'ETHBTC' }
]);

my $all_prices = $api->ticker_price;
ok(defined $all_prices, 'Requested all ticker_price');
ok(defined $all_prices->[0]->{'symbol'}, 'Got a successful response');
ok($all_prices > 0, 'Got multiple ticker prices');

api_result(
{ 'symbol' => 'ETHBTC' }
);

my $one_price = $api->ticker_price( symbol => 'ETHBTC' );
ok(defined $one_price, 'Requested one ticker_price');
is($one_price->{'symbol'}, 'ETHBTC', 'Got a successful response');
Expand All @@ -98,11 +125,19 @@ subtest 'depth() tests' => sub {

ok(Binance::API->can('depth'), 'method depth() available');

api_result([
{ 'symbol' => 'ETHBTC' }
]);

eval { $api->depth };
is(ref($@), 'Binance::Exception::Parameter::Required', 'Exception thrown '
.'when missing a parameter');
is($@->parameters->[0], 'symbol', 'Missing parameter "symbol"');

api_result(
{ 'asks' => [0,1], 'bids' => [0,1] }
);

my $depth = $api->depth( symbol => 'ETHBTC' );
ok(defined $depth, 'Requested depth');
ok(@{$depth->{'asks'}} > 0 , 'Depth has returned some asks');
Expand All @@ -114,6 +149,10 @@ subtest 'klines() tests' => sub {

ok(Binance::API->can('klines'), 'method klines() available');

api_result([
[ 1 ]
]);

eval { $api->klines };
is(ref($@), 'Binance::Exception::Parameter::Required', 'Exception thrown '
.'when missing a parameter');
Expand All @@ -134,6 +173,10 @@ subtest 'order() tests' => sub {

ok(Binance::API->can('order'), 'method order() available');

api_result([
{ 'symbol' => 'ETHBTC' }
]);

eval { $api->order_test( symbol => 'ETHBTC', side => 'SELL' ) };
is(ref($@), 'Binance::Exception::Parameter::Required', 'Exception thrown '
.'when missing a parameter');
Expand Down Expand Up @@ -363,6 +406,8 @@ subtest 'ping() tests' => sub {

ok(Binance::API->can('ping'), 'method ping() available');

api_result({ });

ok($api->ping('/api/v1/ping', 'get'), 'Pinging Binance server');
};

Expand All @@ -371,6 +416,10 @@ subtest 'ticker() tests' => sub {

ok(Binance::API->can('ticker'), 'method ticker() available');

api_result(
{ 'openTime' => 1 }
);

eval { $api->klines };
is(ref($@), 'Binance::Exception::Parameter::Required', 'Exception thrown '
.'when missing a parameter');
Expand All @@ -386,5 +435,7 @@ subtest 'time() tests' => sub {

ok(Binance::API->can('time'), 'method time() available');

api_result({ 'serverTime' => 1513415733605 });

ok($api->time() > 1513415733604, 'Binance server time');
};
12 changes: 12 additions & 0 deletions t/Binance/API/Request.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@
use strict;
use warnings;

use Test::MockModule;
use Test::More tests => 1;
use Test::Warn;
use Data::Dumper;
use Binance::API::Logger;
use Binance::API::Request;

# Mock Binance::API::Request::_exec in order to avoid executing a real http
# request. Instead return whatever we pass to the api_result method.
my $mock = Test::MockModule->new('Binance::API::Request');
sub api_result {
my $expected_result = shift;
$mock->redefine('_exec', sub {
return $expected_result;
});
}
api_result({ 1 => 2 });

subtest '_init() tests' => sub {
plan tests => 2;

Expand Down

0 comments on commit 4c30856

Please sign in to comment.