File: 034_custom_attributes.t

package info (click to toggle)
libdbd-mock-perl 1.59-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 512 kB
  • sloc: perl: 1,251; makefile: 7
file content (127 lines) | stat: -rw-r--r-- 3,696 bytes parent folder | download
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
use 5.008;

use strict;
use warnings;

use Test::More;

BEGIN {
    use_ok('DBD::Mock');  
    use_ok('DBI');
}


{
    my $dbh = DBI->connect('dbi:Mock:', '', '');
    isa_ok($dbh, 'DBI::db');

    $dbh->{mock_add_resultset} = {
        sql => 'SELECT baz FROM qux',
        prepare_attributes => {
            foo => 'bar'
        },
        results => [[ 'baz' ], [ 10 ]]
    };

    my $sth = $dbh->prepare('SELECT baz FROM qux');
    isa_ok($sth, 'DBI::st');

    is( $sth->{foo}, 'bar', "our custom prepare_attribute should be set after the prepare" );

    my $rows = $sth->execute();
    is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');

    is( $sth->{foo}, 'bar', "our custom prepare_attribute should persist after the execute if nothing's changed it" );
}

{
    my $dbh = DBI->connect('dbi:Mock:', '', '');
    isa_ok($dbh, 'DBI::db');

    $dbh->{mock_add_resultset} = {
        sql => 'SELECT baz FROM qux',
        execute_attributes => {
            foo => 'bar'
        },
        results => [[ 'baz' ], [ 10 ]]
    };

    my $sth = $dbh->prepare('SELECT baz FROM qux');
    isa_ok($sth, 'DBI::st');

    is( $sth->{foo}, undef, "our custom execute_attribute should be undefined until we execute the statement" );

    my $rows = $sth->execute();
    is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');

    is( $sth->{foo}, 'bar', "our custom execute_attribute should be present after the statements executed" );
}

{
    my $dbh = DBI->connect('dbi:Mock:', '', '');
    isa_ok($dbh, 'DBI::db');

    $dbh->{mock_add_resultset} = {
        sql => 'SELECT baz FROM qux',
        execute_attributes => {
            foo => 'should be overwritten',
        },
        callback => sub {
            my @bound_params = @_;

            my %result = (
                fields => [ 'baz'],
                rows => [],
                last_insert_id => 99,
                execute_attributes => {
                    foo => 'bar'
                },
            );

            return %result;
        },
        results => [[ 'baz' ], [ 10 ]]
    };

    my $sth = $dbh->prepare('SELECT baz FROM qux');
    isa_ok($sth, 'DBI::st');

    is( $sth->{foo}, undef, "our custom execute_attribute should be undefined until we execute the statement" );

    my $rows = $sth->execute();
    is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');

    is( $sth->{foo}, 'bar', "our custom execute_attribute should be present after the statements executed" );
}


{
    my $dbh = DBI->connect('dbi:Mock:', '', '');
    isa_ok($dbh, 'DBI::db');

    $dbh->{mock_add_resultset} = {
        sql => 'SELECT baz FROM qux',
        prepare_attributes => {
            foo => 'prepare_bar',
            qux => 'prepare_quz',
        },
        execute_attributes => {
            foo => 'execute_bar'
        },
        results => [[ 'baz' ], [ 10 ]]
    };

    my $sth = $dbh->prepare('SELECT baz FROM qux');
    isa_ok($sth, 'DBI::st');

    is( $sth->{foo}, 'prepare_bar', "our custom prepare_attribute should be present after the statement has been defined" );
    is( $sth->{qux}, 'prepare_quz', "our custom prepare_attribute should be present after the statement has been defined" );

    my $rows = $sth->execute();
    is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');

    is( $sth->{foo}, 'execute_bar', "our custom execute_attribute should take precedence over the the prepare one after the statements executed" );
    is( $sth->{qux}, 'prepare_quz', "our custom prepare_attribute should still be present after the statement has been executed if there's no matching execute_attributes entry" );
}

done_testing();