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