summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBTest.pm
blob: 594dfa82d7a9674a82691d78a3a03c13b0650005 (plain)
  1. =head1 NAME
  2. LedgerSMB::DBTest - LedgerSMB commit filter for test cases.
  3. =head1 SYOPSIS
  4. This module creates a DBI-like interface but ensures autocommit is off,
  5. and filters commit statements such that they don't do anything. This can be
  6. used for making API test cases which involve DB commits safe for production
  7. environments.
  8. =head1 USAGE
  9. Both LedgerSMB.pm and LedgerSMB/Form.pm assign a global database handler for all
  10. database access within a script in the dbh property (for example,
  11. $request->{dbh} or $form->{dbh}). By setting this early to a
  12. LedgerSMB::DBTest (instead of a DBI object), the tests can be made safe.
  13. However, there are a few limitations to be aware of. One cannot run tests
  14. through the standard request handler and use this module. Hence this is limited
  15. to unit tests of files in the LedgerSMB, scripts, and bin directories.
  16. Here is an example of how this could be done:
  17. my $lsmb = LedgerSMB->new();
  18. $lsmb->merge($testdata);
  19. my $dbh = LedgerSMB::DBTest->connect("dbi:Pg:dbname=$company", "$username",
  20. "$password",)
  21. $lsmb->{dbh} = $dbh;
  22. =head1 METHODS
  23. =over
  24. =item connect($dsn, $user, $pass)
  25. Connects to the database and returns a LedgerSMB::DBTest object
  26. =item commit()
  27. Tests the current transaction (issues a 'SELECT 1;' to the database). If this
  28. is successful returns 1, if not, rolls back and returns false.
  29. Note that this means all past tests are rolled back and this is inconsistent
  30. with normal transactional behavior.
  31. =item prepare()
  32. Returns a statement handle, via the private DBI database handle.
  33. =item do()
  34. passes this statement on to the private database handle
  35. =item errstr()
  36. passes this call on to the private database handle
  37. =item err()
  38. passes this call on to the private database handle
  39. =item quote()
  40. passes this call on to the private database handle
  41. =item quote_identifier()
  42. passes this call on to the private database handle
  43. =item rollback()
  44. passes this call on to the private database handle. Note that this will roll
  45. back all statements issues through this object.
  46. =back
  47. =cut
  48. use DBI;
  49. package LedgerSMB::DBTest;
  50. sub DISTROY {
  51. my ($self) = @_;
  52. $self->disconnect;
  53. }
  54. sub connect{
  55. my ($class, $dsn, $user, $pass) = @_;
  56. my $self = {};
  57. $self->{_dbh} = DBI->connect($dsn, $user, $pass, {AutoCommit => 0 });
  58. bless $self, $class;
  59. return $self;
  60. }
  61. sub commit {
  62. my ($self) = shift;
  63. my $sth = $self->{_dbh}->prepare('SELECT 1');
  64. $sth->execute;
  65. my ($retval) = $sth->fetchrow_array;
  66. return $retval;
  67. }
  68. sub selectrow_array {
  69. my ($self) = shift;
  70. return $self->{_dbh}->selectrow_array(@_);
  71. }
  72. sub disconnect {
  73. my ($self) = @_;
  74. $self->rollback;
  75. $self->{_dbh}->disconnect;
  76. }
  77. sub do {
  78. my ($self, $statement) = @_;
  79. return $self->{_dbh}->do($statement);
  80. }
  81. sub err{
  82. my ($self) = @_;
  83. return $self->{_dbh}->err;
  84. }
  85. sub errstr{
  86. my ($self) = @_;
  87. return $self->{_dbh}->errstr;
  88. }
  89. sub quote{
  90. my $self = shift @_;
  91. return $self->{_dbh}->quote(@_);
  92. }
  93. sub quote_identifier{
  94. my $self = shift @_;
  95. return $self->{_dbh}->quote_identifier(@_);
  96. }
  97. sub prepare{
  98. my ($self, $statement) = @_;
  99. return $self->{_dbh}->prepare($statement);
  100. }
  101. sub rollback {
  102. my ($self) = @_;
  103. return $self->{_dbh}->rollback;
  104. }
  105. sub state{
  106. my ($self) = @_;
  107. return $self->{_dbh}->state;
  108. }
  109. 1;