|
| 1 | +#! /usr/bin/env perl |
| 2 | + |
| 3 | +# vim: noet ts=2 sw=2: |
| 4 | + |
| 5 | +use strict; |
| 6 | +use warnings; |
| 7 | +use Test::More tests => 17; |
| 8 | + |
| 9 | +use Storable qw(dclone); |
| 10 | +use DBI qw(:sql_types); |
| 11 | + |
| 12 | +# our reference table: |
| 13 | +# |
| 14 | +# A0 B1 C2 |
| 15 | +# ------- --------- ------- |
| 16 | +# foo NULL bazooka |
| 17 | +# foolery bar NULL |
| 18 | +# NULL barrowman baz |
| 19 | +# |
| 20 | + |
| 21 | +# Historically, DBD::Sponge defaulted an sth's PRECISION to the length |
| 22 | +# of its column names, meaning that some DBI shells could truncate row |
| 23 | +# display. For example, formatting a row ('fo', NULL, 'ba') from our |
| 24 | +# reference table above. |
| 25 | + |
| 26 | +our @NAMES = ( 'A0', 'B1', 'C2' ); |
| 27 | +our @ROWS = (['foo', undef, 'bazooka'], |
| 28 | + ['foolery', 'bar', undef ], |
| 29 | + [undef, 'barrowman', 'baz' ]); |
| 30 | + |
| 31 | +my $dbh = DBI->connect("dbi:Sponge:", '', ''); |
| 32 | +ok($dbh, "connect(dbi:Sponge:) succeeds"); |
| 33 | + |
| 34 | +my $sth = $dbh->prepare("simple, correct sponge", { |
| 35 | + rows => dclone( \@ROWS ), |
| 36 | + NAME => [ @NAMES ], |
| 37 | + }); |
| 38 | + |
| 39 | +ok($sth, "prepare() of 3x3 result succeeded"); |
| 40 | +is_deeply($sth->{NAME}, ['A0', 'B1', 'C2'], "column NAMEs as expected"); |
| 41 | +is_deeply($sth->{TYPE}, [SQL_VARCHAR, SQL_VARCHAR, SQL_VARCHAR], |
| 42 | + "column TYPEs default to SQL_VARCHAR"); |
| 43 | +# |
| 44 | +# Old versions of DBD-Sponge defaulted PRECISION (data "length") to |
| 45 | +# length of the field _names_ rather than the length of the _data_. |
| 46 | +# |
| 47 | +is_deeply($sth->{PRECISION}, [7, 9, 7], |
| 48 | + "column PRECISION matches lengths of longest field data"); |
| 49 | +is_deeply($sth->fetch(), $ROWS[0], "first row fetch as expected"); |
| 50 | +is_deeply($sth->fetch(), $ROWS[1], "second row fetch as expected"); |
| 51 | +is_deeply($sth->fetch(), $ROWS[2], "third row fetch as expected"); |
| 52 | +ok(!defined($sth->fetch()), "fourth fetch returns undef"); |
| 53 | + |
| 54 | +# Test that DBD-Sponge preserves bogus user-supplied attributes but |
| 55 | +# ignores them when returning rows |
| 56 | +$sth = $dbh->prepare('user-supplied silly TYPE and PRECISION', { |
| 57 | + rows => dclone( \@ROWS ), |
| 58 | + NAME => [qw( first_col second_col third_col )], |
| 59 | + TYPE => [SQL_INTEGER, SQL_DATETIME, SQL_CHAR], |
| 60 | + PRECISION => [1, 100_000, 0], |
| 61 | + }); |
| 62 | +ok($sth, "prepare() 3x3 result with TYPE and PRECISION succeeded"); |
| 63 | +is_deeply($sth->{NAME}, ['first_col','second_col','third_col'], |
| 64 | + "column NAMEs again as expected"); |
| 65 | +is_deeply($sth->{TYPE}, [SQL_INTEGER, SQL_DATETIME, SQL_CHAR], |
| 66 | + "column TYPEs not overwritten"); |
| 67 | +is_deeply($sth->{PRECISION}, [1, 100_000, 0], |
| 68 | + "column PRECISION not overwritten"); |
| 69 | +is_deeply($sth->fetch(), $ROWS[0], "first row fetch as expected, despite bogus attributes"); |
| 70 | +is_deeply($sth->fetch(), $ROWS[1], "second row fetch as expected, despite bogus attributes"); |
| 71 | +is_deeply($sth->fetch(), $ROWS[2], "third row fetch as expected, despite bogus attributes"); |
| 72 | +ok(!defined($sth->fetch()), "fourth fetch returns undef, despite bogus attributes"); |
0 commit comments