switch tests to Test::TempDir::Tiny to enable parallelization
[dbsrgits/DBIx-Class-Fixtures.git] / t / 05-dump-rules.t
1 #!perl
2
3 use DBIx::Class::Fixtures;
4 use Test::More tests => 15;
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 => 'rules.json', schema => $schema, directory => $tempdir }), 'fetch dump executed okay');
22
23 # check dump is okay
24 my $dir = dir($tempdir);
25 my $cd_dir = dir($dir, 'CD');
26 my $track_dir = dir($dir, 'track');
27
28 # check only artist1's cds that matched the rule were fetched
29 my $artist1 = $schema->resultset('Artist')->find(1);
30 my $artist1_cds = $artist1->cds;
31 while (my $a1_cd = $artist1_cds->next) {
32   my $cd_fix_file = file($cd_dir, $a1_cd->id . '.fix');
33   if ($a1_cd->tags->search({ tag => 'Cheesy' })->count) {
34     ok(-e $cd_fix_file, 'cd matching rule fetched');
35   } else {
36     isnt(-e $cd_fix_file, 1, 'cd not matching rule not fetched');
37   }
38 }
39
40 # check only cds' tracks that matched the rule were fetched
41 foreach my $cd_fix_file ($cd_dir->children) {
42   my $HASH1; eval($cd_fix_file->slurp());
43   is(ref $HASH1, 'HASH', 'cd fixture evals into hash');
44
45   my $cd = $schema->resultset('CD')->find($HASH1->{cdid});
46   foreach my $track ($cd->tracks->all) {
47     my $track_fix_file = file($track_dir, $track->id . '.fix');
48     if ($track->get_column('position') eq 2) {
49       is(-e $track_fix_file, 1, 'track matching rule fetched');
50     } else {
51       isnt(-e $track_fix_file, 1, 'track not matching rule not fetched');
52     }
53   }
54 }
55
56 ok $fixtures->available_config_sets, 'Found sets';