summaryrefslogtreecommitdiffstats
path: root/t/05-batch.t
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-26 14:40:05 -0300
committerMurilo Ijanc <[email protected]>2026-07-26 14:40:05 -0300
commit0bc578d6d334c7f1ccaf329cd61bb8bce76b7f59 (patch)
tree86f73108f0432fd8330edcaab8b8ae7018322dee /t/05-batch.t
downloadperl-6661cb35162c38099b86f248b938f5e4d4d11515.tar.gz
Initial importv0.1.0
Diffstat (limited to 't/05-batch.t')
-rw-r--r--t/05-batch.t146
1 files changed, 146 insertions, 0 deletions
diff --git a/t/05-batch.t b/t/05-batch.t
new file mode 100644
index 0000000..3ae51d0
--- /dev/null
+++ b/t/05-batch.t
@@ -0,0 +1,146 @@
+use strict;
+use warnings;
+use utf8;
+
+use FindBin ();
+use lib "$FindBin::Bin/lib";
+
+use Test::More;
+use TestServer qw(csv_body new_client);
+
+use Cepis;
+
+{
+ my $body = csv_body(
+ '01001000,Praça da Sé,lado ímpar,,Sé,São Paulo,SP,São Paulo,Sudeste,3550308,1004,11,7107',
+ '99999999,,,,,,,,,,,,',
+ '20040020,Avenida Rio Branco,,,Centro,Rio de Janeiro,RJ,Rio de Janeiro,'
+ . 'Sudeste,3304557,,21,6001',
+ );
+ my $server = TestServer->new(
+ routes => { '/csv?ceps=01001000%2C99999999%2C20040020' => [ 200, $body, {} ] } );
+ my $client = new_client($server);
+
+ my $result = $client->lookup_many( [qw(01001000 99999999 20040020)] );
+ is( scalar @$result, 3, 'the result is as long as the input' );
+ is( $result->[0]->localidade, 'São Paulo', 'the first CEP resolved' );
+ is( $result->[1], undef, 'a missing CEP is undef at its position' );
+ is( $result->[2]->uf, 'RJ', 'and the third is not shifted up' );
+ $server->stop;
+}
+
+{
+ my $body = csv_body(
+ '01001000,Praça da Sé,,,Sé,São Paulo,SP,São Paulo,Sudeste,3550308,1004,11,7107');
+ my $server
+ = TestServer->new( routes => { '/csv?ceps=01001000' => [ 200, $body, {} ] } );
+ my $client = new_client($server);
+
+ my $address = $client->lookup_many( ['01001000'] )->[0];
+ is( $address->localidade, 'São Paulo', 'the batch resolved it' );
+ is( $address->geo, undef, 'but a batch never carries a coordinate' );
+ $server->stop;
+}
+
+{
+ my $body = csv_body(
+ '01001000,"Rua Um, Dois",,,Centro,São Paulo,SP,São Paulo,Sudeste,1,,11,1');
+ my $server
+ = TestServer->new( routes => { '/csv?ceps=01001000' => [ 200, $body, {} ] } );
+ my $client = new_client($server);
+
+ is( $client->lookup_many( ['01001000'] )->[0]->logradouro,
+ 'Rua Um, Dois', 'a quoted field holding a comma is parsed' );
+ $server->stop;
+}
+
+{
+ my $server = TestServer->new;
+ my $client = new_client($server);
+
+ is_deeply( $client->lookup_many( [] ), [], 'an empty list yields an empty list' );
+ is( scalar( $server->calls ), 0, 'and makes no request' );
+ $server->stop;
+}
+
+{
+ my @ceps = map { sprintf '%08d', $_ } 1 .. 501;
+ my @rows = map {"$_,,,,,X,SP,São Paulo,Sudeste,1,,11,1"} @ceps;
+
+ my $server = TestServer->new(
+ handler => sub {
+ my ( $path, $seen ) = @_;
+ my @slice = $seen == 1 ? @rows[ 0 .. 499 ] : @rows[ 500 .. 500 ];
+ return [ 200, csv_body(@slice), {} ];
+ }
+ );
+ my $client = new_client($server);
+
+ my $result = $client->lookup_many( \@ceps );
+ is( scalar( $server->calls ), 2, '501 CEPs are split into two requests' );
+ is( scalar @$result, 501, 'and the result is still 501 long' );
+ is( $result->[500]->localidade, 'X', 'the last one is present' );
+ $server->stop;
+}
+
+{
+ my $body = csv_body('01001000,,,,,São Paulo,SP,São Paulo,Sudeste,1,,11,1');
+ my $server = TestServer->new(
+ routes => { '/csv?ceps=01001000%2C20040020' => [ 200, $body, {} ] } );
+ my $client = new_client($server);
+
+ my $result = $client->lookup_many( [qw(01001000 20040020)] );
+ is( scalar @$result, 2, 'a truncated response is padded, not returned short' );
+ is( $result->[1], undef, 'the missing tail is undef' );
+ $server->stop;
+}
+
+{
+ my $body = csv_body(
+ '01001000,,,,,X,SP,São Paulo,Sudeste,1,,11,1',
+ '01001001,,,,,Y,SP,São Paulo,Sudeste,1,,11,1',
+ '01001002,,,,,Z,SP,São Paulo,Sudeste,1,,11,1',
+ '01001003,,,,,W,SP,São Paulo,Sudeste,1,,11,1',
+ );
+ my $server = TestServer->new(
+ routes => { '/csv?ceps=01001000%2C01001001' => [ 200, $body, {} ] } );
+ my $client = new_client($server);
+
+ my $result = $client->lookup_many( [qw(01001000 01001001)] );
+ is( scalar @$result, 2, 'an over-long response is truncated to the input length' );
+ $server->stop;
+}
+
+{
+ my $body = csv_body( '123,,,,,,,,,,,,', '01001000,,,,,X,SP,São Paulo,Sudeste,1,,11,1' );
+ my $server = TestServer->new(
+ routes => { '/csv?ceps=123%2C01001000' => [ 200, $body, {} ] } );
+ my $client = new_client($server);
+
+ my $result = $client->lookup_many( [qw(123 01001000)] );
+ is( $result->[0], undef, 'a malformed CEP is undef rather than an exception' );
+ is( $result->[1]->localidade, 'X', 'and does not cost the rest of the batch' );
+ $server->stop;
+}
+
+{
+ my $server = TestServer->new(
+ routes => { '/csv?ceps=01001000' => [ 200, '<html>captive portal</html>', {} ] } );
+ my $client = new_client($server);
+
+ my $ok = eval { $client->lookup_many( ['01001000'] ); 1 };
+ ok( !$ok, 'a body that is not the expected CSV throws' );
+ isa_ok( $@, 'Cepis::Error::InvalidResponse' );
+ $server->stop;
+}
+
+{
+ my $server = TestServer->new;
+ my $client = new_client($server);
+
+ my $ok = eval { $client->lookup_many('01001000'); 1 };
+ ok( !$ok, 'lookup_many wants an array reference' );
+ $server->stop;
+}
+
+done_testing();