added the GetPriceEstimateCommand
[breadcrumbs.git] / src / t / TestCreditsAndDebits.pm
blob75829e1de5040f6d3e14fb33c750861294de9f56
1 package t::TestCreditsAndDebits;
3 # This file is part of the breadcrumbs daemon (bcd).
4 # Copyright (C) 2007 Pasqualino Ferrentino
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
23 use strict;
24 use warnings;
27 use t::TestCommonDb;
28 our @ISA = qw(t::TestCommonDb);
31 use Data::Dumper;
32 use Bcd::Data::CreditsAndDebits;
33 use Bcd::Errors::ErrorCodes;
34 use Bcd::Data::Model;
36 sub test_get_price_report_command{
37 my $self = shift;
38 my $stash= $self->{stash};
40 my $sql = qq{INSERT INTO credits_and_debits VALUES(16, 18, 2000)};
41 $stash->prep_exec($sql);
43 my $session = $self->_connect_as_a_boss();
44 my $command = Bcd::Data::Model->instance()->get_command("cad_get_price_estimate");
45 $self->assert_not_null($command);
46 $command->parse_line($session);
47 $command->parse_line(18);
48 $command->parse_line("100000");
49 $command->parse_line("1");
50 $command->eot();
52 $command->exec_only_for_test($stash);
53 my $exit_code = $command->get_exit_code();
54 $self->assert_num_equals(Bcd::Errors::ErrorCodes::BEC_OK, $exit_code);
56 my $res = Bcd::Commands::CommandParser->parse_command_output($command->get_output());
58 #$self->_test_get_price_estimate_for_a_payment
59 # (16, 18, 100_000, 1, 2000, 50_000, 25, 48000, 48_025, 0);
61 $self->assert_equals(2000, $res->{credits_towards_seller});
62 $self->assert_equals(50000, $res->{cheque_amount});
63 $self->assert_equals(48025, $res->{total_price});
64 $self->assert_equals(25, $res->{cheque_price});
65 $self->assert_equals(0, $res->{can_be_payed_now});
66 $self->assert_equals(48000, $res->{convertible_tao});
69 sub test_get_price_report{
70 my $self = shift;
71 my $stash= $self->{stash};
74 #some tests with no funds in the account of the buyer
76 #test with no cheque
77 $self->_test_get_price_estimate_for_a_payment
78 (16, 18, 10000, 0, 0, 0, 0, 10000, 10000, 0);
80 #test with all cheque
81 $self->_test_get_price_estimate_for_a_payment
82 (16, 18, 10000, 1, 0, 10000, 5, 0, 5, 0);
84 #partial cheque
85 $self->_test_get_price_estimate_for_a_payment
86 (16, 18, 100_000, 1, 0, 50_000, 25, 50_000, 50_025, 0);
88 #now with some credits
89 my $sql = qq{INSERT INTO credits_and_debits VALUES(16, 18, 2000)};
90 $stash->prep_exec($sql);
92 #partial credit and cash
93 $self->_test_get_price_estimate_for_a_payment
94 (16, 18, 10000, 0, 2000, 0, 0, 8000, 8000, 0);
96 #partial credit and cheque
97 $self->_test_get_price_estimate_for_a_payment
98 (16, 18, 10000, 1, 2000, 8000, 4, 0, 4, 0);
100 #partial credit and partial cheque
101 $self->_test_get_price_estimate_for_a_payment
102 (16, 18, 100_000, 1, 2000, 50_000, 25, 48000, 48_025, 0);
105 sub _test_get_price_estimate_for_a_payment{
106 my ($self, $buyer, $seller, $amount, $can_use_credit,
107 $check_credits_towards_seller, $check_cheque_amount,
108 $check_cheque_price, $check_convertible_tao,
109 $check_total_price, $check_can_be_payed_now) = @_;
111 my $stash= $self->{stash};
115 $credits_towards_seller,
116 $cheque_amount,
117 $cheque_price,
118 $convertible_tao,
119 $total_price,
120 $can_be_payed_now) = Bcd::Data::CreditsAndDebits->get_price_estimate_report_for_a_payment
121 ($stash, $buyer, $seller, $amount, $can_use_credit);
123 $self->assert_equals($check_credits_towards_seller, $credits_towards_seller);
124 $self->assert_equals($check_cheque_amount, $cheque_amount);
125 $self->assert_equals($check_cheque_price, $cheque_price);
126 $self->assert_equals($check_convertible_tao, $convertible_tao);
127 $self->assert_equals($check_total_price, $total_price);
128 $self->assert_equals($check_can_be_payed_now, $can_be_payed_now);
131 sub test_credit_report{
132 my $self = shift;
133 my $stash= $self->{stash};
136 my $report = Bcd::Data::CreditsAndDebits->get_credit_reports_towards_ants($stash, 16, 8012898);
138 #all the values should be zero.
139 my $first = 1;
140 foreach(@{$report}){
141 if ($first == 1){
142 $first = 0;
143 next;
145 $self->assert_equals(0, $_->[1]);
148 my $sql = qq{INSERT INTO credits_and_debits VALUES(18, 16, 1001)};
149 $stash->prep_exec($sql);
151 $report = Bcd::Data::CreditsAndDebits->get_credit_reports_towards_ants($stash, 16, 8012898);
153 #ok, I should have a value of -1001, with 18.
154 $first = 1;
155 foreach(@{$report}){
156 if ($first == 1){
157 $first = 0;
158 next;
160 $self->assert_equals(0, $_->[1]) if $_->[0] != 18;
161 $self->assert_equals(-1001, $_->[2]) if $_->[0] == 18;
166 sub test_credit_report_command{
167 my $self = shift;
168 my $stash= $self->{stash};
170 #I connect as the boss
171 my $session = $self->_connect_as_a_boss();
173 my $command = Bcd::Data::Model->instance()->get_command("cad_get_credit_report");
174 $self->assert_not_null($command);
175 $command->parse_line($session);
176 $command->eot();
178 $command->exec_only_for_test($stash);
179 my $exit_code = $command->get_exit_code();
181 $self->assert_num_equals(Bcd::Errors::ErrorCodes::BEC_OK, $exit_code);
183 my $res = Bcd::Commands::CommandParser->parse_command_output($command->get_output());
185 #print Dumper($res);
187 my $sql = qq{INSERT INTO credits_and_debits VALUES(18, 16, 1001)};
188 $stash->prep_exec($sql);
190 $command->eot();
191 $command->exec_only_for_test($stash);
192 $exit_code = $command->get_exit_code();
193 $self->assert_num_equals(Bcd::Errors::ErrorCodes::BEC_OK, $exit_code);
194 $res = Bcd::Commands::CommandParser->parse_command_output($command->get_output());
196 $self->assert_num_equals(1001, $res->{total_debit});
199 sub test_initial_credits{
200 my $self = shift;
201 my $stash= $self->{stash};
204 my $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16);
205 $self->assert_num_equals(0, $net_credit);
207 #ok, now I put some credits
209 my $sql = qq{INSERT INTO credits_and_debits VALUES(12, 16, 1001)};
210 $stash->prep_exec($sql);
212 $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16);
213 $self->assert_num_equals(-1001, $net_credit);
215 #a frozen debit
216 $sql = qq{INSERT INTO frozen_credits VALUES(16, 999)};
217 $stash->prep_exec($sql);
219 $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16);
220 $self->assert_num_equals(-2000, $net_credit);
222 #a credit
223 $sql = qq{INSERT INTO credits_and_debits VALUES(16, 9, 1500)};
224 $stash->prep_exec($sql);
226 $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16);
227 $self->assert_num_equals(-500, $net_credit);
229 my $score = Bcd::Data::CreditsAndDebits->get_ant_credit_score($stash, 16);
230 $self->assert_num_equals(100, $score);
233 sub test_credit_score{
234 my $self = shift;
235 my $stash= $self->{stash};
238 my $score;
239 $score = Bcd::Data::CreditsAndDebits::credit_score(32000, 10000);
240 $self->assert_num_equals(100, $score);
242 $score = Bcd::Data::CreditsAndDebits::credit_score(32000, 0);
243 $self->assert_num_equals(100, $score);
245 $score = Bcd::Data::CreditsAndDebits::credit_score(82000, 0);
246 $self->assert_num_equals(100, $score);
248 $score = Bcd::Data::CreditsAndDebits::credit_score(0, 82000);
249 $self->assert_num_equals(-100, $score);
251 $score = Bcd::Data::CreditsAndDebits::credit_score(1, 82000);
252 $self->assert_num_equals("-99.7776809245822", $score);
254 $score = Bcd::Data::CreditsAndDebits::credit_score(82000, 1);
255 $self->assert_num_equals("99.7776809245822", $score);
257 $score = Bcd::Data::CreditsAndDebits::credit_score(82000, 82000);
258 $self->assert_num_equals("0", $score);
260 $score = Bcd::Data::CreditsAndDebits::credit_score(23000, 82000);
261 $self->assert_num_equals("-59.6048313960983", $score);
264 sub test_credit_score_from_db{
266 my $self = shift;
267 my $stash= $self->{stash};
269 my $sql = qq{INSERT INTO credits_and_debits VALUES(12, 16, 82000)};
270 $stash->prep_exec($sql);
272 $sql = qq{INSERT INTO credits_and_debits VALUES(16, 15, 1)};
273 $stash->prep_exec($sql);
275 my $score = Bcd::Data::CreditsAndDebits->get_ant_credit_score($stash, 16);
276 $self->assert_num_equals("-99.7776809245822", $score);
279 sub test_ant_credit_ant{
281 my $self = shift;
282 my $stash= $self->{stash};
284 #barbara issues a cheque, so she will have a debit
285 Bcd::Data::CreditsAndDebits->ant_credits_ant($stash, 20, 16, 20000);
287 my $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16);
288 $self->assert_num_equals(-20000, $net_credit);
290 my $res;
291 ($res,$net_credit) = Bcd::Data::CreditsAndDebits->get_net_position_between_two_ants($stash, 16, 20);
292 $self->assert_num_equals(-20000, $net_credit);
294 ($res, $net_credit) = Bcd::Data::CreditsAndDebits->get_net_position_between_two_ants($stash, 20, 16);
295 $self->assert_num_equals(20000, $net_credit);
298 #ok, now barbara emits another cheque
299 Bcd::Data::CreditsAndDebits->ant_credits_ant($stash, 20, 16, 7000);
300 $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16);
301 $self->assert_num_equals(-27000, $net_credit);
304 #now paola returns some cheques to barbara
305 Bcd::Data::CreditsAndDebits->ant_credits_ant($stash, 16, 20, 13000);
306 $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16);
307 $self->assert_num_equals(-14000, $net_credit);
310 #another cheque, now paola becomes a debitor
311 Bcd::Data::CreditsAndDebits->ant_credits_ant($stash, 16, 20, 15000);
312 $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16);
313 $self->assert_num_equals(1000, $net_credit);
315 $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 20);
316 $self->assert_num_equals(-1000, $net_credit);
318 ($res, $net_credit) = Bcd::Data::CreditsAndDebits->get_net_position_between_two_ants($stash, 20, 16);
319 $self->assert_num_equals(-1000, $net_credit);
322 #this is the same in the bank
323 use constant R_MAX => 1.22;
325 sub test_get_maximum_cheque{
326 my $self = shift;
327 my $stash= $self->{stash};
329 my $max_cheque;
331 $max_cheque = Bcd::Data::CreditsAndDebits->get_maximum_debt_to_reach_this_rate
332 ($stash, 16, R_MAX);
334 $self->assert_equals(Bcd::Data::CreditsAndDebits::MINIMUM_DEBIT_CREDIT, $max_cheque);
336 #ok, now I have some credit
337 Bcd::Data::CreditsAndDebits->ant_credits_ant($stash, 16, 20, 15000);
339 $max_cheque = Bcd::Data::CreditsAndDebits->get_maximum_debt_to_reach_this_rate
340 ($stash, 16, R_MAX);
341 $self->assert_equals(Bcd::Data::CreditsAndDebits::MINIMUM_DEBIT_CREDIT, $max_cheque);
343 #some more credit
344 Bcd::Data::CreditsAndDebits->ant_credits_ant($stash, 16, 20, 27000);
346 $max_cheque = Bcd::Data::CreditsAndDebits->get_maximum_debt_to_reach_this_rate
347 ($stash, 16, R_MAX);
348 $self->assert_equals(51240, $max_cheque);
350 #some debit
351 Bcd::Data::CreditsAndDebits->ant_credits_ant($stash, 19, 16, 17000);
353 #some frozen debit
354 my $sql = qq{INSERT INTO frozen_credits VALUES(16, 10000)};
355 $stash->prep_exec($sql);
357 $max_cheque = Bcd::Data::CreditsAndDebits->get_maximum_debt_to_reach_this_rate
358 ($stash, 16, R_MAX);
359 $self->assert_equals(24240, $max_cheque);