1
0

mapper.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use File::Temp qw{tempfile};
  5. use_ok('Test::Mapper');
  6. # Spin up a temp DB for each test run
  7. my ( undef, $dbfile ) = tempfile( UNLINK => 1, SUFFIX => '.db' );
  8. my $mapper = Test::Mapper->new(
  9. db => $dbfile,
  10. product => 'TestProduct',
  11. environment => 'linux',
  12. );
  13. isa_ok( $mapper, 'Test::Mapper' );
  14. # ---- map_test / tests_for_resources ----------------------------------------
  15. $mapper->map_test(
  16. test => 't/acceptance/login.t',
  17. resources => [ 'lib/MyApp/Auth.pm', 'lib/MyApp/Session.pm' ],
  18. );
  19. $mapper->map_test(
  20. test => 't/acceptance/logout.t',
  21. resources => [ 'lib/MyApp/Session.pm' ],
  22. );
  23. my @tests = $mapper->tests_for_resources( resources => ['lib/MyApp/Auth.pm'] );
  24. is( scalar @tests, 1, 'one test for Auth.pm' );
  25. is( $tests[0], 't/acceptance/login.t', 'correct test for Auth.pm' );
  26. my @shared = sort $mapper->tests_for_resources( resources => ['lib/MyApp/Session.pm'] );
  27. is( scalar @shared, 2, 'two tests for Session.pm' );
  28. is_deeply( \@shared, [ 't/acceptance/login.t', 't/acceptance/logout.t' ],
  29. 'correct tests for Session.pm' );
  30. my @none = $mapper->tests_for_resources( resources => ['lib/MyApp/Unknown.pm'] );
  31. is( scalar @none, 0, 'no tests for unmapped resource' );
  32. $mapper->map_test(
  33. test => 't/acceptance/login.t',
  34. resources => ['lib/MyApp/Auth.pm'],
  35. );
  36. my @dedup = $mapper->tests_for_resources( resources => ['lib/MyApp/Auth.pm'] );
  37. is( scalar @dedup, 1, 'duplicate mapping is idempotent' );
  38. # ---- build lifecycle -------------------------------------------------------
  39. my $build = 'abc123';
  40. my @queued = $mapper->start_build(
  41. build => $build,
  42. resources => ['lib/MyApp/Auth.pm'],
  43. );
  44. is( scalar @queued, 1, 'start_build queues one test for changed Auth.pm' );
  45. is( $queued[0], 't/acceptance/login.t', 'correct test queued' );
  46. my @pending = $mapper->pending_tests( build => $build );
  47. is( scalar @pending, 1, 'one pending test before any claims' );
  48. # claim_test returns 1 on success
  49. my $claimed = $mapper->claim_test(
  50. build => $build,
  51. test => 't/acceptance/login.t',
  52. worker => 'worker-1',
  53. );
  54. is( $claimed, 1, 'claim_test succeeds first time' );
  55. # double-claim returns 0
  56. my $double = $mapper->claim_test(
  57. build => $build,
  58. test => 't/acceptance/login.t',
  59. worker => 'worker-2',
  60. );
  61. is( $double, 0, 'claim_test fails when already claimed' );
  62. # no more pending after claim
  63. my @after_claim = $mapper->pending_tests( build => $build );
  64. is( scalar @after_claim, 0, 'no pending tests after claim' );
  65. # complete the test
  66. $mapper->complete_test(
  67. build => $build,
  68. test => 't/acceptance/login.t',
  69. result => 0,
  70. );
  71. my $results = $mapper->build_results( build => $build );
  72. is( scalar @$results, 1, 'build_results returns one row' );
  73. is( $results->[0]{test_file}, 't/acceptance/login.t', 'correct test_file in result' );
  74. is( $results->[0]{result}, 0, 'result is 0 (pass)' );
  75. ok( defined $results->[0]{finish}, 'finish timestamp set after complete_test' );
  76. # ---- start_build is idempotent for same build+resources -------------------
  77. my @requeued = $mapper->start_build(
  78. build => $build,
  79. resources => ['lib/MyApp/Auth.pm'],
  80. );
  81. my $results2 = $mapper->build_results( build => $build );
  82. is( scalar @$results2, 1, 'start_build idempotent — no duplicate rows' );
  83. # ---- empty resources list -------------------------------------------------
  84. my @empty_queued = $mapper->start_build(
  85. build => 'no-changes',
  86. resources => [],
  87. );
  88. is( scalar @empty_queued, 0, 'start_build with empty resources returns empty list' );
  89. done_testing;