use strict; use warnings; use Test::More; use File::Temp qw{tempfile}; use_ok('Test::Mapper'); # Spin up a temp DB for each test run my ( undef, $dbfile ) = tempfile( UNLINK => 1, SUFFIX => '.db' ); my $mapper = Test::Mapper->new( db => $dbfile, product => 'TestProduct', environment => 'linux', ); isa_ok( $mapper, 'Test::Mapper' ); # ---- map_test / tests_for_resources ---------------------------------------- $mapper->map_test( test => 't/acceptance/login.t', resources => [ 'lib/MyApp/Auth.pm', 'lib/MyApp/Session.pm' ], ); $mapper->map_test( test => 't/acceptance/logout.t', resources => [ 'lib/MyApp/Session.pm' ], ); my @tests = $mapper->tests_for_resources( resources => ['lib/MyApp/Auth.pm'] ); is( scalar @tests, 1, 'one test for Auth.pm' ); is( $tests[0], 't/acceptance/login.t', 'correct test for Auth.pm' ); my @shared = sort $mapper->tests_for_resources( resources => ['lib/MyApp/Session.pm'] ); is( scalar @shared, 2, 'two tests for Session.pm' ); is_deeply( \@shared, [ 't/acceptance/login.t', 't/acceptance/logout.t' ], 'correct tests for Session.pm' ); my @none = $mapper->tests_for_resources( resources => ['lib/MyApp/Unknown.pm'] ); is( scalar @none, 0, 'no tests for unmapped resource' ); $mapper->map_test( test => 't/acceptance/login.t', resources => ['lib/MyApp/Auth.pm'], ); my @dedup = $mapper->tests_for_resources( resources => ['lib/MyApp/Auth.pm'] ); is( scalar @dedup, 1, 'duplicate mapping is idempotent' ); # ---- build lifecycle ------------------------------------------------------- my $build = 'abc123'; my @queued = $mapper->start_build( build => $build, resources => ['lib/MyApp/Auth.pm'], ); is( scalar @queued, 1, 'start_build queues one test for changed Auth.pm' ); is( $queued[0], 't/acceptance/login.t', 'correct test queued' ); my @pending = $mapper->pending_tests( build => $build ); is( scalar @pending, 1, 'one pending test before any claims' ); # claim_test returns 1 on success my $claimed = $mapper->claim_test( build => $build, test => 't/acceptance/login.t', worker => 'worker-1', ); is( $claimed, 1, 'claim_test succeeds first time' ); # double-claim returns 0 my $double = $mapper->claim_test( build => $build, test => 't/acceptance/login.t', worker => 'worker-2', ); is( $double, 0, 'claim_test fails when already claimed' ); # no more pending after claim my @after_claim = $mapper->pending_tests( build => $build ); is( scalar @after_claim, 0, 'no pending tests after claim' ); # complete the test $mapper->complete_test( build => $build, test => 't/acceptance/login.t', result => 0, ); my $results = $mapper->build_results( build => $build ); is( scalar @$results, 1, 'build_results returns one row' ); is( $results->[0]{test_file}, 't/acceptance/login.t', 'correct test_file in result' ); is( $results->[0]{result}, 0, 'result is 0 (pass)' ); ok( defined $results->[0]{finish}, 'finish timestamp set after complete_test' ); # ---- start_build is idempotent for same build+resources ------------------- my @requeued = $mapper->start_build( build => $build, resources => ['lib/MyApp/Auth.pm'], ); my $results2 = $mapper->build_results( build => $build ); is( scalar @$results2, 1, 'start_build idempotent — no duplicate rows' ); # ---- empty resources list ------------------------------------------------- my @empty_queued = $mapper->start_build( build => 'no-changes', resources => [], ); is( scalar @empty_queued, 0, 'start_build with empty resources returns empty list' ); done_testing;