switch tests to Test::TempDir::Tiny to enable parallelization
[dbsrgits/DBIx-Class-Fixtures.git] / t / 15-multiple-belongs-to.t
1 #!perl
2
3 use DBIx::Class::Fixtures;
4 use Test::More tests => 7;
5 use lib qw(t/lib);
6 use DBICTest;
7 use Path::Class;
8 use Data::Dumper;
9 use Test::TempDir::Tiny;
10 use IO::All;
11
12 my $tempdir = tempdir;
13
14 # set up and populate schema
15 ok(my $schema = DBICTest->init_schema(db_dir => $tempdir), 'got schema');
16
17 my $config_dir = io->catfile(qw't var configs')->name;
18
19 # do dump
20 ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
21 ok($fixtures->dump({ config => 'multiple-has-many.json', schema => $schema, directory => $tempdir }), 'fetch dump executed okay');
22
23 # check dump is okay
24 my $dir = dir($tempdir);
25
26 ok( -e io->catfile($tempdir, qw'producer')->name, "We fetched some producers" );
27 ok( -e io->catfile($tempdir, qw'cd_to_producer')->name, "We fetched some cd producer xrefs" );
28 ok( -e io->catfile($tempdir, qw'CD')->name, "We fetched some cds" );
29 ok( -e io->catfile($tempdir, qw'artist')->name, "We fetched some artists" );
30
31 __END__
32 while ( my ($dirname, $sourcename) = each %dirs ) {
33   my $this_dir = dir($dir, $dirname);
34 }
35
36 my $cd_dir = dir($dir, 'cd');
37 my $track_dir = dir($dir, 'track');
38
39 # check only artist1's cds that matched the rule were fetched
40 my $artist1 = $schema->resultset('Artist')->find(1);
41 my $artist1_cds = $artist1->cds;
42 while (my $a1_cd = $artist1_cds->next) {
43   my $cd_fix_file = file($cd_dir, $a1_cd->id . '.fix');
44   if ($a1_cd->tags->search({ tag => 'Cheesy' })->count) {
45     ok(-e $cd_fix_file, 'cd matching rule fetched');
46   } else {
47     isnt(-e $cd_fix_file, 1, 'cd not matching rule not fetched');
48   }
49 }
50
51 # check only cds' tracks that matched the rule were fetched
52 foreach my $cd_fix_file ($cd_dir->children) {
53   my $HASH1; eval($cd_fix_file->slurp());
54   is(ref $HASH1, 'HASH', 'cd fixture evals into hash');
55
56   my $cd = $schema->resultset('CD')->find($HASH1->{cdid});
57   foreach my $track ($cd->tracks->all) {
58     my $track_fix_file = file($track_dir, $track->id . '.fix');
59     if ($track->get_column('position') eq 2) {
60       is(-e $track_fix_file, 1, 'track matching rule fetched');
61     } else {
62       isnt(-e $track_fix_file, 1, 'track not matching rule not fetched');
63     }
64   }
65 }
66