summaryrefslogtreecommitdiff
path: root/t/62-api.t
blob: f05eb07264017e4946b5b7713a1647647d0fc446 (plain)
  1. BEGIN {
  2. use LedgerSMB;
  3. use Test::More;
  4. use LedgerSMB::Template;
  5. use LedgerSMB::DBTest;
  6. }
  7. our $api_test_cases = {
  8. };
  9. if (defined $ENV{LSMB_TEST_DB}){
  10. if (defined $ENV{LSMB_NEW_DB}){
  11. $ENV{PGDATABASE} = $ENV{LSMB_NEW_DB};
  12. }
  13. if (!defined $ENV{PGDATABASE}){
  14. die "Oops... LSMB_TEST_DB set but no db selected!";
  15. }
  16. plan 'no_plan';
  17. } else {
  18. plan skip_all => 'Skipping, LSMB_TEST_DB environment variable not set.';
  19. }
  20. do 't/data/62-request-data'; # Import test case hashes
  21. for (qw( drafts.pl login.pl payment.pl
  22. report.pl employee.pl menu.pl vendor.pl
  23. customer.pl inventory.pl vouchers.pl)
  24. ){
  25. ok(eval { require "scripts/$_" }, "Importing $_");
  26. if ($@){
  27. print STDERR "Error: $@\n";
  28. }
  29. } # Import new code namespaces
  30. my $dbh = LedgerSMB::DBTest->connect("dbi:Pg:dbname=$ENV{PGDATABASE}", undef, undef);
  31. for my $test (@$test_request_data){
  32. if (lc $test->{_codebase} eq 'old'){
  33. next; # skip old codebase tests for now
  34. old_code_test::_load_script($test->{module});
  35. my $qtring = "$test->{module}?";
  36. for $key (keys(%$test)){
  37. if ($key !~ /^_/){
  38. $qstring .= qq|$key=$test->{"$key"}&|;
  39. }
  40. }
  41. $qstring =~ s/&$//;
  42. $old_code_test::form = Form->new($qstring);
  43. for (keys (%$test)){
  44. $form->{$_} = $test->{$_};
  45. }
  46. is('old_code_test'->can($test->{action}), 0,
  47. "$test->{_test_id}: Action Successful");
  48. } else {
  49. my $request = LedgerSMB->new();
  50. $request->merge($test);
  51. my $script = $test->{module};
  52. $request->{dbh} = $dbh;
  53. $script =~ s/\.pl$//;
  54. is(ref "LedgerSMB::Scripts::$script"->can($request->{action}),
  55. 'CODE',
  56. "$test->{_test_id}: Action ($request->{action}) Defined");
  57. ok("LedgerSMB::Scripts::$script"->can($request->{action})->($request), "$test->{_test_id}: Action Successful");
  58. }
  59. for (@{$api_test_cases->{"$test->{_test_id}"}}){
  60. &$_;
  61. }
  62. ok($dbh->rollback, "$test->{_test_id}: rollback");
  63. }
  64. package LedgerSMB::Template;
  65. # Don't render templates. Just return so we can run tests on data structures.
  66. sub render {
  67. return 1;
  68. }
  69. package old_code_test;
  70. # Keeps old code isolated in a different namespace, and provides for reasonable
  71. # reload facilities.
  72. our $form;
  73. sub _load_script {
  74. do "bin/arapprn.pl";
  75. do "bin/arap.pl";
  76. do "bin/io.pl";
  77. do "bin/$1[0]";
  78. }