summaryrefslogtreecommitdiffstats
path: root/t/05-batch.t
blob: 3ae51d0389fd420e45d2272e0535c5f3e0294d48 (plain)
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
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();