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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
#!perl
#
# tests for "extract" and "fetch*" methods
#
use strict;
use File::Spec::Functions;
use FindBin '$Bin';
use Readonly;
use Test::Exception;
use Test::More tests => 38;
use Text::RecordParser;
use Text::RecordParser::Tab;
use Text::RecordParser::Object;
Readonly my $TEST_DATA_DIR => catdir( $Bin, 'data' );
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
$p->bind_header;
# Extract nothing
my $undef = $p->extract;
is( $undef, undef, 'Fetched nothing' );
# Extract one thing
my $name = $p->extract('Name');
is( $name, '"Simpson, Homer"', 'Name is "Simpson, Homer"' );
# Extract several things
my ( $address, $city ) = $p->extract(qw[ Address City ]);
is( $address, '748 Evergreen Terrace',
'Address is "748 Evergreen Terrace"'
);
is( $city, 'Springfield', 'City is "Springfield"' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'empty' );
my $p = Text::RecordParser->new( $file );
throws_ok { my $data = $p->extract( qw[ foo ] ) } qr/Can't find columns/,
'extract dies without bound fields';
$p->bind_fields( qw[ foo bar baz ] );
my $data = $p->extract( qw[ foo ] );
is( $data, undef, 'extract returns undef on read of empty file' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
$p->bind_header;
throws_ok { my $data = $p->extract('non-existent-field') }
qr/invalid field/i, 'extract dies on bad field request';
}
{
my $file = catfile( $TEST_DATA_DIR, 'bad-file' );
my $p = Text::RecordParser->new( $file );
lives_ok { my @row = $p->fetchrow_array }
'fetchrow_array does not die reading unescaped quote';
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
my $row = $p->fetchrow_hashref;
is( $row->{'City'}, 'Springfield',
'fetchrow_hashref works without binding fields' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
$p->bind_header;
my @row = $p->fetchrow_array;
is( $row[0], '"Simpson, Homer"', 'Field "Simpson, Homer"' );
is( $row[1], '747 Evergreen Terrace', 'Field "747 Evergreen Terrace"' );
is( $row[-1], q["Bart,Lisa,Maggie,Santa's Little Helper"],
'Correct dependents list'
);
my $row = $p->fetchrow_hashref;
is( $row->{'Name'}, '"Flanders, Ned"', 'Name is "Flanders, Ned"' );
is( $row->{'City'}, 'Springfield', 'City is "Springfield"' );
is( $row->{'State'}, '', 'State is empty' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
$p->set_field_alias({
Moniker => 'Name,Name',
City => [ qw( town township ) ],
});
my @aliases = $p->get_field_aliases('City');
is(join(',', @aliases), 'town,township', 'City => town,township');
my $row = $p->fetchrow_hashref;
is( $row->{'Moniker'}, '"Simpson, Homer"',
'Moniker alias for Name' );
is( $row->{'town'}, 'Springfield',
'town alias for city' );
is( $row->{'township'}, 'Springfield',
'township alias for city' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
$p->bind_header;
my $data = $p->fetchall_arrayref;
is( scalar @$data, 2, 'fetchall_arrayref gets 2 records' );
my $row = $data->[0];
is( $row->[0], '"Simpson, Homer"', 'Field "Simpson, Homer"' );
is( $row->[1], '747 Evergreen Terrace', 'Field "747 Evergreen Terrace"' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv');
my $p = Text::RecordParser->new( $file );
$p->bind_header;
my $data = $p->fetchall_arrayref( { Columns => {} } );
is( scalar @$data, 2, 'fetchall_hashref gets 2 records' );
my $row = $data->[1];
is( $row->{'Name'}, '"Flanders, Ned"', 'Name is "Flanders, Ned"' );
is( $row->{'City'}, 'Springfield', 'City is "Springfield"' );
is( $row->{'State'}, '', 'State is empty' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
$p->bind_header;
my $data = $p->fetchall_arrayref('Bad');
is( scalar @$data, 2, 'fetchall_arrayref ignores bad param' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv');
my $p = Text::RecordParser->new( $file );
$p->bind_header;
throws_ok { my $data = $p->fetchall_hashref('Bad Field') }
qr/Invalid key field/, 'fetchall_hashref dies on bad field';
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
$p->bind_header;
my $data = $p->fetchall_hashref('Name');
is( scalar keys %$data, 2, 'fetchall_hashref gets 2 records' );
my $row = $data->{'"Simpson, Homer"'};
is( $row->{'Wife'}, 'Marge', 'Wife is "Marge"' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
$p->bind_header;
$p->field_compute(
'crazy_name',
sub {
my ( $field, $others ) = @_;
my $name = $others->{'Name'};
$name =~ s/"//g;
$name =~ s/^.*,\s+//g;
return "Crazy $name!";
}
);
my $data = $p->fetchall_hashref('crazy_name');
is( scalar keys %$data, 2, 'fetchall_hashref gets 2 records' );
my $row = $data->{'Crazy Homer!'};
is( $row->{'Wife'}, 'Marge', 'Wife is "Marge"' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.ssv' );
my $p = Text::RecordParser->new(
trim => 1,
field_separator => qr/\s+/,
filename => $file,
);
$p->bind_header;
my $row = $p->fetchrow_hashref;
is( $row->{'Address'}, '747 Evergreen Terrace',
'Address is "747 Evergreen Terrace"' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.pdd' );
my $p = Text::RecordParser->new(
trim => 1,
field_separator => '|',
filename => $file,
);
$p->bind_header;
my $row = $p->fetchrow_hashref;
is( $row->{'Address'}, '747 Evergreen Terrace',
'Address is "747 Evergreen Terrace"' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.tab' );
my $p = Text::RecordParser::Tab->new( $file );
my $row = $p->fetchrow_hashref;
is( $row->{'Pets'}, q[Snowball(s),Santa's Little Helper],
'Pets OK (apostrophe backslashed-unescaped)' );
}
{
my $p = Text::RecordParser->new( { fh => \*DATA } );
my $o1 = $p->fetchrow_object;
is( $o1->name, 'moose', 'moose OK' );
my $o2 = $p->fetchrow_object;
is( $o2->name, 'poodle', 'poodle OK' );
my $o3 = $p->fetchrow_object;
is( $o3, undef, 'No problem reading off the end' );
}
__DATA__
name,id
moose,1
poodle,2
|