1 use Test::More tests => 17;
 
  11 use Support::TestSetup;
 
  13 use SL::Dev::Part qw(new_part);
 
  15 Support::TestSetup::login();
 
  17 SL::DB::Manager::Part->delete_all(all => 1, cascade => 1);
 
  19 # silence the Test::Harness warn handler
 
  20 local $SIG{__WARN__} = sub {};
 
  22 # test simple transaction
 
  24 my $part = new_part();
 
  25 SL::DB->client->with_transaction(sub {
 
  30   ok 0, 'error saving part';
 
  33 # test failing transaction
 
  34 my $part2 = new_part(partnumber => $part->partnumber); # woops, duplicate partnumber
 
  35 SL::DB->client->with_transaction(sub {
 
  40   ok 1, 'saving part with duplicate partnumber generates graceful error';
 
  43 # test transaction with run time exception
 
  45   SL::DB->client->with_transaction(sub {
 
  46     $part->method_that_does_not_exist;
 
  47     ok 0, 'this should have died';
 
  50     ok 0, 'this should not get here';
 
  52 } 'method not found in transaction died as expect';
 
  54 # test transaction with hook error
 
  55 # TODO - not possible to test without locally adding hooks in run time
 
  57 # test if error gets correctly stored in db->error
 
  58 $part2 = new_part(partnumber => $part->partnumber); # woops, duplicate partnumber
 
  59 SL::DB->client->with_transaction(sub {
 
  64   like(SL::DB->client->error, qr/unique.constraint/i, 'error is in db->error');
 
  67 # test stacked transactions
 
  68 # 1. test that it works
 
  69 SL::DB->client->with_transaction(sub {
 
  73   SL::DB->client->with_transaction(sub {
 
  77     ok 0, 'error saving part';
 
  84   ok 0, 'error saving part';
 
  88 is $part->sellprice, "3.00000", 'part saved';
 
  90 # 2. with a transaction rollback
 
  91 SL::DB->client->with_transaction(sub {
 
  96   SL::DB->client->with_transaction(sub {
 
 100     ok 0, 'should not get here';
 
 105   ok 0, 'should not get here';
 
 108   ok 1, 'sql error skips rest of the transaction';
 
 112 SL::DB->client->with_transaction(sub {
 
 116   SL::DB->client->with_transaction(sub {
 
 121     ok 0, 'should not get here';
 
 126   ok 0, 'should not get here';
 
 129   ok 1, 'sql error in nested transaction rolls back';
 
 130   like(SL::DB->client->error, qr/unique.constraint/i, 'error from nested transaction is in db->error');
 
 134 is $part->sellprice, "3.00000", 'saved part is not affected';
 
 138 SL::DB->client->with_transaction(sub {
 
 142   SL::DB->client->with_transaction(sub {
 
 146     ok 0, 'should not get here';
 
 152   ok 0, 'should not get here';
 
 155   ok 1, 'sql error after nested transaction rolls back';
 
 159 is $part->sellprice, "3.00000", 'saved part is not affected';
 
 162   SL::DB->client->with_transaction(sub {
 
 164     $part->not_existing_function();
 
 167     SL::DB->client->with_transaction(sub {
 
 171       ok 0, 'should not get here';
 
 176     ok 0, 'should not get here';
 
 179     ok 0, 'should not get here';
 
 183   ok 1, 'runtime exception error before nested transaction rolls back';
 
 187 is $part->sellprice, "3.00000", 'saved part is not affected';
 
 190   SL::DB->client->with_transaction(sub {
 
 194     SL::DB->client->with_transaction(sub {
 
 196       $part->not_existing_function();
 
 199       ok 0, 'should not get here';
 
 204     ok 0, 'should not get here';
 
 207     ok 0, 'should not get here';
 
 211   ok 1, 'runtime exception error in nested transaction rolls back';
 
 215 is $part->sellprice, "3.00000", 'saved part is not affected';
 
 219   SL::DB->client->with_transaction(sub {
 
 223     SL::DB->client->with_transaction(sub {
 
 227       ok 0, 'should not get here';
 
 232     $part->not_existing_function();
 
 233     ok 0, 'should not get here';
 
 236     ok 0, 'should not get here';
 
 240   ok 1, 'runtime exception error after nested transaction rolls back';
 
 244 is $part->sellprice, "3.00000", 'saved part is not affected';