From a022b5739494f6b88cfd3651fc6c1f32d679778a Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Thu, 4 Feb 2021 19:35:02 +0000 Subject: [PATCH] Bug 27630: Add tests This proves different behavior between floats and decimals for values built by TestBuilder vs values returned by DBI/DBIx. To test: 1. prove t/db_dependent/TestBuilder.t 2. Observe failure Subtest: _gen_real formatting 1..4 not ok 1 - There can be more decimals when the column size allows it. Failed test 'There can be more decimals when the column size allows it.' at t/db_dependent/TestBuilder.t line 112. got: '10.10' expected: '10.100000' not ok 2 - decimal - Builder builds same format as DBIx returns. Failed test 'decimal - Builder builds same format as DBIx returns.' at t/db_dependent/TestBuilder.t line 118. got: '10.10' expected: '10.100000' not ok 3 - float - Builder builds same precision as DBIx returns. Failed test 'float - Builder builds same precision as DBIx returns.' at t/db_dependent/TestBuilder.t line 124. got: '10.10' expected: '10.1' not ok 4 - double - Builder builds same precision as DBIx returns. Failed test 'double - Builder builds same precision as DBIx returns.' at t/db_dependent/TestBuilder.t line 131. got: '10.10' expected: '10.1' Looks like you failed 4 tests of 4. not ok 4 - _gen_real formatting Failed test '_gen_real formatting' Sponsored-by: The National Library of Finland --- t/db_dependent/TestBuilder.t | 40 +++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/TestBuilder.t b/t/db_dependent/TestBuilder.t index e2fbba7a9b..deb08d3c7d 100755 --- a/t/db_dependent/TestBuilder.t +++ b/t/db_dependent/TestBuilder.t @@ -19,6 +19,7 @@ use Modern::Perl; +use Test::MockModule; use Test::More tests => 13; use Test::Warn; use File::Basename qw(dirname); @@ -84,7 +85,7 @@ subtest 'Build all sources' => sub { subtest 'Test length of some generated fields' => sub { - plan tests => 3; + plan tests => 4; # Test the length of a returned character field my $bookseller = $builder->build({ source => 'Aqbookseller' }); @@ -96,6 +97,43 @@ subtest 'Test length of some generated fields' => sub { my $item = $builder->build({ source => 'Item' }); is( $item->{replacementprice}, sprintf("%.2f", $item->{replacementprice}), "The number of decimals for floats should not be more than 2" ); + + subtest '_gen_real formatting' => sub { + plan tests => 4; + + my $module = Test::MockModule->new('t::lib::TestBuilder'); + $module->mock(_gen_real => sub { + return '10.10'; + }); + $builder = t::lib::TestBuilder->new; + + my $accountline = $builder->build({ source => 'Accountline' }); + is( $accountline->{amountoutstanding}, + sprintf("%.6f", $accountline->{amountoutstanding}), + "There can be more decimals when the column size allows it." ); + + my $rs = $schema->source('Accountline')->resultset; + my $accountline_dbix = $rs->find($accountline->{accountlines_id}); + is( $accountline->{amountoutstanding}, + $accountline_dbix->amountoutstanding, + "decimal - Builder builds same format as DBIx returns." ); + + my $aqorder = $builder->build({ source => 'Aqorder' }); + $rs = $schema->source('Aqorder')->resultset; + my $aqorder_dbix = $rs->find($aqorder->{ordernumber}); + is( $aqorder->{discount}, $aqorder_dbix->discount, + "float - Builder builds same precision as DBIx returns." ); + + my $statistic = $builder->build({ source => 'Statistic' }); + $rs = $schema->source('Statistic')->resultset; + my $statistic_dbix = $rs->search({ + datetime => $statistic->{datetime} })->next; + is( $statistic->{value}, $statistic_dbix->value, + "double - Builder builds same precision as DBIx returns." ); + + $module->unmock('_gen_real'); + $builder = t::lib::TestBuilder->new; + }; }; -- 2.17.1