View | Details | Raw Unified | Return to bug 33608
Collapse All | Expand All

(-)a/t/db_dependent/Stats.t (-133 / +132 lines)
Lines 30-169 my $dbh = C4::Context->dbh; Link Here
30
30
31
subtest 'UpdateStats' => sub {
31
subtest 'UpdateStats' => sub {
32
    plan tests => 16;
32
    plan tests => 16;
33
is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
33
    is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
34
34
35
my $params = {
35
    my $params = {
36
              branch => "BRA",
36
                  branch => "BRA",
37
              itemnumber => 31,
37
                  itemnumber => 31,
38
              borrowernumber => 5,
38
                  borrowernumber => 5,
39
              amount =>5.1,
39
                  amount =>5.1,
40
              other => "bla",
40
                  other => "bla",
41
              itemtype => "BK",
41
                  itemtype => "BK",
42
              location => "LOC",
42
                  location => "LOC",
43
              ccode => "CODE",
43
                  ccode => "CODE",
44
};
44
    };
45
my $return_error;
45
    my $return_error;
46
46
47
# returns undef and croaks if type is not allowed
47
    # returns undef and croaks if type is not allowed
48
$params -> {type} = "bla";
48
    $params -> {type} = "bla";
49
eval {UpdateStats($params)};
49
    eval {UpdateStats($params)};
50
$return_error = $@;
50
    $return_error = $@;
51
isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
51
    isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
52
52
53
delete $params->{type};
53
    delete $params->{type};
54
# returns undef and croaks if type is missing
54
    # returns undef and croaks if type is missing
55
eval {UpdateStats($params)};
55
    eval {UpdateStats($params)};
56
$return_error = $@;
56
    $return_error = $@;
57
isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
57
    isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
58
58
59
$params -> {type} = undef;
59
    $params -> {type} = undef;
60
# returns undef and croaks if type is undef
60
    # returns undef and croaks if type is undef
61
eval {UpdateStats($params)};
61
    eval {UpdateStats($params)};
62
$return_error = $@;
62
    $return_error = $@;
63
isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
63
    isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
64
64
65
# returns undef and croaks if mandatory params are missing
65
    # returns undef and croaks if mandatory params are missing
66
my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall);
66
    my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall);
67
my @allowed_accounts_types = qw (writeoff payment);
67
    my @allowed_accounts_types = qw (writeoff payment);
68
my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
68
    my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
69
my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
69
    my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
70
70
71
my @missing_errors = ();
71
    my @missing_errors = ();
72
foreach my $key (@circulation_mandatory_keys) {
72
    foreach my $key (@circulation_mandatory_keys) {
73
    my $value = $params->{$key};
73
        my $value = $params->{$key};
74
    delete $params->{$key};
74
        delete $params->{$key};
75
    foreach my $type (@allowed_circulation_types) {
75
        foreach my $type (@allowed_circulation_types) {
76
        $params->{type} = $type;
76
            $params->{type} = $type;
77
        eval {UpdateStats($params)};
77
            eval {UpdateStats($params)};
78
        $return_error = $@;
78
            $return_error = $@;
79
        push @missing_errors, "key:$key for type:$type" unless $return_error;
79
            push @missing_errors, "key:$key for type:$type" unless $return_error;
80
        }
81
        $params->{$key} = $value;
80
    }
82
    }
81
    $params->{$key} = $value;
83
    foreach my $key (@accounts_mandatory_keys) {
82
}
84
        my $value = $params->{$key};
83
foreach my $key (@accounts_mandatory_keys) {
85
        delete $params->{$key};
84
    my $value = $params->{$key};
86
        foreach my $type (@allowed_accounts_types) {
85
    delete $params->{$key};
87
            $params->{type} = $type;
86
    foreach my $type (@allowed_accounts_types) {
88
            eval {UpdateStats($params)};
87
        $params->{type} = $type;
89
            $return_error = $@;
88
        eval {UpdateStats($params)};
90
            push @missing_errors, "key:$key for type:$type" unless $return_error;
89
        $return_error = $@;
91
        }
90
        push @missing_errors, "key:$key for type:$type" unless $return_error;
92
        $params->{$key} = $value;
93
91
    }
94
    }
92
    $params->{$key} = $value;
95
    is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
93
96
94
}
97
    # returns undef and croaks if forbidden params are given
95
is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
98
    $params -> {type} = "return";
96
99
    $params -> {newparam} = "true";
97
# returns undef and croaks if forbidden params are given
100
    eval {UpdateStats($params)};
98
$params -> {type} = "return";
101
    $return_error = $@;
99
$params -> {newparam} = "true";
102
    isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
100
eval {UpdateStats($params)};
103
    delete $params->{newparam};
101
$return_error = $@;
104
102
isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
105
    # save the params in the right database fields
103
delete $params->{newparam};
106
    $dbh->do(q|DELETE FROM statistics|);
