Skip to content
Browse files

uploadin

  • Loading branch information...
1 parent 87484ee commit 05c3a3111e0677248c0cc2a5a8ed8d60250159d0 Stevan Little committed Jan 24, 2005
Showing with 203 additions and 0 deletions.
  1. +52 −0 t/36_mock_add_resultset_test.t
  2. +151 −0 t/61_DBD_Mock_Session_bound_params_test.t
View
52 t/36_mock_add_resultset_test.t
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 7;
+
+BEGIN {
+ use_ok('DBD::Mock');
+ use_ok('DBI');
+}
+
+# test the ability to overwrite a
+# hash based 'mock_add_resultset'
+# and have it work as expected
+
+my $dbh = DBI->connect('dbi:Mock:', '', '');
+isa_ok($dbh, 'DBI::db');
+
+$dbh->{mock_add_resultset} = {
+ sql => 'SELECT foo FROM bar',
+ results => [[ 'foo' ], [ 10 ]]
+};
+
+{
+ my $sth = $dbh->prepare('SELECT foo FROM bar');
+ isa_ok($sth, 'DBI::st');
+
+ $sth->execute();
+ my ($result) = $sth->fetchrow_array();
+
+ cmp_ok($result, '==', 10, '... got the result we expected');
+
+ $sth->finish();
+}
+
+$dbh->{mock_add_resultset} = {
+ sql => 'SELECT foo FROM bar',
+ results => [[ 'foo' ], [ 50 ]]
+};
+
+{
+ my $sth = $dbh->prepare('SELECT foo FROM bar');
+ isa_ok($sth, 'DBI::st');
+
+ $sth->execute();
+ my ($result) = $sth->fetchrow_array();
+
+ cmp_ok($result, '==', 50, '... got the result we expected');
+
+ $sth->finish();
+}
View
151 t/61_DBD_Mock_Session_bound_params_test.t
@@ -0,0 +1,151 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 24;
+
+BEGIN {
+ use_ok('DBD::Mock');
+ use_ok('DBI');
+}
+
+{
+ my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
+ isa_ok($dbh, 'DBI::db');
+
+ my $session = DBD::Mock::Session->new((
+ {
+ statement => 'SELECT foo FROM bar WHERE baz = ?',
+ bound_params => [ 100 ],
+ results => [[ 'foo' ], [ 10 ]]
+ },
+ {
+ statement => 'SELECT bar FROM foo WHERE baz = ?',
+ bound_params => [ 125 ],
+ results => [[ 'bar' ], [ 15 ]]
+ },
+ ));
+ isa_ok($session, 'DBD::Mock::Session');
+
+ $dbh->{mock_session} = $session;
+
+ eval {
+ my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
+ $sth->execute(100);
+ my ($result) = $sth->fetchrow_array();
+ cmp_ok($result, '==', 10, '... got the right value');
+ };
+ ok(!$@, '... everything worked as planned');
+
+ eval {
+ my $sth = $dbh->prepare('SELECT bar FROM foo WHERE baz = ?');
+ $sth->execute(125);
+ my ($result) = $sth->fetchrow_array();
+ cmp_ok($result, '==', 15, '... got the right value');
+ };
+ ok(!$@, '... everything worked as planned');
+}
+
+{
+ my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
+ isa_ok($dbh, 'DBI::db');
+
+ my $session = DBD::Mock::Session->new((
+ {
+ statement => 'SELECT foo FROM bar WHERE baz = ?',
+ bound_params => [ 100 ],
+ results => [[ 'foo' ], [ 10 ]]
+ },
+ {
+ statement => 'SELECT bar FROM foo WHERE baz = 125',
+ results => [[ 'bar' ], [ 15 ]]
+ },
+ {
+ statement => 'DELETE FROM bar WHERE baz = ?',
+ results => [[], [], []],
+ bound_params => [ 100 ]
+ }
+ ));
+ isa_ok($session, 'DBD::Mock::Session');
+
+ $dbh->{mock_session} = $session;
+
+ eval {
+ my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
+ $sth->execute(100);
+ my ($result) = $sth->fetchrow_array();
+ cmp_ok($result, '==', 10, '... got the right value');
+ };
+ ok(!$@, '... first state worked as planned');
+
+ eval {
+ my $sth = $dbh->prepare('SELECT bar FROM foo WHERE baz = 125');
+ $sth->execute();
+ my ($result) = $sth->fetchrow_array();
+ cmp_ok($result, '==', 15, '... got the right value');
+ };
+ ok(!$@, '... second state worked as planned');
+
+ eval {
+ my $sth = $dbh->prepare('DELETE FROM bar WHERE baz = ?');
+ $sth->execute(100);
+ cmp_ok($sth->rows(), '==', 2, '... got the right number of affected rows');
+ };
+ ok(!$@, '... third state worked as planned');
+}
+
+# check some errors
+
+{
+ my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
+ isa_ok($dbh, 'DBI::db');
+
+ my $session = DBD::Mock::Session->new((
+ {
+ statement => 'SELECT foo FROM bar WHERE baz = ?',
+ bound_params => [ 100 ],
+ results => [[ 'foo' ], [ 10 ]]
+ }
+ ));
+ isa_ok($session, 'DBD::Mock::Session');
+
+ $dbh->{mock_session} = $session;
+
+ eval {
+ my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
+ $sth->execute(100, 200);
+ my ($result) = $sth->fetchrow_array();
+ };
+ ok($@, '... everything failed as planned');
+ like($@,
+ qr/Session Error\: Not the same number of bound params in current state in DBD\:\:Mock\:\:Session/,
+ '... everything failed as planned');
+}
+
+{
+ my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
+ isa_ok($dbh, 'DBI::db');
+
+ my $session = DBD::Mock::Session->new((
+ {
+ statement => 'SELECT foo FROM bar WHERE baz = ?',
+ bound_params => [ 100 ],
+ results => [[ 'foo' ], [ 10 ]]
+ }
+ ));
+ isa_ok($session, 'DBD::Mock::Session');
+
+ $dbh->{mock_session} = $session;
+
+ eval {
+ my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
+ $sth->execute(200);
+ my ($result) = $sth->fetchrow_array();
+ };
+ ok($@, '... everything failed as planned');
+ like($@,
+ qr/Session Error\: Bound params do not match in current state in DBD\:\:Mock\:\:Session/,
+ '... everything failed as planned');
+}
+

0 comments on commit 05c3a31

Please sign in to comment.
Something went wrong with that request. Please try again.