Debian Codegraph @

t/025_mock_last_insert_id.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBI;
9
10 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
11 || die "Cannot create handle: $DBI::errstr\n";
12
13 $dbh->{mock_start_insert_id} = ['Foo', 123];
14 $dbh->{mock_start_insert_id} = ['Baz', 345];
15
16 {
17 my $sth = $dbh->prepare('INSERT INTO Foo (foo, bar) values (?, ?)');
18
19 $sth->execute(15, 17);
20 is($dbh->{mock_last_insert_id}, 123, '... got the right insert id');
21 is($dbh->last_insert_id((undef)x4), 123, "... got the right insert id from the database's last_insert_id");
22
23 SKIP: {
24 skip "Version of DBI::st doesn't support last_insert_id", 1 unless $sth->can('last_insert_id');
25
26 is($sth->last_insert_id((undef)x4), 123, "... got the right insert id from the statement handle's last_insert_id");
27 }
28
29 $sth->execute(16, 18);
30 is($dbh->{mock_last_insert_id}, 124, '... got the right insert id');
31 is($dbh->last_insert_id((undef)x4), 124, "... got the right insert id from the database handle's last_insert_id");
32 SKIP: {
33 skip "Version of DBI::st doesn't support last_insert_id", 1 unless $sth->can('last_insert_id');
34
35 is($sth->last_insert_id((undef)x4), 124, "... got the right insert id from the statement handle's last_insert_id");
36 }
37
38 $sth->execute(19, 34);
39 is($dbh->{mock_last_insert_id}, 125, '... got the right insert id');
40 is($dbh->last_insert_id((undef)x4), 125, "... got the right insert id from the database handle's last_insert_id");
41 SKIP: {
42 skip "Version of DBI::st doesn't support last_insert_id", 1 unless $sth->can('last_insert_id');
43
44 is($sth->last_insert_id((undef)x4), 125, "... got the right insert id from the statement handle's last_insert_id");
45 }
46 }
47
48 {
49 my $sth = $dbh->prepare('INSERT IGNORE INTO Baz (foo, bar) values (?, ?)');
50
51 $sth->execute(90, 41);
52 is($dbh->{mock_last_insert_id}, 345, '... got the right insert id');
53 is($dbh->last_insert_id((undef)x4), 345, "... got the right insert id from the database handle's last_insert_id");
54 SKIP: {
55 skip "Version of DBI::st doesn't support last_insert_id", 1 unless $sth->can('last_insert_id');
56
57 is($sth->last_insert_id((undef)x4), 345, "... got the right insert id from the statement handle's last_insert_id");
58 }
59
60 $sth->execute(32, 71);
61 is($dbh->{mock_last_insert_id}, 346, '... got the right insert id');
62 is($dbh->last_insert_id((undef)x4), 346, "... got the right insert id from the database handle's last_insert_id");
63 SKIP: {
64 skip "Version of DBI::st doesn't support last_insert_id", 1 unless $sth->can('last_insert_id');
65
66 is($sth->last_insert_id((undef)x4), 346, "... got the right insert id from the statement handle's last_insert_id");
67 }
68
69 $sth->execute(77, 42);
70 is($dbh->{mock_last_insert_id}, 347, '... got the right insert id');
71 is($dbh->last_insert_id((undef)x4), 347, "... got the right insert id from the database handle's last_insert_id");
72 SKIP: {
73 skip "Version of DBI::st doesn't support last_insert_id", 1 unless $sth->can('last_insert_id');
74
75 is($sth->last_insert_id((undef)x4), 347, "... got the right insert id from the statement handle's last_insert_id");
76 }
77 }
78
79 done_testing();