104
107
    $params = {
105
# save the params in the right database fields
108
                  branch => "BRA",
106
$dbh->do(q|DELETE FROM statistics|);
109
                  itemnumber => 31,
107
$params = {
110
                  borrowernumber => 5,
108
              branch => "BRA",
111
                  amount =>5.1,
109
              itemnumber => 31,
112
                  other => "bla",
110
              borrowernumber => 5,
113
                  itemtype => "BK",
111
              amount =>5.1,
114
                  location => "LOC",
112
              other => "bla",
115
                  ccode => "CODE",
113
              itemtype => "BK",
116
                  type => "return"
114
              location => "LOC",
117
    };
115
              ccode => "CODE",
118
    UpdateStats ($params);
116
              type => "return"
119
    my $sth = $dbh->prepare("SELECT * FROM statistics");
117
};
120
    $sth->execute();
118
UpdateStats ($params);
121
    my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
119
my $sth = $dbh->prepare("SELECT * FROM statistics");
122
    is ($params->{branch},         $line->{branch},         "UpdateStats save branch param in branch field of statistics table");
120
$sth->execute();
123
    is ($params->{type},           $line->{type},           "UpdateStats save type param in type field of statistics table");
121
my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
124
    is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
122
is ($params->{branch},         $line->{branch},         "UpdateStats save branch param in branch field of statistics table");
125
    cmp_ok($params->{amount},'==', $line->{value},          "UpdateStats save amount param in value field of statistics table");
123
is ($params->{type},           $line->{type},           "UpdateStats save type param in type field of statistics table");
126
    is ($params->{other},          $line->{other},          "UpdateStats save other param in other field of statistics table");
124
is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
127
    is ($params->{itemtype},       $line->{itemtype},       "UpdateStats save itemtype param in itemtype field of statistics table");
125
cmp_ok($params->{amount},'==', $line->{value},          "UpdateStats save amount param in value field of statistics table");
128
    is ($params->{location},       $line->{location},       "UpdateStats save location param in location field of statistics table");
126
is ($params->{other},          $line->{other},          "UpdateStats save other param in other field of statistics table");
129
    is ($params->{ccode},          $line->{ccode},          "UpdateStats save ccode param in ccode field of statistics table");
127
is ($params->{itemtype},       $line->{itemtype},       "UpdateStats save itemtype param in itemtype field of statistics table");
130
128
is ($params->{location},       $line->{location},       "UpdateStats save location param in location field of statistics table");
131
    $dbh->do(q|DELETE FROM statistics|);
129
is ($params->{ccode},          $line->{ccode},          "UpdateStats save ccode param in ccode field of statistics table");
132
    $params = {
130
133
        branch         => "BRA",
131
$dbh->do(q|DELETE FROM statistics|);
134
        itemnumber     => 31,
132
$params = {
135
        borrowernumber => 5,
133
    branch         => "BRA",
136
        amount         => 5.1,
134
    itemnumber     => 31,
137
        other          => "bla",
135
    borrowernumber => 5,
138
        itemtype       => "BK",
136
    amount         => 5.1,
139
        ccode          => "CODE",
137
    other          => "bla",
140
        type           => "return"
138
    itemtype       => "BK",
141
    };
139
    ccode          => "CODE",
142
    UpdateStats($params);
140
    type           => "return"
143
    $sth = $dbh->prepare("SELECT * FROM statistics");
141
};
144
    $sth->execute();
142
UpdateStats($params);
145
    $line = ${ $sth->fetchall_arrayref( {} ) }[0];
143
$sth = $dbh->prepare("SELECT * FROM statistics");
146
    is( $line->{location}, undef,
144
$sth->execute();
147
        "UpdateStats sets location to NULL if no location is passed in." );
145
$line = ${ $sth->fetchall_arrayref( {} ) }[0];
148
146
is( $line->{location}, undef,
149
    $dbh->do(q|DELETE FROM statistics|);
147
    "UpdateStats sets location to NULL if no location is passed in." );
150
    $params = {
148
151
        branch         => "BRA",
149
$dbh->do(q|DELETE FROM statistics|);
152
        itemnumber     => 31,
150
$params = {
153
        borrowernumber => 5,
151
    branch         => "BRA",
154
        amount         => 5.1,
152
    itemnumber     => 31,
155
        other          => "bla",
153
    borrowernumber => 5,
156
        itemtype       => "BK",
154
    amount         => 5.1,
157
        location       => undef,
155
    other          => "bla",
158
        ccode          => "CODE",
156
    itemtype       => "BK",
159
        type           => "return"
157
    location       => undef,
160
    };
158
    ccode          => "CODE",
161
    UpdateStats($params);
159
    type           => "return"
162
    $sth = $dbh->prepare("SELECT * FROM statistics");
160
};
163
    $sth->execute();
161
UpdateStats($params);
164
    $line = ${ $sth->fetchall_arrayref( {} ) }[0];
162
$sth = $dbh->prepare("SELECT * FROM statistics");
165
    is( $line->{location}, undef,
163
$sth->execute();
166
        "UpdateStats sets location to NULL if undef is passed in." );
164
$line = ${ $sth->fetchall_arrayref( {} ) }[0];
165
is( $line->{location}, undef,
166
    "UpdateStats sets location to NULL if undef is passed in." );
167
};
167
};
168
168
169
$schema->storage->txn_rollback;
169
$schema->storage->txn_rollback;
170
- 

Return to bug 33608