mapper.t 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 a test to some resources
  15. $mapper->map_test(
  16. test => 't/acceptance/login.t',
  17. resources => [ 'lib/MyApp/Auth.pm', 'lib/MyApp/Session.pm' ],
  18. );
  19. # Map a second test to a partially overlapping set
  20. $mapper->map_test(
  21. test => 't/acceptance/logout.t',
  22. resources => [ 'lib/MyApp/Session.pm' ],
  23. );
  24. # Resource that only login.t touches
  25. my @tests = $mapper->tests_for_resources( resources => ['lib/MyApp/Auth.pm'] );
  26. is( scalar @tests, 1, 'one test for Auth.pm' );
  27. is( $tests[0], 't/acceptance/login.t', 'correct test for Auth.pm' );
  28. # Resource shared by both tests
  29. my @shared = sort $mapper->tests_for_resources( resources => ['lib/MyApp/Session.pm'] );
  30. is( scalar @shared, 2, 'two tests for Session.pm' );
  31. is_deeply( \@shared, [ 't/acceptance/login.t', 't/acceptance/logout.t' ],
  32. 'correct tests for Session.pm' );
  33. # Resource not mapped to anything
  34. my @none = $mapper->tests_for_resources( resources => ['lib/MyApp/Unknown.pm'] );
  35. is( scalar @none, 0, 'no tests for unmapped resource' );
  36. # Idempotency: mapping same test+resource again should not create duplicates
  37. $mapper->map_test(
  38. test => 't/acceptance/login.t',
  39. resources => ['lib/MyApp/Auth.pm'],
  40. );
  41. my @dedup = $mapper->tests_for_resources( resources => ['lib/MyApp/Auth.pm'] );
  42. is( scalar @dedup, 1, 'duplicate mapping is idempotent' );
  43. done_testing;