summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBTest.pm
blob: b9eb6ae204919d50f2ee3485621c83b0b2f86775 (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 disconnect {
  62. my ($self) = @_;
  63. $self->rollback;
  64. $self->{_dbh}->disconnect;
  65. }
  66. sub do {
  67. my ($self, $statement) = @_;
  68. return $self->{_dbh}->do($statement);
  69. }
  70. sub err{
  71. my ($self) = @_;
  72. return $self->{_dbh}->err;
  73. }
  74. sub errstr{
  75. my ($self) = @_;
  76. return $self->{_dbh}->errstr;
  77. }
  78. sub quote{
  79. my $self = shift @_;
  80. return $self->{_dbh}->quote(@_);
  81. }
  82. sub quote_identifier{
  83. my $self = shift @_;
  84. return $self->{_dbh}->quote_identifier(@_);
  85. }
  86. sub prepare{
  87. my ($self, $statement) = @_;
  88. return $self->{_dbh}->prepare($statement);
  89. }
  90. sub rollback {
  91. my ($self) = @_;
  92. return $self->{_dbh}->rollback;
  93. }
  94. sub state{
  95. my ($self) = @_;
  96. return $self->{_dbh}->state;
  97. }
  98. 1;